spicycode-micronaut 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. data/Rakefile +23 -10
  2. data/examples/example_helper.rb +3 -3
  3. data/examples/lib/micronaut/behaviour_group_example.rb +175 -0
  4. data/examples/lib/micronaut/expectations/differs/default_example.rb +1 -2
  5. data/examples/lib/micronaut/expectations/extensions/object_example.rb +15 -8
  6. data/examples/lib/micronaut/expectations/fail_with_example.rb +2 -2
  7. data/examples/lib/micronaut/matchers/be_close_example.rb +42 -0
  8. data/examples/lib/micronaut/matchers/be_example.rb +257 -0
  9. data/examples/lib/micronaut/matchers/change_example.rb +329 -0
  10. data/examples/lib/micronaut/matchers/description_generation_example.rb +167 -0
  11. data/examples/lib/micronaut/matchers/eql_example.rb +29 -0
  12. data/examples/lib/micronaut/matchers/equal_example.rb +29 -0
  13. data/examples/lib/micronaut/matchers/exist_example.rb +69 -0
  14. data/examples/lib/micronaut/matchers/handler_example.rb +146 -0
  15. data/examples/lib/micronaut/matchers/has_example.rb +63 -0
  16. data/examples/lib/micronaut/matchers/have_example.rb +575 -0
  17. data/examples/lib/micronaut/matchers/include_example.rb +88 -0
  18. data/examples/lib/micronaut/matchers/match_example.rb +41 -0
  19. data/examples/lib/micronaut/matchers/matcher_methods_example.rb +66 -0
  20. data/examples/lib/micronaut/matchers/operator_matcher_example.rb +191 -0
  21. data/examples/lib/micronaut/matchers/raise_error_example.rb +315 -0
  22. data/examples/lib/micronaut/matchers/respond_to_example.rb +54 -0
  23. data/examples/lib/micronaut/matchers/satisfy_example.rb +36 -0
  24. data/examples/lib/micronaut/matchers/simple_matcher_example.rb +93 -0
  25. data/examples/lib/micronaut/matchers/throw_symbol_example.rb +96 -0
  26. data/examples/resources/example_classes.rb +67 -0
  27. data/lib/autotest/micronaut.rb +9 -4
  28. data/lib/micronaut/behaviour_group.rb +43 -0
  29. data/lib/micronaut/behaviour_group_class_methods.rb +134 -0
  30. data/lib/micronaut/example_runner.rb +7 -9
  31. data/lib/micronaut/example_world.rb +11 -7
  32. data/lib/micronaut/expectations/differs/default.rb +6 -15
  33. data/lib/micronaut/expectations/errors.rb +7 -0
  34. data/lib/micronaut/expectations/{object_extensions.rb → extensions/object.rb} +5 -4
  35. data/lib/micronaut/expectations/{string_and_symbol_extensions.rb → extensions/string_and_symbol.rb} +0 -0
  36. data/lib/micronaut/expectations/extensions.rb +2 -0
  37. data/lib/micronaut/expectations/wrap_expectation.rb +5 -0
  38. data/lib/micronaut/expectations.rb +5 -5
  39. data/lib/micronaut/extensions/kernel.rb +2 -4
  40. data/lib/micronaut/matchers/be_close.rb +6 -22
  41. data/lib/micronaut/matchers/eql.rb +7 -25
  42. data/lib/micronaut/matchers/equal.rb +6 -24
  43. data/lib/micronaut/matchers/exist.rb +8 -14
  44. data/lib/micronaut/matchers/has.rb +12 -28
  45. data/lib/micronaut/matchers/include.rb +12 -9
  46. data/lib/micronaut/matchers/match.rb +8 -27
  47. data/lib/micronaut/matchers/method_missing.rb +1 -1
  48. data/lib/micronaut/matchers/operator_matcher.rb +23 -46
  49. data/lib/micronaut/matchers/raise_error.rb +4 -8
  50. data/lib/micronaut/matchers/respond_to.rb +2 -1
  51. data/lib/micronaut/matchers/throw_symbol.rb +9 -3
  52. data/lib/micronaut/matchers.rb +10 -3
  53. data/lib/micronaut/mocking/with_mocha.rb +0 -1
  54. data/lib/micronaut.rb +3 -3
  55. metadata +31 -7
  56. data/examples/lib/micronaut/example_group_example.rb +0 -116
  57. data/lib/micronaut/example_group.rb +0 -100
  58. data/lib/micronaut/exceptions.rb +0 -7
@@ -0,0 +1,146 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../../example_helper")
2
+
3
+ module ExampleExpectations
4
+
5
+ class ArbitraryMatcher
6
+ def initialize(*args, &block)
7
+ if args.last.is_a? Hash
8
+ @expected = args.last[:expected]
9
+ end
10
+ if block_given?
11
+ @expected = block.call
12
+ end
13
+ @block = block
14
+ end
15
+
16
+ def matches?(target)
17
+ @target = target
18
+ return @expected == target
19
+ end
20
+
21
+ def with(new_value)
22
+ @expected = new_value
23
+ self
24
+ end
25
+
26
+ def failure_message
27
+ "expected #{@expected}, got #{@target}"
28
+ end
29
+
30
+ def negative_failure_message
31
+ "expected not #{@expected}, got #{@target}"
32
+ end
33
+ end
34
+
35
+ class PositiveOnlyMatcher < ArbitraryMatcher
36
+ undef negative_failure_message rescue nil
37
+ end
38
+
39
+ def arbitrary_matcher(*args, &block)
40
+ ArbitraryMatcher.new(*args, &block)
41
+ end
42
+
43
+ def positive_only_matcher(*args, &block)
44
+ PositiveOnlyMatcher.new(*args, &block)
45
+ end
46
+
47
+ end
48
+
49
+ describe Micronaut::Expectations::ExpectationMatcherHandler do
50
+ describe "#handle_matcher" do
51
+ it "should ask the matcher if it matches" do
52
+ matcher = mock("matcher")
53
+ actual = Object.new
54
+ matcher.expects(:matches?).with(actual).returns(true)
55
+ Micronaut::Expectations::ExpectationMatcherHandler.handle_matcher(actual, matcher)
56
+ end
57
+
58
+ it "should explain when the matcher parameter is not a matcher" do
59
+ begin
60
+ nonmatcher = mock("nonmatcher")
61
+ actual = Object.new
62
+ Micronaut::Expectations::ExpectationMatcherHandler.handle_matcher(actual, nonmatcher)
63
+ rescue Micronaut::Expectations::InvalidMatcherError => e
64
+ end
65
+
66
+ e.message.should =~ /^Expected a matcher, got /
67
+ end
68
+
69
+ it "should return the match value" do
70
+ matcher = mock("matcher")
71
+ actual = Object.new
72
+ matcher.expects(:matches?).with(actual).returns(:this_value)
73
+ Micronaut::Expectations::ExpectationMatcherHandler.handle_matcher(actual, matcher).should == :this_value
74
+ end
75
+ end
76
+ end
77
+
78
+ describe Micronaut::Expectations::NegativeExpectationMatcherHandler do
79
+ describe "#handle_matcher" do
80
+ it "should explain when matcher does not support should_not" do
81
+ matcher = mock("matcher")
82
+ matcher.stubs(:matches?)
83
+ actual = Object.new
84
+ lambda {
85
+ Micronaut::Expectations::NegativeExpectationMatcherHandler.handle_matcher(actual, matcher)
86
+ }.should fail_with(/Matcher does not support should_not.\n/)
87
+ end
88
+
89
+ it "should ask the matcher if it matches" do
90
+ matcher = mock("matcher")
91
+ actual = Object.new
92
+ matcher.stubs(:negative_failure_message)
93
+ matcher.expects(:matches?).with(actual).returns(false)
94
+ Micronaut::Expectations::NegativeExpectationMatcherHandler.handle_matcher(actual, matcher)
95
+ end
96
+
97
+ it "should explain when the matcher parameter is not a matcher" do
98
+ begin
99
+ nonmatcher = mock("nonmatcher")
100
+ actual = Object.new
101
+ Micronaut::Expectations::NegativeExpectationMatcherHandler.handle_matcher(actual, nonmatcher)
102
+ rescue Micronaut::Expectations::InvalidMatcherError => e
103
+ end
104
+
105
+ e.message.should =~ /^Expected a matcher, got /
106
+ end
107
+
108
+
109
+ it "should return the match value" do
110
+ matcher = mock("matcher")
111
+ actual = Object.new
112
+ matcher.expects(:matches?).with(actual).returns(false)
113
+ matcher.stubs(:negative_failure_message).returns("ignore")
114
+ Micronaut::Expectations::NegativeExpectationMatcherHandler.handle_matcher(actual, matcher).should be_false
115
+ end
116
+ end
117
+ end
118
+ #
119
+ # describe Micronaut::Expectations::ExpectationMatcherHandler do
120
+ # include ExampleExpectations
121
+ #
122
+ # it "should handle submitted args" do
123
+ # 5.should arbitrary_matcher(:expected => 5)
124
+ # 5.should arbitrary_matcher(:expected => "wrong").with(5)
125
+ # lambda { 5.should arbitrary_matcher(:expected => 4) }.should fail_with("expected 4, got 5")
126
+ # lambda { 5.should arbitrary_matcher(:expected => 5).with(4) }.should fail_with("expected 4, got 5")
127
+ # 5.should_not arbitrary_matcher(:expected => 4)
128
+ # 5.should_not arbitrary_matcher(:expected => 5).with(4)
129
+ # lambda { 5.should_not arbitrary_matcher(:expected => 5) }.should fail_with("expected not 5, got 5")
130
+ # lambda { 5.should_not arbitrary_matcher(:expected => 4).with(5) }.should fail_with("expected not 5, got 5")
131
+ # end
132
+ #
133
+ # it "should handle the submitted block" do
134
+ # 5.should arbitrary_matcher { 5 }
135
+ # 5.should arbitrary_matcher(:expected => 4) { 5 }
136
+ # 5.should arbitrary_matcher(:expected => 4).with(5) { 3 }
137
+ # end
138
+ #
139
+ # it "should explain when matcher does not support should_not" do
140
+ # lambda {
141
+ # 5.should_not positive_only_matcher(:expected => 5)
142
+ # }.should fail_with(/Matcher does not support should_not.\n/)
143
+ # end
144
+ #
145
+ #
146
+ # end
@@ -0,0 +1,63 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../../example_helper")
2
+
3
+ describe "should have_sym(*args)" do
4
+ it "should pass if #has_sym?(*args) returns true" do
5
+ {:a => "A"}.should have_key(:a)
6
+ end
7
+
8
+ it "should fail if #has_sym?(*args) returns false" do
9
+ lambda {
10
+ {:b => "B"}.should have_key(:a)
11
+ }.should fail_with("expected #has_key?(:a) to return true, got false")
12
+ end
13
+
14
+ it "should fail if target does not respond to #has_sym?" do
15
+ lambda {
16
+ Object.new.should have_key(:a)
17
+ }.should raise_error(NoMethodError)
18
+ end
19
+
20
+ it "should reraise an exception thrown in #has_sym?(*args)" do
21
+ o = Object.new
22
+ def o.has_sym?(*args)
23
+ raise "Funky exception"
24
+ end
25
+ lambda { o.should have_sym(:foo) }.should raise_error("Funky exception")
26
+ end
27
+ end
28
+
29
+ describe "should_not have_sym(*args)" do
30
+ it "should pass if #has_sym?(*args) returns false" do
31
+ {:a => "A"}.should_not have_key(:b)
32
+ end
33
+
34
+ it "should fail if #has_sym?(*args) returns true" do
35
+ lambda {
36
+ {:a => "A"}.should_not have_key(:a)
37
+ }.should fail_with("expected #has_key?(:a) to return false, got true")
38
+ end
39
+
40
+ it "should fail if target does not respond to #has_sym?" do
41
+ lambda {
42
+ Object.new.should have_key(:a)
43
+ }.should raise_error(NoMethodError)
44
+ end
45
+
46
+ it "should reraise an exception thrown in #has_sym?(*args)" do
47
+ o = Object.new
48
+ def o.has_sym?(*args)
49
+ raise "Funky exception"
50
+ end
51
+ lambda { o.should_not have_sym(:foo) }.should raise_error("Funky exception")
52
+ end
53
+ end
54
+
55
+ describe "has" do
56
+ it "should work when the target implements #send" do
57
+ o = {:a => "A"}
58
+ def o.send(*args); raise "DOH! Library developers shouldn't use #send!" end
59
+ lambda {
60
+ o.should have_key(:a)
61
+ }.should_not raise_error
62
+ end
63
+ end