github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/mxjson/structs_test.go (about) 1 package mxjson 2 3 import ( 4 "encoding/json" 5 "strconv" 6 "testing" 7 8 "github.com/lmorg/murex/test/count" 9 ) 10 11 func TestQuote(t *testing.T) { 12 count.Tests(t, 3) 13 14 var q quote 15 16 if q.IsOpen() { 17 t.Error("q.IsOpen incorrectly returned true") 18 } 19 20 q.Open(1) 21 22 if !q.IsOpen() { 23 t.Error("q.IsOpen incorrectly returned false") 24 } 25 26 q.Close() 27 28 if q.IsOpen() { 29 t.Error("q.IsOpen incorrectly returned true") 30 } 31 } 32 33 func TestPair(t *testing.T) { 34 count.Tests(t, 4) 35 36 var p pair 37 38 if p.IsOpen() { 39 t.Error("p.IsOpen incorrectly returned true") 40 } 41 42 p.Open(1) 43 44 if !p.IsOpen() { 45 t.Error("p.IsOpen incorrectly returned false") 46 } 47 48 err := p.Close() 49 if err != nil { 50 t.Errorf("p.Close incorrectly returned err: %s", err.Error()) 51 } 52 53 if p.IsOpen() { 54 t.Error("p.IsOpen incorrectly returned true") 55 } 56 57 err = p.Close() 58 if err == nil { 59 t.Error("p.Close incorrectly returned no err") 60 } 61 } 62 63 func TestPairNested(t *testing.T) { 64 count.Tests(t, 1) 65 66 p := newPair() 67 68 p.Open(1) 69 p.Open(2) 70 p.Open(3) 71 if err := p.Close(); err != nil { 72 t.Errorf("p.Close incorrectly returned err in iteration %d: %s", 0, err.Error()) 73 } 74 if err := p.Close(); err != nil { 75 t.Errorf("p.Close incorrectly returned err in iteration %d: %s", 1, err.Error()) 76 } 77 if err := p.Close(); err != nil { 78 t.Errorf("p.Close incorrectly returned err in iteration %d: %s", 2, err.Error()) 79 } 80 } 81 82 func TestPairOverflow(t *testing.T) { 83 count.Tests(t, 1) 84 85 p := newPair() 86 max := cap(p.pos) * cap(p.pos) 87 88 for i := 0; i < max; i++ { 89 p.Open(i) 90 } 91 92 for i := 0; i < max; i++ { 93 if err := p.Close(); err != nil { 94 t.Errorf("p.Close incorrectly returned err in iteration %d: %s", 1, err.Error()) 95 } 96 } 97 } 98 99 func TestLazyStringGet(t *testing.T) { 100 nums := []string{ 101 "zero.", 102 "one.", 103 "two.", 104 "three.", 105 "four.", 106 "five.", 107 "six.", 108 "seven.", 109 "eight.", 110 "nine.", 111 } 112 113 count.Tests(t, 1) 114 115 s := newStr() 116 max := cap(s.b) 117 118 for i := 0; i < max; i++ { 119 ints := strconv.Itoa(i) 120 var new string 121 for _, b := range []byte(ints) { 122 new += nums[b-48] // 48 == ASCII code for 0 123 } 124 125 for _, b := range []byte(new) { 126 s.Append(b) 127 } 128 129 b := s.Get() 130 if new != string(b) { 131 t.Errorf("String mismatch in test %d", i) 132 t.Logf(" Expected: %s", new) 133 t.Logf(" Actual: %s", string(b)) 134 } 135 } 136 } 137 138 func TestLazyStringStringer(t *testing.T) { 139 nums := []string{ 140 "zero.", 141 "one.", 142 "two.", 143 "three.", 144 "four.", 145 "five.", 146 "six.", 147 "seven.", 148 "eight.", 149 "nine.", 150 } 151 152 count.Tests(t, 1) 153 154 s := newStr() 155 max := cap(s.b) 156 157 for i := 0; i < max; i++ { 158 ints := strconv.Itoa(i) 159 var new string 160 for _, b := range []byte(ints) { 161 new += nums[b-48] // 48 == ASCII code for 0 162 } 163 164 for _, b := range []byte(new) { 165 s.Append(b) 166 } 167 168 act := s.String() 169 if new != act { 170 t.Errorf("String mismatch in test %d", i) 171 t.Logf(" Expected: %s", new) 172 t.Logf(" Actual: %s", act) 173 } 174 } 175 } 176 177 func TestObjectsMap(t *testing.T) { 178 count.Tests(t, 1) 179 180 key := "foo" 181 val := "bar" 182 exp := `{"foo":"bar"}` 183 184 obj := newObjs() 185 obj.New(objMap) 186 187 s := obj.GetKeyPtr() 188 for _, b := range []byte(key) { 189 s.Append(b) 190 } 191 192 obj.SetValue(val) 193 194 obj.MergeDown() 195 196 b, err := json.Marshal(obj.nest[0].value) 197 if err != nil { 198 t.Errorf("Unable to marshal Go struct, this is possibly an error with the standard library: %s", err.Error()) 199 } 200 201 if string(b) != exp { 202 t.Error("Output doesn't match expected:") 203 t.Logf(" Exp: %s", exp) 204 t.Logf(" Act: %s", string(b)) 205 } 206 }