require 'test/unit' require 'movie_finder' require 'set' class MovieFinderTest < Test::Unit::TestCase def assert_array_equal a, b a = Set.new(a) b = Set.new(b) a.each do |x| ret = false b.each do |y| if x.eql?(y) ret = true break end end assert ret, "#{a.inspect} expected but was\n#{b.inspect}" end assert_equal a.size, b.size, "#{a.inspect} expected but was\n#{b.inspect}" end Tests = { "find at readings starting after 8:00 or finishing before 12:00" => [OrConstraint.new([TimeConstraint.new("8:00", :>), TimeConstraint.new("12:00", :<, "finish_time")]), LocationConstraint.new("readings")], "find finishing before 3pm at village" => [TimeConstraint.new("3pm", :<, "finish_time"), LocationConstraint.new("village")], "find at village finishing before 3pm" => [TimeConstraint.new("3pm", :<, "finish_time"), LocationConstraint.new("village")], "find finishing before 3pm at village or readings" => [TimeConstraint.new("3pm", :<, "finish_time"), OrConstraint.new([LocationConstraint.new("village"), LocationConstraint.new("readings")])], "find finishing before 3pm at village or at readings" => [TimeConstraint.new("3pm", :<, "finish_time"), OrConstraint.new([LocationConstraint.new("village"), LocationConstraint.new("readings")])], "find finishing before 3pm or at village or at readings" => [OrConstraint.new([TimeConstraint.new("3pm", :<, "finish_time"), LocationConstraint.new("village"), LocationConstraint.new("readings")])], "find finishing before 3pm or starting after 7pm at village or readings" => [OrConstraint.new([TimeConstraint.new("3pm", :<, "finish_time"), TimeConstraint.new("7pm", :>, "start_time")]), OrConstraint.new([LocationConstraint.new("village"), LocationConstraint.new("readings")])], "find finishing before 3pm or after 7pm" => [OrConstraint.new([TimeConstraint.new("3pm", :<, "finish_time"), TimeConstraint.new("7pm", :>, "finish_time")])], "find starting before 10:00 and at village" => [TimeConstraint.new("10:00", :<, "start_time"), LocationConstraint.new("village")], "find finishing after 3pm" => [TimeConstraint.new("3pm", :>, "finish_time")], "find finishing after 3pm or village" => [OrConstraint.new([TimeConstraint.new("3pm", :>, "finish_time"), LocationConstraint.new("village")])], "find starting after 8:00 and finishing before 12:00 or after 14:00 at village" => [TimeConstraint.new("8:00", :>, "start_time"), OrConstraint.new([TimeConstraint.new("12:00", :<, "finish_time"), TimeConstraint.new("14:00", :>, "finish_time")]), LocationConstraint.new("village")] } Tests.each_pair do |x, expected| define_method "test_#{x.gsub(/\s/, '_')}" do assert_array_equal expected, MovieFinderContext.evaluate(x.dup).constraints end end end