2023-09-27 00:12:51 +00:00
|
|
|
package jsonstr_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"encoding/json"
|
|
|
|
|
2024-08-02 00:05:16 +00:00
|
|
|
"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: `[]`,
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{
|
2023-09-27 03:28:37 +00:00
|
|
|
Strings: jsonstr.CompileStrings("apple"),
|
2023-09-27 00:12:51 +00:00
|
|
|
Expected: `["apple"]`,
|
|
|
|
},
|
|
|
|
{
|
2023-09-27 03:28:37 +00:00
|
|
|
Strings: jsonstr.CompileStrings("apple", "banana"),
|
2023-09-27 00:12:51 +00:00
|
|
|
Expected: `["apple","banana"]`,
|
|
|
|
},
|
|
|
|
{
|
2023-09-27 03:28:37 +00:00
|
|
|
Strings: jsonstr.CompileStrings("apple", "banana", "cherry"),
|
2023-09-27 00:12:51 +00:00
|
|
|
Expected: `["apple","banana","cherry"]`,
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{
|
2023-09-27 03:28:37 +00:00
|
|
|
Strings: jsonstr.CompileStrings("😈"),
|
2023-09-27 00:12:51 +00:00
|
|
|
Expected: `["😈"]`,
|
|
|
|
},
|
|
|
|
{
|
2023-09-27 03:28:37 +00:00
|
|
|
Strings: jsonstr.CompileStrings("😈", "🙂🙁"),
|
2023-09-27 00:12:51 +00:00
|
|
|
Expected: `["😈","🙂🙁"]`,
|
|
|
|
},
|
|
|
|
{
|
2023-09-27 03:28:37 +00:00
|
|
|
Strings: jsonstr.CompileStrings("😈", "🙂🙁", ""),
|
2023-09-27 00:12:51 +00:00
|
|
|
Expected: `["😈","🙂🙁",""]`,
|
|
|
|
},
|
|
|
|
{
|
2023-09-27 03:28:37 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|