- Revision
- 671
- Author
- tastapod
- Date
- 2007-01-12 11:03:42 -0600 (Fri, 12 Jan 2007)
Log Message
[dn] renamed constraints to matchers
Modified Paths
Added Paths
Removed Paths
Diff
Modified: rbehave/trunk/rbehave/behaviour.rb (670 => 671)
--- rbehave/trunk/rbehave/behaviour.rb 2007-01-12 17:02:49 UTC (rev 670) +++ rbehave/trunk/rbehave/behaviour.rb 2007-01-12 17:03:42 UTC (rev 671) @@ -1,4 +1,4 @@ -require 'rbehave/constraint' +require 'rbehave/matcher' require 'rbehave/mock' module RBehave @@ -9,7 +9,7 @@ class Behaviour # include RBehave # all the stuff at RBehave module level - include Constraints + include Matchers include Mocks def initialize
Deleted: rbehave/trunk/rbehave/constraint.rb (670 => 671)
--- rbehave/trunk/rbehave/constraint.rb 2007-01-12 17:02:49 UTC (rev 670) +++ rbehave/trunk/rbehave/constraint.rb 2007-01-12 17:03:42 UTC (rev 671) @@ -1,167 +0,0 @@ -require 'rbehave/exceptions' - -module RBehave - module Constraints - - # superclass for all constraint types - class Constraint - def matches(arg); raise VerificationError, "matches(arg) must be overridden"; end - def to_s; raise VerificationError, "to_s() must be overridden"; end - end - - class CustomConstraint < Constraint - def initialize(description, &matches) - @description, @matches = description, matches - end - - def matches(arg) - # puts "Checking match for [EMAIL PROTECTED]" - return @matches.call(arg) - end - - def to_s - return @description - end - end - - # - # Utility method for creating constraints - # - # Use like: - # - # <pre><code> - # def my_constraint(arg) - # return constraint("description including #{arg}") do - # # body of constraint goes here - # end - # end - def constraint(description, &matches) - return CustomConstraint.new(description, &matches) - end - - #========================= The Constraints ============================ - - def eq(expected) - return constraint("equal to <#{expected.to_s}>") do |arg| - expected == arg - end - end - - alias :is :eq - - def anything - return constraint("anything") do |arg| - true - end - end - - def is_true - return constraint("true") do |arg| - arg - end - end - - def is_false - return constraint("false") do |arg| - not arg - end - end - - # string constraints - - def contains(fragment) - return constraint("string containing <#{fragment}>") do |arg| - if arg < Enumerable - arg.member? fragment - else - arg.to_s =~ /#{fragment}/ - end - end - end - - def starts_with(fragment) - return constraint("string starting with <#{fragment}>") do - |arg| arg.to_s =~ /^#{fragment}/ - end - end - - def ends_with(fragment) - return constraint("string ending with <#{fragment}>") do |arg| - arg.to_s =~ /#{fragment}$/ - end - end - - # logical operators - - def to_constraint(c) - c.is_a?(Constraint) ? c : eq(c) - end - - def _not(c) - c = to_constraint(c) - return constraint("not #{constraint.to_s}") do |arg| - result = constraint.matches(arg) - puts result - raise RuntimeError - not result - end - end - - alias :is_not :_not - - def _and(c1, c2) - c1, c2 = to_constraint(c1), to_constraint(c2) - return constraint("( #{c1.to_s} and #{c2.to_s} )") do |arg| - c1.matches(arg) && c2.matches(arg) - end - end - - alias :both :_and - alias :_both :_and - - def _or(c1, c2) - c1, c2 = to_constraint(c1), to_constraint(c2) - return constraint("( #{c1.to_s} or #{c2.to_s} )") do |arg| - c1.matches(arg) || c2.matches(arg) - end - end - - alias :either :_or - alias :_either :_or - - def _not(c) - c = to_constraint(c) - return constraint("not( #{c.to_s} )") do |arg| - not c.matches(arg) - end - end - - # add methods to Constraint class - class Constraint - def _and(constraint); both(self, constraint); end - def _or(constraint); either(self, constraint); end - end - - # ensure_that - - def ensure_that arg, constraint = is_true, message = nil - unless constraint.matches arg - raise VerificationError, "Expected: " + (message ? message : "") + "[" + constraint.to_s + "]" + - "\nbut got: [" + arg.to_s + "]" #, clean_backtrace(caller) - end - end - - def ensure_raises(expected_error, &block) - begin - yield - rescue expected_error - return - end - raise VerificationError, "Should have failed with #{expected_error}" #, clean_backtrace(caller) - end - - def fail_with(message) - raise VerificationError, message #, clean_backtrace(caller) - end - end -end
Modified: rbehave/trunk/rbehave/expectation.rb (670 => 671)
--- rbehave/trunk/rbehave/expectation.rb 2007-01-12 17:02:49 UTC (rev 670) +++ rbehave/trunk/rbehave/expectation.rb 2007-01-12 17:03:42 UTC (rev 671) @@ -1,9 +1,9 @@ -require 'rbehave/constraint' +require 'rbehave/matcher' module RBehave module Mocks class Expectation - include Constraints + include Matchers def initialize(type, method) @type = type @@ -15,10 +15,10 @@ def matches(method, *args) return false if @method != method - return true if @constraints == nil # don't care about args - return false if @constraints.length != args.length - @constraints.each_index do |i| - return false unless @constraints[i].matches(args[i]) + return true if @matchers == nil # don't care about args + return false if @matchers.length != args.length + @matchers.each_index do |i| + return false unless @matchers[i].matches(args[i]) end return true end @@ -58,12 +58,12 @@ end def with(*args) - @constraints = args.map { |arg| arg.is_a?(Constraint) ? arg : eq(arg) } + @matchers = args.map { |arg| arg.is_a?(Matcher) ? arg : eq(arg) } self end def with_no_arguments - @constraints = [] + @matchers = [] self end
Copied: rbehave/trunk/rbehave/matcher.rb (from rev 603, rbehave/trunk/rbehave/constraint.rb) (0 => 671)
--- rbehave/trunk/rbehave/matcher.rb (rev 0) +++ rbehave/trunk/rbehave/matcher.rb 2007-01-12 17:03:42 UTC (rev 671) @@ -0,0 +1,164 @@ +require 'rbehave/exceptions' + +module RBehave + module Matchers + + # superclass for all matcher types + class Matcher + def matches(arg); raise VerificationError, "matches(arg) must be overridden"; end + def to_s; raise VerificationError, "to_s() must be overridden"; end + end + + class CustomMatcher < Matcher + def initialize(description, &matches) + @description, @matches = description, matches + end + + def matches(arg) + # puts "Checking match for [EMAIL PROTECTED]" + return @matches.call(arg) + end + + def to_s + return @description + end + end + + # + # Utility method for creating matchers + # + # Use like: + # + # <pre><code> + # def my_matcher(arg) + # return matcher("description including #{arg}") do + # # body of matcher goes here + # end + # end + def matcher(description, &matches) + return CustomMatcher.new(description, &matches) + end + + #========================= The Matchers ============================ + + def eq(expected) + return matcher("equal to <#{expected.to_s}>") do |arg| + expected == arg + end + end + + alias :is :eq + + def anything + return matcher("anything") do |arg| + true + end + end + + def is_true + return matcher("true") do |arg| + arg + end + end + + def is_false + return matcher("false") do |arg| + not arg + end + end + + # string matchers + + def contains(fragment) + return matcher("string containing <#{fragment}>") do |arg| + if arg < Enumerable + arg.member? fragment + else + arg.to_s =~ /#{fragment}/ + end + end + end + + def starts_with(fragment) + return matcher("string starting with <#{fragment}>") do + |arg| arg.to_s =~ /^#{fragment}/ + end + end + + def ends_with(fragment) + return matcher("string ending with <#{fragment}>") do |arg| + arg.to_s =~ /#{fragment}$/ + end + end + + # logical operators + + def to_matcher(c) + c.is_a?(Matcher) ? c : eq(c) + end + + def _not(c) + c = to_matcher(c) + return matcher("not #{matcher.to_s}") do |arg| + not matcher.matches(arg) + end + end + + alias :is_not :_not + + def _and(c1, c2) + c1, c2 = to_matcher(c1), to_matcher(c2) + return matcher("( #{c1.to_s} and #{c2.to_s} )") do |arg| + c1.matches(arg) && c2.matches(arg) + end + end + + alias :both :_and + alias :_both :_and + + def _or(c1, c2) + c1, c2 = to_matcher(c1), to_matcher(c2) + return matcher("( #{c1.to_s} or #{c2.to_s} )") do |arg| + c1.matches(arg) || c2.matches(arg) + end + end + + alias :either :_or + alias :_either :_or + + def _not(c) + c = to_matcher(c) + return matcher("not( #{c.to_s} )") do |arg| + not c.matches(arg) + end + end + + # add methods to Matcher class + class Matcher + def _and(matcher); both(self, matcher); end + def _or(matcher); either(self, matcher); end + end + + # ensure_that + + def ensure_that arg, matcher = is_true, message = nil + unless matcher.matches arg + raise VerificationError, "Expected: " + (message ? message : "") + "[" + matcher.to_s + "]" + + "\nbut got: [" + arg.to_s + "]" #, clean_backtrace(caller) + end + end + + def ensure_raises(expected_error, &block) + begin + yield + rescue expected_error + return + end + raise VerificationError, "Should have failed with #{expected_error}" #, clean_backtrace(caller) + end + + def fail_with(message) + raise VerificationError, message #, clean_backtrace(caller) + end + end +end
To unsubscribe from this list please visit:
