-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonapi_test.go
65 lines (61 loc) · 1.4 KB
/
jsonapi_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package jsonapi
import (
"bytes"
"testing"
)
func TestSetJSONPrefix(t *testing.T) {
def := ""
if def != jsonPrefix {
t.Errorf("default jsonPrefix was incorrect, got: %s, want: %s.", jsonPrefix, "")
}
space := " "
SetJSONPrefix(space)
if space != jsonPrefix {
t.Errorf("space jsonPrefix was incorrect, got: %s, want: %s.", jsonPrefix, " ")
}
SetJSONPrefix(def)
}
func TestSetJSONIndent(t *testing.T) {
def := "\t"
if def != jsonIndent {
t.Errorf("default jsonIndent was incorrect, got: %s, want: %s.", jsonIndent, "\t")
}
space := " "
SetJSONIndent(space)
if space != jsonIndent {
t.Errorf("space jsonIndent was incorrect, got: %s, want: %s.", jsonIndent, " ")
}
SetJSONIndent(def)
}
func TestSetTagKey(t *testing.T) {
SetTagKey("customKey")
type Article struct {
ID string `customKey:"primary,articles"`
Title string `customKey:"attribute,title"`
}
article := Article{
ID: "article-id",
Title: "Hello World!",
}
articleExpected := []byte(`{
"data": {
"id": "article-id",
"type": "articles",
"attributes": {
"title": "Hello World!"
}
},
"jsonapi": {
"version": "1.0"
}
}`)
if got, err := Marshal(&article, nil); err != nil {
t.Errorf(err.Error())
} else {
if bytes.Compare(got, articleExpected) != 0 {
t.Errorf("Expected:\n%s\nGot:\n%s\n", string(articleExpected), string(got))
}
}
// TODO fix this so we don't have to reset the key
SetTagKey("jsonapi")
}