Added marshalling tests based on json mock files

This commit is contained in:
Marius Orcsik 2018-06-07 16:48:08 +02:00
parent a28fd115cd
commit 2ecb5245ec
No known key found for this signature in database
GPG key ID: 889CE8E4FB2D877A
2 changed files with 84 additions and 0 deletions

1
tests/mocks/empty.json Normal file
View file

@ -0,0 +1 @@
{}

View file

@ -0,0 +1,83 @@
package tests
import (
"bytes"
"fmt"
"io"
"os"
"reflect"
"testing"
a "activitypub"
j "jsonld"
)
const dir = "./mocks"
var stopOnFailure = false
type testPair struct {
path string
expected bool
blank a.ObjectOrLink
result a.ObjectOrLink
}
type tests map[string]testPair
var allTests tests = tests{
"nil": testPair{
path: "./mocks/empty.json",
expected: true,
blank: a.Object{},
result: a.Object{},
},
}
func getFileContents(path string) []byte {
f, _ := os.Open(path)
data := make([]byte, 512)
io.ReadFull(f, data)
data = bytes.Trim(data, "\x00")
return data
}
func Test_ActivityPubUnmarshall(t *testing.T) {
var err error
var f = t.Errorf
if stopOnFailure {
f = t.Fatalf
}
if len(allTests) == 0 {
t.Skip("No tests found")
}
for _, pair := range allTests {
fmt.Printf("=== %s\n", pair.path)
data := getFileContents(pair.path)
object := pair.blank
err = j.Unmarshal(data, object)
if err != nil {
f("Error: %s", err)
continue
}
expLbl := ""
if !pair.expected {
expLbl = "not be "
}
if pair.expected != reflect.DeepEqual(object, pair.result) {
f("\n%#v\n should %sequal to\n%#v", object, expLbl, pair.result)
continue
}
if err == nil {
fmt.Print("--- PASS\n")
}
}
}