Debian Codegraph @

decode_test.go

raw symbols SCIP
1 package jsonld
2
3 import (
4 "strconv"
5 "testing"
6 )
7
8 func TestUnmarshalWithEmptyJsonObject(t *testing.T) {
9 obj := mockTypeA{}
10 err := Unmarshal([]byte("{}"), &obj)
11 if err != nil {
12 t.Error(err)
13 }
14 if obj.Id != "" {
15 t.Errorf("Id should have been an empty string, found %s", obj.Id)
16 }
17 if obj.Name != "" {
18 t.Errorf("Name should have been an empty string, found %s", obj.Name)
19 }
20 if obj.Type != "" {
21 t.Errorf("Type should have been an empty string, found %s", obj.Type)
22 }
23 if obj.PropA != "" {
24 t.Errorf("PropA should have been an empty string, found %s", obj.PropA)
25 }
26 if obj.PropB != 0 {
27 t.Errorf("PropB should have been 0.0, found %f", obj.PropB)
28 }
29 }
30
31 type mockWithContext struct {
32 mockTypeA
33 Context Context `jsonld:"@context"`
34 }
35
36 func TestUnmarshalWithEmptyJsonObjectWithStringContext(t *testing.T) {
37 obj := mockWithContext{}
38 url := "http://www.habarnam.ro"
39 data := []byte(`{"@context": "` + url + `" }`)
40 err := Unmarshal(data, &obj)
41 if err != nil {
42 t.Error(err)
43 }
44 if obj.Id != "" {
45 t.Errorf("Id should have been an empty string, found %s", obj.Id)
46 }
47 if obj.Name != "" {
48 t.Errorf("Name should have been an empty string, found %s", obj.Name)
49 }
50 if obj.Type != "" {
51 t.Errorf("Type should have been an empty string, found %s", obj.Type)
52 }
53 if obj.PropA != "" {
54 t.Errorf("PropA should have been an empty string, found %s", obj.PropA)
55 }
56 if obj.PropB != 0 {
57 t.Errorf("PropB should have been 0.0, found %f", obj.PropB)
58 }
59 }
60
61 func TestUnmarshalWithEmptyJsonObjectWithObjectContext(t *testing.T) {
62 obj := mockWithContext{}
63 url := "http://www.habarnam.ro"
64 data := []byte(`{"@context": { "@url": "` + url + `"} }`)
65 err := Unmarshal(data, &obj)
66 if err != nil {
67 t.Error(err)
68 }
69 if obj.Id != "" {
70 t.Errorf("Id should have been an empty string, found %s", obj.Id)
71 }
72 if obj.Name != "" {
73 t.Errorf("Name should have been an empty string, found %s", obj.Name)
74 }
75 if obj.Type != "" {
76 t.Errorf("Type should have been an empty string, found %s", obj.Type)
77 }
78 if obj.PropA != "" {
79 t.Errorf("PropA should have been an empty string, found %s", obj.PropA)
80 }
81 if obj.PropB != 0 {
82 t.Errorf("PropB should have been 0.0, found %f", obj.PropB)
83 }
84 }
85
86 func TestUnmarshalWithEmptyJsonObjectWithOneLanguageContext(t *testing.T) {
87 obj := mockWithContext{}
88 url := "http://www.habarnam.ro"
89 langEn := "en-US"
90
91 data := []byte(`{"@context": { "@url": "` + url + `", "@language": "` + langEn + `"} }`)
92 err := Unmarshal(data, &obj)
93 if err != nil {
94 t.Error(err)
95 }
96 if obj.Id != "" {
97 t.Errorf("Id should have been an empty string, found %s", obj.Id)
98 }
99 if obj.Name != "" {
100 t.Errorf("Name should have been an empty string, found %s", obj.Name)
101 }
102 if obj.Type != "" {
103 t.Errorf("Type should have been an empty string, found %s", obj.Type)
104 }
105 if obj.PropA != "" {
106 t.Errorf("PropA should have been an empty string, found %s", obj.PropA)
107 }
108 if obj.PropB != 0 {
109 t.Errorf("PropB should have been 0.0, found %f", obj.PropB)
110 }
111 }
112 func TestUnmarshalWithEmptyJsonObjectWithFullObject(t *testing.T) {
113 obj := mockWithContext{}
114 url := "http://www.habarnam.ro"
115 langEn := "en-US"
116 propA := "ana"
117 var propB float32 = 6.66
118 typ := "test"
119 name := "test object #1"
120 id := "777sdad"
121
122 data := []byte(`{
123 "@context": { "@url": "` + url + `", "@language": "` + langEn + `"},
124 "PropA": "` + propA + `",
125 "PropB": ` + strconv.FormatFloat(float64(propB), 'f', 2, 32) + `,
126 "Id" : "` + id + `",
127 "Name" : "` + name + `",
128 "Type" : "` + typ + `"
129 }`)
130 err := Unmarshal(data, &obj)
131 if err != nil {
132 t.Error(err)
133 }
134 if obj.Id != id {
135 t.Errorf("Id should have been %q, found %q", id, obj.Id)
136 }
137 if obj.Name != name {
138 t.Errorf("Name should have been %q, found %q", name, obj.Name)
139 }
140 if obj.Type != typ {
141 t.Errorf("Type should have been %q, found %q", typ, obj.Type)
142 }
143 if obj.PropA != propA {
144 t.Errorf("PropA should have been %q, found %q", propA, obj.PropA)
145 }
146 if obj.PropB != propB {
147 t.Errorf("PropB should have been %f, found %f", propB, obj.PropB)
148 }
149 }