require File.join(__FILE__.gsub(/(.*)?\/spec\/.*$/, '\1'), 'spec/spec_helper') describe Rtml::Widgets::Subroutine do before :each do @doc = Rtml::Document.new @doc.boolean :condition_var @doc.boolean :txn_is_emv @doc.subroutine :return_true do |args| return true end @doc.subroutine :return_false do |args| return false end @doc.subroutine :check_is_emv do |args| _if(tvars[:txn_is_emv] == true) { return true } _else { return false } end @doc.subroutine :check_condition do |args| tvars[args[:name]] = args[:value] _if(tvars[args[:name]] == true) { goto :condition_is_true } _else { goto :condition_is_untrue } end end it "should declare the subroutine return variable" do # the var isn't created until a sub is called. @doc.screen :i do call(:return_true) end assert @doc.variable?(:rtml_sub_return_val) end it "should be able to query whether a symbol or string is a subroutine name" do assert @doc.subroutine?("return_true") assert @doc.subroutine?(:return_false) assert @doc.subroutine?(:check_condition) assert !@doc.subroutine?(:not_a_sub) assert !@doc.subroutine?('not_a_sub') # and now from a child... assert_raises RuntimeError, "subroutine :return_true not found" do @doc.screen :some_screen do raise if subroutine?("return_true") end end end it "should be able to transform the document directly" do @doc.screen :condition_check_screen do call :check_condition, :name => :condition_var, :value => true end #puts @tml_docs.to_tml end # add your own tests here it "should contain 2 subroutines called return_true and return_false" do #assert_length 4, @tml_docs.subroutines assert @doc.subroutines.keys.include?('return_true') assert @doc.subroutines.keys.include?('return_false') end it "should raise NoMethodError if subroutine does not exist" do assert_raises NoMethodError do @doc.screen(:a_screen).call :no_method end end it "should be able to call a subroutine and get the correct response" do @scr = @doc.screen :a_screen assert_equal ["tmlvar:rtml_sub_return_val", "equal", "tmlvar:rtml_sub_return_val"], @scr.call(:return_true) == "tmlvar:rtml_sub_return_val" assert_not_nil(@var = (@scr / "setvar[name=rtml_sub_return_val]").first) assert_equal true, @var.property('lo') @var.to_tml.should be_valid_tml assert((tml = @scr.to_tml) =~ /setvar[^>]+?lo=["']1["']/, tml) assert_equal ["tmlvar:rtml_sub_return_val", "equal", "tmlvar:rtml_sub_return_val"], @scr.call(:return_false) == "tmlvar:rtml_sub_return_val" assert_not_nil(@var = (@scr / "setvar[name=rtml_sub_return_val]").last) assert_equal false, @var.property('lo') @var.to_tml.should be_valid_tml assert((tml = @scr.to_tml) =~ /setvar[^>]+?lo=["']0["']/, tml) # make sure it generated setvars -- why is this necessary?? # update - turns out to be an artifact from reordering elements in ElementBuilder#rebuild_invalid_elements. # Resolved, by simply re-adding the same element instead of building an all new one. assert @doc.to_tml =~ /setvar[^>]+?lo=["']1["']/, "element missing - check ElementBuilder#rebuild_invalid_elements" assert @doc.to_tml =~ /setvar[^>]+?lo=["']0["']/, "element missing - check ElementBuilder#rebuild_invalid_elements" end it "should be able to conditionally respond to subroutine results" do @doc.string :ret @doc.screen :get_return_value, :check_return_value do tvars[:ret] = call(:check_is_emv) end @doc.screen :check_return_value do _if(tvars[:ret] == true) { goto :condition_was_true } _elsif(tvars[:ret] == false) { goto :condition_was_false } _else { goto :condition_was_not_boolean } end # TODO: Above produces the expected TML result. Assert that this is true, and then start playing with syntax. #puts @tml_docs.to_tml end end