-
Notifications
You must be signed in to change notification settings - Fork 11
/
type_test.go
140 lines (131 loc) · 3.23 KB
/
type_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2015-2017 Jean Niklas L'orange. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package edn
import (
"testing"
"testing/quick"
)
func TestRunes(t *testing.T) {
rStr := `[\a \b \c \newline \space \tab \ŋ \' \" \u002c \u002C]`
var runes []Rune
err := UnmarshalString(rStr, &runes)
if err != nil {
t.Errorf("Reading `%s` failed with following error:", rStr)
t.Error(err.Error())
} else {
actualRunes := []Rune{
'a', 'b', 'c', '\n', ' ', '\t', 'ŋ', '\'', '"', ',', ',',
}
for i := range actualRunes {
if actualRunes[i] != runes[i] {
t.Errorf("Expected rune at position %d to be %q, but was %q", i, actualRunes[i], runes[i])
}
}
}
}
func TestQuickRunes(t *testing.T) {
f := func(s string) bool {
good := true
for _, r := range []rune(s) {
bs, err := Marshal(Rune(r))
if err != nil {
t.Log(err)
good = false
continue
}
var res Rune
err = Unmarshal(bs, &res)
if err != nil {
t.Log(err)
good = false
continue
}
if rune(res) != r {
good = false
}
}
return good
}
conf := quick.Config{MaxCountScale: 100}
if testing.Short() {
conf.MaxCountScale = 5
}
if err := quick.Check(f, &conf); err != nil {
t.Error(err)
}
}
func TestTagRunes(t *testing.T) {
type Foo struct {
Value rune `edn:",rune"`
}
f := Foo{Value: ' '}
bs, err := Marshal(f)
if err != nil {
t.Fatal(err)
}
if string(bs) != `{:value\space}` {
t.Errorf("Expected result to be `{:value\\space}`, but was `%s`", string(bs))
}
f.Value = '\''
bs, err = Marshal(f)
if err != nil {
t.Fatal(err)
}
if string(bs) != `{:value\'}` {
t.Errorf("Expected result to be `{:value\\'}`, but was `%s`", string(bs))
}
}
func TestSpacing(t *testing.T) {
type Foo struct {
Value Rune `edn:",sym"`
Data Rune `edn:",sym"`
}
f := Foo{Value: Rune('a'), Data: Rune('b')}
bs, err := Marshal(f)
if err != nil {
t.Fatal(err)
}
if string(bs) != `{value \a data \b}` {
t.Errorf("Expected result to be `{value \\a data \\b}`, but was `%s`", string(bs))
}
}
func TestMarshalRawMessageValue(t *testing.T) {
type Foo struct {
SomeVal string `edn:"some-val"`
Leftovers RawMessage
OtherVal string `edn:"other-val"`
}
f := Foo{
SomeVal: "egg",
Leftovers: []byte(`[\space #foo bar :baz 100 {#{} 1.0 "zap" nil}]`),
OtherVal: "spam",
}
bs, err := Marshal(f)
if err != nil {
t.Fatal(err)
}
if string(bs) != `{:some-val"egg":leftovers [\space #foo bar :baz 100{#{}1.0"zap"nil}] :other-val"spam"}` {
t.Errorf("Expected result to be `{:some-val\"egg\":leftovers [\\space #foo bar :baz 100{#{}1.0\"zap\"nil}] :other-val\"spam\"}`, but was `%s`", string(bs))
}
}
func TestUnmarshalRawMessageValue(t *testing.T) {
type Foo struct {
SomeVal string `edn:"some-val"`
Leftovers RawMessage
OtherVal string `edn:"other-val"`
}
const raw = `{
:some-val"egg"
:leftovers [\space #foo bar :baz 100{#{} 1.0 "zap" nil}]
:other-val"spam"
}`
var f Foo
err := UnmarshalString(raw, &f)
if err != nil {
t.Fatal(err)
}
if string(f.Leftovers) != `[\space #foo bar :baz 100{#{} 1.0 "zap" nil}]` {
t.Errorf("Expected result to be `[\\space #foo bar :baz 100{#{} 1.0 \"zap\" nil}]`, but was `%s`", string(f.Leftovers))
}
}