forked from torbiak/gopl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sexpr_test.go
140 lines (132 loc) · 3.29 KB
/
sexpr_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 © 2016 Alan A. A. Donovan & Brian W. Kernighan.
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
package sexpr
import (
"fmt"
"reflect"
"testing"
)
// Test verifies that encoding and decoding a complex data value
// produces an equal result.
//
// The test does not make direct assertions about the encoded output
// because the output depends on map iteration order, which is
// nondeterministic. The output of the t.Log statements can be
// inspected by running the test with the -v flag:
//
// $ go test -v gopl.io/ch12/sexpr
//
func Test(t *testing.T) {
type Movie struct {
Title, Subtitle string
Year int
Actor map[string]string
Oscars []string
Sequel *string
}
strangelove := Movie{
Title: "Dr. Strangelove",
Subtitle: "How I Learned to Stop Worrying and Love the Bomb",
Year: 1964,
Actor: map[string]string{
"Dr. Strangelove": "Peter Sellers",
"Grp. Capt. Lionel Mandrake": "Peter Sellers",
"Pres. Merkin Muffley": "Peter Sellers",
"Gen. Buck Turgidson": "George C. Scott",
"Brig. Gen. Jack D. Ripper": "Sterling Hayden",
`Maj. T.J. "King" Kong`: "Slim Pickens",
},
Oscars: []string{
"Best Actor (Nomin.)",
"Best Adapted Screenplay (Nomin.)",
"Best Director (Nomin.)",
"Best Picture (Nomin.)",
},
}
// Encode it
data, err := Marshal(strangelove)
if err != nil {
t.Fatalf("Marshal failed: %v", err)
}
t.Logf("Marshal() = %s\n", data)
fmt.Println(string(data))
// Decode it
var movie Movie
if err := Unmarshal(data, &movie); err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
t.Logf("Unmarshal() = %+v\n", movie)
// Check equality.
if !reflect.DeepEqual(movie, strangelove) {
t.Fatal("not equal")
}
}
func TestMarshal(t *testing.T) {
type Interface interface{}
type Record struct {
B bool
F32 float32
F64 float64
C64 complex64
C128 complex128
I Interface
}
tests := []struct {
r Record
want string
}{
{
Record{true, 2.5, 0, 1 + 2i, 2 + 3i, Interface(5)},
`((B t) (F32 2.5) (F64 0) (C64 #C(1 2)) (C128 #C(2 3)) (I ("sexpr.Interface" 5)))`,
},
{
Record{false, 0, 1.5, 0, 1i, Interface(0)},
`((B nil) (F32 0) (F64 1.5) (C64 #C(0 0)) (C128 #C(0 1)) (I ("sexpr.Interface" 0)))`,
},
}
for _, test := range tests {
data, err := Marshal(test.r)
s := string(data)
if err != nil {
t.Errorf("Marshal(%s): %s", s, err)
}
if s != test.want {
t.Errorf("Marshal(%#v) got %s, wanted %s", test.r, s, test.want)
}
}
}
func TestUnmarshal(t *testing.T) {
type Interface interface{}
type Record struct {
B bool
F32 float32
F64 float64
C64 complex64
C128 complex128
I Interface
}
Interfaces["sexpr.Interface"] = reflect.TypeOf(int(0))
tests := []struct {
s string
want Record
}{
{
`((B t) (F32 2.5) (F64 0) (I ("sexpr.Interface" 5)))`,
Record{true, 2.5, 0, 0, 0, Interface(5)},
},
{
`((B nil) (F32 0) (F64 1.5) (I ("sexpr.Interface" 0)))`,
Record{false, 0, 1.5, 0, 0, Interface(0)},
},
}
for _, test := range tests {
var r Record
err := Unmarshal([]byte(test.s), &r)
if err != nil {
t.Errorf("Unmarshal(%q): %s", test.s, err)
}
if !reflect.DeepEqual(r, test.want) {
t.Errorf("Unmarshal(%q) got %#v, wanted %#v", test.s, r, test.want)
}
}
}