minispec 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. checksums.yaml +7 -0
  2. data/.pryrc +2 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE +22 -0
  5. data/README.md +2140 -0
  6. data/Rakefile +11 -0
  7. data/bin/minispec +4 -0
  8. data/lib/minispec.rb +175 -0
  9. data/lib/minispec/api.rb +2 -0
  10. data/lib/minispec/api/class.rb +195 -0
  11. data/lib/minispec/api/class/after.rb +49 -0
  12. data/lib/minispec/api/class/around.rb +54 -0
  13. data/lib/minispec/api/class/before.rb +101 -0
  14. data/lib/minispec/api/class/helpers.rb +116 -0
  15. data/lib/minispec/api/class/let.rb +44 -0
  16. data/lib/minispec/api/class/tests.rb +33 -0
  17. data/lib/minispec/api/instance.rb +158 -0
  18. data/lib/minispec/api/instance/mocks/doubles.rb +36 -0
  19. data/lib/minispec/api/instance/mocks/mocks.rb +319 -0
  20. data/lib/minispec/api/instance/mocks/spies.rb +17 -0
  21. data/lib/minispec/api/instance/mocks/stubs.rb +105 -0
  22. data/lib/minispec/helpers.rb +1 -0
  23. data/lib/minispec/helpers/array.rb +56 -0
  24. data/lib/minispec/helpers/booleans.rb +108 -0
  25. data/lib/minispec/helpers/generic.rb +24 -0
  26. data/lib/minispec/helpers/mocks/expectations.rb +29 -0
  27. data/lib/minispec/helpers/mocks/spies.rb +36 -0
  28. data/lib/minispec/helpers/raise.rb +44 -0
  29. data/lib/minispec/helpers/throw.rb +29 -0
  30. data/lib/minispec/mocks.rb +11 -0
  31. data/lib/minispec/mocks/expectations.rb +77 -0
  32. data/lib/minispec/mocks/stubs.rb +178 -0
  33. data/lib/minispec/mocks/validations.rb +80 -0
  34. data/lib/minispec/mocks/validations/amount.rb +63 -0
  35. data/lib/minispec/mocks/validations/arguments.rb +161 -0
  36. data/lib/minispec/mocks/validations/caller.rb +43 -0
  37. data/lib/minispec/mocks/validations/order.rb +47 -0
  38. data/lib/minispec/mocks/validations/raise.rb +111 -0
  39. data/lib/minispec/mocks/validations/return.rb +74 -0
  40. data/lib/minispec/mocks/validations/throw.rb +91 -0
  41. data/lib/minispec/mocks/validations/yield.rb +141 -0
  42. data/lib/minispec/proxy.rb +201 -0
  43. data/lib/minispec/reporter.rb +185 -0
  44. data/lib/minispec/utils.rb +139 -0
  45. data/lib/minispec/utils/differ.rb +325 -0
  46. data/lib/minispec/utils/pretty_print.rb +51 -0
  47. data/lib/minispec/utils/raise.rb +123 -0
  48. data/lib/minispec/utils/throw.rb +140 -0
  49. data/minispec.gemspec +27 -0
  50. data/test/mocks/expectations/amount.rb +67 -0
  51. data/test/mocks/expectations/arguments.rb +126 -0
  52. data/test/mocks/expectations/caller.rb +55 -0
  53. data/test/mocks/expectations/generic.rb +35 -0
  54. data/test/mocks/expectations/order.rb +46 -0
  55. data/test/mocks/expectations/raise.rb +166 -0
  56. data/test/mocks/expectations/return.rb +71 -0
  57. data/test/mocks/expectations/throw.rb +113 -0
  58. data/test/mocks/expectations/yield.rb +109 -0
  59. data/test/mocks/spies/amount.rb +68 -0
  60. data/test/mocks/spies/arguments.rb +57 -0
  61. data/test/mocks/spies/generic.rb +61 -0
  62. data/test/mocks/spies/order.rb +38 -0
  63. data/test/mocks/spies/raise.rb +158 -0
  64. data/test/mocks/spies/return.rb +71 -0
  65. data/test/mocks/spies/throw.rb +113 -0
  66. data/test/mocks/spies/yield.rb +101 -0
  67. data/test/mocks/test__doubles.rb +98 -0
  68. data/test/mocks/test__expectations.rb +27 -0
  69. data/test/mocks/test__mocks.rb +197 -0
  70. data/test/mocks/test__proxies.rb +61 -0
  71. data/test/mocks/test__spies.rb +43 -0
  72. data/test/mocks/test__stubs.rb +427 -0
  73. data/test/proxified_asserts.rb +34 -0
  74. data/test/setup.rb +53 -0
  75. data/test/test__around.rb +58 -0
  76. data/test/test__assert.rb +510 -0
  77. data/test/test__before_and_after.rb +117 -0
  78. data/test/test__before_and_after_all.rb +71 -0
  79. data/test/test__helpers.rb +197 -0
  80. data/test/test__raise.rb +104 -0
  81. data/test/test__skip.rb +41 -0
  82. data/test/test__throw.rb +103 -0
  83. metadata +196 -0
@@ -0,0 +1,109 @@
1
+ class MinispecTest::Expectations::Unit
2
+
3
+ should 'pass when yield expected and occurred' do
4
+ expect(o).to_receive(:y).and_yield
5
+ o.y {|a,b|}
6
+ end
7
+
8
+ should 'pass when yield not expected and not occurred' do
9
+ o = Class.new { def y; end }.new
10
+ expect(o).to_receive(:y).without_yield
11
+ o.y
12
+ end
13
+
14
+ should ':fail when yield expected but not occurred' do
15
+ expect(o).to_receive(:a).and_yield
16
+ o.a
17
+ end
18
+
19
+ should ':fail when yield occurred but not expected' do
20
+ expect(o).to_receive(:y).without_yield
21
+ o.y {|a,b|}
22
+ end
23
+
24
+ should 'pass when yielded with expected arguments' do
25
+ expect(o).to_receive(:y).and_yield(1, 2)
26
+ o.y {|a, b|}
27
+ end
28
+
29
+ should 'flatten arguments when single value expected' do
30
+ o.define_singleton_method(:y) {|&b| b.call(1)}
31
+ expect(o).to_receive(:y).and_yield(1)
32
+ o.y {|a, b|}
33
+ end
34
+
35
+ should ':fail when yielded with wrong arguments' do
36
+ expect(o).to_receive(:y).and_yield(4, 5)
37
+ o.y {|a, b|}
38
+ end
39
+
40
+ should 'pass when proc validates yielded arguments' do
41
+ expect(o).to_receive(:y).and_yield {|args| args[0] == [1, 2]}
42
+ o.y {|a, b|}
43
+ end
44
+
45
+ should ':fail when proc does not validate yielded arguments' do
46
+ expect(o).to_receive(:y).and_yield {false}
47
+ o.y {|a, b|}
48
+ end
49
+
50
+ should 'not raise when arity not respected and proc used' do
51
+ expect(o).to_receive(:y)
52
+ assure { o.y {|a|} }.does_not.raise
53
+ end
54
+
55
+ should 'raise when arity not respected and lambda used' do
56
+ expect(o).to_receive(:y)
57
+ does { o.y(&lambda {|a|}) }.raise? ArgumentError, /2 for 1/
58
+ end
59
+
60
+ should 'use same expectation for all messages' do
61
+ expect(o).to_receive(:y, :x).and_yield([1, 2])
62
+ o.y {|a,b|}
63
+ o.x {|a,b|}
64
+ end
65
+
66
+ should 'pass when multiple yielded arguments match expected ones' do
67
+ expect(o).to_receive(:y, :x).and_yield([1, 2], [1, 2])
68
+ o.y {|a,b|}
69
+ o.x {|a,b|}
70
+ end
71
+
72
+ should 'flatten arguments when some message expects a single value' do
73
+ o.define_singleton_method(:y) {|&b| b.call(1)}
74
+ expect(o).to_receive(:y, :x).and_yield(1, [1, 2])
75
+ o.y {|a,b|}
76
+ o.x {|a,b|}
77
+ end
78
+
79
+ should 'NOT flatten arguments when validated by a block' do
80
+ o.define_singleton_method(:y) {|&b| b.call(1)}
81
+ expect(o).to_receive(:y, :x).and_yield {|y,x| y[0] == [1] && x[0] == [1, 2]}
82
+ o.y {|a,b|}
83
+ o.x {|a,b|}
84
+ end
85
+
86
+ should ':fail when at least one message does not yield as expected' do
87
+ expect(o).to_receive(:y, :x).and_yield([1, 2], 0)
88
+ o.y {|a,b|}
89
+ o.x {|a,b|}
90
+ end
91
+
92
+ should ':fail when proc does not validate multiple yielded arguments' do
93
+ expect(o).to_receive(:y, :x).and_yield {false}
94
+ o.y {|a,b|}
95
+ o.x {|a,b|}
96
+ end
97
+
98
+ should ':fail when multiple yields expected but none occurred' do
99
+ expect(o).to_receive(:a, :b).and_yield
100
+ o.a
101
+ o.b
102
+ end
103
+
104
+ should ':fail when at least one yield occurred but none expected' do
105
+ expect(o).to_receive(:a, :x).without_yield
106
+ o.a {}
107
+ o.x {|a,b|}
108
+ end
109
+ end
@@ -0,0 +1,68 @@
1
+ class MinispecTest::Spies::Unit
2
+
3
+ should 'pass when received expected amount' do
4
+ o.a
5
+ o.a
6
+ expect(o).received(:a).count(2)
7
+ end
8
+
9
+ should ':fail when wrong amount expected' do
10
+ o.a
11
+ expect(o).received(:a).count(2)
12
+ end
13
+
14
+ should 'pass when proc validates received amount' do
15
+ o.a
16
+ o.a
17
+ expect(o).received(:a).count {|received| received == 2}
18
+ end
19
+
20
+ should ':fail when proc does not validate received amount' do
21
+ o.a
22
+ expect(o).received(:a).count {|received| false}
23
+ end
24
+
25
+ should 'pass when multiple messages uses one expectation' do
26
+ o.a
27
+ o.b
28
+ o.b
29
+ o.a
30
+ expect(o).received(:a, :b).count(2)
31
+ end
32
+
33
+ should ':fail when received multiple messages amount are wrong' do
34
+ o.a
35
+ o.b
36
+ o.b
37
+ expect(o).received(:a, :b).count(2)
38
+ end
39
+
40
+ should 'pass when each of multiple messages uses its own expectation' do
41
+ o.a
42
+ o.b
43
+ o.b
44
+ expect(o).received(:a, :b).count(1, 2)
45
+ end
46
+
47
+ should ':fail when at least one messages does not match its expectation' do
48
+ o.a
49
+ o.b
50
+ o.b
51
+ expect(o).received(:a, :b).count(1, 1)
52
+ end
53
+
54
+ should 'pass when proc validates received multiple messages amount' do
55
+ o.a
56
+ o.b
57
+ o.b
58
+ o.a
59
+ expect(o).received(:a, :b).count {|*r| r == [2, 2]}
60
+ end
61
+
62
+ should ':fail when proc does not validate received multiple messages amount' do
63
+ o.a
64
+ o.b
65
+ expect(o).received(:a, :b).count {|*r| false}
66
+ end
67
+
68
+ end
@@ -0,0 +1,57 @@
1
+ class MinispecTest::Spies::Unit
2
+
3
+ should 'pass when expected arguments matches given ones' do
4
+ o.a(1, 2)
5
+ expect(o).received(:a).with(1, 2)
6
+ end
7
+
8
+ should ':fail when wrong arguments given' do
9
+ o.a(1, 2)
10
+ expect(o).received(:a).with(4, 5)
11
+ end
12
+
13
+ should 'pass when proc validates arguments' do
14
+ o.a(1)
15
+ expect(o).received(:a).with {|a| a[0] == [1]}
16
+ end
17
+
18
+ should ':fail when proc does not validate arguments' do
19
+ o.a(1)
20
+ expect(o).received(:a).with {|a| false}
21
+ end
22
+
23
+ should 'pass when given multiple arguments matching expected ones' do
24
+ o.a 1
25
+ o.b 2
26
+ o.c 3
27
+ expect(o).received(:a, :b, :c).with(1, 2, 3)
28
+ end
29
+
30
+ it 'plays well with Array arguments' do
31
+ o.a [1]
32
+ o.b [4, 5, [6]]
33
+ o.c :c
34
+ expect(o).received(:a, :b, :c).with([1], [4, 5, [6]], :c)
35
+ end
36
+
37
+ should 'pass when proc validates passed arguments' do
38
+ o.a 1
39
+ o.b [2]
40
+ o.c 3
41
+ expect(o).received(:a, :b, :c).with {|a,b,c| a[0] == [1] && b[0] == [[2]] && c[0] == [3]}
42
+ end
43
+
44
+ should ':fail when at least one argument does not meet expectations' do
45
+ o.a 1
46
+ o.b 2
47
+ o.c 3
48
+ expect(o).received(:a, :b, :c).with(1, 2, 5)
49
+ end
50
+
51
+ should ':fail when proc does not validate passed arguments' do
52
+ o.a 1
53
+ o.b 2
54
+ o.c 3
55
+ expect(o).received(:a, :b, :c).with {|a| false}
56
+ end
57
+ end
@@ -0,0 +1,61 @@
1
+ class MinispecTest::Spies::Unit
2
+
3
+ should 'pass if message received' do
4
+ o.a
5
+ expect(o).received(:a)
6
+ end
7
+
8
+ should ':fail if message not received' do
9
+ expect(o).received(:a)
10
+ end
11
+
12
+ should 'pass when no message expected and none received' do
13
+ assert(o).not.received(:a)
14
+ end
15
+
16
+ should ':fail when message sent but not expected' do
17
+ o.a
18
+ assert(o).not.received(:a)
19
+ end
20
+
21
+ should 'pass when no messages expected and none received' do
22
+ assert(o).not.received(:a, :b)
23
+ end
24
+
25
+ should ':fail when at least one message sent but none expected' do
26
+ o.a
27
+ assert(o).not.received(:a, :b)
28
+ end
29
+
30
+ should ':fail when all messages sent but none expected' do
31
+ o.a
32
+ o.b
33
+ assert(o).not.received(:a, :b)
34
+ end
35
+
36
+ it 'spies on multiple messages' do
37
+ o = Class.new do
38
+ def a; __method__; end
39
+ def b; __method__; end
40
+ def c; __method__; end
41
+ end.new
42
+ spy(o, :a, :b, :c)
43
+ o.a
44
+ o.b
45
+ o.c
46
+ assert(o).received(:a, :b, :c).and_returned(:a, :b, :c)
47
+ end
48
+
49
+ it 'spies on multiple messages and :fail when at least one message not received' do
50
+ o = Class.new do
51
+ def a; __method__; end
52
+ def b; __method__; end
53
+ def c; __method__; end
54
+ end.new
55
+ spy(o, :a, :b, :c)
56
+ o.a
57
+ # o.b intentionally commented
58
+ o.c
59
+ assert(o).received(:a, :b, :c)
60
+ end
61
+ end
@@ -0,0 +1,38 @@
1
+ class MinispecTest::Spies::Unit
2
+
3
+ should 'pass when messages received in expected order' do
4
+ o.a
5
+ o.b
6
+ o.c
7
+ assert(o).received(:a, :b, :c).ordered
8
+ end
9
+
10
+ should 'pass when middle messages missing' do
11
+ o.a
12
+ # b intentionally not called to make sure it iterates only through expected messages
13
+ o.c
14
+ assert(o).received(:a, :c).ordered
15
+ end
16
+
17
+ should ':fail when messages received in wrong order' do
18
+ o.b
19
+ o.c
20
+ o.a
21
+ assert(o).received(:a, :b).ordered
22
+ end
23
+
24
+ should 'pass when same sequence repeated n times' do
25
+ o.a
26
+ o.b
27
+ o.a
28
+ o.b
29
+ assert(o).received(:a, :b).ordered(2)
30
+ end
31
+
32
+ should ':fail when expected sequence not respected' do
33
+ o.a
34
+ o.b
35
+ o.a
36
+ assert(o).received(:a, :b).ordered(2)
37
+ end
38
+ end
@@ -0,0 +1,158 @@
1
+ class MinispecTest::Spies::Unit
2
+
3
+ should 'pass when raise expected and occurred' do
4
+ o.r rescue nil
5
+ expect(o).received(:r).and_raise
6
+ end
7
+
8
+ should 'pass when raise not expected and not occurred' do
9
+ o.a
10
+ expect(o).received(:a).without_raise
11
+ end
12
+
13
+ should ':fail when raise expected but not occurred' do
14
+ o.a
15
+ expect(o).received(:r).and_raise
16
+ end
17
+
18
+ should ':fail when raise occurred but not expected' do
19
+ o.r rescue nil
20
+ expect(o).received(:r).without_raise
21
+ end
22
+
23
+ should 'pass when raised an expected error' do
24
+ o.r rescue nil
25
+ expect(o).received(:r).and_raise ArgumentError
26
+ end
27
+
28
+ should ':fail when raised an unexpected error' do
29
+ o.a
30
+ expect(o).received(:r).and_raise RuntimeError
31
+ end
32
+
33
+ should 'pass when raised an expected message' do
34
+ o.r rescue nil
35
+ expect(o).received(:r).and_raise ArgumentError, 'some blah'
36
+ end
37
+
38
+ should 'pass when raised message matches expected one' do
39
+ o.r rescue nil
40
+ expect(o).received(:r).and_raise ArgumentError, /blah/
41
+ end
42
+
43
+ should ':fail when raised an unexpected message' do
44
+ o.r rescue nil
45
+ expect(o).received(:r).and_raise [ArgumentError, 'another blah']
46
+ end
47
+
48
+ should ':fail when raised message does not match expected one' do
49
+ o.r rescue nil
50
+ expect(o).received(:r).and_raise [ArgumentError, /x/]
51
+ end
52
+
53
+ should 'pass when one expectation used for all messages' do
54
+ o.e rescue nil
55
+ o.r rescue nil
56
+ expect(o).received(:e, :r).and_raise ArgumentError
57
+ end
58
+
59
+ should 'pass when one expectation used for all messages and error matching used' do
60
+ o.e rescue nil
61
+ o.r rescue nil
62
+ expect(o).received(:e, :r).and_raise [ArgumentError, 'some blah']
63
+ end
64
+
65
+ should ':fail when one expectation used for all messages and type does not match' do
66
+ o.e rescue nil
67
+ o.r rescue nil
68
+ expect(o).received(:e, :r).and_raise Exception
69
+ end
70
+
71
+ should ':fail when one expectation used for all messages, type matches but message does not' do
72
+ o.e rescue nil
73
+ o.r rescue nil
74
+ expect(o).received(:e, :r).and_raise [ArgumentError, 'some error']
75
+ end
76
+
77
+ should ':fail when one expectation used for all messages
78
+ and at least one message does not raise at all' do
79
+ o.a
80
+ o.r rescue nil
81
+ expect(o).received(:a, :r).and_raise ArgumentError
82
+ end
83
+
84
+ should 'pass when each message uses own expectation' do
85
+ o.e rescue nil
86
+ o.r rescue nil
87
+ expect(o).received(:e, :r).and_raise ArgumentError, ArgumentError
88
+ end
89
+
90
+ should ':fail when each message uses own expectation
91
+ and at least message does not raise as expected' do
92
+ o.e rescue nil
93
+ o.r rescue nil
94
+ expect(o).received(:e, :r).and_raise ArgumentError, Exception
95
+ end
96
+
97
+ should ':fail when each message uses own expectation
98
+ and at least message does not raise at all' do
99
+ o.a
100
+ o.r rescue nil
101
+ expect(o).received(:a, :r).and_raise ArgumentError, Exception
102
+ end
103
+
104
+ should 'pass when each message uses own expectation and error matching used' do
105
+ o.e rescue nil
106
+ o.r rescue nil
107
+ expect(o).received(:e, :r).and_raise [ArgumentError, /blah/], [ArgumentError, /blah/]
108
+ end
109
+
110
+ should ':fail when each message uses own expectation and error matching used
111
+ and at least one message does not raise as expected' do
112
+ o.e rescue nil
113
+ o.r rescue nil
114
+ expect(o).received(:e, :r).and_raise [ArgumentError, /blah/], [ArgumentError, /doh/]
115
+ end
116
+
117
+ should ':fail when at least one raise occurred but none expected' do
118
+ o.a
119
+ o.r rescue nil
120
+ expect(o).received(:a, :r).without_raise
121
+ end
122
+
123
+ should ':fail when multiple raises expected but none occurred' do
124
+ o.a
125
+ o.b
126
+ expect(o).received(:a, :b).and_raise
127
+ end
128
+
129
+ should 'pass when no raises expected and none expected' do
130
+ o.a
131
+ o.b
132
+ expect(o).received(:a, :b).without_raise
133
+ end
134
+
135
+ should 'pass when given block validates raised exception' do
136
+ o.r rescue nil
137
+ expect(o).received(:r).and_raised {|e| e[0].class == ArgumentError}
138
+ end
139
+
140
+ should ':fail when given block does not validate raised exception' do
141
+ o.r rescue nil
142
+ expect(o).received(:r).and_raised {false}
143
+ end
144
+
145
+ should 'pass when given block validates raised exceptions' do
146
+ o.r rescue nil
147
+ o.e rescue nil
148
+ expect(o).received(:r, :e).and_raised do |r,e|
149
+ r[0].class == ArgumentError && e[0].class == ArgumentError
150
+ end
151
+ end
152
+
153
+ should ':fail when given block does not validate raised exceptions' do
154
+ o.r rescue nil
155
+ o.e rescue nil
156
+ expect(o).received(:r, :e).and_raised {false}
157
+ end
158
+ end