github.com/pietrocarrara/hugo@v0.47.1/common/maps/scratch_test.go (about) 1 // Copyright 2018 The Hugo Authors. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package maps 15 16 import ( 17 "reflect" 18 "sync" 19 "testing" 20 21 "github.com/stretchr/testify/assert" 22 ) 23 24 func TestScratchAdd(t *testing.T) { 25 t.Parallel() 26 scratch := NewScratch() 27 scratch.Add("int1", 10) 28 scratch.Add("int1", 20) 29 scratch.Add("int2", 20) 30 31 assert.Equal(t, int64(30), scratch.Get("int1")) 32 assert.Equal(t, 20, scratch.Get("int2")) 33 34 scratch.Add("float1", float64(10.5)) 35 scratch.Add("float1", float64(20.1)) 36 37 assert.Equal(t, float64(30.6), scratch.Get("float1")) 38 39 scratch.Add("string1", "Hello ") 40 scratch.Add("string1", "big ") 41 scratch.Add("string1", "World!") 42 43 assert.Equal(t, "Hello big World!", scratch.Get("string1")) 44 45 scratch.Add("scratch", scratch) 46 _, err := scratch.Add("scratch", scratch) 47 48 if err == nil { 49 t.Errorf("Expected error from invalid arithmetic") 50 } 51 52 } 53 54 func TestScratchAddSlice(t *testing.T) { 55 t.Parallel() 56 scratch := NewScratch() 57 58 _, err := scratch.Add("intSlice", []int{1, 2}) 59 assert.Nil(t, err) 60 _, err = scratch.Add("intSlice", 3) 61 assert.Nil(t, err) 62 63 sl := scratch.Get("intSlice") 64 expected := []int{1, 2, 3} 65 66 if !reflect.DeepEqual(expected, sl) { 67 t.Errorf("Slice difference, go %q expected %q", sl, expected) 68 } 69 70 _, err = scratch.Add("intSlice", []int{4, 5}) 71 72 assert.Nil(t, err) 73 74 sl = scratch.Get("intSlice") 75 expected = []int{1, 2, 3, 4, 5} 76 77 if !reflect.DeepEqual(expected, sl) { 78 t.Errorf("Slice difference, go %q expected %q", sl, expected) 79 } 80 81 } 82 83 func TestScratchSet(t *testing.T) { 84 t.Parallel() 85 scratch := NewScratch() 86 scratch.Set("key", "val") 87 assert.Equal(t, "val", scratch.Get("key")) 88 } 89 90 func TestScratchDelete(t *testing.T) { 91 t.Parallel() 92 scratch := NewScratch() 93 scratch.Set("key", "val") 94 scratch.Delete("key") 95 scratch.Add("key", "Lucy Parsons") 96 assert.Equal(t, "Lucy Parsons", scratch.Get("key")) 97 } 98 99 // Issue #2005 100 func TestScratchInParallel(t *testing.T) { 101 var wg sync.WaitGroup 102 scratch := NewScratch() 103 key := "counter" 104 scratch.Set(key, int64(1)) 105 for i := 1; i <= 10; i++ { 106 wg.Add(1) 107 go func(j int) { 108 for k := 0; k < 10; k++ { 109 newVal := int64(k + j) 110 111 _, err := scratch.Add(key, newVal) 112 if err != nil { 113 t.Errorf("Got err %s", err) 114 } 115 116 scratch.Set(key, newVal) 117 118 val := scratch.Get(key) 119 120 if counter, ok := val.(int64); ok { 121 if counter < 1 { 122 t.Errorf("Got %d", counter) 123 } 124 } else { 125 t.Errorf("Got %T", val) 126 } 127 } 128 wg.Done() 129 }(i) 130 } 131 wg.Wait() 132 } 133 134 func TestScratchGet(t *testing.T) { 135 t.Parallel() 136 scratch := NewScratch() 137 nothing := scratch.Get("nothing") 138 if nothing != nil { 139 t.Errorf("Should not return anything, but got %v", nothing) 140 } 141 } 142 143 func TestScratchSetInMap(t *testing.T) { 144 t.Parallel() 145 scratch := NewScratch() 146 scratch.SetInMap("key", "lux", "Lux") 147 scratch.SetInMap("key", "abc", "Abc") 148 scratch.SetInMap("key", "zyx", "Zyx") 149 scratch.SetInMap("key", "abc", "Abc (updated)") 150 scratch.SetInMap("key", "def", "Def") 151 assert.Equal(t, []interface{}{0: "Abc (updated)", 1: "Def", 2: "Lux", 3: "Zyx"}, scratch.GetSortedMapValues("key")) 152 } 153 154 func TestScratchGetSortedMapValues(t *testing.T) { 155 t.Parallel() 156 scratch := NewScratch() 157 nothing := scratch.GetSortedMapValues("nothing") 158 if nothing != nil { 159 t.Errorf("Should not return anything, but got %v", nothing) 160 } 161 } 162 163 func BenchmarkScratchGet(b *testing.B) { 164 scratch := NewScratch() 165 scratch.Add("A", 1) 166 b.ResetTimer() 167 for i := 0; i < b.N; i++ { 168 scratch.Get("A") 169 } 170 }