ruby_speech 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,156 @@
1
+ require 'spec_helper'
2
+
3
+ module RubySpeech
4
+ module GRXML
5
+ describe Grammar do
6
+ it { should be_a_valid_grxml_document }
7
+
8
+ its(:name) { should == 'grammar' }
9
+ its(:language) { should == 'en-US' }
10
+
11
+ describe "setting options in initializers" do
12
+ subject { Grammar.new :language => 'jp', :base_uri => 'blah', :root => "main_rule", :tag_format => "semantics/1.0" }
13
+
14
+ its(:language) { should == 'jp' }
15
+ its(:base_uri) { should == 'blah' }
16
+ its(:root) { should == 'main_rule' }
17
+ its(:tag_format) { should == 'semantics/1.0' }
18
+ end
19
+
20
+ describe "setting dtmf mode" do
21
+ subject { Grammar.new :mode => 'dtmf' }
22
+ its(:mode) { should == 'dtmf' }
23
+ end
24
+
25
+ describe "setting voice mode" do
26
+ subject { Grammar.new :mode => 'voice' }
27
+ its(:mode) { should == 'voice' }
28
+ end
29
+
30
+ it 'registers itself' do
31
+ Element.class_from_registration(:grammar).should == Grammar
32
+ end
33
+
34
+ describe "from a document" do
35
+ let(:document) { '<grammar mode="dtmf" root="main_rule" version="1.0" xml:lang="jp" xml:base="blah"
36
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
37
+ xsi:schemaLocation="http://www.w3.org/2001/06/grammar
38
+ http://www.w3.org/TR/speech-grammar/grammar.xsd"
39
+ xmlns="http://www.w3.org/2001/06/grammar" />' }
40
+
41
+ subject { Element.import parse_xml(document).root }
42
+
43
+ it { should be_instance_of Grammar }
44
+
45
+ its(:language) { pending; should == 'jp' }
46
+ its(:base_uri) { should == 'blah' }
47
+ its(:mode) { should == 'dtmf' }
48
+ its(:root) { should == 'main_rule' }
49
+ end
50
+
51
+ describe "#language" do
52
+ before { subject.language = 'jp' }
53
+
54
+ its(:language) { should == 'jp' }
55
+ end
56
+
57
+ describe "#base_uri" do
58
+ before { subject.base_uri = 'blah' }
59
+
60
+ its(:base_uri) { should == 'blah' }
61
+ end
62
+
63
+ describe "comparing objects" do
64
+ it "should be equal if the content, language and base uri are the same" do
65
+ Grammar.new(:language => 'en-GB', :base_uri => 'blah', :content => "Hello there").should == Grammar.new(:language => 'en-GB', :base_uri => 'blah', :content => "Hello there")
66
+ end
67
+
68
+ describe "when the content is different" do
69
+ it "should not be equal" do
70
+ Grammar.new(:content => "Hello").should_not == Grammar.new(:content => "Hello there")
71
+ end
72
+ end
73
+
74
+ describe "when the language is different" do
75
+ it "should not be equal" do
76
+ Grammar.new(:language => 'en-US').should_not == Grammar.new(:language => 'en-GB')
77
+ end
78
+ end
79
+
80
+ describe "when the base URI is different" do
81
+ it "should not be equal" do
82
+ Grammar.new(:base_uri => 'foo').should_not == Grammar.new(:base_uri => 'bar')
83
+ end
84
+ end
85
+
86
+ describe "when the children are different" do
87
+ it "should not be equal" do
88
+ g1 = Grammar.new
89
+ g1 << Rule.new(:id => 'main1')
90
+ g2 = Grammar.new
91
+ g2 << Rule.new(:id => 'main2')
92
+
93
+ g1.should_not == g2
94
+ end
95
+ end
96
+ end
97
+
98
+ it "should allow creating child GRXML elements" do
99
+ g = Grammar.new
100
+ g.Rule :id => :main, :scope => 'public'
101
+ expected_g = Grammar.new
102
+ expected_g << Rule.new(:id => :main, :scope => 'public')
103
+ g.should == expected_g
104
+ end
105
+
106
+ describe "<<" do
107
+ it "should accept Rule" do
108
+ lambda { subject << Rule.new }.should_not raise_error
109
+ end
110
+
111
+ it "should accept Tag" do
112
+ lambda { subject << Tag.new }.should_not raise_error
113
+ end
114
+
115
+ it "should raise InvalidChildError with non-acceptable objects" do
116
+ lambda { subject << 1 }.should raise_error(InvalidChildError, "A Grammar can only accept Rule and Tag as children")
117
+ end
118
+ end
119
+
120
+ describe "#to_doc" do
121
+ let(:expected_doc) do
122
+ Nokogiri::XML::Document.new.tap do |doc|
123
+ doc << Grammar.new
124
+ end
125
+ end
126
+
127
+ it "should create an XML document from the grammar" do
128
+ Grammar.new.to_doc.to_s.should == expected_doc.to_s
129
+ end
130
+ end
131
+
132
+ describe "#tag_format" do
133
+ it "should allow setting tag-format identifier" do
134
+ lambda { subject.tag_format = "semantics/1.0" }.should_not raise_error
135
+ end
136
+ end
137
+
138
+ describe "concat" do
139
+ it "should allow concatenation" do
140
+ grammar1 = Grammar.new
141
+ grammar1 << Rule.new(:id => 'frank', :scope => 'public', :content => "Hi Frank")
142
+ grammar2 = Grammar.new
143
+ grammar2 << Rule.new(:id => 'millie', :scope => 'public', :content => "Hi Millie")
144
+
145
+ expected_concat = Grammar.new
146
+ expected_concat << Rule.new(:id => 'frank', :scope => 'public', :content => "Hi Frank")
147
+ expected_concat << Rule.new(:id => 'millie', :scope => 'public', :content => "Hi Millie")
148
+
149
+ concat = grammar1 + grammar2
150
+ concat.should == expected_concat
151
+ concat.to_s.should_not include('default')
152
+ end
153
+ end
154
+ end # Grammar
155
+ end # GRXML
156
+ end # RubySpeech
@@ -0,0 +1,160 @@
1
+ require 'spec_helper'
2
+
3
+ module RubySpeech
4
+ module GRXML
5
+ describe Item do
6
+ subject { Item.new :weight => 1.1, :repeat => '1' }
7
+
8
+ its(:name) { should == 'item' }
9
+
10
+ its(:weight) { should == 1.1 }
11
+ its(:repeat) { should == '1' }
12
+
13
+ it 'registers itself' do
14
+ Element.class_from_registration(:item).should == Item
15
+ end
16
+
17
+ describe "everything from a document" do
18
+ let(:document) { '<item weight="1.1" repeat="1">one</item>' }
19
+
20
+ subject { Element.import parse_xml(document).root }
21
+
22
+ it { should be_instance_of Item }
23
+
24
+ its(:weight) { should == 1.1 }
25
+ its(:repeat) { should == '1' }
26
+ its(:content) { should == 'one' }
27
+ end
28
+
29
+ describe "#weight" do
30
+ context "from a document" do
31
+ subject { Element.import parse_xml(document).root }
32
+
33
+ describe "using .1" do
34
+ let(:document) { '<item weight=".1" repeat="1">one</item>' }
35
+ its(:weight) { should == 0.1 }
36
+ end
37
+
38
+ describe "using 1." do
39
+ let(:document) { '<item weight="1." repeat="1">one</item>' }
40
+ its(:weight) { should == 1.0 }
41
+ end
42
+
43
+ describe "using 1" do
44
+ let(:document) { '<item weight="1" repeat="1">one</item>' }
45
+ its(:weight) { should == 1.0 }
46
+ end
47
+ end
48
+
49
+ context "positive floating point numbers" do
50
+ before { subject.weight = 1.1 }
51
+
52
+ its(:weight) { should == 1.1 }
53
+
54
+ it "with valid value" do
55
+ lambda { subject.weight = 1 }.should_not raise_error
56
+ lambda { subject.weight = 1.0 }.should_not raise_error
57
+ lambda { subject.weight = 0.1 }.should_not raise_error
58
+ lambda { subject.weight = '.1' }.should_not raise_error
59
+ lambda { subject.weight = '1.' }.should_not raise_error
60
+ end
61
+
62
+ it "with an invalid value" do
63
+ lambda { subject.weight = 'one' }.should raise_error(ArgumentError, "A Item's weight attribute must be a positive floating point number")
64
+ lambda { subject.weight = -1 }.should raise_error(ArgumentError, "A Item's weight attribute must be a positive floating point number")
65
+ end
66
+ end
67
+ end
68
+
69
+ # Validate various values for repeat -- http://www.w3.org/TR/speech-grammar/#S2.5
70
+ describe "#repeat" do
71
+ context "exact" do
72
+ it "valid values (0 or a positive integer)" do
73
+ lambda { subject.repeat = 0 }.should_not raise_error
74
+ lambda { subject.repeat = 5 }.should_not raise_error
75
+ lambda { subject.repeat = '1' }.should_not raise_error
76
+ end
77
+
78
+ it "invalid values" do
79
+ lambda { subject.repeat = -1 }.should raise_error(ArgumentError, "A Item's repeat must be 0 or a positive integer")
80
+ lambda { subject.repeat = 'one' }.should raise_error(ArgumentError, "A Item's repeat must be 0 or a positive integer")
81
+ end
82
+ end
83
+
84
+ context "ranges" do
85
+ it "valid ranges from m to n" do
86
+ lambda { subject.repeat = '1-5' }.should_not raise_error
87
+ lambda { subject.repeat = '0-5' }.should_not raise_error
88
+ end
89
+
90
+ it "illegal ranges from m to n" do
91
+ lambda { subject.repeat = '5-1' }.should raise_error(ArgumentError, "A Item's repeat must be 0 or a positive integer")
92
+ lambda { subject.repeat = '-1-2' }.should raise_error(ArgumentError, "A Item's repeat must be 0 or a positive integer")
93
+ lambda { subject.repeat = '1-2-3' }.should raise_error(ArgumentError, "A Item's repeat must be 0 or a positive integer")
94
+ lambda { subject.repeat = '1-B' }.should raise_error(ArgumentError, "A Item's repeat must be 0 or a positive integer")
95
+ end
96
+
97
+ it "valid ranges of m or more" do
98
+ lambda { subject.repeat = '3-' }.should_not raise_error
99
+ lambda { subject.repeat = '0-' }.should_not raise_error
100
+ end
101
+
102
+ it "illegal ranges for m or more" do
103
+ lambda { subject.repeat = '-1-' }.should raise_error(ArgumentError, "A Item's repeat must be 0 or a positive integer")
104
+ lambda { subject.repeat = 'B-' }.should raise_error(ArgumentError, "A Item's repeat must be 0 or a positive integer")
105
+ end
106
+ end
107
+ end
108
+
109
+ # repeat probability (repeat-prob) -- http://www.w3.org/TR/speech-grammar/#S2.5.1
110
+ describe "#repeat_prob" do
111
+ it "should handle all valid values" do
112
+ lambda { subject.repeat_prob = 0 }.should_not raise_error
113
+ lambda { subject.repeat_prob = 1 }.should_not raise_error
114
+ lambda { subject.repeat_prob = 1.0 }.should_not raise_error
115
+ lambda { subject.repeat_prob = '1.' }.should_not raise_error
116
+ lambda { subject.repeat_prob = '1.0' }.should_not raise_error
117
+ lambda { subject.repeat_prob = '.5' }.should_not raise_error
118
+ end
119
+
120
+ it "should raise an error for invalid values" do
121
+ lambda { subject.repeat_prob = -1 }.should raise_error(ArgumentError, "A Item's repeat probablity attribute must be a floating point number between 0.0 and 1.0")
122
+ lambda { subject.repeat_prob = 1.5 }.should raise_error(ArgumentError, "A Item's repeat probablity attribute must be a floating point number between 0.0 and 1.0")
123
+ end
124
+ end
125
+
126
+ describe "#language" do
127
+ before { subject.language = 'jp' }
128
+
129
+ its(:language) { should == 'jp' }
130
+ end
131
+
132
+
133
+ describe "<<" do
134
+ it "should accept String" do
135
+ lambda { subject << 'anything' }.should_not raise_error
136
+ end
137
+
138
+ it "should accept OneOf" do
139
+ lambda { subject << OneOf.new }.should_not raise_error
140
+ end
141
+
142
+ it "should accept Item" do
143
+ lambda { subject << Item.new }.should_not raise_error
144
+ end
145
+
146
+ it "should accept Ruleref" do
147
+ lambda { subject << Ruleref.new }.should_not raise_error
148
+ end
149
+
150
+ it "should accept Tag" do
151
+ lambda { subject << Tag.new }.should_not raise_error
152
+ end
153
+
154
+ it "should accept Token" do
155
+ lambda { subject << Token.new }.should_not raise_error
156
+ end
157
+ end
158
+ end # Item
159
+ end # GRXML
160
+ end # RubySpeech
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ module RubySpeech
4
+ module GRXML
5
+ describe OneOf do
6
+ its(:name) { should == 'one-of' }
7
+
8
+ it 'registers itself' do
9
+ Element.class_from_registration(:'one-of').should == OneOf
10
+ end
11
+
12
+ describe "from a document" do
13
+ let(:document) { '<one-of> <item>test</item> </one-of>' }
14
+
15
+ subject { Element.import parse_xml(document).root }
16
+
17
+ it { should be_instance_of OneOf }
18
+ end
19
+
20
+ describe "#language" do
21
+ before { subject.language = 'fr-CA' }
22
+
23
+ its(:language) { should == 'fr-CA' }
24
+ end
25
+
26
+ describe "<<" do
27
+ it "should accept Item" do
28
+ lambda { subject << Item.new }.should_not raise_error
29
+ end
30
+
31
+ it "should raise InvalidChildError with non-acceptable objects" do
32
+ lambda { subject << 1 }.should raise_error(InvalidChildError, "A OneOf can only accept Item as children")
33
+ end
34
+ end
35
+
36
+ describe "comparing objects" do
37
+ it "should be equal if the language (when specified) is the same" do
38
+ OneOf.new(:language => "jp").should == OneOf.new(:language => "jp")
39
+ end
40
+
41
+ describe "when the language is different" do
42
+ it "should not be equal" do
43
+ OneOf.new(:language => "jp").should_not == OneOf.new(:content => "fr-CA")
44
+ end
45
+ end
46
+ end
47
+
48
+ end # OneOf
49
+ end # GRXML
50
+ end # RubySpeech
@@ -0,0 +1,125 @@
1
+ require 'spec_helper'
2
+
3
+ module RubySpeech
4
+ module GRXML
5
+ describe Rule do
6
+ subject { Rule.new :id => 'one', :scope => 'public' }
7
+
8
+ its(:name) { should == 'rule' }
9
+
10
+ its(:id) { should == :one }
11
+ its(:scope) { should == :public }
12
+
13
+ it 'registers itself' do
14
+ Element.class_from_registration(:rule).should == Rule
15
+ end
16
+
17
+ describe "from a document" do
18
+ let(:document) { '<rule id="one" scope="public"> <item /> </rule>' }
19
+
20
+ subject { Element.import parse_xml(document).root }
21
+
22
+ it { should be_instance_of Rule }
23
+
24
+ its(:id) { should == :one }
25
+ its(:scope) { should == :public }
26
+ end
27
+
28
+ describe "#language" do
29
+ before { subject.language = 'jp' }
30
+
31
+ its(:language) { should == 'jp' }
32
+ end
33
+
34
+ describe "#id" do
35
+ before { subject.id = :main }
36
+
37
+ its(:id) { should == :main }
38
+
39
+ context "without an id" do
40
+ before { subject.id = nil }
41
+ pending
42
+ end
43
+
44
+ context "with a non-unique id" do
45
+ pending 'this should probably go into the grammar spec'
46
+ end
47
+ end
48
+
49
+ describe "#scope" do
50
+ before { subject.scope = :public }
51
+
52
+ its(:scope) { should == :public }
53
+
54
+ it "with a valid scope" do
55
+ lambda { subject.scope = :public }.should_not raise_error
56
+ lambda { subject.scope = :private }.should_not raise_error
57
+ end
58
+
59
+ it "with an invalid scope" do
60
+ lambda { subject.scope = :something }.should raise_error(ArgumentError, "A Rule's scope can only be 'public' or 'private'")
61
+ end
62
+ end
63
+
64
+ describe "comparing objects" do
65
+ it "should be equal if the content, language, id, and scope are the same" do
66
+ Rule.new(:language => 'jp', :id => :main, :scope => :public, :content => "hello").should == Rule.new(:language => 'jp', :id => :main, :scope => :public, :content => "hello")
67
+ end
68
+
69
+ describe "when the content is different" do
70
+ it "should not be equal" do
71
+ Rule.new(:content => "Hello").should_not == Rule.new(:content => "Hello there")
72
+ end
73
+ end
74
+
75
+ describe "when the language is different" do
76
+ it "should not be equal" do
77
+ Rule.new(:language => "jp").should_not == Rule.new(:language => "esperanto")
78
+ end
79
+ end
80
+
81
+ describe "when the id is different" do
82
+ it "should not be equal" do
83
+ Rule.new(:id => :main).should_not == Rule.new(:id => :dtmf)
84
+ end
85
+ end
86
+
87
+ describe "when the scope is different" do
88
+ it "should not be equal" do
89
+ Rule.new(:scope => :public).should_not == Rule.new(:scope => :private)
90
+ end
91
+ end
92
+ end
93
+
94
+ describe "<<" do
95
+ it "should accept String" do
96
+ lambda { subject << 'anything' }.should_not raise_error
97
+ end
98
+
99
+ it "should accept OneOf" do
100
+ lambda { subject << OneOf.new }.should_not raise_error
101
+ end
102
+
103
+ it "should accept Item" do
104
+ lambda { subject << Item.new }.should_not raise_error
105
+ end
106
+
107
+ it "should accept Ruleref" do
108
+ lambda { subject << Ruleref.new }.should_not raise_error
109
+ end
110
+
111
+ it "should accept Tag" do
112
+ lambda { subject << Tag.new }.should_not raise_error
113
+ end
114
+
115
+ it "should accept Token" do
116
+ lambda { subject << Token.new }.should_not raise_error
117
+ end
118
+ end
119
+
120
+ it "should raise ArgumentError with any other scope" do
121
+ lambda { Rule.new :id => 'one', :scope => 'invalid_scope' }.should raise_error(ArgumentError, "A Rule's scope can only be 'public' or 'private'")
122
+ end
123
+ end # Rule
124
+ end # GRXML
125
+ end # RubySpeech
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ module RubySpeech
4
+ module GRXML
5
+ describe Ruleref do
6
+ subject { Ruleref.new :uri => '#testrule' }
7
+
8
+ its(:name) { should == 'ruleref' }
9
+ its(:uri) { should == '#testrule' }
10
+
11
+ it 'registers itself' do
12
+ Element.class_from_registration(:ruleref).should == Ruleref
13
+ end
14
+
15
+ describe "from a document" do
16
+ let(:document) { '<ruleref uri="#one" />' }
17
+
18
+ subject { Element.import parse_xml(document).root }
19
+
20
+ it { should be_instance_of Ruleref }
21
+
22
+ its(:uri) { should == '#one' }
23
+ end
24
+
25
+ describe "#special" do
26
+ subject { Ruleref.new }
27
+
28
+ context "with reserved values" do
29
+ it "with a valid value" do
30
+ lambda { subject.special = :NULL }.should_not raise_error
31
+ lambda { subject.special = :VOID }.should_not raise_error
32
+ lambda { subject.special = 'GARBAGE' }.should_not raise_error
33
+ end
34
+ it "with an invalid value" do
35
+ lambda { subject.special = :SOMETHINGELSE }.should raise_error
36
+ end
37
+ end
38
+ end
39
+
40
+ describe "#uri" do
41
+ it "allows implict, explicit and external references" do
42
+ lambda { subject.uri = '#dtmf' }.should_not raise_error
43
+ lambda { subject.uri = '../test.grxml' }.should_not raise_error
44
+ lambda { subject.uri = 'http://grammar.example.com/world-cities.grxml#canada' }.should_not raise_error
45
+ end
46
+ end
47
+
48
+ describe "only uri or special can be specified" do
49
+ it "should raise an error" do
50
+ lambda { subject << Ruleref.new(:uri => '#test', :special => :NULL) }.should raise_error(ArgumentError, "A Ruleref can only take uri or special")
51
+ end
52
+ end
53
+ end # Ruleref
54
+ end # GRXML
55
+ end # RubySpeech
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ module RubySpeech
4
+ module GRXML
5
+ describe Tag do
6
+ its(:name) { should == 'tag' }
7
+
8
+ it 'registers itself' do
9
+ Element.class_from_registration(:tag).should == Tag
10
+ end
11
+
12
+ describe "from a document" do
13
+ let(:document) { '<tag>hello</tag>' }
14
+
15
+ subject { Element.import parse_xml(document).root }
16
+
17
+ it { should be_instance_of Tag }
18
+
19
+ its(:content) { should == 'hello' }
20
+ end
21
+
22
+ describe "comparing objects" do
23
+ it "should be equal if the content is the same" do
24
+ Tag.new(:content => "hello").should == Tag.new(:content => "hello")
25
+ end
26
+
27
+ describe "when the content is different" do
28
+ it "should not be equal" do
29
+ Tag.new(:content => "Hello").should_not == Tag.new(:content => "Hello there")
30
+ end
31
+ end
32
+ end
33
+
34
+ describe "<<" do
35
+ it "should accept String" do
36
+ lambda { subject << 'anything' }.should_not raise_error
37
+ end
38
+ end
39
+ end # Tag
40
+ end # GRXML
41
+ end # RubySpeech
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ module RubySpeech
4
+ module GRXML
5
+ describe Token do
6
+ its(:name) { should == 'token' }
7
+
8
+ it 'registers itself' do
9
+ Element.class_from_registration(:token).should == Token
10
+ end
11
+
12
+ describe "from a document" do
13
+ let(:document) { '<token>hello</token>' }
14
+
15
+ subject { Element.import parse_xml(document).root }
16
+
17
+ it { should be_instance_of Token }
18
+
19
+ its(:content) { should == 'hello' }
20
+ end
21
+
22
+ describe "#language" do
23
+ before { subject.language = 'jp' }
24
+
25
+ its(:language) { should == 'jp' }
26
+ end
27
+
28
+ describe "comparing objects" do
29
+ it "should be equal if the content is the same" do
30
+ Token.new(:content => "hello").should == Token.new(:content => "hello")
31
+ end
32
+
33
+ describe "when the content is different" do
34
+ it "should not be equal" do
35
+ Token.new(:content => "Hello").should_not == Token.new(:content => "Hello there")
36
+ end
37
+ end
38
+ end
39
+
40
+ describe "<<" do
41
+ it "should accept String" do
42
+ lambda { subject << 'anything' }.should_not raise_error
43
+ end
44
+ end
45
+ end # Token
46
+ end # GRXML
47
+ end # RubySpeech