sequitur 0.1.13 → 0.1.14

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,95 +1,92 @@
1
- require_relative '../../spec_helper'
2
- require 'stringio'
3
-
4
- require_relative '../../../lib/sequitur/dynamic_grammar'
5
-
6
- # Load the class under test
7
- require_relative '../../../lib/sequitur/formatter/base_text'
8
-
9
- module Sequitur # Re-open the module to get rid of qualified names
10
- module Formatter
11
-
12
- describe BaseText do
13
- # Factory method. Build a production with the given sequence
14
- # of symbols as its rhs.
15
- def build_production(*symbols)
16
- prod = Production.new
17
- symbols.each { |symb| prod.append_symbol(symb) }
18
- return prod
19
- end
20
-
21
- let(:p_a) { build_production(:a) }
22
- let(:p_b) { build_production(:b) }
23
- let(:p_c) { build_production(:c) }
24
- let(:p_bc) { build_production(p_b, p_c) }
25
-
26
- let(:empty_grammar) { DynamicGrammar.new }
27
- let(:sample_grammar) do
28
- grm = DynamicGrammar.new
29
- grm.add_production(p_a)
30
- grm.add_production(p_b)
31
- grm.add_production(p_c)
32
- grm.add_production(p_bc)
33
- grm
34
- end
35
-
36
- let(:destination) { StringIO.new('', 'w') }
37
-
38
- context 'Standard creation & initialization:' do
39
-
40
- it 'should be initialized with an IO argument' do
41
- expect { BaseText.new(StringIO.new('', 'w')) }.not_to raise_error
42
- end
43
-
44
- it 'should know its output destination' do
45
- instance = BaseText.new(destination)
46
- expect(instance.output).to eq(destination)
47
- end
48
- end # context
49
-
50
-
51
-
52
- context 'Formatting events:' do
53
- it 'should support events of an empty grammar' do
54
- instance = BaseText.new(destination)
55
- a_visitor = empty_grammar.visitor
56
- instance.render(a_visitor)
57
- expectations = <<-SNIPPET
58
- start :.
59
- SNIPPET
60
- expect(destination.string).to eq(expectations)
61
- end
62
-
63
- it 'should support visit events with an explicit visitor' do
64
- instance = BaseText.new(destination)
65
- a_visitor = sample_grammar.visitor # Use visitor explicitly
66
- instance.render(a_visitor)
67
- expectations = <<-SNIPPET
68
- start :.
69
- P1 : a.
70
- P2 : b.
71
- P3 : c.
72
- P4 : P2 P3.
73
- SNIPPET
74
- expect(destination.string).to eq(expectations)
75
- end
76
-
77
- it 'should support visit events without an explicit visitor' do
78
- instance = BaseText.new(destination)
79
- instance.render(sample_grammar)
80
- expectations = <<-SNIPPET
81
- start :.
82
- P1 : a.
83
- P2 : b.
84
- P3 : c.
85
- P4 : P2 P3.
86
- SNIPPET
87
- expect(destination.string).to eq(expectations)
88
- end
89
- end # context
90
- end # describe
91
-
92
- end # module
93
- end # module
94
-
95
- # End of file
1
+ require_relative '../../spec_helper'
2
+ require 'stringio'
3
+
4
+ require_relative '../../../lib/sequitur/dynamic_grammar'
5
+
6
+ # Load the class under test
7
+ require_relative '../../../lib/sequitur/formatter/base_text'
8
+
9
+ module Sequitur # Re-open the module to get rid of qualified names
10
+ module Formatter
11
+ describe BaseText do
12
+ # Factory method. Build a production with the given sequence
13
+ # of symbols as its rhs.
14
+ def build_production(*symbols)
15
+ prod = Production.new
16
+ symbols.each { |symb| prod.append_symbol(symb) }
17
+ return prod
18
+ end
19
+
20
+ let(:p_a) { build_production(:a) }
21
+ let(:p_b) { build_production(:b) }
22
+ let(:p_c) { build_production(:c) }
23
+ let(:p_bc) { build_production(p_b, p_c) }
24
+
25
+ let(:empty_grammar) { DynamicGrammar.new }
26
+ let(:sample_grammar) do
27
+ grm = DynamicGrammar.new
28
+ grm.add_production(p_a)
29
+ grm.add_production(p_b)
30
+ grm.add_production(p_c)
31
+ grm.add_production(p_bc)
32
+ grm
33
+ end
34
+
35
+ let(:destination) { StringIO.new('', 'w') }
36
+
37
+ context 'Standard creation & initialization:' do
38
+ it 'should be initialized with an IO argument' do
39
+ expect { BaseText.new(StringIO.new('', 'w')) }.not_to raise_error
40
+ end
41
+
42
+ it 'should know its output destination' do
43
+ instance = BaseText.new(destination)
44
+ expect(instance.output).to eq(destination)
45
+ end
46
+ end # context
47
+
48
+
49
+
50
+ context 'Formatting events:' do
51
+ it 'should support events of an empty grammar' do
52
+ instance = BaseText.new(destination)
53
+ a_visitor = empty_grammar.visitor
54
+ instance.render(a_visitor)
55
+ expectations = <<-SNIPPET
56
+ start :.
57
+ SNIPPET
58
+ expect(destination.string).to eq(expectations)
59
+ end
60
+
61
+ it 'should support visit events with an explicit visitor' do
62
+ instance = BaseText.new(destination)
63
+ a_visitor = sample_grammar.visitor # Use visitor explicitly
64
+ instance.render(a_visitor)
65
+ expectations = <<-SNIPPET
66
+ start :.
67
+ P1 : a.
68
+ P2 : b.
69
+ P3 : c.
70
+ P4 : P2 P3.
71
+ SNIPPET
72
+ expect(destination.string).to eq(expectations)
73
+ end
74
+
75
+ it 'should support visit events without an explicit visitor' do
76
+ instance = BaseText.new(destination)
77
+ instance.render(sample_grammar)
78
+ expectations = <<-SNIPPET
79
+ start :.
80
+ P1 : a.
81
+ P2 : b.
82
+ P3 : c.
83
+ P4 : P2 P3.
84
+ SNIPPET
85
+ expect(destination.string).to eq(expectations)
86
+ end
87
+ end # context
88
+ end # describe
89
+ end # module
90
+ end # module
91
+
92
+ # End of file
@@ -1,114 +1,111 @@
1
- require_relative '../../spec_helper'
2
- require 'stringio'
3
-
4
- require_relative '../../../lib/sequitur/dynamic_grammar'
5
-
6
- # Load the class under test
7
- require_relative '../../../lib/sequitur/formatter/debug'
8
-
9
- module Sequitur # Re-open the module to get rid of qualified names
10
- module Formatter
11
-
12
- describe Debug do
13
- # Factory method. Build a production with the given sequence
14
- # of symbols as its rhs.
15
- def build_production(*symbols)
16
- prod = Production.new
17
- symbols.each { |symb| prod.append_symbol(symb) }
18
- return prod
19
- end
20
-
21
- let(:p_a) { build_production(:a) }
22
- let(:p_b) { build_production(:b) }
23
- let(:p_c) { build_production(:c) }
24
- let(:p_bc) { build_production(p_b, p_c) }
25
-
26
- let(:empty_grammar) { DynamicGrammar.new }
27
- let(:sample_grammar) do
28
- grm = DynamicGrammar.new
29
- grm.add_production(p_a)
30
- grm.add_production(p_b)
31
- grm.add_production(p_c)
32
- grm.add_production(p_bc)
33
- grm
34
- end
35
-
36
- let(:destination) { StringIO.new('', 'w') }
37
-
38
- context 'Standard creation & initialization:' do
39
-
40
- it 'should be initialized with an IO argument' do
41
- expect { Debug.new(StringIO.new('', 'w')) }.not_to raise_error
42
- end
43
-
44
- it 'should know its output destination' do
45
- instance = Debug.new(destination)
46
- expect(instance.output).to eq(destination)
47
- end
48
- end # context
49
-
50
-
51
-
52
- context 'Formatting events:' do
53
- it 'should support events of an empty grammar' do
54
- instance = Debug.new(destination)
55
- a_visitor = empty_grammar.visitor
56
- instance.render(a_visitor)
57
- expectations = <<-SNIPPET
58
- before_grammar
59
- before_production
60
- before_rhs
61
- after_rhs
62
- after_production
63
- after_grammar
64
- SNIPPET
65
- expect(destination.string).to eq(expectations)
66
- end
67
-
68
- it 'should support events of a non-empty grammar' do
69
- instance = Debug.new(destination)
70
- a_visitor = sample_grammar.visitor
71
- instance.render(a_visitor)
72
- expectations = <<-SNIPPET
73
- before_grammar
74
- before_production
75
- before_rhs
76
- after_rhs
77
- after_production
78
- before_production
79
- before_rhs
80
- before_terminal
81
- after_terminal
82
- after_rhs
83
- after_production
84
- before_production
85
- before_rhs
86
- before_terminal
87
- after_terminal
88
- after_rhs
89
- after_production
90
- before_production
91
- before_rhs
92
- before_terminal
93
- after_terminal
94
- after_rhs
95
- after_production
96
- before_production
97
- before_rhs
98
- before_non_terminal
99
- after_non_terminal
100
- before_non_terminal
101
- after_non_terminal
102
- after_rhs
103
- after_production
104
- after_grammar
105
- SNIPPET
106
- expect(destination.string).to eq(expectations)
107
- end
108
- end # context
109
- end # describe
110
-
111
- end # module
112
- end # module
113
-
114
- # End of file
1
+ require_relative '../../spec_helper'
2
+ require 'stringio'
3
+
4
+ require_relative '../../../lib/sequitur/dynamic_grammar'
5
+
6
+ # Load the class under test
7
+ require_relative '../../../lib/sequitur/formatter/debug'
8
+
9
+ module Sequitur # Re-open the module to get rid of qualified names
10
+ module Formatter
11
+ describe Debug do
12
+ # Factory method. Build a production with the given sequence
13
+ # of symbols as its rhs.
14
+ def build_production(*symbols)
15
+ prod = Production.new
16
+ symbols.each { |symb| prod.append_symbol(symb) }
17
+ return prod
18
+ end
19
+
20
+ let(:p_a) { build_production(:a) }
21
+ let(:p_b) { build_production(:b) }
22
+ let(:p_c) { build_production(:c) }
23
+ let(:p_bc) { build_production(p_b, p_c) }
24
+
25
+ let(:empty_grammar) { DynamicGrammar.new }
26
+ let(:sample_grammar) do
27
+ grm = DynamicGrammar.new
28
+ grm.add_production(p_a)
29
+ grm.add_production(p_b)
30
+ grm.add_production(p_c)
31
+ grm.add_production(p_bc)
32
+ grm
33
+ end
34
+
35
+ let(:destination) { StringIO.new('', 'w') }
36
+
37
+ context 'Standard creation & initialization:' do
38
+ it 'should be initialized with an IO argument' do
39
+ expect { Debug.new(StringIO.new('', 'w')) }.not_to raise_error
40
+ end
41
+
42
+ it 'should know its output destination' do
43
+ instance = Debug.new(destination)
44
+ expect(instance.output).to eq(destination)
45
+ end
46
+ end # context
47
+
48
+
49
+
50
+ context 'Formatting events:' do
51
+ it 'should support events of an empty grammar' do
52
+ instance = Debug.new(destination)
53
+ a_visitor = empty_grammar.visitor
54
+ instance.render(a_visitor)
55
+ expectations = <<-SNIPPET
56
+ before_grammar
57
+ before_production
58
+ before_rhs
59
+ after_rhs
60
+ after_production
61
+ after_grammar
62
+ SNIPPET
63
+ expect(destination.string).to eq(expectations)
64
+ end
65
+
66
+ it 'should support events of a non-empty grammar' do
67
+ instance = Debug.new(destination)
68
+ a_visitor = sample_grammar.visitor
69
+ instance.render(a_visitor)
70
+ expectations = <<-SNIPPET
71
+ before_grammar
72
+ before_production
73
+ before_rhs
74
+ after_rhs
75
+ after_production
76
+ before_production
77
+ before_rhs
78
+ before_terminal
79
+ after_terminal
80
+ after_rhs
81
+ after_production
82
+ before_production
83
+ before_rhs
84
+ before_terminal
85
+ after_terminal
86
+ after_rhs
87
+ after_production
88
+ before_production
89
+ before_rhs
90
+ before_terminal
91
+ after_terminal
92
+ after_rhs
93
+ after_production
94
+ before_production
95
+ before_rhs
96
+ before_non_terminal
97
+ after_non_terminal
98
+ before_non_terminal
99
+ after_non_terminal
100
+ after_rhs
101
+ after_production
102
+ after_grammar
103
+ SNIPPET
104
+ expect(destination.string).to eq(expectations)
105
+ end
106
+ end # context
107
+ end # describe
108
+ end # module
109
+ end # module
110
+
111
+ # End of file
@@ -1,88 +1,80 @@
1
- require_relative '../spec_helper'
2
-
3
- # Load the class under test
4
- require_relative '../../lib/sequitur/grammar_visitor'
5
-
6
- module Sequitur # Re-open the module to get rid of qualified names
7
-
8
- describe GrammarVisitor do
9
- # Use a double(mock) as a grammar
10
- let(:fake) { double('fake-grammar') }
11
-
12
- context 'Standard creation & initialization:' do
13
-
14
- # Default instantiation rule
15
- subject { GrammarVisitor.new(fake) }
16
-
17
- it 'should be initialized with a grammar argument' do
18
- expect { GrammarVisitor.new(fake) }.not_to raise_error
19
- end
20
-
21
- it 'should know the grammar to visit' do
22
- expect(subject.grammar).to eq(fake)
23
- end
24
-
25
- it "shouldn't have subscribers at start" do
26
- expect(subject.subscribers).to be_empty
27
- end
28
- end # context
29
-
30
-
31
- context 'Subscribing:' do
32
-
33
- # Default instantiation rule
34
- subject { GrammarVisitor.new(fake) }
35
-
36
- let(:listener1) { double('fake-formatter1') }
37
- let(:listener2) { double('fake-formatter2') }
38
-
39
- it 'should allow subscriptions' do
40
- subject.subscribe(listener1)
41
- expect(subject.subscribers.size).to eq(1)
42
- expect(subject.subscribers).to eq([listener1])
43
-
44
- subject.subscribe(listener2)
45
- expect(subject.subscribers.size).to eq(2)
46
- expect(subject.subscribers).to eq([listener1, listener2])
47
- end
48
-
49
- it 'should allow un-subcriptions' do
50
- subject.subscribe(listener1)
51
- subject.subscribe(listener2)
52
- subject.unsubscribe(listener2)
53
- expect(subject.subscribers.size).to eq(1)
54
- expect(subject.subscribers).to eq([listener1])
55
- subject.unsubscribe(listener1)
56
- expect(subject.subscribers).to be_empty
57
- end
58
-
59
- end # context
60
-
61
- context 'Notifying visit events:' do
62
-
63
- # Default instantiation rule
64
- subject do
65
- instance = GrammarVisitor.new(fake)
66
- instance.subscribe(listener1)
67
- instance
68
- end
69
-
70
- # Use doubles/mocks to simulate formatters
71
- let(:listener1) { double('fake-formatter1') }
72
- let(:listener2) { double('fake-formatter2') }
73
- let(:mock_production) { double('fake-production') }
74
-
75
- it 'should react to the start_visit_grammar message' do
76
- # Notify subscribers when start the visit of the grammar
77
- expect(listener1).to receive(:before_grammar).with(fake)
78
-
79
- subject.start_visit_grammar(fake)
80
- end
81
-
82
- end # context
83
-
84
- end # describe
85
-
86
- end # module
87
-
88
- # End of file
1
+ require_relative '../spec_helper'
2
+
3
+ # Load the class under test
4
+ require_relative '../../lib/sequitur/grammar_visitor'
5
+
6
+ module Sequitur # Re-open the module to get rid of qualified names
7
+ describe GrammarVisitor do
8
+ # Use a double(mock) as a grammar
9
+ let(:fake) { double('fake-grammar') }
10
+
11
+ context 'Standard creation & initialization:' do
12
+ # Default instantiation rule
13
+ subject { GrammarVisitor.new(fake) }
14
+
15
+ it 'should be initialized with a grammar argument' do
16
+ expect { GrammarVisitor.new(fake) }.not_to raise_error
17
+ end
18
+
19
+ it 'should know the grammar to visit' do
20
+ expect(subject.grammar).to eq(fake)
21
+ end
22
+
23
+ it "shouldn't have subscribers at start" do
24
+ expect(subject.subscribers).to be_empty
25
+ end
26
+ end # context
27
+
28
+
29
+ context 'Subscribing:' do
30
+ # Default instantiation rule
31
+ subject { GrammarVisitor.new(fake) }
32
+
33
+ let(:listener1) { double('fake-formatter1') }
34
+ let(:listener2) { double('fake-formatter2') }
35
+
36
+ it 'should allow subscriptions' do
37
+ subject.subscribe(listener1)
38
+ expect(subject.subscribers.size).to eq(1)
39
+ expect(subject.subscribers).to eq([listener1])
40
+
41
+ subject.subscribe(listener2)
42
+ expect(subject.subscribers.size).to eq(2)
43
+ expect(subject.subscribers).to eq([listener1, listener2])
44
+ end
45
+
46
+ it 'should allow un-subcriptions' do
47
+ subject.subscribe(listener1)
48
+ subject.subscribe(listener2)
49
+ subject.unsubscribe(listener2)
50
+ expect(subject.subscribers.size).to eq(1)
51
+ expect(subject.subscribers).to eq([listener1])
52
+ subject.unsubscribe(listener1)
53
+ expect(subject.subscribers).to be_empty
54
+ end
55
+ end # context
56
+
57
+ context 'Notifying visit events:' do
58
+ # Default instantiation rule
59
+ subject do
60
+ instance = GrammarVisitor.new(fake)
61
+ instance.subscribe(listener1)
62
+ instance
63
+ end
64
+
65
+ # Use doubles/mocks to simulate formatters
66
+ let(:listener1) { double('fake-formatter1') }
67
+ let(:listener2) { double('fake-formatter2') }
68
+ let(:mock_production) { double('fake-production') }
69
+
70
+ it 'should react to the start_visit_grammar message' do
71
+ # Notify subscribers when start the visit of the grammar
72
+ expect(listener1).to receive(:before_grammar).with(fake)
73
+
74
+ subject.start_visit_grammar(fake)
75
+ end
76
+ end # context
77
+ end # describe
78
+ end # module
79
+
80
+ # End of file