github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/mxjson/parser_test.go (about) 1 package mxjson_test 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 "github.com/lmorg/murex/test/count" 8 "github.com/lmorg/murex/utils/mxjson" 9 ) 10 11 type testCase struct { 12 Json string 13 Expected string 14 Error bool 15 } 16 17 func runTestCases(t *testing.T, tests []testCase) { 18 count.Tests(t, len(tests)) 19 20 for i := range tests { 21 testMx(t, tests[i].Json, tests[i].Expected, tests[i].Error, i) 22 } 23 } 24 25 func testMx(t *testing.T, src string, sExp string, fail bool, testNum int) { 26 v, err := mxjson.Parse([]byte(src)) 27 bAct, jsonErr := json.Marshal(v) 28 if jsonErr != nil { 29 t.Errorf("Unable to marshal Go struct from mxjson unmarshaller, this is possibly an error with the standard library: %s", jsonErr.Error()) 30 } 31 32 sAct := string(bAct) 33 bExp := []byte(sExp) 34 35 if (err != nil) != fail { 36 t.Errorf("Error response not as expected in test %d: ", testNum) 37 t.Logf(" mxjson: %s", src) 38 t.Logf(" exp err: %v", fail) 39 t.Logf(" act err: %v", err) 40 t.Logf(" exp str: %s", sExp) 41 t.Logf(" act str: %s", sAct) 42 t.Log(" exp byt: ", bExp) 43 t.Log(" act byt: ", bAct) 44 } 45 46 if sExp == "" && v == nil { 47 return 48 } 49 50 if sExp != sAct { 51 t.Errorf("Output doesn't match expected in test %d: ", testNum) 52 t.Logf(" mxjson: %s", src) 53 t.Logf(" exp err: %v", fail) 54 t.Logf(" act err: %v", err) 55 t.Logf(" exp str: %s", sExp) 56 t.Logf(" act str: %s", sAct) 57 t.Log(" exp byt: ", bExp) 58 t.Log(" act byt: ", bAct) 59 } 60 }