| 1 | //Copyright 2013 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 items |
| 16 | |
| 17 | import ( |
| 18 | // "fmt" |
| 19 | // "github.com/goccmack/gocc/internal/ast" |
| 20 | "testing" |
| 21 | ) |
| 22 | |
| 23 | func TestItemPos1(t *testing.T) { |
| 24 | src := `id : . {.};` |
| 25 | |
| 26 | g := parse(src, t) |
| 27 | prod := g.LexPart.Production("id") |
| 28 | itempos := newItemPos(prod.LexPattern()) |
| 29 | if itempos.level() != 0 { |
| 30 | t.Fatalf("itempos.level() == %d", itempos.level()) |
| 31 | } |
| 32 | if itempos.pos() != 0 { |
| 33 | t.Fatalf("itempos.pos() == %d", itempos.pos()) |
| 34 | } |
| 35 | if itempos.ntNode() != prod.LexPattern() { |
| 36 | t.Fatalf("itempos.ntNode() == %T", itempos.ntNode()) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func TestItemPosClone1(t *testing.T) { |
| 41 | src := `id : . {.};` |
| 42 | |
| 43 | g := parse(src, t) |
| 44 | prod := g.LexPart.Production("id") |
| 45 | itempos := newItemPos(prod.LexPattern()) |
| 46 | clone := itempos.clone() |
| 47 | if clone.level() != itempos.level() { |
| 48 | t.Fatalf("clone.level() == %d, itempos.level() == %d", clone.level(), itempos.level()) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestItemPos2(t *testing.T) { |
| 53 | src := `id : . {.};` |
| 54 | |
| 55 | g := parse(src, t) |
| 56 | prod := g.LexPart.Production("id") |
| 57 | itempos := newItemPos(prod.LexPattern()) |
| 58 | node := prod.LexPattern().Alternatives[0] |
| 59 | itempos.push(node, 0) |
| 60 | if itempos.level() != 1 { |
| 61 | t.Fatalf("itempos.level() == %d", itempos.level()) |
| 62 | } |
| 63 | if itempos.pos() != 0 { |
| 64 | t.Fatalf("itempos.pos() == %d", itempos.pos()) |
| 65 | } |
| 66 | if itempos.ntNode() != node { |
| 67 | t.Fatalf("itempos.ntNode() == %T", itempos.ntNode()) |
| 68 | } |
| 69 | if itempos.level() != 1 { |
| 70 | t.Fatalf("itempos.level() == %d", itempos.level()) |
| 71 | } |
| 72 | } |