forked from sleeyax/aternos-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_test.go
68 lines (52 loc) · 1.04 KB
/
data_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
66
67
68
package aternos_api
import (
"encoding/json"
"testing"
)
type DataContent struct {
Foo string `json:"foo"`
Bar int `json:"bar"`
}
type Example struct {
Id int `json:"id"`
Data Data `json:"data"`
}
func TestData(t *testing.T) {
raw := `{"id": 0, "data": {"foo": "hello world!", "bar": 123}}`
var example Example
if err := json.Unmarshal([]byte(raw), &example); err != nil {
t.Fatal(err)
}
var dataContent DataContent
if err := json.Unmarshal(example.Data.ContentBytes, &dataContent); err != nil {
t.Fatal(err)
}
if dataContent.Foo != "hello world!" {
t.Fail()
}
if dataContent.Bar != 123 {
t.Fail()
}
}
func TestData_MarshalJSON(t *testing.T) {
data := &Data{
Content: "hello world!",
}
serialized, err := json.Marshal(data)
if err != nil {
t.Fatal(err)
}
str := string(serialized)
if str != `"hello world!"` {
t.FailNow()
}
data.Content = `{"foo": "bar"}`
serialized, err = json.Marshal(data)
if err != nil {
t.Fatal(err)
}
str = string(serialized)
if str != `{"foo":"bar"}` {
t.FailNow()
}
}