forked from go-edn/edn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode_test.go
51 lines (42 loc) · 1.13 KB
/
encode_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
// Copyright 2015 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 (
"bytes"
"testing"
)
func TestEncoding(t *testing.T) {
testEncode(t, Keyword("foo"), ":foo")
testEncode(t, Symbol("foo"), "foo")
testEncode(t, "foo", `"foo"`)
var val interface{}
val = struct {
Foo int `edn:"quux"`
}{10}
testEncode(t, val, "{:quux 10}")
val = struct {
Bar [2]int `edn:",sym,list"`
}{[2]int{3, 4}}
testEncode(t, val, "{bar(3 4)}")
val = struct {
Comp string `edn:",str"`
}{"ressed"}
testEncode(t, val, `{"comp""ressed"}`)
val = struct {
TheSet [2]int `edn:"the-set,set,sym"`
Slice []string `edn:",set"`
}{
[2]int{3, 4},
[]string{"foo", "bar"},
}
testEncode(t, val, `{the-set #{3 4}:slice #{"foo""bar"}}`)
}
func testEncode(t *testing.T, val interface{}, expects string) {
bs, err := Marshal(val)
if err != nil {
t.Errorf("Unexpected error marshalling %q: %s", val, err.Error())
} else if !bytes.Equal([]byte(expects), bs) {
t.Errorf("Expected to see '%s', but got '%s' instead", expects, string(bs))
}
}