Debian Codegraph @

encode_test.go

raw symbols SCIP
1 package jsonld
2
3 import (
4 "bytes"
5 "fmt"
6 "reflect"
7 "strings"
8 "testing"
9 )
10
11 type mockBase struct {
12 Id string
13 Name string
14 Type string
15 }
16
17 type mockTypeA struct {
18 mockBase
19 PropA string
20 PropB float32
21 }
22
23 func TestMarshal(t *testing.T) {
24 a := mockTypeA{mockBase{"base_id", "MockObjA", "mock_obj"}, "prop_a", 0.001}
25 b := mockTypeA{}
26
27 url := "http://www.habarnam.ro"
28 p := WithContext(IRI(url))
29
30 var err error
31 var out []byte
32
33 out, err = p.Marshal(a)
34 if err != nil {
35 t.Errorf("%s", err)
36 }
37 if !strings.Contains(string(out), string(ContextKw)) {
38 t.Errorf("Context name not found %q in %s", ContextKw, out)
39 }
40 if !strings.Contains(string(out), url) {
41 t.Errorf("Context url not found %q in %s", url, out)
42 }
43 err = Unmarshal(out, &b)
44 if err != nil {
45 t.Errorf("%s", err)
46 }
47 if a.Id != b.Id {
48 t.Errorf("Id isn't equal %q expected %q in %s", a.Id, b.Id, out)
49 }
50 if a.Name != b.Name {
51 t.Errorf("Name isn't equal %q expected %q", a.Name, b.Name)
52 }
53 if a.Type != b.Type {
54 t.Errorf("Type isn't equal %q expected %q", a.Type, b.Type)
55 }
56 if a.PropA != b.PropA {
57 t.Errorf("PropA isn't equal %q expected %q", a.PropA, b.PropA)
58 }
59 if a.PropB != b.PropB {
60 t.Errorf("PropB isn't equal %f expected %f", a.PropB, b.PropB)
61 }
62 }
63
64 func TestMarshalNullContext(t *testing.T) {
65 var a = struct {
66 PropA string
67 PropB float64
68 }{"test", 0.0004}
69
70 outL, errL := Marshal(a)
71 if errL != nil {
72 t.Errorf("%s", errL)
73 }
74 outJ, errJ := Marshal(a)
75 if errJ != nil {
76 t.Errorf("%s", errJ)
77 }
78 if !bytes.Equal(outL, outJ) {
79 t.Errorf("Json output should be equal %q, received %q", outL, outJ)
80 }
81 }
82
83 func TestMarshalNullValue(t *testing.T) {
84 var a = any(nil)
85
86 url := "http://www.habarnam.ro"
87 p := WithContext(IRI(url))
88 outL, errL := p.Marshal(a)
89 if errL != nil {
90 t.Errorf("%s", errL)
91 }
92 outJ := []byte(fmt.Sprintf(`{"@context":"%s"}`, url))
93 if !bytes.Equal(outL, outJ) {
94 t.Errorf("JsonLD output is wrong: %s, expected %s", outL, outJ)
95 }
96 }
97
98 func TestMarshalSliceValue(t *testing.T) {
99 var a = any([]IRI{"http://example.com", "http://example.com/1"})
100
101 url := "http://www.habarnam.ro"
102 p := WithContext(IRI(url))
103 outL, errL := p.Marshal(a)
104 if errL != nil {
105 t.Errorf("%s", errL)
106 }
107 // NOTE(marius): currently for slices we strip the context
108 outJ := []byte(`["http://example.com","http://example.com/1"]`)
109 if !bytes.Equal(outL, outJ) {
110 t.Errorf("JsonLD output is wrong: %s, expected %s", outL, outJ)
111 }
112 }
113
114 func TestIsEmpty(t *testing.T) {
115 var a int
116 if !isEmptyValue(reflect.ValueOf(a)) {
117 t.Errorf("Invalid empty value %v", a)
118 }
119 if !isEmptyValue(reflect.ValueOf(uint(a))) {
120 t.Errorf("Invalid empty value %v", uint(a))
121 }
122 var b float64
123 if !isEmptyValue(reflect.ValueOf(b)) {
124 t.Errorf("Invalid empty value %v", b)
125 }
126 var c string
127 if !isEmptyValue(reflect.ValueOf(c)) {
128 t.Errorf("Invalid empty value %s", c)
129 }
130 var d []byte
131 if !isEmptyValue(reflect.ValueOf(d)) {
132 t.Errorf("Invalid empty value %v", d)
133 }
134 var e *any
135 if !isEmptyValue(reflect.ValueOf(e)) {
136 t.Errorf("Invalid empty value %v", e)
137 }
138 g := false
139 if !isEmptyValue(reflect.ValueOf(g)) {
140 t.Errorf("Invalid empty value %v", g)
141 }
142 h := true
143 if isEmptyValue(reflect.ValueOf(h)) {
144 t.Errorf("Invalid empty value %v", h)
145 }
146 }
147
148 func TestWithContext_MarshalJSON(t *testing.T) {
149 tv := "value_test"
150 v := struct{ Test string }{Test: tv}
151
152 data, err := WithContext(IRI("http://example.com")).Marshal(v)
153 if err != nil {
154 t.Error(err)
155 }
156 if !bytes.Contains(data, []byte(ContextKw)) {
157 t.Errorf("%q not found in %s", ContextKw, data)
158 }
159 m := reflect.TypeOf(v)
160 mv := reflect.ValueOf(v)
161 for i := 0; i < m.NumField(); i++ {
162 f := m.Field(i)
163 v := mv.Field(i)
164 if !bytes.Contains(data, []byte(f.Name)) {
165 t.Errorf("%q not found in %s", f.Name, data)
166 }
167 if !bytes.Contains(data, []byte(v.String())) {
168 t.Errorf("%q not found in %s", v.String(), data)
169 }
170 }
171 }