Sort @context when testing

Eliminates flakiness.
This commit is contained in:
Cory Slep 2019-10-23 19:06:19 +02:00
parent 84ee361478
commit 9acafe5f97
3 changed files with 42 additions and 5 deletions

View File

@ -5837,8 +5837,8 @@ func example159Type() vocab.ActivityStreamsMove {
const personExampleWithPublicKey = `{
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1"
"https://w3id.org/security/v1",
"https://www.w3.org/ns/activitystreams"
],
"id":"https://mastodon.technology/users/cj",
"type":"Person",

View File

@ -6,6 +6,7 @@ import (
"github.com/go-fed/activity/streams/vocab"
"github.com/go-test/deep"
"net/url"
"sort"
"testing"
)
@ -28,6 +29,42 @@ func IsKnownResolverError(t TestTable) (isError bool, reason string) {
return
}
// SerializeForTest calls Serialize and stabilizes the @context value for
// testing purposes.
func SerializeForTest(a vocab.Type) (m map[string]interface{}, e error) {
m, e = Serialize(a)
if e != nil {
return
}
ctx, ok := m["@context"]
if !ok {
return
}
arr, ok := ctx.([]interface{})
if !ok {
return
}
var s []string
var o []interface{}
for _, v := range arr {
if str, ok := v.(string); ok {
s = append(s, str)
} else {
o = append(o, v)
}
}
sort.Sort(sort.StringSlice(s))
newArr := make([]interface{}, 0, len(arr))
for _, v := range s {
newArr = append(newArr, v)
}
for _, v := range o {
newArr = append(newArr, v)
}
m["@context"] = newArr
return
}
// PostSerializationAdjustment is needed in rare cases when a test example
// requires post processing on the serialized map to match expectations.
func PostSerializationAdjustment(t TestTable, m map[string]interface{}) (map[string]interface{}, string) {
@ -54,7 +91,7 @@ func makeResolver(t *testing.T, tc TestTable, expected []byte) (*JSONResolver, e
if t != nil {
resFn = func(s vocab.Type) error {
m, err := Serialize(s)
m, err := SerializeForTest(s)
if err != nil {
return err
}
@ -449,7 +486,7 @@ func TestNulls(t *testing.T) {
t.Log(d)
}
}
m, err = Serialize(actual)
m, err = SerializeForTest(actual)
if err != nil {
t.Errorf("Cannot Serialize: %v", err)
return

View File

@ -1367,7 +1367,7 @@ func TestSerialization(t *testing.T) {
m := make(map[string]interface{})
var err error
if r.expectedStruct != nil {
m, err = Serialize(r.expectedStruct)
m, err = SerializeForTest(r.expectedStruct)
if err != nil {
t.Errorf("Cannot Serialize: %v", err)
return