require File.join(__FILE__.gsub(/(.*)?\/spec\/.*$/, '\1'), 'spec/spec_helper') describe Rtml::Widgets::ScreenVariants do before :each do @doc = Rtml::Document.new @scr = @doc.screen :test_screen, :next => "default_next" @variant_options = {:uri => 'loop_again', :lo => 'tmlvar:counter', :op => 'less', :ro => 'tmlvar:loop_count'} end it "should produce 3 separate variants" do @scr.goto "1", :if => 'tmlvar:one == 1' @scr.goto "2", :if => 'tmlvar:one < 2' @scr.goto "3", :if => 'tmlvar:one > 2' (nxt = (@scr / "next")).should have(1).item (nxt.first / "variant").should have(3).items end it "should produce a valid VARIANT via timeout" do @scr.goto @variant_options[:uri], :timeout => '10' assert_not_nil nxt = (@scr / "next").first, "screen/next not found" assert_equal 'default_next', nxt.property('uri') assert_not_nil var = (nxt / "variant").first, "screen/next/variant not found" assert_equal @variant_options[:uri], var.property('uri') assert_equal '10', var.property('timeout') end it "should produce a valid VARIANT via hotkey" do @scr.goto @variant_options[:uri], :key => "00" assert_not_nil nxt = (@scr / "next").first, "screen/next not found" assert_equal 'default_next', nxt.property('uri') assert_not_nil var = (nxt / "variant").first, "screen/next/variant not found" assert_equal @variant_options[:uri], var.property('uri') assert_equal '00', var.property('key') end it "should produce a valid VARIANT element indirectly from an array" do @scr.property('next', '') @scr.next_screen 'default_next' @scr.goto @variant_options[:uri], :if => ["tmlvar:counter", '<', 'tmlvar:loop_count'] assert_not_nil nxt = (@scr / "next").first, "screen/next not found" assert_equal 'default_next', nxt.property('uri') assert_not_nil var = (nxt / "variant").first, "screen/next/variant not found" assert_equal @variant_options[:uri], var.property('uri') assert_equal @variant_options[:lo], var.property('lo') assert_equal @variant_options[:ro], var.property('ro') assert_equal @variant_options[:op], var.property('op') end it "should produce a valid VARIANT element indirectly from a string" do @scr.property('next', '') @scr.next_screen 'default_next' @scr.goto @variant_options[:uri], :if => "tmlvar:counter < tmlvar:loop_count" assert_not_nil nxt = (@scr / "next").first, "screen/next not found" assert_equal 'default_next', nxt.property('uri') assert_not_nil var = (nxt / "variant").first, "screen/next/variant not found" assert_equal @variant_options[:uri], var.property('uri') assert_equal @variant_options[:lo], var.property('lo') assert_equal @variant_options[:ro], var.property('ro') assert_equal @variant_options[:op], var.property('op') end it "should produce a valid VARIANT element directly" do @scr.variant @variant_options assert_not_nil (ele = (@scr / "next").first), "screen/next not found" assert_not_nil (var = (ele / "variant").first), "screen/next/variant not found" assert_equal @variant_options[:uri], var.property('uri') assert_equal @variant_options[:lo], var.property('lo') assert_equal @variant_options[:ro], var.property('ro') assert_equal @variant_options[:op], var.property('op') end it "should infer default NEXT element pointing to ASSERT if it does not otherwise exist" do @scr.property('next', '') #assert_raises Rtml::Errors::ScreenVariantError do @scr.variant @variant_options #end # assertion screen is now generated last, just prior to #to_tml #assert_length 1, @tml_docs / "screen[id=assert]", "screen#assert not found" assert_equal "assert", (@scr / "next").first.property('uri'), "generated NEXT['uri'] is not an assertion failure" end # test that it raises an error when any required key is missing [:uri, :lo, :op, :ro].each do |key| it "should raise on variant with no #{key}" do assert_raises ArgumentError do @scr.variant @variant_options.without(key) end end end it "should respond to entry points" do Rtml::Widgets::ScreenVariants.entry_points.each do |entry_point| assert @scr.respond_to?(entry_point), "Screen does not respond_to? #{entry_point.inspect}" end end end