go-jsonstr/strings_marshaljson_test.go

82 lines
1.6 KiB
Go
Raw Normal View History

2023-09-27 00:12:51 +00:00
package jsonstr_test
import (
"testing"
"encoding/json"
"github.com/reiver/go-jsonstr"
2023-09-27 00:12:51 +00:00
)
func TestString_MarshalJSON(t *testing.T) {
tests := []struct{
Strings jsonstr.Strings
Expected string
}{
{
Strings: jsonstr.Strings{},
Expected: `[]`,
},
{
Strings: jsonstr.CompileStrings("apple"),
2023-09-27 00:12:51 +00:00
Expected: `["apple"]`,
},
{
Strings: jsonstr.CompileStrings("apple", "banana"),
2023-09-27 00:12:51 +00:00
Expected: `["apple","banana"]`,
},
{
Strings: jsonstr.CompileStrings("apple", "banana", "cherry"),
2023-09-27 00:12:51 +00:00
Expected: `["apple","banana","cherry"]`,
},
{
Strings: jsonstr.CompileStrings("😈"),
2023-09-27 00:12:51 +00:00
Expected: `["😈"]`,
},
{
Strings: jsonstr.CompileStrings("😈", "🙂🙁"),
2023-09-27 00:12:51 +00:00
Expected: `["😈","🙂🙁"]`,
},
{
Strings: jsonstr.CompileStrings("😈", "🙂🙁", ""),
2023-09-27 00:12:51 +00:00
Expected: `["😈","🙂🙁",""]`,
},
{
Strings: jsonstr.CompileStrings("😈", "🙂🙁", "", "٠١٢٣۴۵۶٧٨٩"),
2023-09-27 00:12:51 +00:00
Expected: `["😈","🙂🙁","","٠١٢٣۴۵۶٧٨٩"]`,
},
}
for testNumber, test := range tests {
actualBytes, err := json.Marshal(test.Strings)
if nil != err {
t.Errorf("For test #%d, did not expect an error but actually got one.", testNumber)
t.Logf("ERROR: (%T) %s", err, err)
t.Logf("STRINGS: %#v", test.Strings)
t.Logf("EXPECTED: %#v", test.Expected)
continue
}
{
actual := string(actualBytes)
expected := test.Expected
if expected != actual {
t.Errorf("For test #%d, the actual marshaled value is not what was expected.", testNumber)
t.Logf("EXPECTED: %s", expected)
t.Logf("ACTUAL: %s", actual)
t.Logf("STRINGS: %#v", test.Strings)
continue
}
}
}
}