/
parsemath_test.go
148 lines (124 loc) · 3.04 KB
/
parsemath_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
// Copyright 2013 The gopp AUTHORS. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gopp_test
import (
"fmt"
"github.com/skelterjohn/gopp"
"strings"
"testing"
)
// tests where we create a grammar and parse a document
const mathgopp = `
# The root is an equation, with a left-hand and right-hand side.
Eqn => {type=MathEqn} {field=Left} <<Expr>> '=' {field=Right} <<Expr>> '\n'
# An Expr is either the sum of two terms,
Expr => {type=MathSum} {field=First} <<Term>> '+' {field=Second} <<Term>>
# or just another term.
Expr => <Term>
# A Term is either the product of two factors,
Term => {type=MathProduct} {field=First} <<Factor>> '*' {field=Second} <<Factor>>
# or just another factor.
Term => <Factor>
# A factor is either a parenthesized expression,
Factor => {type=MathExprFactor} '(' {field=Expr} <<Expr>> ')'
# or just a number.
Factor => {type=MathNumberFactor} {field=Number} <number>
# A number is a string of consecutive digits.
number = /(\d+)/
`
type MathEqn struct {
Left, Right interface{}
}
func (e MathEqn) String() string {
return fmt.Sprintf("%d=%d", e.Left, e.Right)
}
type MathSum struct {
First, Second interface{}
}
func (s MathSum) String() string {
return fmt.Sprintf("%d+%d", s.First, s.Second)
}
type MathProduct struct {
First, Second interface{}
}
func (p MathProduct) String() string {
return fmt.Sprintf("%d*%d", p.First, p.Second)
}
type MathExprFactor struct {
Expr interface{}
}
func (ef MathExprFactor) String() string {
return fmt.Sprintf("(%d)", ef.Expr)
}
type MathNumberFactor struct {
Number int
}
func (nf MathNumberFactor) String() string {
return fmt.Sprint(nf.Number)
}
func TestMath(t *testing.T) {
df, err := gopp.NewDecoderFactory(mathgopp, "Eqn")
if err != nil {
t.Error(err)
return
}
df.RegisterType(MathExprFactor{})
df.RegisterType(MathNumberFactor{})
df.RegisterType(MathSum{})
df.RegisterType(MathProduct{})
dec := df.NewDecoder(strings.NewReader("5+1=6\n"))
var eqn MathEqn
err = dec.Decode(&eqn)
if err != nil {
t.Error(err)
return
}
expectedEqn := MathEqn{
Left: MathSum{
First: MathNumberFactor{5},
Second: MathNumberFactor{1},
},
Right: MathNumberFactor{6},
}
if eqn != expectedEqn {
t.Errorf("Expected %q, got %q.", expectedEqn, eqn)
}
}
func TestMathPrecedence(t *testing.T) {
df, err := gopp.NewDecoderFactory(mathgopp, "Eqn")
if err != nil {
t.Error(err)
return
}
df.RegisterType(MathExprFactor{})
df.RegisterType(MathNumberFactor{})
df.RegisterType(MathSum{})
df.RegisterType(MathProduct{})
dec := df.NewDecoder(strings.NewReader("5+5*2=6*2+3\n"))
var eqn MathEqn
err = dec.Decode(&eqn)
if err != nil {
t.Error(err)
return
}
expectedEqn := MathEqn{
Left: MathSum{
MathNumberFactor{5},
MathProduct{
MathNumberFactor{5},
MathNumberFactor{2},
},
},
Right: MathSum{
MathProduct{
MathNumberFactor{6},
MathNumberFactor{2},
},
MathNumberFactor{3},
},
}
if eqn != expectedEqn {
t.Errorf("Expected %q, got %q.", expectedEqn, eqn)
}
}