Akron | b7e1f35 | 2025-05-16 15:45:23 +0200 | [diff] [blame^] | 1 | package ast |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/stretchr/testify/assert" |
| 7 | ) |
| 8 | |
| 9 | func TestNodeTypes(t *testing.T) { |
| 10 | tests := []struct { |
| 11 | name string |
| 12 | node Node |
| 13 | expected NodeType |
| 14 | }{ |
| 15 | { |
| 16 | name: "Token node returns correct type", |
| 17 | node: &Token{Wrap: &Term{}}, |
| 18 | expected: TokenNode, |
| 19 | }, |
| 20 | { |
| 21 | name: "TermGroup node returns correct type", |
| 22 | node: &TermGroup{ |
| 23 | Operands: []Node{&Term{}}, |
| 24 | Relation: AndRelation, |
| 25 | }, |
| 26 | expected: TermGroupNode, |
| 27 | }, |
| 28 | { |
| 29 | name: "Term node returns correct type", |
| 30 | node: &Term{ |
| 31 | Foundry: "opennlp", |
| 32 | Key: "DET", |
| 33 | Layer: "p", |
| 34 | Match: MatchEqual, |
| 35 | }, |
| 36 | expected: TermNode, |
| 37 | }, |
| 38 | } |
| 39 | |
| 40 | for _, tt := range tests { |
| 41 | t.Run(tt.name, func(t *testing.T) { |
| 42 | assert.Equal(t, tt.expected, tt.node.Type()) |
| 43 | }) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func TestTermGroupConstruction(t *testing.T) { |
| 48 | term1 := &Term{ |
| 49 | Foundry: "opennlp", |
| 50 | Key: "DET", |
| 51 | Layer: "p", |
| 52 | Match: MatchEqual, |
| 53 | } |
| 54 | |
| 55 | term2 := &Term{ |
| 56 | Foundry: "opennlp", |
| 57 | Key: "AdjType", |
| 58 | Layer: "m", |
| 59 | Match: MatchEqual, |
| 60 | Value: "Pdt", |
| 61 | } |
| 62 | |
| 63 | group := &TermGroup{ |
| 64 | Operands: []Node{term1, term2}, |
| 65 | Relation: AndRelation, |
| 66 | } |
| 67 | |
| 68 | assert.Len(t, group.Operands, 2) |
| 69 | assert.Equal(t, AndRelation, group.Relation) |
| 70 | assert.Equal(t, TermGroupNode, group.Type()) |
| 71 | |
| 72 | // Test operands are correctly set |
| 73 | assert.Equal(t, term1, group.Operands[0]) |
| 74 | assert.Equal(t, term2, group.Operands[1]) |
| 75 | } |
| 76 | |
| 77 | func TestTokenConstruction(t *testing.T) { |
| 78 | term := &Term{ |
| 79 | Foundry: "opennlp", |
| 80 | Key: "DET", |
| 81 | Layer: "p", |
| 82 | Match: MatchEqual, |
| 83 | } |
| 84 | |
| 85 | token := &Token{Wrap: term} |
| 86 | |
| 87 | assert.Equal(t, TokenNode, token.Type()) |
| 88 | assert.Equal(t, term, token.Wrap) |
| 89 | } |
| 90 | |
| 91 | func TestTermConstruction(t *testing.T) { |
| 92 | tests := []struct { |
| 93 | name string |
| 94 | term *Term |
| 95 | foundry string |
| 96 | key string |
| 97 | layer string |
| 98 | match MatchType |
| 99 | hasValue bool |
| 100 | value string |
| 101 | }{ |
| 102 | { |
| 103 | name: "Term without value", |
| 104 | term: &Term{ |
| 105 | Foundry: "opennlp", |
| 106 | Key: "DET", |
| 107 | Layer: "p", |
| 108 | Match: MatchEqual, |
| 109 | }, |
| 110 | foundry: "opennlp", |
| 111 | key: "DET", |
| 112 | layer: "p", |
| 113 | match: MatchEqual, |
| 114 | hasValue: false, |
| 115 | }, |
| 116 | { |
| 117 | name: "Term with value", |
| 118 | term: &Term{ |
| 119 | Foundry: "opennlp", |
| 120 | Key: "AdjType", |
| 121 | Layer: "m", |
| 122 | Match: MatchEqual, |
| 123 | Value: "Pdt", |
| 124 | }, |
| 125 | foundry: "opennlp", |
| 126 | key: "AdjType", |
| 127 | layer: "m", |
| 128 | match: MatchEqual, |
| 129 | hasValue: true, |
| 130 | value: "Pdt", |
| 131 | }, |
| 132 | { |
| 133 | name: "Term with not equal match", |
| 134 | term: &Term{ |
| 135 | Foundry: "opennlp", |
| 136 | Key: "DET", |
| 137 | Layer: "p", |
| 138 | Match: MatchNotEqual, |
| 139 | }, |
| 140 | foundry: "opennlp", |
| 141 | key: "DET", |
| 142 | layer: "p", |
| 143 | match: MatchNotEqual, |
| 144 | hasValue: false, |
| 145 | }, |
| 146 | } |
| 147 | |
| 148 | for _, tt := range tests { |
| 149 | t.Run(tt.name, func(t *testing.T) { |
| 150 | assert.Equal(t, TermNode, tt.term.Type()) |
| 151 | assert.Equal(t, tt.foundry, tt.term.Foundry) |
| 152 | assert.Equal(t, tt.key, tt.term.Key) |
| 153 | assert.Equal(t, tt.layer, tt.term.Layer) |
| 154 | assert.Equal(t, tt.match, tt.term.Match) |
| 155 | if tt.hasValue { |
| 156 | assert.Equal(t, tt.value, tt.term.Value) |
| 157 | } else { |
| 158 | assert.Empty(t, tt.term.Value) |
| 159 | } |
| 160 | }) |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | func TestPatternAndReplacement(t *testing.T) { |
| 165 | // Create a simple pattern |
| 166 | patternTerm := &Term{ |
| 167 | Foundry: "opennlp", |
| 168 | Key: "DET", |
| 169 | Layer: "p", |
| 170 | Match: MatchEqual, |
| 171 | } |
| 172 | pattern := Pattern{Root: patternTerm} |
| 173 | |
| 174 | // Create a simple replacement |
| 175 | replacementTerm := &Term{ |
| 176 | Foundry: "opennlp", |
| 177 | Key: "COMBINED_DET", |
| 178 | Layer: "p", |
| 179 | Match: MatchEqual, |
| 180 | } |
| 181 | replacement := Replacement{Root: replacementTerm} |
| 182 | |
| 183 | // Test pattern |
| 184 | assert.NotNil(t, pattern.Root) |
| 185 | assert.Equal(t, patternTerm, pattern.Root) |
| 186 | |
| 187 | // Test replacement |
| 188 | assert.NotNil(t, replacement.Root) |
| 189 | assert.Equal(t, replacementTerm, replacement.Root) |
| 190 | } |