-
Notifications
You must be signed in to change notification settings - Fork 5
/
calyx_spec.rb
211 lines (177 loc) · 5.95 KB
/
calyx_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
require 'spec_helper'
describe Calyx do
specify 'registry evaluates the start rule' do
registry = Calyx::Grammar::Registry.new
registry.start('atom')
expect(registry.evaluate).to eq('atom')
end
specify 'registry evaluates recursive rules' do
registry = Calyx::Grammar::Registry.new
registry.start(:atom)
registry.rule(:atom, 'atom')
expect(registry.evaluate).to eq('atom')
end
specify 'construct non-terminal production rule' do
registry = double('registry')
expect(registry).to receive(:expand).and_return(Calyx::Grammar::Production::Terminal.new(:atom))
rule = Calyx::Grammar::Production::NonTerminal.new(:statement, registry)
rule.evaluate
end
specify 'construct terminal production rule' do
atom = Calyx::Grammar::Production::Terminal.new(:atom)
expect(atom.evaluate).to eq(:atom)
end
specify 'construct string formatting production' do
nonterminal = double(:nonterminal)
allow(nonterminal).to receive(:evaluate).and_return('hello')
rule = Calyx::Grammar::Production::Expression.new(nonterminal, ['upcase'])
expect(rule.evaluate).to eq('HELLO')
end
specify 'split and join with delimiters' do
class OneTwo < Calyx::Grammar
start '{one} {two}'
rule :one, 'One.'
rule :two, 'Two.'
end
grammar = OneTwo.new
expect(grammar.generate).to eq('One. Two.')
end
specify 'rule inheritance' do
class BaseRules < Calyx::Grammar
rule :one, 'One.'
rule :two, 'Two.'
end
class StartRule < BaseRules
start '{one} {two}'
end
grammar = StartRule.new
expect(grammar.generate).to eq('One. Two.')
end
specify 'reference rule symbols in other rules' do
class RuleSymbols < Calyx::Grammar
start :rule_symbol
rule :rule_symbol, :terminal_symbol
rule :terminal_symbol, 'OK'
end
grammar = RuleSymbols.new
expect(grammar.generate).to eq('OK')
end
specify 'weighted choices must sum to 1' do
expect do
class BadWeights < Calyx::Grammar
start ['10%', 0.1], ['70%', 0.7]
end
end.to raise_error('Weights must sum to 1')
end
specify 'weighted choices in rule definition' do
class WeightedChoices < Calyx::Grammar
start ['20%', 0.2], ['80%', 0.8]
end
grammar = WeightedChoices.new(12345)
expect(grammar.generate).to eq('20%')
end
specify 'string formatters in rule definition' do
class StringFormatters < Calyx::Grammar
start :hello_world
rule :hello_world, '{hello.capitalize} world'
rule :hello, 'hello'
end
grammar = StringFormatters.new(12345)
expect(grammar.generate).to eq('Hello world')
end
specify 'instance constructor with block eval' do
Hue = Calyx::Grammar.new do
start 'rgb({r},{g},{b})'
rule :r, '255'
rule :g, '0'
rule :b, '0'
end
expect(Hue.generate).to eq('rgb(255,0,0)')
end
specify 'construct dynamic rules with context hash of values' do
class TemplateStringValues < Calyx::Grammar
start '{one}{two}{three}'
end
grammar = TemplateStringValues.new
expect(grammar.generate({one: 1, two: 2, three: 3})).to eq('123')
end
specify 'construct dynamic rules with context hash of expansion strings' do
class TemplateStringExpansions < Calyx::Grammar
start '{how}'
rule :a, 'piece of string?'
end
grammar = TemplateStringExpansions.new
expect(grammar.generate({how: '{long}', long: '{is}', is: '{a}'})).to eq('piece of string?')
end
specify 'construct dynamic rules with context hash of choices' do
class TemplateStringChoices < Calyx::Grammar
start '{fruit}'
end
grammar = TemplateStringChoices.new
expect(grammar.generate({fruit: ['apple', 'orange']})).to match(/apple|orange/)
end
specify 'construct dynamic rules with two context hashes' do
class StockReport < Calyx::Grammar
start 'You should buy shares in {company}.'
end
grammar = StockReport.new
cyberdyne_context_hash = {company: "Cyberdyne" }
bridgestone_context_hash = {company: "Bridgestone" }
expect(grammar.generate(cyberdyne_context_hash)).to eq("You should buy shares in Cyberdyne.")
expect(grammar.generate(bridgestone_context_hash)).to eq("You should buy shares in Bridgestone.")
end
specify 'construct a Calyx::Grammar class using a YAML file that references other rules' do
yaml_text = <<-EOF
start: :hello_world
hello_world: :statement
statement: "Hello World"
EOF
yaml = YAML.load(yaml_text)
filepath = "test.yml"
allow(YAML).to receive(:load_file).with(filepath).and_return(yaml)
grammar = Calyx::Grammar.load_yml(filepath)
expect(grammar.generate).to eq("Hello World")
end
specify 'construct a Calyx::Grammar class using a YAML file that can handle multiple parameters' do
yaml_text = <<-EOF
start: "{fruit}"
fruit:
- apple
- orange
EOF
yaml = YAML.load(yaml_text)
filepath = "test.yml"
allow(YAML).to receive(:load_file).with(filepath).and_return(yaml)
grammar = Calyx::Grammar.load_yml(filepath)
array = []
10.times { array << grammar.generate }
expect(array.uniq.sort).to eq(["apple","orange"])
end
specify 'construct a Calyx::Grammar class with weighted choices' do
yaml_text = <<-EOF
start:
- [20%, 0.2]
- [80%, 0.8]
EOF
yaml = YAML.load(yaml_text)
filepath = "test.yml"
allow(YAML).to receive(:load_file).with(filepath).and_return(yaml)
grammar = Calyx::Grammar.load_yml(filepath)
array = []
10.times { array << grammar.generate }
expect(array.uniq.sort).to eq(["20%","80%"])
end
specify 'raise an error if weighted choices do not equal 1' do
yaml_text = <<-EOF
start:
- [90%, 0.9]
- [80%, 0.8]
EOF
yaml = YAML.load(yaml_text)
filepath = "test.yml"
allow(YAML).to receive(:load_file).with(filepath).and_return(yaml)
expect do
Calyx::Grammar.load_yml(filepath)
end.to raise_error('Weights must sum to 1')
end
end