This repository has been archived by the owner on Jul 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sexpr_test.go
127 lines (119 loc) · 2.94 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
//-----------------------------------------------------------------------------
// Copyright (c) 2022 Detlef Stern
//
// This file is part of sxpf.
//
// sxpf is licensed under the latest version of the EUPL // (European Union
// Public License). Please see file LICENSE.txt for your rights and obligations
// under this license.
//-----------------------------------------------------------------------------
package sxpf_test
import (
"testing"
"github.com/t73fde/sxpf"
)
func TestSymbol(t *testing.T) {
t.Parallel()
testcases := []struct {
val string
ok bool
exp string
}{
{"", false, ""},
{"a", true, "A"},
}
smk := sxpf.NewTrivialSymbolMaker()
for i, tc := range testcases {
s := smk.MakeSymbol(tc.val)
if (s != nil) != tc.ok {
if s == nil {
t.Errorf("%d: NewSymbol(%q) must not be nil, but is", i, tc.val)
} else {
t.Errorf("%d: NewSymbol(%q) must be nil, but is not: %q", i, tc.val, s.GetValue())
}
continue
}
if s == nil {
continue
}
got := s.GetValue()
if tc.exp != got {
t.Errorf("%d: GetValue(%q) != %q, but got %q", i, tc.val, tc.exp, got)
}
if !s.Equal(s) {
t.Errorf("%d: %q is not equal to itself", i, got)
}
s2 := smk.MakeSymbol(tc.val)
if s2 != s {
t.Errorf("%d: NewSymbol(%q) produces different values if called multiple times", i, tc.val)
}
}
}
func FuzzSymbol(f *testing.F) {
smk := sxpf.NewTrivialSymbolMaker()
f.Fuzz(func(t *testing.T, in string) {
t.Parallel()
s := smk.MakeSymbol(in)
if !s.Equal(s) {
if s == nil {
t.Errorf("nil symbol is not equal to itself")
} else {
t.Errorf("%q is not equal to itself", s.GetValue())
}
}
})
}
func TestStringString(t *testing.T) {
t.Parallel()
testcases := []struct {
val string
exp string
}{
{"", ""},
{"a", "a"},
{"\n", "\\n"},
}
for i, tc := range testcases {
s := sxpf.NewString(tc.val)
if s == nil {
t.Errorf("%d: NewString(%q) == nil", i, tc.val)
continue
}
sVal := s.GetValue()
if sVal != tc.val {
t.Errorf("%d: NewString(%q) changed value to %q", i, tc.val, sVal)
continue
}
got := s.String()
if length := len(got); length < 2 {
t.Errorf("%d: len(String(%q)) < 2: %q (%d)", i, tc.val, got, length)
continue
}
exp := "\"" + tc.exp + "\""
if got != exp {
t.Errorf("%d: String(%q) expected %q, but got %q", i, tc.val, exp, got)
}
}
}
func TestNewList(t *testing.T) {
t.Parallel()
st := sxpf.NewSymbolTable()
symA, symB, symC, symD := st.MakeSymbol("a"), st.MakeSymbol("b"), st.MakeSymbol("c"), st.MakeSymbol("d")
testcases := []struct {
values []sxpf.Value
exp string
}{
{[]sxpf.Value{}, "()"},
{[]sxpf.Value{symA}, "(A)"},
{[]sxpf.Value{symA, symB}, "(A B)"},
{[]sxpf.Value{symA, symB, symC}, "(A B C)"},
{[]sxpf.Value{symA, symB, symC, symD}, "[A B C D]"},
}
for i, tc := range testcases {
lst := sxpf.NewSequence(tc.values...)
got := lst.String()
if got != tc.exp {
t.Errorf("%d: expected %q, but got %q", i, tc.exp, got)
}
}
}