require 'rubygems' require 'spec' require File.dirname(__FILE__) + '/../lib/gecoder' module CustomVarMatchers class HaveDomain def initialize(expected) @expected = expected end def matches?(target) @target = target if @expected.kind_of? Range last = @expected.end last -= 1 if @expected.exclude_end? return @target.range? && @expected.begin == @target.min && last == @target.max else @expected = @expected.to_a return false unless @target.size == @expected.size @expected.each do |element| return false unless @target.include? element end return true end return false end def failure_message "expected #{@target.inspect} to have domain #{@expected.inspect}" end def negative_failure_message "expected #{@target.inspect} not to have domain #{@expected.inspect}" end end # Tests whether a variable has the expected domain. def have_domain(expected) HaveDomain.new(expected) end class HaveBounds def initialize(expected_glb, expected_lub) @expected_glb = expected_glb.to_a @expected_lub = expected_lub.to_a end def matches?(target) @target = target return @target.lower_bound.size == @expected_glb.size && @target.upper_bound.size == @expected_lub.size && @target.lower_bound.to_a == @expected_glb && @target.upper_bound.to_a == @expected_lub end def failure_message "expected #{@target.inspect} to have greatest lower bound " + "#{@expected_glb.inspect} and least upper bound #{@expected_lub.inspect}" end def negative_failure_message "expected #{@target.inspect} to not have greatest lower bound " + "#{@expected_glb.inspect} and least upper bound #{@expected_lub.inspect}" end end # Tests whether a set variable has the expected bounds. def have_bounds(expected_glb, expected_lub) HaveBounds.new(expected_glb, expected_lub) end end Spec::Runner.configure do |config| config.include(CustomVarMatchers) end # Help methods for the GecodeR specs. module GecodeR::Specs module SetHelper module_function # Returns the arguments that should be used in a partial mock to expect the # specified constant set (possibly an array of arguments). def expect_constant_set(constant_set) if constant_set.kind_of? Range return constant_set.first, constant_set.last elsif constant_set.kind_of? Fixnum constant_set else an_instance_of(Gecode::Raw::IntSet) end end end module GeneralHelper module_function # Produces a base operand that can be used to mock specific types of # operands. def general_operand_base(model) mock_op_class = Class.new mock_op_class.class_eval do attr :model def bind raise 'Bind should not be called directly for an operand.' end alias_method :bind_array, :bind end op = mock_op_class.new op.instance_eval do @model = model end return op end # Produces a general int operand. The method returns two objects: # the operand itself and the variable it returns when #to_int_var # is called. def general_int_operand(model) op = general_operand_base(model) int_var = model.int_var class <