go-mstdn/ent/field_marshaljson_test.go

149 lines
4.3 KiB
Go
Raw Normal View History

2023-09-26 05:29:10 +00:00
package ent_test
2023-09-26 04:41:46 +00:00
import (
"testing"
"bytes"
"encoding/json"
"github.com/reiver/go-mstdn/ent"
2023-09-26 04:41:46 +00:00
)
func TestField_MarshalJSON(t *testing.T) {
tests := []struct{
2023-09-26 05:29:10 +00:00
Value ent.Field
2023-09-26 04:41:46 +00:00
Expected string
}{
{
2023-09-26 05:29:10 +00:00
Value: ent.FieldNameValue("", ""),
2023-09-26 04:41:46 +00:00
Expected:
"{" +"\n"+
"\t"+ `"name":` +`""` +"," +"\n"+
"\t"+ `"value":` +`""` +"," +"\n"+
"\t"+ `"verified_at":` +`null` +"" +"\n"+
"}" +"\n",
},
{
2023-09-26 05:29:10 +00:00
Value: ent.FieldNameValue("NAME", ""),
2023-09-26 04:41:46 +00:00
Expected:
"{" +"\n"+
"\t"+ `"name":` +`"NAME"` +"," +"\n"+
"\t"+ `"value":` +`""` +"," +"\n"+
"\t"+ `"verified_at":` +`null` +"" +"\n"+
"}" +"\n",
},
{
2023-09-26 05:29:10 +00:00
Value: ent.FieldNameValue("", "VALUE"),
2023-09-26 04:41:46 +00:00
Expected:
"{" +"\n"+
"\t"+ `"name":` +`""` +"," +"\n"+
"\t"+ `"value":` +`"VALUE"` +"," +"\n"+
"\t"+ `"verified_at":` +`null` +"" +"\n"+
"}" +"\n",
},
{
2023-09-26 05:29:10 +00:00
Value: ent.FieldNameValue("NAME", "VALUE"),
2023-09-26 04:41:46 +00:00
Expected:
"{" +"\n"+
"\t"+ `"name":` +`"NAME"` +"," +"\n"+
"\t"+ `"value":` +`"VALUE"` +"," +"\n"+
"\t"+ `"verified_at":` +`null` +"" +"\n"+
"}" +"\n",
},
{
2023-09-26 05:29:10 +00:00
Value: ent.FieldNameValue("age", "47"),
2023-09-26 04:41:46 +00:00
Expected:
"{" +"\n"+
"\t"+ `"name":` +`"age"` +"," +"\n"+
"\t"+ `"value":` +`"47"` +"," +"\n"+
"\t"+ `"verified_at":` +`null` +"" +"\n"+
"}" +"\n",
},
{
2023-09-26 05:29:10 +00:00
Value: ent.FieldVerifiedNameValue("2023-09-22T22:45:22Z", "NAME", ""),
2023-09-26 04:41:46 +00:00
Expected:
"{" +"\n"+
"\t"+ `"name":` +`"NAME"` +"," +"\n"+
"\t"+ `"value":` +`""` +"," +"\n"+
"\t"+ `"verified_at":` +`"2023-09-22T22:45:22Z"` +"" +"\n"+
"}" +"\n",
},
{
2023-09-26 05:29:10 +00:00
Value: ent.FieldVerifiedNameValue("2023-09-22T22:45:22Z", "", "VALUE"),
2023-09-26 04:41:46 +00:00
Expected:
"{" +"\n"+
"\t"+ `"name":` +`""` +"," +"\n"+
"\t"+ `"value":` +`"VALUE"` +"," +"\n"+
"\t"+ `"verified_at":` +`"2023-09-22T22:45:22Z"` +"" +"\n"+
"}" +"\n",
},
{
2023-09-26 05:29:10 +00:00
Value: ent.FieldVerifiedNameValue("2023-09-22T22:45:22Z", "NAME", "VALUE"),
2023-09-26 04:41:46 +00:00
Expected:
"{" +"\n"+
"\t"+ `"name":` +`"NAME"` +"," +"\n"+
"\t"+ `"value":` +`"VALUE"` +"," +"\n"+
"\t"+ `"verified_at":` +`"2023-09-22T22:45:22Z"` +"" +"\n"+
"}" +"\n",
},
{
2023-09-26 05:29:10 +00:00
Value: ent.FieldVerifiedNameValue("2023-09-22T22:45:22Z", "age", "47"),
2023-09-26 04:41:46 +00:00
Expected:
"{" +"\n"+
"\t"+ `"name":` +`"age"` +"," +"\n"+
"\t"+ `"value":` +`"47"` +"," +"\n"+
"\t"+ `"verified_at":` +`"2023-09-22T22:45:22Z"` +"" +"\n"+
"}" +"\n",
},
}
for testNumber, test := range tests {
actualBytes, err := test.Value.MarshalJSON()
if nil != err {
t.Errorf("For test #%d, did not expect to get an error but actually got one.", testNumber)
t.Logf("ERROR: (%T) %s", err, err)
t.Logf("VALUE: %#v", test.Value)
continue
}
actual := string(actualBytes)
var expected string
{
var buffer bytes.Buffer
err := json.Compact(&buffer, []byte(test.Expected))
if nil != err {
panic(err)
}
expected = buffer.String()
}
if expected != actual {
t.Errorf("For test #%d, the actual value is not what was expected.", testNumber)
t.Logf("EXPECTED:\n%s", expected)
t.Logf("ACTUAL:\n%s", actual)
t.Logf("EXPECTED: %q", expected)
t.Logf("ACTUAL: %q", actual)
t.Logf("VALUE: %#v", test.Value)
continue
}
}
}