-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_test.go
166 lines (151 loc) · 4.07 KB
/
parse_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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright © 2019 Luis Ángel Méndez Gort
// This file is part of Predicate.
// Predicate is free software: you can redistribute it and/or
// modify it under the terms of the GNU Lesser General
// Public License as published by the Free Software
// Foundation, either version 3 of the License, or (at your
// option) any later version.
// Predicate is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A
// PARTICULAR PURPOSE. See the GNU Lesser General Public
// License for more details.
// You should have received a copy of the GNU Lesser General
// Public License along with Predicate. If not, see
// <https://www.gnu.org/licenses/>.
package predicate
import (
"errors"
alg "github.com/lamg/algorithms"
"github.com/stretchr/testify/require"
"strings"
"testing"
)
func TestScan(t *testing.T) {
txt := "true¬∧∨≡≢⇒⇐()bla9 x3 (Abla)true"
tks := []string{"true",
NotOp, AndOp, OrOp, EquivalesOp, NotEquivalesOp,
ImpliesOp, FollowsOp, OPar, CPar, "bla9", "", "x3", "",
"(", "Abla", ")", "true"}
ss := []scanner{
strScan(NotOp),
strScan(AndOp),
strScan(OrOp),
strScan(EquivalesOp),
strScan(NotEquivalesOp),
strScan(ImpliesOp),
strScan(FollowsOp),
strScan(OPar),
strScan(CPar),
identScan,
spaceScan,
}
testScan(t, ss, txt, tks)
}
func testScan(t *testing.T, ss []scanner, txt string,
tks []string) {
scan := tokens(strings.NewReader(txt), ss)
inf := func(i int) {
tk, e := scan()
require.NoError(t, e)
require.Equal(t, tks[i], tk.value)
t.Log(tk)
}
alg.Forall(inf, len(tks))
}
func TestFuncPointer(t *testing.T) {
var f func()
g := func() {
f()
}
a := false
f = func() { a = true }
g()
require.True(t, a)
}
func TestStrScan(t *testing.T) {
ns := strScan(NotOp)()
tk, cont, prod := ns('¬')
require.True(t, prod)
require.True(t, cont)
require.Equal(t, "¬", tk.value)
}
func TestIdentScan(t *testing.T) {
ids := identScan()
rs := []rune{'a', 'b', 'c', '0'}
var tk *token
var cont, prod bool
inf := func(i int) {
_, cont, prod = ids(rs[i])
require.True(t, cont)
require.False(t, prod)
}
alg.Forall(inf, len(rs))
tk, cont, prod = ids(' ')
require.False(t, cont)
require.True(t, prod)
require.Equal(t, "abc0", tk.value)
ids0 := identScan()
_, cont, prod = ids0(' ')
require.False(t, cont)
require.False(t, prod)
}
func TestParseOp(t *testing.T) {
ps := []string{"a", "¬a", "a ∧ b", "a ∧ b ∧ ¬c ∧ d"}
ss := []scanner{spaceScan, identScan, strScan(AndOp),
strScan(NotOp)}
inf := func(i int) {
rd := strings.NewReader(ps[i])
st := &predState{tkf: tokens(rd, ss)}
p, e := st.parseOp("factor", st.factor(), false, AndOp)()
require.NoError(t, e, "At %d", i)
require.Equal(t, ps[i], String(p), "At %d", i)
}
alg.Forall(inf, len(ps))
}
func TestParse(t *testing.T) {
errEof := &NotRecognizedErr{
String: string(eof),
Expecting: []string{Identifier, OPar},
}
errAnd := &NotRecognizedErr{String: "∧", Expecting: []string{"∨"}}
errFl := &NotRecognizedErr{String: "⇐", Expecting: []string{"⇒"}}
ps := []struct {
pred string
e error
}{
{"", errEof},
{"true ∧ false", nil},
{"true ∧", errEof},
{"¬A", nil},
{"¬A ∧ (B ∨ C)", nil},
{"A ∨ ¬(B ∧ C)", nil},
{"A ≡ B ≢ ¬C ⇒ D", nil},
{"A ≡ B ≡ ¬C ⇐ D", nil},
{"A ≡ B ≡ ¬(C ⇐ D)", nil},
{"A ∨ B ∨ C", nil},
{"A ∨ B ∧ C", errAnd},
{"A ⇒ B ⇐ C", errFl},
{"A ∨ (B ∧ C)", nil},
{"A ⇒ (B ⇐ C)", nil},
{"a ≡ b ≢ c ≡ ¬x ∧ (¬z ≡ y) ≢ true", nil},
}
inf := func(i int) {
np, e := Parse(strings.NewReader(ps[i].pred))
require.Equal(t, e == nil, ps[i].e == nil,
"At %d: %s %v", i, ps[i].pred, e)
if e == nil {
s := String(np)
t.Logf("'%s'", s)
require.Equal(t, ps[i].pred, s, "At %d", i)
require.True(t, np.Valid())
} else {
t.Logf("'%s' → %s", ps[i].pred, e.Error())
var nr *NotRecognizedErr
require.True(t, errors.As(e, &nr), "At %d '%s' ≠ '%s'", i,
e.Error(), nr)
require.Equal(t, ps[i].e, nr)
}
}
alg.Forall(inf, len(ps))
}