changelog shortlog tags changeset manifest revisions annotate raw

vendor/plugins/rspec/lib/spec/matchers/operator_matcher.rb

changeset 15: 64acf98d15f4
author: moriq@moriq.com
date: Mon Mar 10 10:12:58 2008 +0900 (16 years ago)
permissions: -rw-r--r--
description: add plugins rspec
1module Spec
2 module Matchers
3 class BaseOperatorMatcher
4 attr_reader :generated_description
5
6 def initialize(target)
7 @target = target
8 end
9
10 def ==(expected)
11 @expected = expected
12 __delegate_method_missing_to_target("==", expected)
13 end
14
15 def ===(expected)
16 @expected = expected
17 __delegate_method_missing_to_target("===", expected)
18 end
19
20 def =~(expected)
21 @expected = expected
22 __delegate_method_missing_to_target("=~", expected)
23 end
24
25 def >(expected)
26 @expected = expected
27 __delegate_method_missing_to_target(">", expected)
28 end
29
30 def >=(expected)
31 @expected = expected
32 __delegate_method_missing_to_target(">=", expected)
33 end
34
35 def <(expected)
36 @expected = expected
37 __delegate_method_missing_to_target("<", expected)
38 end
39
40 def <=(expected)
41 @expected = expected
42 __delegate_method_missing_to_target("<=", expected)
43 end
44
45 def fail_with_message(message)
46 Spec::Expectations.fail_with(message, @expected, @target)
47 end
48
49 end
50
51 class PositiveOperatorMatcher < BaseOperatorMatcher #:nodoc:
52
53 def __delegate_method_missing_to_target(operator, expected)
54 ::Spec::Matchers.generated_description = "should #{operator} #{expected.inspect}"
55 return if @target.send(operator, expected)
56 return fail_with_message("expected: #{expected.inspect},\n got: #{@target.inspect} (using #{operator})") if ['==','===', '=~'].include?(operator)
57 return fail_with_message("expected: #{operator} #{expected.inspect},\n got: #{operator.gsub(/./, ' ')} #{@target.inspect}")
58 end
59
60 end
61
62 class NegativeOperatorMatcher < BaseOperatorMatcher #:nodoc:
63
64 def __delegate_method_missing_to_target(operator, expected)
65 ::Spec::Matchers.generated_description = "should not #{operator} #{expected.inspect}"
66 return unless @target.send(operator, expected)
67 return fail_with_message("expected not: #{operator} #{expected.inspect},\n got: #{operator.gsub(/./, ' ')} #{@target.inspect}")
68 end
69
70 end
71
72 end
73end