cofgratx 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +64 -0
- data/Rakefile +2 -0
- data/cofgratx.gemspec +24 -0
- data/lib/cofgratx.rb +10 -0
- data/lib/cofgratx/cfg/grammar.rb +46 -0
- data/lib/cofgratx/cfg/grammar_error.rb +2 -0
- data/lib/cofgratx/cfg/non_terminal.rb +54 -0
- data/lib/cofgratx/cfg/repetition.rb +9 -0
- data/lib/cofgratx/cfg/rule.rb +160 -0
- data/lib/cofgratx/cfg/rule_error.rb +2 -0
- data/lib/cofgratx/cfg/terminal.rb +25 -0
- data/lib/cofgratx/cfg/translation_repetition_set.rb +33 -0
- data/lib/cofgratx/cfg/translation_repetition_set_error.rb +2 -0
- data/lib/cofgratx/version.rb +3 -0
- data/spec/grammar_spec.rb +423 -0
- data/spec/non_terminal_spec.rb +126 -0
- data/spec/repetition_spec.rb +48 -0
- data/spec/rule_spec.rb +247 -0
- data/spec/spec_helper.rb +91 -0
- data/spec/terminal_spec.rb +60 -0
- data/spec/translation_repetition_set_spec.rb +68 -0
- metadata +119 -0
@@ -0,0 +1,126 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'cofgratx/cfg/rule'
|
3
|
+
require 'cofgratx/cfg/non_terminal'
|
4
|
+
|
5
|
+
describe NonTerminal do
|
6
|
+
context ".initialize" do
|
7
|
+
before do
|
8
|
+
@rule = Rule.new
|
9
|
+
@rule2 = Rule.new
|
10
|
+
end
|
11
|
+
|
12
|
+
it "can initialize with nothing" do
|
13
|
+
expect{ described_class.new() }.to_not raise_error
|
14
|
+
end
|
15
|
+
|
16
|
+
it "can initialize with a rule" do
|
17
|
+
expect{ described_class.new(@rule) }.to_not raise_error
|
18
|
+
end
|
19
|
+
|
20
|
+
it "can initialize with a set of rules" do
|
21
|
+
expect{ described_class.new(@rule, @rule2) }.to_not raise_error
|
22
|
+
end
|
23
|
+
|
24
|
+
it "can initialize with a set of rules and ignores duplicates" do
|
25
|
+
@non_terminal = described_class.new(@rule, @rule2)
|
26
|
+
expect( @non_terminal.instance_variable_get("@rules").size ).to equal 1
|
27
|
+
end
|
28
|
+
|
29
|
+
it "raises an exception on bad initial objects" do
|
30
|
+
expect{ described_class.new(@rule, 12345) }.to raise_error(ArgumentError, "expected a list of Rules; found bad items: " +
|
31
|
+
[12345].map{|bad_arg| "#{bad_arg.class.name} #{bad_arg}"}.join("\n"))
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context ".match?" do
|
36
|
+
before do
|
37
|
+
@rule = Rule.new
|
38
|
+
@rule2 = Rule.new
|
39
|
+
@rule.instance_variable_set("@rule", [/a/])
|
40
|
+
@rule2.instance_variable_set("@rule", [/b/])
|
41
|
+
end
|
42
|
+
|
43
|
+
context "returns false when the non terminal has no matching rules at the strings beginning" do
|
44
|
+
before do
|
45
|
+
allow(@rule).to receive(:match?){ false }
|
46
|
+
allow(@rule2).to receive(:match?){ false }
|
47
|
+
end
|
48
|
+
it{ expect( described_class.new().match?("no match") ).to be_falsey }
|
49
|
+
it{ expect( described_class.new(@rule).match?("still no match") ).to be_falsey }
|
50
|
+
it{ expect( described_class.new(@rule,@rule2).match?("stubbed so no match") ).to be_falsey }
|
51
|
+
end
|
52
|
+
|
53
|
+
context "returns true when the non terminal has a matching rule at the start of the string" do
|
54
|
+
before do
|
55
|
+
allow(@rule).to receive(:match?){ false }
|
56
|
+
allow(@rule2).to receive(:match?){ true }
|
57
|
+
end
|
58
|
+
it{ expect( described_class.new(@rule2).match?("test") ).to be_truthy }
|
59
|
+
it{ expect( described_class.new(@rule,@rule2).match?("test first") ).to be_truthy }
|
60
|
+
it{ expect( described_class.new(@rule2,@rule2).match?("tony, get some sandwiches") ).to be_truthy }
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context ".extract" do
|
65
|
+
before do
|
66
|
+
@rule = Rule.new
|
67
|
+
@rule2 = Rule.new
|
68
|
+
@rule3 = Rule.new
|
69
|
+
@rule.instance_variable_set("@rule", [/a/])
|
70
|
+
@rule2.instance_variable_set("@rule", [/b/])
|
71
|
+
@rule2.instance_variable_set("@rule", [/c/])
|
72
|
+
end
|
73
|
+
|
74
|
+
context "returns nil and the unmodified string when the terminal is not found at the strings beginning" do
|
75
|
+
before do
|
76
|
+
allow(@rule).to receive(:extract){ |param| [ [nil,param,[[]]] ] }
|
77
|
+
allow(@rule2).to receive(:extract){ |param| [ [nil,param,[[]]] ] }
|
78
|
+
end
|
79
|
+
it{ expect( described_class.new().extract("no match") ).to match_array( [ [nil,"no match",[[]]] ] ) }
|
80
|
+
it{ expect( described_class.new(@rule).extract("still no match") ).to match_array( [ [nil,"still no match",[[]]] ] ) }
|
81
|
+
it{ expect( described_class.new(@rule,@rule2).extract("stubbed so no match") ).to match_array( [ [nil,"stubbed so no match",[[]]] ] ) }
|
82
|
+
end
|
83
|
+
|
84
|
+
context "returns the terminal match and remainder of string" do
|
85
|
+
before do
|
86
|
+
allow(@rule).to receive(:extract){ |param| [ [nil,param,[[]]] ] }
|
87
|
+
allow(@rule2).to receive(:extract){ |param| [ ["D",param[1..-1],[["D"]]] ] }
|
88
|
+
allow(@rule3).to receive(:extract){ |param| [ ["Don",param[3..-1],[["Don"]]] ] }
|
89
|
+
end
|
90
|
+
it "does not mutate the input string" do
|
91
|
+
input_string = "Don't change me!"
|
92
|
+
expect( described_class.new(@rule2,@rule,@rule3).extract(input_string) ).to match_array( [ ["D","on't change me!",[["D"]]], ["Don","'t change me!", [["Don"]]] ] )
|
93
|
+
expect( input_string ).to match "Don't change me!"
|
94
|
+
end
|
95
|
+
it{ expect( described_class.new(@rule,@rule3).extract("Donald") ).to match_array( [ ["Don","ald",[["Don"]]] ] ) }
|
96
|
+
it{ expect( described_class.new(@rule,@rule3,@rule2).extract("Donald") ).to match_array( [ ["D","onald",[["D"]]], ["Don","ald",[["Don"]]] ] ) }
|
97
|
+
it{ expect( described_class.new(@rule2).extract("Donald") ).to match_array( [ ["D","onald",[["D"]]] ] ) }
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
context ".translate" do
|
102
|
+
before do
|
103
|
+
@rule = Rule.new
|
104
|
+
@rule2 = Rule.new
|
105
|
+
@rule3 = Rule.new
|
106
|
+
@rule.instance_variable_set("@rule", [/a/])
|
107
|
+
@rule2.instance_variable_set("@rule", [/b/])
|
108
|
+
@rule2.instance_variable_set("@rule", [/c/])
|
109
|
+
|
110
|
+
allow(@rule).to receive(:translate){ |param| [[nil, param]] }
|
111
|
+
allow(@rule2).to receive(:translate){ |param| [["ba", param[2..-1]]] }
|
112
|
+
allow(@rule3).to receive(:translate){ |param| [["bba", param[3..-1]]] }
|
113
|
+
end
|
114
|
+
|
115
|
+
it "does not mutate the input string" do
|
116
|
+
input_string = "abb"
|
117
|
+
expect( described_class.new(@rule2,@rule,@rule3).translate(input_string) ).to match_array( [ ["ba","b"], ["bba",""] ] )
|
118
|
+
expect( input_string ).to match "abb"
|
119
|
+
end
|
120
|
+
|
121
|
+
it{ expect( described_class.new(@rule).translate("ab") ).to match_array( [ [nil, "ab"] ] ) }
|
122
|
+
it{ expect( described_class.new(@rule,@rule2).translate("abba") ).to match_array( [ ["ba", "ba"] ] ) }
|
123
|
+
it{ expect( described_class.new(@rule,@rule2).translate("abba") ).to match_array( [ ["ba", "ba"] ] ) }
|
124
|
+
it{ expect( described_class.new(@rule,@rule2,@rule3).translate("abbadabab") ).to match_array( [ ["ba", "badabab"], ["bba", "adabab"] ] ) }
|
125
|
+
end
|
126
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'cofgratx/cfg/terminal'
|
3
|
+
require 'cofgratx/cfg/repetition'
|
4
|
+
|
5
|
+
describe Repetition do
|
6
|
+
|
7
|
+
context ".initialize" do
|
8
|
+
it "can initialize with a string" do
|
9
|
+
expect{ described_class.new("something") }.to_not raise_error
|
10
|
+
end
|
11
|
+
|
12
|
+
it "doesn not initialize with a regular expression" do
|
13
|
+
expect{ described_class.new(/,/) }.to raise_error(ArgumentError, "expected String; got #{/,/.class.name}")
|
14
|
+
end
|
15
|
+
|
16
|
+
it "raises an exception on bad initial objects" do
|
17
|
+
expect{ described_class.new(12345) }.to raise_error(ArgumentError, "expected String; got #{12345.class.name}")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context ".match?" do
|
22
|
+
context "returns false when the terminal is not found at the strings beginning" do
|
23
|
+
it{ expect( described_class.new(",").match?("no match") ).to be_falsey }
|
24
|
+
it{ expect( described_class.new(":").match?("one: two") ).to be_falsey }
|
25
|
+
end
|
26
|
+
|
27
|
+
context "returns true when the terminal is found at the start of the string" do
|
28
|
+
it{ expect( described_class.new(",").match?(",test") ).to be_truthy }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context ".extract" do
|
33
|
+
context "returns nil and the unmodified string when the terminal is not found at the strings beginning" do
|
34
|
+
it{ expect( described_class.new(",").extract("no match") ).to match_array( [nil, "no match"] ) }
|
35
|
+
it{ expect( described_class.new(",").extract("no, test first") ).to match_array( [nil, "no, test first"] ) }
|
36
|
+
end
|
37
|
+
|
38
|
+
context "returns the terminal match and remainder of string" do
|
39
|
+
it "does not mutate the input string" do
|
40
|
+
input_string = ","
|
41
|
+
expect( described_class.new(",").extract(input_string) ).to match_array( [",", ""] )
|
42
|
+
expect( input_string ).to match ","
|
43
|
+
end
|
44
|
+
it{ expect( described_class.new(",").extract(",") ).to match_array( [",", ""] ) }
|
45
|
+
it{ expect( described_class.new(",").extract(", first") ).to match_array( [",", " first"] ) }
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/spec/rule_spec.rb
ADDED
@@ -0,0 +1,247 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'cofgratx/cfg/terminal'
|
3
|
+
require 'cofgratx/cfg/repetition'
|
4
|
+
require 'cofgratx/cfg/translation_repetition_set_error'
|
5
|
+
require 'cofgratx/cfg/translation_repetition_set'
|
6
|
+
require 'cofgratx/cfg/rule_error'
|
7
|
+
require 'cofgratx/cfg/rule'
|
8
|
+
require 'cofgratx/cfg/non_terminal'
|
9
|
+
|
10
|
+
|
11
|
+
describe Rule do
|
12
|
+
before(:all) do
|
13
|
+
@rule = Rule.new
|
14
|
+
@terminal_a = Terminal.new("a")
|
15
|
+
@terminal_b = Terminal.new(/b/)
|
16
|
+
@repetition_comma = Repetition.new( "," )
|
17
|
+
@nonterminal = NonTerminal.new(@rule)
|
18
|
+
end
|
19
|
+
|
20
|
+
context ".set_rule" do
|
21
|
+
before do
|
22
|
+
@rule = described_class.new
|
23
|
+
end
|
24
|
+
|
25
|
+
it { expect{ @rule.set_rule() }.to_not raise_error }
|
26
|
+
it { expect{ @rule.set_rule(@terminal_a) }.to_not raise_error }
|
27
|
+
it { expect{ @rule.set_rule(@nonterminal) }.to_not raise_error }
|
28
|
+
it { expect{ @rule.set_rule(@nonterminal, @terminal_b) }.to_not raise_error }
|
29
|
+
it { expect{ @rule.set_rule(@terminal_b) }.to_not raise_error }
|
30
|
+
it { expect{ @rule.set_rule(@terminal_a, @repetition_comma) }.to_not raise_error }
|
31
|
+
it { expect{ @rule.set_rule(@terminal_a, @terminal_b, @terminal_a, @repetition_comma) }.to_not raise_error }
|
32
|
+
it { expect{ @rule.set_rule(@terminal_a, @nonterminal, @terminal_b, @repetition_comma) }.to_not raise_error }
|
33
|
+
|
34
|
+
it "raises an exception on bad initial objects" do
|
35
|
+
expect{ @rule.set_rule(12345) }.to raise_error(ArgumentError, "expected Terminal, NonTerminal or Repetition; got #{12345.class.name}")
|
36
|
+
end
|
37
|
+
|
38
|
+
it "the repetition cannot be the first for the rule" do
|
39
|
+
expect{ @rule.set_rule(@repetition_comma) }.to raise_error(RuleError, "cannot have repetition as the first part of the rule")
|
40
|
+
end
|
41
|
+
|
42
|
+
context "nothing can follow the repetition" do
|
43
|
+
it {expect{
|
44
|
+
@rule.set_rule(@terminal_a, @repetition_comma, @terminal_a)
|
45
|
+
}.to raise_error(RuleError, "nothing can follow the repetition") }
|
46
|
+
it {expect{
|
47
|
+
@rule.set_rule(@terminal_a, @repetition_comma, @repetition_comma)
|
48
|
+
}.to raise_error(RuleError, "nothing can follow the repetition") }
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context ".set_translation" do
|
53
|
+
before(:all) do
|
54
|
+
@tx_rep_set = TranslationRepetitionSet.new(1, 2, 4)
|
55
|
+
end
|
56
|
+
|
57
|
+
before do
|
58
|
+
@rule = described_class.new
|
59
|
+
end
|
60
|
+
|
61
|
+
it { expect{ @rule.set_translation() }.to_not raise_error }
|
62
|
+
it { expect{ @rule.set_translation(1) }.to_not raise_error }
|
63
|
+
it { expect{ @rule.set_translation(1,2,3) }.to_not raise_error }
|
64
|
+
it { expect{ @rule.set_translation([1,"foo","bar"]) }.to_not raise_error }
|
65
|
+
it { expect{ @rule.set_translation(1, @tx_rep_set) }.to_not raise_error }
|
66
|
+
|
67
|
+
context "bad input" do
|
68
|
+
def message_helper obj
|
69
|
+
"expected Fixnum, String or TranslationRepetitionSet; got #{obj.class.name}"
|
70
|
+
end
|
71
|
+
|
72
|
+
it { expect{ @rule.set_translation(1.32) }.to raise_error(ArgumentError, message_helper(1.32)) }
|
73
|
+
it { expect{ @rule.set_translation(1, {1=>2}) }.to raise_error(ArgumentError, message_helper({1=>2})) }
|
74
|
+
it { expect{ @rule.set_translation(/asd/) }.to raise_error(ArgumentError, message_helper(/asd/)) }
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
|
79
|
+
context ".initialize" do
|
80
|
+
before(:all) do
|
81
|
+
@tx_rep_set = TranslationRepetitionSet.new(1, 2, 4)
|
82
|
+
|
83
|
+
@rule = [@terminal_a, @terminal_b]
|
84
|
+
@translation = [1, 2, @tx_rep_set]
|
85
|
+
end
|
86
|
+
|
87
|
+
it "sets the subrules and translations by calling the appropriate methods" do
|
88
|
+
expect_any_instance_of(described_class).to receive(:set_rule).with(@rule).and_call_original
|
89
|
+
expect_any_instance_of(described_class).to receive(:set_translation).with(@translation).and_call_original
|
90
|
+
|
91
|
+
described_class.new(@rule, @translation)
|
92
|
+
end
|
93
|
+
|
94
|
+
it "sets the subrules and translations when no translation is given" do
|
95
|
+
expect_any_instance_of(described_class).to receive(:set_rule).with(@rule).and_call_original
|
96
|
+
expect_any_instance_of(described_class).to receive(:set_translation).with([]).and_call_original
|
97
|
+
|
98
|
+
described_class.new(@rule)
|
99
|
+
end
|
100
|
+
|
101
|
+
it "sets the subrules and translations when no input" do
|
102
|
+
expect_any_instance_of(described_class).to receive(:set_rule).with([]).and_call_original
|
103
|
+
expect_any_instance_of(described_class).to receive(:set_translation).with([]).and_call_original
|
104
|
+
|
105
|
+
described_class.new()
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
context ".valid_translation?" do
|
110
|
+
before(:all) do
|
111
|
+
@tx_rep_set = TranslationRepetitionSet.new(2, 1)
|
112
|
+
@repeat_rule = [@terminal_a, @repetition_comma]
|
113
|
+
end
|
114
|
+
|
115
|
+
it { expect(described_class.new().valid_translation?).to be_truthy }
|
116
|
+
it { expect(described_class.new(@terminal_a).valid_translation?).to be_truthy }
|
117
|
+
it { expect(described_class.new(@repeat_rule).valid_translation?).to be_truthy }
|
118
|
+
it { expect(described_class.new(@repeat_rule, [2,1,@tx_rep_set]).valid_translation?).to be_truthy }
|
119
|
+
|
120
|
+
it "is not valid if a translation number is larger than the number of sub rules" do
|
121
|
+
expect(described_class.new(@repeat_rule, [3]).valid_translation?).to be_falsy
|
122
|
+
end
|
123
|
+
|
124
|
+
it "is not valid if a translation number is larger than the number of sub rules" do
|
125
|
+
expect(described_class.new(@repeat_rule, TranslationRepetitionSet.new(2, 4)).valid_translation?).to be_falsy
|
126
|
+
end
|
127
|
+
|
128
|
+
it "offset can be any positive number" do
|
129
|
+
expect(described_class.new(@repeat_rule, TranslationRepetitionSet.new(999, 2)).valid_translation?).to be_truthy
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
|
134
|
+
context ".match?" do
|
135
|
+
before(:all) do
|
136
|
+
@term_rule = described_class.new [@terminal_a, @terminal_b]
|
137
|
+
@repeat_rule = described_class.new [@terminal_a, @repetition_comma]
|
138
|
+
@nonterminal_rule = NonTerminal.new(@term_rule)
|
139
|
+
@mix_rule = described_class.new [@terminal_a, @nonterminal_rule]
|
140
|
+
end
|
141
|
+
|
142
|
+
context "returns false when the rule does not match a substring starting at the strings beginning" do
|
143
|
+
it{ expect( @term_rule.match?("aab") ).to be_falsey }
|
144
|
+
it{ expect( @term_rule.match?("b") ).to be_falsey }
|
145
|
+
it{ expect( @term_rule.match?("here ababab") ).to be_falsey }
|
146
|
+
it{ expect( @term_rule.match?("") ).to be_falsey }
|
147
|
+
it{ expect( @repeat_rule.match?(",a") ).to be_falsey }
|
148
|
+
it{ expect( @repeat_rule.match?("b,a") ).to be_falsey }
|
149
|
+
it{ expect( @repeat_rule.match?("") ).to be_falsey }
|
150
|
+
it{ expect( @repeat_rule.match?("no match") ).to be_falsey }
|
151
|
+
it{ expect( @mix_rule.match?("abb") ).to be_falsey }
|
152
|
+
it{ expect( @mix_rule.match?("b") ).to be_falsey }
|
153
|
+
it{ expect( @mix_rule.match?("daab") ).to be_falsey }
|
154
|
+
it{ expect( @mix_rule.match?("") ).to be_falsey }
|
155
|
+
end
|
156
|
+
|
157
|
+
context "returns true when the rule matches a substring starting at the strings beginning" do
|
158
|
+
it{ expect( @term_rule.match?("ab") ).to be_truthy }
|
159
|
+
it{ expect( @term_rule.match?("ab something else") ).to be_truthy }
|
160
|
+
it{ expect( @repeat_rule.match?("a,") ).to be_truthy }
|
161
|
+
it{ expect( @repeat_rule.match?("a,a") ).to be_truthy }
|
162
|
+
it{ expect( @repeat_rule.match?("anothing") ).to be_truthy }
|
163
|
+
it{ expect( @repeat_rule.match?("a,nothing else") ).to be_truthy }
|
164
|
+
it{ expect( @mix_rule.match?("aabb more stuff") ).to be_truthy }
|
165
|
+
it{ expect( @mix_rule.match?("aabbb") ).to be_truthy }
|
166
|
+
it{ expect( @mix_rule.match?("aab") ).to be_truthy }
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
context ".extract" do
|
171
|
+
before(:all) do
|
172
|
+
@term_rule = described_class.new [@terminal_a, @terminal_b]
|
173
|
+
@repeat_rule = described_class.new [@terminal_a, @repetition_comma]
|
174
|
+
@nonterminal_rule = NonTerminal.new(@term_rule)
|
175
|
+
@mix_rule = described_class.new [@terminal_a, @nonterminal_rule]
|
176
|
+
end
|
177
|
+
|
178
|
+
context "returns nil and the unmodified string when the rule is not matched at the strings beginning" do
|
179
|
+
it{ expect( @term_rule.extract("aab") ).to match_array( [ [nil,"aab",[[]]] ] ) }
|
180
|
+
it{ expect( @term_rule.extract("b") ).to match_array( [ [nil,"b",[[]]] ] ) }
|
181
|
+
it{ expect( @term_rule.extract("here ababab") ).to match_array( [ [nil,"here ababab",[[]]] ] ) }
|
182
|
+
it{ expect( @term_rule.extract("") ).to match_array( [ [nil,"",[[]]] ] ) }
|
183
|
+
it{ expect( @repeat_rule.extract(",a") ).to match_array( [ [nil,",a",[[]]] ] ) }
|
184
|
+
it{ expect( @repeat_rule.extract("b,a") ).to match_array( [ [nil,"b,a",[[]]] ] ) }
|
185
|
+
it{ expect( @repeat_rule.extract("") ).to match_array( [ [nil,"",[[]]] ] ) }
|
186
|
+
it{ expect( @repeat_rule.extract("no match") ).to match_array( [ [nil,"no match",[[]]] ] ) }
|
187
|
+
it{ expect( @mix_rule.extract("abb") ).to match_array( [ [nil,"abb",[[]]] ] ) }
|
188
|
+
it{ expect( @mix_rule.extract("b") ).to match_array( [ [nil,"b",[[]]] ] ) }
|
189
|
+
it{ expect( @mix_rule.extract("daab") ).to match_array( [ [nil,"daab",[[]]] ] ) }
|
190
|
+
it{ expect( @mix_rule.extract("") ).to match_array( [ [nil,"",[[]]] ] ) }
|
191
|
+
end
|
192
|
+
|
193
|
+
context "returns the rule match and remainder of string" do
|
194
|
+
it "does not mutate the input string" do
|
195
|
+
input_string = "Don't change me!"
|
196
|
+
expect( described_class.new(Terminal.new("Don't change me!")).extract(input_string) ).to match_array( [ ["Don't change me!","",[["Don't change me!"]]] ] )
|
197
|
+
expect( input_string ).to match "Don't change me!"
|
198
|
+
end
|
199
|
+
|
200
|
+
it{ expect( @term_rule.extract("ab") ).to match_array( [ ["ab","",[["a","b"]]] ] ) }
|
201
|
+
it{ expect( @term_rule.extract("ab something else") ).to match_array( [ ["ab"," something else",[["a","b"]]] ] ) }
|
202
|
+
it{ expect( @repeat_rule.extract("a,") ).to match_array( [ ["a",",",[["a"]]] ] ) }
|
203
|
+
it{ expect( @repeat_rule.extract("a,a") ).to match_array( [ ["a,a","",[["a",","],["a"]]] ] ) }
|
204
|
+
it{ expect( @repeat_rule.extract("anothing") ).to match_array( [ ["a","nothing",[["a"]]] ] ) }
|
205
|
+
it{ expect( @repeat_rule.extract("a,nothing else") ).to match_array( [ ["a",",nothing else",[["a"]]] ] ) }
|
206
|
+
it{ expect( @repeat_rule.extract("a,a,anothing else") ).to match_array( [ ["a,a,a","nothing else",[["a",","],["a",","],["a"]]] ] ) }
|
207
|
+
it{ expect( @repeat_rule.extract("a,a,a,nothing else") ).to match_array( [ ["a,a,a",",nothing else",[["a",","],["a",","],["a"]]] ] ) }
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
context ".translate" do
|
212
|
+
before(:all) do
|
213
|
+
@terminal_c = Terminal.new("c")
|
214
|
+
@tx_rep_set = TranslationRepetitionSet.new(2, " repeats:", 2, 1, 3)
|
215
|
+
@simple_rule = described_class.new [@terminal_a, @terminal_b], [2,1]
|
216
|
+
@simple_rule2 = described_class.new [@terminal_a, @terminal_b], [2,1,2,"moo"]
|
217
|
+
@repeat_rule = described_class.new [@terminal_a, @terminal_b, @repetition_comma], [3,2,1,@tx_rep_set]
|
218
|
+
@nonterminal_rule = NonTerminal.new(@simple_rule)
|
219
|
+
@mix_rule = described_class.new [Terminal.new("c"), @nonterminal_rule], [2,1]
|
220
|
+
@mix_rule_repeat = described_class.new [@terminal_c, @nonterminal_rule, @repetition_comma], [3,2,1,@tx_rep_set]
|
221
|
+
end
|
222
|
+
|
223
|
+
it "does not modify the original parameter" do
|
224
|
+
original_string = "ab"
|
225
|
+
expect( @simple_rule.translate original_string ).to match_array( [ ["ba",""] ] )
|
226
|
+
expect( original_string ).to match "ab"
|
227
|
+
end
|
228
|
+
|
229
|
+
it { expect( @simple_rule.translate "abab" ).to match_array( [ ["ba","ab"] ] ) }
|
230
|
+
it { expect( @simple_rule.translate "ab and nothing else" ).to match_array( [ ["ba"," and nothing else"] ] ) }
|
231
|
+
it { expect( @simple_rule2.translate "abab" ).to match_array( [ ["babmoo","ab"] ] ) }
|
232
|
+
it { expect( @simple_rule2.translate "ab and nothing else" ).to match_array( [ ["babmoo"," and nothing else"] ] ) }
|
233
|
+
|
234
|
+
it { expect( @repeat_rule.translate "ab," ).to match_array( [ ["ba",","] ] ) }
|
235
|
+
it { expect( @repeat_rule.translate "ab,ab" ).to match_array( [ [",ba repeats:ba",""] ] ) }
|
236
|
+
it { expect( @repeat_rule.translate "ab,ab,ab" ).to match_array( [ [",ba repeats:ba, repeats:ba",""] ] ) }
|
237
|
+
it { expect( @repeat_rule.translate "ab,ab,ab," ).to match_array( [ [",ba repeats:ba, repeats:ba",","] ] ) }
|
238
|
+
|
239
|
+
it { expect( @mix_rule.translate "cab" ).to match_array( [ ["bac",""] ] ) }
|
240
|
+
it { expect( @mix_rule.translate "cab," ).to match_array( [ ["bac",","] ] ) }
|
241
|
+
it { expect( @mix_rule.translate "cab,cab" ).to match_array( [ ["bac",",cab"] ] ) }
|
242
|
+
it { expect( @mix_rule_repeat.translate "cab," ).to match_array( [ ["bac",","] ] ) }
|
243
|
+
it { expect( @mix_rule_repeat.translate "cab,cab" ).to match_array( [ [",bac repeats:bac",""] ] ) }
|
244
|
+
it { expect( @mix_rule_repeat.translate "cab,cab,cab" ).to match_array( [ [",bac repeats:bac, repeats:bac",""] ] ) }
|
245
|
+
|
246
|
+
end
|
247
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
4
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
5
|
+
# files.
|
6
|
+
#
|
7
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
8
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
9
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
10
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
11
|
+
# a separate helper file that requires the additional dependencies and performs
|
12
|
+
# the additional setup, and require it from the spec files that actually need
|
13
|
+
# it.
|
14
|
+
#
|
15
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
16
|
+
# users commonly want.
|
17
|
+
#
|
18
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
19
|
+
RSpec.configure do |config|
|
20
|
+
# rspec-expectations config goes here. You can use an alternate
|
21
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
22
|
+
# assertions if you prefer.
|
23
|
+
config.expect_with :rspec do |expectations|
|
24
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
25
|
+
# and `failure_message` of custom matchers include text for helper methods
|
26
|
+
# defined using `chain`, e.g.:
|
27
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
28
|
+
# # => "be bigger than 2 and smaller than 4"
|
29
|
+
# ...rather than:
|
30
|
+
# # => "be bigger than 2"
|
31
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
32
|
+
end
|
33
|
+
|
34
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
35
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
36
|
+
config.mock_with :rspec do |mocks|
|
37
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
38
|
+
# a real object. This is generally recommended, and will default to
|
39
|
+
# `true` in RSpec 4.
|
40
|
+
mocks.verify_partial_doubles = true
|
41
|
+
end
|
42
|
+
|
43
|
+
# The settings below are suggested to provide a good initial experience
|
44
|
+
# with RSpec, but feel free to customize to your heart's content.
|
45
|
+
=begin
|
46
|
+
# These two settings work together to allow you to limit a spec run
|
47
|
+
# to individual examples or groups you care about by tagging them with
|
48
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
49
|
+
# get run.
|
50
|
+
config.filter_run :focus
|
51
|
+
config.run_all_when_everything_filtered = true
|
52
|
+
|
53
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
54
|
+
# recommended. For more details, see:
|
55
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
56
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
57
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
58
|
+
config.disable_monkey_patching!
|
59
|
+
|
60
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
61
|
+
# be too noisy due to issues in dependencies.
|
62
|
+
config.warnings = true
|
63
|
+
|
64
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
65
|
+
# file, and it's useful to allow more verbose output when running an
|
66
|
+
# individual spec file.
|
67
|
+
if config.files_to_run.one?
|
68
|
+
# Use the documentation formatter for detailed output,
|
69
|
+
# unless a formatter has already been configured
|
70
|
+
# (e.g. via a command-line flag).
|
71
|
+
config.default_formatter = 'doc'
|
72
|
+
end
|
73
|
+
|
74
|
+
# Print the 10 slowest examples and example groups at the
|
75
|
+
# end of the spec run, to help surface which specs are running
|
76
|
+
# particularly slow.
|
77
|
+
config.profile_examples = 10
|
78
|
+
|
79
|
+
# Run specs in random order to surface order dependencies. If you find an
|
80
|
+
# order dependency and want to debug it, you can fix the order by providing
|
81
|
+
# the seed, which is printed after each run.
|
82
|
+
# --seed 1234
|
83
|
+
config.order = :random
|
84
|
+
|
85
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
86
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
87
|
+
# test failures related to randomization by passing the same `--seed` value
|
88
|
+
# as the one that triggered the failure.
|
89
|
+
Kernel.srand config.seed
|
90
|
+
=end
|
91
|
+
end
|