MacSpec 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. data/.document +5 -0
  2. data/.gitignore +21 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +20 -0
  5. data/Rakefile +53 -0
  6. data/lib/mac_spec.rb +59 -0
  7. data/lib/mac_spec/matcher_system.rb +19 -0
  8. data/lib/mac_spec/matcher_system/built_in/change_expectations.rb +36 -0
  9. data/lib/mac_spec/matcher_system/built_in/enumerable_expectations.rb +43 -0
  10. data/lib/mac_spec/matcher_system/built_in/error_expectations.rb +68 -0
  11. data/lib/mac_spec/matcher_system/built_in/operator_expectations.rb +45 -0
  12. data/lib/mac_spec/matcher_system/built_in/truth_expectations.rb +148 -0
  13. data/lib/mac_spec/matcher_system/core/def_matcher.rb +14 -0
  14. data/lib/mac_spec/matcher_system/core/exceptions.rb +7 -0
  15. data/lib/mac_spec/matcher_system/core/expectation_builder.rb +11 -0
  16. data/lib/mac_spec/matcher_system/core/matcher_builder.rb +53 -0
  17. data/lib/mac_spec/matcher_system/core/modals.rb +36 -0
  18. data/lib/mac_spec/mocking_framework.rb +13 -0
  19. data/lib/mac_spec/mocking_framework/extensions/kernel_extension.rb +14 -0
  20. data/lib/mac_spec/mocking_framework/extensions/object_extension.rb +74 -0
  21. data/lib/mac_spec/mocking_framework/message_expectation.rb +127 -0
  22. data/lib/mac_spec/mocking_framework/mock.rb +20 -0
  23. data/lib/mac_spec/testing_framework.rb +13 -0
  24. data/lib/mac_spec/testing_framework/core/functions.rb +26 -0
  25. data/lib/mac_spec/testing_framework/core/kernel_extension.rb +27 -0
  26. data/lib/mac_spec/testing_framework/core/test_case_class_methods.rb +90 -0
  27. data/lib/mac_spec/version.rb +3 -0
  28. data/test/all.rb +6 -0
  29. data/test/mac_spec/matcher_system/built_in/change_expectations_test.rb +68 -0
  30. data/test/mac_spec/matcher_system/built_in/enumerable_expectations_test.rb +91 -0
  31. data/test/mac_spec/matcher_system/built_in/error_expectations_test.rb +144 -0
  32. data/test/mac_spec/matcher_system/built_in/operator_expectations_test.rb +136 -0
  33. data/test/mac_spec/matcher_system/built_in/truth_expectations_test.rb +373 -0
  34. data/test/mac_spec/matcher_system/core/def_matcher_test.rb +100 -0
  35. data/test/mac_spec/matcher_system/core/expectation_builder_test.rb +34 -0
  36. data/test/mac_spec/matcher_system/core/matcher_builder_test.rb +72 -0
  37. data/test/mac_spec/matcher_system/core/modals_test.rb +39 -0
  38. data/test/mac_spec/matcher_system/script.rb +29 -0
  39. data/test/mac_spec/matcher_system/test_helper.rb +2 -0
  40. data/test/mac_spec/mocking_framework/extensions/kernel_extension_test.rb +37 -0
  41. data/test/mac_spec/mocking_framework/extensions/object_extension_test.rb +9 -0
  42. data/test/mac_spec/mocking_framework/message_expectation_test.rb +9 -0
  43. data/test/mac_spec/mocking_framework/mock_test.rb +9 -0
  44. data/test/mac_spec/mocking_framework/regression/mocking_class_methods_test.rb +96 -0
  45. data/test/mac_spec/mocking_framework/regression/negative_expectation_test.rb +33 -0
  46. data/test/mac_spec/mocking_framework/regression/stub_test.rb +19 -0
  47. data/test/mac_spec/mocking_framework/test_helper.rb +1 -0
  48. data/test/mac_spec/test_helper.rb +1 -0
  49. data/test/mac_spec/testing_framework/performance/x_test_performance.rb +125 -0
  50. data/test/mac_spec/testing_framework/regression/before_test.rb +32 -0
  51. data/test/mac_spec/testing_framework/regression/deep_nested_example_groups_test.rb +20 -0
  52. data/test/mac_spec/testing_framework/regression/description_string_test.rb +13 -0
  53. data/test/mac_spec/testing_framework/regression/example_name_test.rb +11 -0
  54. data/test/mac_spec/testing_framework/regression/inherit_not_double_test.rb +39 -0
  55. data/test/mac_spec/testing_framework/regression/surrounding_module_scope_test.rb +31 -0
  56. data/test/mac_spec/testing_framework/regression/testing_functions_test.rb +66 -0
  57. data/test/mac_spec/testing_framework/test_helper.rb +1 -0
  58. data/test/test_helper.rb +1 -0
  59. metadata +150 -0
@@ -0,0 +1,34 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ module MacSpec
4
+ module MatcherSystem
5
+ module ExpectationBuilder
6
+ describe "build_expectation" do
7
+
8
+ before do
9
+ @obj = Object.new
10
+ end
11
+
12
+ it "should" do
13
+ exp = MacSpec::MatcherSystem::ExpectationBuilder.build_expectation(true, nil, @obj)
14
+ exp.send(:==, @obj)
15
+ end
16
+
17
+ it "should fails" do
18
+ expect_1 = MacSpec::MatcherSystem::ExpectationBuilder.build_expectation(true, nil, 1)
19
+ lambda {expect_1.send(:==, 2)}.should raise_error
20
+ end
21
+
22
+ it "should not" do
23
+ exp = MacSpec::MatcherSystem::ExpectationBuilder.build_expectation(false, nil, @obj)
24
+ exp.send(:==, 1)
25
+ end
26
+
27
+ it "should not fails" do
28
+ expect_not_1 = MacSpec::MatcherSystem::ExpectationBuilder.build_expectation(false, nil, 1)
29
+ lambda {expect_not_1.send(:==, 1)}.should raise_error
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,72 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ describe MacSpec::MatcherSystem::MatcherBuilder do
4
+ include MacSpec::MatcherSystem::MatcherBuilder
5
+
6
+ before do
7
+ @obj = Object.new
8
+ end
9
+
10
+ it "matcher responds to matches" do
11
+ block = lambda {|given, matcher, args| true}
12
+ build_matcher(:m, &block).should respond_to(:matches?)
13
+ end
14
+
15
+ it "fail positive" do
16
+ block = lambda {|given, matcher, args| false}
17
+ lambda {@obj.should build_matcher(:m, &block)}.should raise_error
18
+ end
19
+
20
+ it "pass positive" do
21
+ block = lambda {|given, matcher, args| true}
22
+ @obj.should build_matcher(:m, &block)
23
+ end
24
+
25
+ it "fail negative" do
26
+ block = lambda {|given, matcher, args| true}
27
+ lambda {@obj.should_not build_matcher(:m, &block)}.should raise_error
28
+ end
29
+
30
+ it "pass negative" do
31
+ block = lambda {|given, matcher, args| false}
32
+ @obj.should_not build_matcher(:m, &block)
33
+ end
34
+
35
+ it "takes arguments" do
36
+ block = lambda {|given, matcher, args| $args = args; true}
37
+ @obj.should build_matcher(:m,[1,2,3], &block)
38
+ $args.should eql([1,2,3])
39
+ end
40
+
41
+ it "received method" do
42
+ block = lambda {|given, matcher, args| $msgs = matcher.msgs; true}
43
+ @obj.should build_matcher(:m, &block).method1
44
+ $msgs[0].name.should eql(:method1)
45
+ end
46
+
47
+ it "received method takes args" do
48
+ block = lambda {|given, matcher, args| $msgs = matcher.msgs; true}
49
+ @obj.should build_matcher(:m, &block).method1(1,2,3)
50
+ $msgs[0].args.should eql([1,2,3])
51
+ end
52
+
53
+ it "received method takes block" do
54
+ block = lambda {|given, matcher, args| $msgs = matcher.msgs; true}
55
+ @obj.should build_matcher(:m, &block).method1 { "Hello, World!"}
56
+ $msgs[0].block.call.should eql("Hello, World!")
57
+ end
58
+
59
+ it "received method chained" do
60
+ block = lambda {|given, matcher, args| $msgs = matcher.msgs; true}
61
+ @obj.should build_matcher(:m, &block).method1(1,2,3) { "Hello, World!"}.
62
+ method2(4,5,6) { "Hello chained messages" }
63
+
64
+ $msgs[0].name.should eql(:method1)
65
+ $msgs[1].name.should eql(:method2)
66
+ $msgs[0].args.should eql([1,2,3])
67
+ $msgs[1].args.should eql([4,5,6])
68
+ $msgs[0].block.call.should eql("Hello, World!")
69
+ $msgs[1].block.call.should eql("Hello chained messages")
70
+ end
71
+
72
+ end
@@ -0,0 +1,39 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ describe MacSpec::MatcherSystem::Modals do
4
+ before do
5
+ @expectation = build_matcher() {|r,m,a| true}
6
+ @bad_expectation = build_matcher() {|r,m,a| false}
7
+ end
8
+
9
+ it "should" do
10
+ 3.should(@expectation)
11
+ end
12
+
13
+ it "will" do
14
+ 3.will(@expectation)
15
+ end
16
+
17
+ it "should not" do
18
+ 3.should_not(@bad_expectation)
19
+ end
20
+
21
+ it "will not" do
22
+ 3.will_not(@bad_expectation)
23
+ end
24
+
25
+ it "wont" do
26
+ 3.wont(@bad_expectation)
27
+ end
28
+
29
+ it "should operator expectation returned" do
30
+ obj = 3.should
31
+ assert_equal MacSpec::MatcherSystem::Expectations::OperatorExpectation, obj.class
32
+ end
33
+
34
+
35
+ it "should not operator expectation returned" do
36
+ obj = 3.should_not
37
+ assert_equal MacSpec::MatcherSystem::Expectations::OperatorExpectation, obj.class
38
+ end
39
+ end
@@ -0,0 +1,29 @@
1
+ # parses all test files (test_...rb or ..._tests.rb) below current folder
2
+ # and replaces all occurences of
3
+ # def test_whatever
4
+ # with
5
+ # $keyword "whatever" do
6
+ # $keyword defaults to 'test'
7
+
8
+ keyword = "test"
9
+
10
+ dir = File.dirname(__FILE__)
11
+ Dir[File.expand_path("#{dir}/**/*.rb")].uniq.each do |file|
12
+ if ((file =~ /\/test_\w+\.rb$/) || (file =~ /\w+_tests\.rb$/)) && (file !~ /\/test_helper\.rb$/)
13
+ puts file
14
+ File.open(file,File::RDWR) do |f|
15
+ new_lines = []
16
+ old_lines = f.readlines
17
+ old_lines.each do |line|
18
+ if line =~ /\A(\s*)def\s+test_(.*)\Z/
19
+ space = $1
20
+ desc = $2.gsub(/_/, " ")
21
+ line = "#{space}#{keyword} \"#{desc}\" do"
22
+ end
23
+ new_lines << line
24
+ end
25
+ f.pos = 0
26
+ f.puts new_lines
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,2 @@
1
+ require File.dirname(__FILE__) + "/../test_helper.rb"
2
+
@@ -0,0 +1,37 @@
1
+ require File.dirname(__FILE__) + "/../test_helper.rb"
2
+
3
+ module MacSpec
4
+ module MockingFramework
5
+ describe KernelExtension do
6
+ include MacSpec::MockingFramework::KernelExtension
7
+
8
+ describe "mock(name, options)" do
9
+ before do
10
+ @mock = mock("mock")
11
+ end
12
+
13
+ it "creates a Mock" do
14
+ @mock.should be_kind_of(Mock)
15
+ end
16
+
17
+ describe "null_object => true" do
18
+ before do
19
+ @null = mock("mock", :null_object => true)
20
+ end
21
+
22
+ it "creates a null" do
23
+ @null.hello?
24
+ end
25
+
26
+ it "returns another null for each method call" do
27
+ @null.hello?.should be_kind_of(Mock)
28
+ end
29
+
30
+ it "and so on ..." do
31
+ @null.hello?.hello?.hello?.should be_kind_of(Mock)
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,9 @@
1
+ require File.dirname(__FILE__) + "/../test_helper.rb"
2
+
3
+ module MacSpec
4
+ module MockingFramework
5
+ describe ObjectExtension do
6
+
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ require File.dirname(__FILE__) + "/test_helper.rb"
2
+
3
+ module MacSpec
4
+ module MockingFramework
5
+ describe MessageExpectation do
6
+
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ require File.dirname(__FILE__) + "/test_helper.rb"
2
+
3
+ module MacSpec
4
+ module MockingFramework
5
+ describe Mock do
6
+
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,96 @@
1
+ require File.dirname(__FILE__) + "/../test_helper.rb"
2
+
3
+ describe "Mocking existing methods" do
4
+
5
+ before do
6
+ class A
7
+ def self.noarg
8
+ "noarg"
9
+ end
10
+ def self.onearg(arg)
11
+ "onearg"
12
+ end
13
+ def self.twoargs(arg1, arg2)
14
+ "twoargs"
15
+ end
16
+ def self.threeargs(arg1, arg2, arg3)
17
+ "threeargs"
18
+ end
19
+ end
20
+ end
21
+
22
+ describe "not mocked" do
23
+ it "noarg" do
24
+ A.noarg.should eql("noarg")
25
+ end
26
+
27
+ it "onearg" do
28
+ A.onearg("arg").should eql("onearg")
29
+ end
30
+
31
+ it "twoargs" do
32
+ A.twoargs("arg1", "arg2").should eql("twoargs")
33
+ end
34
+
35
+ it "threeargs" do
36
+ A.threeargs("arg1", "arg2", "arg3").should eql("threeargs")
37
+ end
38
+ end
39
+
40
+ describe "Without args" do
41
+ it "should work" do
42
+ A.should_receive(:noarg).and_return("b")
43
+ A.noarg.should eql("b")
44
+ end
45
+ end
46
+
47
+ describe "With one arg" do
48
+ it "will take a number" do
49
+ A.should_receive(:onearg).with(1).and_return("b")
50
+ A.onearg(1).should eql("b")
51
+ end
52
+
53
+ it "will take a string" do
54
+ A.should_receive(:onearg).with("string").and_return("b")
55
+ A.onearg("string").should eql("b")
56
+ end
57
+
58
+ it "will take an array" do
59
+ A.should_receive(:onearg).with([1,2,3]).and_return("b")
60
+ A.onearg([1,2,3]).should eql("b")
61
+ end
62
+
63
+ it "will take an empty array" do
64
+ A.should_receive(:onearg).with([]).and_return("b")
65
+ A.onearg([]).should eql("b")
66
+ end
67
+
68
+ it "will take nil" do
69
+ A.should_receive(:onearg).with(nil).and_return("b")
70
+ A.onearg(nil).should eql("b")
71
+ end
72
+
73
+ it "will take a custom object" do
74
+ obj = Object.new
75
+ A.should_receive(:onearg).with(obj).and_return("b")
76
+ A.onearg(obj).should eql("b")
77
+ end
78
+ end
79
+
80
+ describe "Several Expectations on one class" do
81
+ it "First Expectations, then calling" do
82
+ A.should_receive(:noarg).and_return("b")
83
+ A.should_receive(:onearg).and_return("b")
84
+ A.noarg
85
+ A.onearg
86
+ end
87
+
88
+ it "Mixed, but valid" do
89
+ A.should_receive(:noarg).and_return("b")
90
+ A.noarg
91
+ A.should_receive(:onearg).and_return("b")
92
+ A.onearg
93
+ end
94
+ end
95
+
96
+ end
@@ -0,0 +1,33 @@
1
+ require File.dirname(__FILE__) + "/../test_helper.rb"
2
+
3
+ describe "Negative Message Expactation" do
4
+
5
+ describe "with class methods" do
6
+ before do
7
+ class A
8
+ def self.meth
9
+ "class meth"
10
+ end
11
+ end
12
+ end
13
+
14
+ it "works" do
15
+ A.should_not_receive(:blah)
16
+ end
17
+ end
18
+
19
+ describe "with instance methods" do
20
+ before do
21
+ class A
22
+ def meth
23
+ "instance meth"
24
+ end
25
+ end
26
+ end
27
+
28
+ it "works" do
29
+ A.new.should_not_receive(:blah)
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,19 @@
1
+ require File.dirname(__FILE__) + "/../test_helper.rb"
2
+
3
+ describe "Stubs" do
4
+ describe "mock(name, :meth => 'return_value')" do
5
+ [:any, :kind, :of, :method, :name].each do |m|
6
+ ["string", 1, 1.0, Object.new].each do |val|
7
+ it "returns #{val} for #{m}" do
8
+ mock("name", m => val).send(m).should == val
9
+ end
10
+
11
+ it "stub!(#{m} => #{val})" do
12
+ obj = Object.new
13
+ obj.stub!(m => val)
14
+ obj.send(m).should == val
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/../test_helper.rb"
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/../test_helper.rb"
@@ -0,0 +1,125 @@
1
+ require File.dirname(__FILE__) + "/../test_helper.rb"
2
+
3
+ class Thing
4
+ def widgets
5
+ @widgets ||= []
6
+ end
7
+ end
8
+
9
+ def_matcher :be_in_range do |given, matcher, args|
10
+ range = args[1] ? (args[0]..args[1]) : args[0]
11
+ matcher.positive_msg = "expected #{given} to be in range (#{range})"
12
+ matcher.negative_msg = "expected #{given} not to be in range (#{range})"
13
+ range.include?(given)
14
+ end
15
+
16
+ def_matcher :have do |given, matcher, args|
17
+ number = args[0]
18
+ actual = given.send(matcher.msgs[0].name).length
19
+ matcher.positive_msg = "Expected #{given} to have #{actual}, but found #{number} "
20
+ actual == number
21
+ end
22
+
23
+ 1.times do |i|
24
+
25
+ describe "Matcher: be_in_range#{i}" do
26
+ describe "With lower and upper bound seperately#{i}" do
27
+ describe "Given value of 1 #{i}" do
28
+
29
+ setup do
30
+ @value = 1
31
+ end
32
+
33
+ it "should be in range (0,1), #{i}" do
34
+ @value.should be_in_range(0,1)
35
+ end
36
+
37
+ it "should be in range (1,1), #{i}" do
38
+ @value.should be_in_range(1,1)
39
+ end
40
+
41
+ it "should be in range (1,2), #{i}" do
42
+ @value.should be_in_range(1,2)
43
+ end
44
+
45
+ it "should not be in range (0,0), #{i}" do
46
+ @value.should_not be_in_range(0,0)
47
+ end
48
+
49
+ it "should not be in range (2,2), #{i}" do
50
+ @value.should_not be_in_range(2,2)
51
+ end
52
+ end
53
+
54
+ describe "With range (), #{i}" do
55
+ describe "Given value of 1" do
56
+
57
+ setup do
58
+ @value = 1
59
+ end
60
+
61
+ it "should be in range (0,1), #{i}" do
62
+ @value.should be_in_range(0..1)
63
+ end
64
+
65
+ it "should be in range (1,1), #{i}" do
66
+ @value.should be_in_range(1..1)
67
+ end
68
+
69
+ it "should be in range (1,2), #{i}" do
70
+ @value.should be_in_range(1..2)
71
+ end
72
+
73
+ it "should not be in range (0,0), #{i}" do
74
+ @value.should_not be_in_range(0..0)
75
+ end
76
+
77
+ it "should not be in range (2,2), #{i}" do
78
+ @value.should_not be_in_range(2..2)
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+
85
+
86
+ describe "top level description block with one example, #{i}" do
87
+
88
+ setup do
89
+ @horst = "Horst"
90
+ end
91
+
92
+ it "example3, #{i}" do
93
+ @horst.should == "Horst"
94
+ end
95
+
96
+ describe "second level description block, #{i}" do
97
+
98
+ setup do
99
+ @inge = "Inge"
100
+ end
101
+
102
+ it "Inge should be here, #{i}" do
103
+ @inge.should == "Inge"
104
+ end
105
+
106
+ it "Horst should still be here, #{i}" do
107
+ @horst.should == "Horst"
108
+ end
109
+
110
+ end
111
+ end
112
+
113
+ describe "Thing, #{i}" do
114
+
115
+ setup do
116
+ @thing = Thing.new
117
+ end
118
+ describe "initialized in first before,#{i}" do
119
+ setup {}
120
+ it "has 0 widgets, #{i}" do
121
+ @thing.should have(0).widgets
122
+ end
123
+ end
124
+ end
125
+ end