Debian Codegraph @

example/rr/rr_test.go

raw symbols SCIP
1 //Copyright 2012 Vastech SA (PTY) LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package rr
16
17 import (
18 "fmt"
19 "testing"
20
21 "github.com/goccmack/gocc/example/rr/lexer"
22 "github.com/goccmack/gocc/example/rr/parser"
23 )
24
25 func parse(src string) (ast string, err error) {
26 l := lexer.NewLexer([]byte(src))
27 p := parser.NewParser()
28 res, err := p.Parse(l)
29 if err == nil {
30 ast = res.(string)
31 }
32 return
33 }
34
35 func test(t *testing.T, src, exp string) {
36 ast, err := parse(src)
37 if err != nil {
38 t.Fatalf("\tError: %s\n", err.Error())
39 }
40 if ast != exp {
41 t.Fatalf("\tError: ast= `%s`\n", ast)
42 }
43 }
44
45 type TD struct {
46 src string
47 exp string
48 }
49
50 var testData = []TD{
51 {"a", "B "},
52 {"a a", "A1 "},
53 {"a a a", "A1 "},
54 {"c a", "A1 "},
55 {"c a a a a", "A1 "},
56 }
57
58 func Test(t *testing.T) {
59 for _, td := range testData {
60 fmt.Printf("\tsrc: `%s`; exp: `%s`\n", td.src, td.exp)
61 test(t, td.src, td.exp)
62 }
63 }