| 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 example |
| 16 | |
| 17 | import ( |
| 18 | "testing" |
| 19 | |
| 20 | "github.com/goccmack/gocc/example/bools/ast" |
| 21 | "github.com/goccmack/gocc/example/bools/lexer" |
| 22 | "github.com/goccmack/gocc/example/bools/parser" |
| 23 | ) |
| 24 | |
| 25 | func testEval(t *testing.T, exampleStr string, output bool) { |
| 26 | lex := lexer.NewLexer([]byte(exampleStr)) |
| 27 | p := parser.NewParser() |
| 28 | st, err := p.Parse(lex) |
| 29 | if err != nil { |
| 30 | panic(err) |
| 31 | } |
| 32 | if st.(ast.Val).Eval() != output { |
| 33 | t.Fatalf("Should be %v for %v", output, exampleStr) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestOr(t *testing.T) { |
| 38 | testEval(t, "true | false", true) |
| 39 | } |
| 40 | |
| 41 | func TestAnd(t *testing.T) { |
| 42 | testEval(t, "true & false", false) |
| 43 | } |
| 44 | |
| 45 | func TestSubString(t *testing.T) { |
| 46 | testEval(t, `"true" in "false"`, false) |
| 47 | testEval(t, `"true" in "trues"`, true) |
| 48 | } |
| 49 | |
| 50 | func TestLess(t *testing.T) { |
| 51 | testEval(t, "0 < 5", true) |
| 52 | testEval(t, "0 > 5", false) |
| 53 | } |
| 54 | |
| 55 | func TestMixed(t *testing.T) { |
| 56 | testEval(t, "0 < 5 | false", true) |
| 57 | } |
| 58 | |
| 59 | func TestGroup(t *testing.T) { |
| 60 | testEval(t, "( true | false ) & ( true & true )", true) |
| 61 | } |
| 62 | |
| 63 | func TestGroupMixed(t *testing.T) { |
| 64 | testEval(t, `( true | false ) & 0 > 100000 | "t" in "taddle"`, true) |
| 65 | } |