| 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 | "testing" |
| 20 | ) |
| 21 | |
| 22 | var testItemsets1src = ` |
| 23 | a : '0'- '6' '0'; |
| 24 | b : '4'-'9' '9'; |
| 25 | c : '5' 'a' ; |
| 26 | ` |
| 27 | |
| 28 | var testItemSets1Data = []*itemSetTestData{ |
| 29 | { |
| 30 | CharRange: CharRange{'0', '3'}, |
| 31 | itemSet: []string{"a : '0'-'6' • '0'"}, |
| 32 | }, |
| 33 | { |
| 34 | CharRange: CharRange{'4', '4'}, |
| 35 | itemSet: []string{"a : '0'-'6' • '0'", "b : '4'-'9' • '9'"}, |
| 36 | }, |
| 37 | { |
| 38 | CharRange: CharRange{'5', '5'}, |
| 39 | itemSet: []string{"a : '0'-'6' • '0'", "b : '4'-'9' • '9'", "c : '5' • 'a'"}, |
| 40 | }, |
| 41 | { |
| 42 | CharRange: CharRange{'6', '6'}, |
| 43 | itemSet: []string{"a : '0'-'6' • '0'", "b : '4'-'9' • '9'"}, |
| 44 | }, |
| 45 | { |
| 46 | CharRange: CharRange{'7', '9'}, |
| 47 | itemSet: []string{"b : '4'-'9' • '9'"}, |
| 48 | }, |
| 49 | } |
| 50 | |
| 51 | // func TestItemSets1(t *testing.T) { |
| 52 | // g := parse(testItemsets1src, t) |
| 53 | // symbols := symbols.NewSymbols(g.LexPart) |
| 54 | // s0 := NewItemSet(0, g.LexPart, ItemsSet0(g.LexPart, symbols)) |
| 55 | // for _, data := range testItemSets1Data { |
| 56 | // s1 := NewItemSet(1, g.LexPart, s0.Next(data.CharRange)) |
| 57 | // checkItemsetResult(t, s1, data) |
| 58 | // } |
| 59 | // } |
| 60 | |
| 61 | var testItemsets2src = ` |
| 62 | << import "unicode" >> |
| 63 | |
| 64 | import( |
| 65 | _upcase "unicode.IsUpper" |
| 66 | _lowcase "unicode.IsLower" |
| 67 | _digit "unicode.IsDigit" |
| 68 | ) |
| 69 | _id_char : _upcase | _lowcase | '_' | _digit ; |
| 70 | |
| 71 | _tokId : _lowcase {_id_char} ; |
| 72 | |
| 73 | ignoredTokId : '!' _tokId ; |
| 74 | ` |
| 75 | |
| 76 | func _TestItemSets2(t *testing.T) { |
| 77 | g := parse(testItemsets2src, t) |
| 78 | itemSets := GetItemSets(g.LexPart) |
| 79 | fmt.Printf("%s\n", itemSets) |
| 80 | } |
| 81 | |
| 82 | var testItemsets3src = ` |
| 83 | _lineComment : '/' '/' {.} '\n' ; |
| 84 | |
| 85 | _blockComment : '/' '*' {.} '*' '/' ; |
| 86 | |
| 87 | comment : _lineComment | _blockComment ; |
| 88 | ` |
| 89 | |
| 90 | func _TestItemSets3(t *testing.T) { |
| 91 | g := parse(testItemsets3src, t) |
| 92 | itemSets := GetItemSets(g.LexPart) |
| 93 | fmt.Printf("%s\n", itemSets) |
| 94 | } |
| 95 | |
| 96 | type itemSetTestData struct { |
| 97 | CharRange |
| 98 | itemSet |
| 99 | } |
| 100 | |
| 101 | type itemSet []string |
| 102 | |
| 103 | func (this itemSet) Contain(item string) bool { |
| 104 | for _, it := range this { |
| 105 | if it == item { |
| 106 | return true |
| 107 | } |
| 108 | } |
| 109 | return false |
| 110 | } |
| 111 | |
| 112 | func checkItemsetResult(t *testing.T, set *ItemSet, data *itemSetTestData) { |
| 113 | if len(set.Items) != len(data.itemSet) { |
| 114 | testItemSetsErr(t, set, data, fmt.Sprintf("len(set.Items) == %v", len(set.Items))) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | func testItemSetsErr(t *testing.T, set *ItemSet, data *itemSetTestData, msg string) { |
| 119 | t.Fatal(msg) |
| 120 | } |