Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
benlangfeld committed Sep 22, 2011
1 parent 0431fc6 commit 2c31568
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 93 deletions.
2 changes: 1 addition & 1 deletion lib/ruby_speech/grxml.rb
Expand Up @@ -14,7 +14,7 @@ module GRXML

GRXML_NAMESPACE = 'http://www.w3.org/2001/06/grammar'

def self.draw(attributes={}, &block)
def self.draw(attributes = {}, &block)
Grammar.new(attributes).tap do |grammar|
block_return = grammar.instance_eval(&block) if block_given?
grammar << block_return if block_return.is_a?(String)
Expand Down
27 changes: 12 additions & 15 deletions lib/ruby_speech/grxml/item.rb
Expand Up @@ -62,7 +62,7 @@ def weight
#
def weight=(w)
raise ArgumentError, "A Item's weight attribute must be a positive floating point number" unless w.to_s.match(/[^0-9\.]/) == nil and w.to_f >= 0
write_attr :weight, w.to_s
write_attr :weight, w
end

##
Expand All @@ -83,27 +83,24 @@ def repeat
# @param [String] r
#
def repeat=(r)
r=r.to_s
errormsg = "A Item's repeat must be 0 or a positive integer"
r = r.to_s
error = ArgumentError.new "A Item's repeat must be 0 or a positive integer"

badarg = true unless r.match(/[^0-9-]/) == nil and r.scan("-").size <= 1
raise ArgumentError, errormsg if badarg
raise error unless r.match(/[^0-9-]/) == nil and r.scan("-").size <= 1

di = r.index('-')
case di
raise error if case di = r.index('-')
when nil
badarg=true if r.to_i < 0 # must be 0 or a positive number
r.to_i < 0 # must be 0 or a positive number
when 0
badarg=true # negative numbers are illegal
true # negative numbers are illegal
else
if di == r.length-1 # repeat 'm' or more times, m must be 0 or a positive number
badarg=true unless r[0,r.length-1].to_i >= 0
if di == r.length - 1 # repeat 'm' or more times, m must be 0 or a positive number
r[0, r.length - 1].to_i < 0
else # verify range m,n is valid
m,n = r.split('-')
badarg=true unless m.to_i >= 0 and n.to_i >= m.to_i
m, n = r.split('-').map &:to_i
m < 0 || n < m
end
end
raise ArgumentError, errormsg if badarg
write_attr :repeat, r
end

Expand All @@ -122,7 +119,7 @@ def repeat_prob
#
def repeat_prob=(rp)
raise ArgumentError, "A Item's repeat probablity attribute must be a floating point number between 0.0 and 1.0" unless rp.to_s.match(/[^0-9\.]/) == nil and rp.to_f >= 0 and rp.to_f <= 1.0
write_attr :'repeat-prob', rp.to_s
write_attr :'repeat-prob', rp
end

def <<(arg)
Expand Down
3 changes: 1 addition & 2 deletions lib/ruby_speech/grxml/rule.rb
Expand Up @@ -68,8 +68,7 @@ def scope
# @param [String] ia
#
def scope=(sc)
sc = sc.to_s
raise ArgumentError, "A Rule's scope can only be 'public' or 'private'" unless %w{public private}.include?(sc)
raise ArgumentError, "A Rule's scope can only be 'public' or 'private'" unless %w{public private}.include?(sc.to_s)
write_attr :scope, sc
end

Expand Down
19 changes: 4 additions & 15 deletions lib/ruby_speech/grxml/ruleref.rb
Expand Up @@ -44,10 +44,7 @@ def uri
# @raises ArgumentError if t is nota positive numeric value
#
def uri=(u)
unless self.special.nil?
raise ArgumentError, "A Ruleref can only take uri or special"
end

raise ArgumentError, "A Ruleref can only take uri or special" if special
write_attr :uri, u
end

Expand All @@ -66,21 +63,13 @@ def special
# TODO: raise ArgumentError if not a valid special...
#
def special=(sp)
unless self.uri.nil?
raise ArgumentError, "A Ruleref can only take uri or special"
end

case sp.to_sym
when :NULL, :VOID, :GARBAGE
write_attr :special, sp.to_s
else
raise ArgumentError, "The Ruleref#special method only takes :NULL, :VOID, and :GARBAGE"
end
raise ArgumentError, "A Ruleref can only take uri or special" if uri
raise ArgumentError, "The Ruleref#special method only takes :NULL, :VOID, and :GARBAGE" unless %w{NULL VOID GARBAGE}.include? sp.to_s
write_attr :special, sp
end

def <<(*args)
raise InvalidChildError, "A Ruleref cannot contain children"
super
end

def eql?(o)
Expand Down
10 changes: 5 additions & 5 deletions spec/ruby_speech/grxml/grammar_spec.rb
Expand Up @@ -11,10 +11,10 @@ module GRXML
describe "setting options in initializers" do
subject { Grammar.new :language => 'jp', :base_uri => 'blah', :root => "main_rule", :tag_format => "semantics/1.0" }

its(:language) { should == 'jp' }
its(:base_uri) { should == 'blah' }
its(:root) { should == 'main_rule' }
its(:tag_format) { should == 'semantics/1.0' }
its(:language) { should == 'jp' }
its(:base_uri) { should == 'blah' }
its(:root) { should == 'main_rule' }
its(:tag_format) { should == 'semantics/1.0' }
end

describe "setting dtmf mode" do
Expand Down Expand Up @@ -146,7 +146,7 @@ module GRXML
expected_concat << Rule.new(:id => 'frank', :scope => 'public', :content => "Hi Frank")
expected_concat << Rule.new(:id => 'millie', :scope => 'public', :content => "Hi Millie")

concat = (grammar1 + grammar2)
concat = grammar1 + grammar2
concat.should == expected_concat
concat.to_s.should_not include('default')
end
Expand Down
18 changes: 12 additions & 6 deletions spec/ruby_speech/grxml/item_spec.rb
Expand Up @@ -28,25 +28,27 @@ module GRXML

describe "#weight" do
context "from a document" do
describe "using .1" do
subject { Element.import parse_xml(document).root }

describe "using .1" do
let(:document) { '<item weight=".1" repeat="1">one</item>' }
subject { Element.import parse_xml(document).root }
its(:weight) { should == 0.1 }
end
describe "using 1." do

describe "using 1." do
let(:document) { '<item weight="1." repeat="1">one</item>' }
subject { Element.import parse_xml(document).root }
its(:weight) { should == 1.0 }
end
describe "using 1" do

describe "using 1" do
let(:document) { '<item weight="1" repeat="1">one</item>' }
subject { Element.import parse_xml(document).root }
its(:weight) { should == 1.0 }
end
end

context "positive floating point numbers" do
before { subject.weight = 1.1 }

its(:weight) { should == 1.1 }

it "with valid value" do
Expand All @@ -56,6 +58,7 @@ module GRXML
lambda { subject.weight = '.1' }.should_not raise_error
lambda { subject.weight = '1.' }.should_not raise_error
end

it "with an invalid value" do
lambda { subject.weight = 'one' }.should raise_error(ArgumentError, "A Item's weight attribute must be a positive floating point number")
lambda { subject.weight = -1 }.should raise_error(ArgumentError, "A Item's weight attribute must be a positive floating point number")
Expand All @@ -71,6 +74,7 @@ module GRXML
lambda { subject.repeat = 5 }.should_not raise_error
lambda { subject.repeat = '1' }.should_not raise_error
end

it "invalid values" do
lambda { subject.repeat = -1 }.should raise_error(ArgumentError, "A Item's repeat must be 0 or a positive integer")
lambda { subject.repeat = 'one' }.should raise_error(ArgumentError, "A Item's repeat must be 0 or a positive integer")
Expand All @@ -94,6 +98,7 @@ module GRXML
lambda { subject.repeat = '3-' }.should_not raise_error
lambda { subject.repeat = '0-' }.should_not raise_error
end

it "illegal ranges for m or more" do
lambda { subject.repeat = '-1-' }.should raise_error(ArgumentError, "A Item's repeat must be 0 or a positive integer")
lambda { subject.repeat = 'B-' }.should raise_error(ArgumentError, "A Item's repeat must be 0 or a positive integer")
Expand All @@ -111,6 +116,7 @@ module GRXML
lambda { subject.repeat_prob = '1.0' }.should_not raise_error
lambda { subject.repeat_prob = '.5' }.should_not raise_error
end

it "should raise an error for invalid values" do
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")
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")
Expand Down
3 changes: 0 additions & 3 deletions spec/ruby_speech/grxml/one_of_spec.rb
Expand Up @@ -17,9 +17,6 @@ module GRXML
it { should be_instance_of OneOf }
end

# TODO: ensure it has at least one item element. Maybe this should be at
# the top level of grxml_spec?

describe "#language" do
before { subject.language = 'fr-CA' }

Expand Down
8 changes: 4 additions & 4 deletions spec/ruby_speech/grxml/rule_spec.rb
Expand Up @@ -36,13 +36,13 @@ module GRXML

its(:id) { should == :main }

it "with out an id" do
context "without an id" do
before { subject.id = nil }
pending
end

# FIXME: this should probably go into the grammar spec
it "with an non-unique id" do
pending
context "with a non-unique id" do
pending 'this should probably go into the grammar spec'
end
end

Expand Down
6 changes: 2 additions & 4 deletions spec/ruby_speech/grxml/ruleref_spec.rb
Expand Up @@ -6,7 +6,6 @@ module GRXML
subject { Ruleref.new :uri => '#testrule' }

its(:name) { should == 'ruleref' }

its(:uri) { should == '#testrule' }

it 'registers itself' do
Expand All @@ -25,6 +24,7 @@ module GRXML

describe "#special" do
subject { Ruleref.new }

context "with reserved values" do
it "with a valid value" do
lambda { subject.special = :NULL }.should_not raise_error
Expand All @@ -38,7 +38,6 @@ module GRXML
end

describe "#uri" do
subject { Ruleref.new }
it "allows implict, explicit and external references" do
lambda { subject.uri = '#dtmf' }.should_not raise_error
lambda { subject.uri = '../test.grxml' }.should_not raise_error
Expand All @@ -48,10 +47,9 @@ module GRXML

describe "only uri or special can be specified" do
it "should raise an error" do
lambda {subject << Ruleref.new(:uri => '#test', :special => :NULL)}.should raise_error(ArgumentError, "A Ruleref can only take uri or special")
lambda { subject << Ruleref.new(:uri => '#test', :special => :NULL) }.should raise_error(ArgumentError, "A Ruleref can only take uri or special")
end
end

end # Ruleref
end # GRXML
end # RubySpeech

0 comments on commit 2c31568

Please sign in to comment.