go-mstdn/ent/field_unmarshaljson_test.go

55 lines
1.3 KiB
Go
Raw Normal View History

2023-09-26 05:29:10 +00:00
package ent_test
2023-09-26 04:49:02 +00:00
import (
"testing"
"encoding/json"
"github.com/reiver/go-mstdn/ent"
2023-09-26 04:49:02 +00:00
)
func TestField_UnmarshalJSON(t *testing.T) {
tests := []struct{
JSON string
2023-09-26 05:29:10 +00:00
Expected ent.Field
2023-09-26 04:49:02 +00:00
}{
{
JSON: `{"name":"Location","value":"Metro Vancouver"}`,
2023-09-26 05:29:10 +00:00
Expected: ent.FieldNameValue("Location", "Metro Vancouver"),
2023-09-26 04:49:02 +00:00
},
{
JSON: `{"name":"Location","value":"Metro Vancouver", "verified_at":null}`,
2023-09-26 05:29:10 +00:00
Expected: ent.FieldNameValue("Location", "Metro Vancouver"),
2023-09-26 04:49:02 +00:00
},
{
JSON: `{"name":"Location","value":"Metro Vancouver", "verified_at":"2023-09-22T22:45:22Z"}`,
2023-09-26 05:29:10 +00:00
Expected: ent.FieldVerifiedNameValue("2023-09-22T22:45:22Z", "Location", "Metro Vancouver"),
2023-09-26 04:49:02 +00:00
},
}
for testNumber, test := range tests {
2023-09-26 05:29:10 +00:00
var actual ent.Field
2023-09-26 04:49:02 +00:00
err := json.Unmarshal([]byte(test.JSON), &actual)
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("JSON: %q", test.JSON)
t.Logf("EXPECTED %#v", test.Expected)
continue
}
expected := test.Expected
if expected != actual {
t.Errorf("For test #%d, the actual value is not what was expected.", testNumber)
t.Logf("EXPECTED: %#v", expected)
t.Logf("ACTUAL: %#v", actual)
t.Logf("JSON: %q", test.JSON)
continue
}
}
}