spec/larynx/fields_spec.rb in larynx-0.1.2 vs spec/larynx/fields_spec.rb in larynx-0.1.3

- old
+ new

@@ -1,42 +1,49 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') +class TestApp < Larynx::Application; end + describe Larynx::Fields do attr_reader :call, :app before do @call = TestCallHandler.new(1) - @app = Larynx::Application.new(@call) + @app = define_app.new(@call) end context 'module' do - include Larynx::Fields + before do + @app_class = define_app + end it 'should add field class method' do - self.class.should respond_to(:field) + @app_class.should respond_to(:field) end it 'should add instance accessor for field name' do - self.class.field(:guess) { prompt :speak => 'hello' } - self.methods.include?(:guess) + @app_class.field(:guess) { prompt :speak => 'hello' } + @app_class.methods.include?(:guess) end end context 'next_field' do - include Larynx::Fields - field(:field1) { prompt :speak => 'hello' } - field(:field2) { prompt :speak => 'hello' } - field(:field3) { prompt :speak => 'hello' } + before do + @app = define_app do + field(:field1) { prompt :speak => 'hello' } + field(:field2) { prompt :speak => 'hello' } + field(:field3) { prompt :speak => 'hello' } + end.new(call) + end it 'should iterate over defined fields' do - next_field.name.should == :field1 - next_field.name.should == :field2 - next_field.name.should == :field3 + app.next_field.name.should == :field1 + app.next_field.name.should == :field2 + app.next_field.name.should == :field3 end it 'should jump to field name if supplied' do - next_field(:field2).name.should == :field2 + app.next_field(:field2).name.should == :field2 end end context 'field object' do it 'should raise exception if field has no prompt' do @@ -50,10 +57,22 @@ setup &call_me end fld.run app end + it 'should pass timeout and length options to the prompt object' do + fld = field(:guess, :length => 1, :min_length => 1, :max_length => 2, :interdigit_timeout => 1, :timeout => 2) do + prompt :speak => 'first' + end + fld.run(app) + prompt = fld.current_prompt + prompt.interdigit_timeout.should == 1 + prompt.timeout.should == 2 + prompt.minimum_length.should == 1 + prompt.maximum_length.should == 2 + end + it 'should return same prompt all attempts if single prompt' do fld = field(:guess) do prompt :speak => 'first' end fld.run(app) @@ -84,21 +103,10 @@ fld.current_prompt.message.should == 'first' fld.increment_attempts fld.current_prompt.message.should == 'second' end - context 'valid_length?' do - it 'should be false if input size less than minimum' do - fld = field(:guess) do - prompt :speak => 'first' - end - fld.run app - fld.current_prompt.finalise - fld.valid_length?.should be_false - end - end - context 'input evaluation' do it 'should run validate callback if input minimum length' do call_me = should_be_called fld = field(:guess, :min_length => 1) do prompt :speak => 'first' @@ -202,7 +210,14 @@ end def field(name, options={}, &block) @app.class.class_eval { attr_accessor name } Larynx::Fields::Field.new(name, options, &block) + end + + def define_app(&block) + reset_class(TestApp) do + include Larynx::Fields + instance_eval &block if block_given? + end end end