spec/larynx/fields_spec.rb in larynx-0.1.4 vs spec/larynx/fields_spec.rb in larynx-0.1.5

- old
+ new

@@ -23,11 +23,11 @@ @app_class.field(:guess) { prompt :speak => 'hello' } @app_class.methods.include?(:guess) end end - context 'next_field' do + context '#next_field' do before do @app = define_app do field(:field1) { prompt :speak => 'hello' } field(:field2) { prompt :speak => 'hello' } field(:field3) { prompt :speak => 'hello' } @@ -43,10 +43,25 @@ it 'should jump to field name if supplied' do app.next_field(:field2).name.should == :field2 end end + context "#current_field" do + it 'should return field of current position' do + @app = define_app do + field(:field1) { prompt :speak => 'hello' } + field(:field2) { prompt :speak => 'hello' } + end.new(call) + app.run + + app.next_field + app.current_field.should == app.fields[0] + app.next_field + app.current_field.should == app.fields[1] + end + end + context 'field object' do it 'should raise exception if field has no prompt' do lambda { field(:guess) {} }.should raise_exception(Larynx::NoPromptDefined) end @@ -103,10 +118,31 @@ fld.current_prompt.message.should == 'first' fld.increment_attempts fld.current_prompt.message.should == 'second' end + context "#last_attempt?" do + it 'should return false when current attempt not equal to max attempts' do + fld = field(:guess, :attempts => 2) do + prompt :speak => 'first' + end + fld.run(app) + fld.attempt.should == 1 + fld.last_attempt?.should be_false + end + + it 'should return true when current attempt equals max attempts' do + fld = field(:guess, :attempts => 2) do + prompt :speak => 'first' + end + fld.run(app) + fld.increment_attempts + fld.attempt.should == 2 + fld.last_attempt?.should be_true + 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' @@ -130,9 +166,21 @@ it 'should run invalid callback if validate callback returns false' do call_me = should_be_called fld = field(:guess, :min_length => 1) do prompt :speak => 'first' validate { false } + invalid &call_me + end + fld.run app + call.input << '1' + fld.current_prompt.finalise + end + + it 'should run invalid callback if validate callback returns nil' do + call_me = should_be_called + fld = field(:guess, :min_length => 1) do + prompt :speak => 'first' + validate { nil } invalid &call_me end fld.run app call.input << '1' fld.current_prompt.finalise