github.com/Jeffail/benthos/v3@v3.65.0/lib/util/config/sanitised_test.go (about) 1 package config 2 3 import ( 4 "encoding/json" 5 "testing" 6 7 yaml "gopkg.in/yaml.v3" 8 ) 9 10 func TestSanitisedJSON(t *testing.T) { 11 exp := `{"type":"foo","a":"a","z":"z"}` 12 obj := Sanitised{ 13 "a": "a", 14 "type": "foo", 15 "z": "z", 16 } 17 actBytes, err := json.Marshal(obj) 18 if err != nil { 19 t.Fatal(err) 20 } 21 22 act := string(actBytes) 23 if act != exp { 24 t.Errorf("Unexpected result: %v != %v", act, exp) 25 } 26 27 exp = `{"type":5,"a":"hello","b":2.3}` 28 obj = Sanitised{ 29 "a": "hello", 30 "b": 2.3, 31 "type": 5, 32 } 33 if actBytes, err = json.Marshal(obj); err != nil { 34 t.Fatal(err) 35 } 36 37 act = string(actBytes) 38 if act != exp { 39 t.Errorf("Unexpected result: %v != %v", act, exp) 40 } 41 42 exp = `{"type":"foo"}` 43 obj = Sanitised{ 44 "type": "foo", 45 } 46 if actBytes, err = json.Marshal(obj); err != nil { 47 t.Fatal(err) 48 } 49 50 act = string(actBytes) 51 if act != exp { 52 t.Errorf("Unexpected result: %v != %v", act, exp) 53 } 54 } 55 56 func TestSanitisedYAML(t *testing.T) { 57 exp := `type: foo 58 a: a 59 z: z 60 ` 61 62 obj := Sanitised{ 63 "a": "a", 64 "type": "foo", 65 "z": "z", 66 } 67 actBytes, err := yaml.Marshal(obj) 68 if err != nil { 69 t.Fatal(err) 70 } 71 72 act := string(actBytes) 73 if act != exp { 74 t.Errorf("Unexpected result: %v != %v", act, exp) 75 } 76 }