opal-rspec-cj 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (176) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +3 -0
  3. data/.gitmodules +15 -0
  4. data/.travis.yml +13 -0
  5. data/.yardopts +5 -0
  6. data/CHANGELOG.md +25 -0
  7. data/Gemfile +8 -0
  8. data/README.md +147 -0
  9. data/Rakefile +26 -0
  10. data/config.ru +10 -0
  11. data/example/Gemfile +4 -0
  12. data/example/README.md +13 -0
  13. data/example/Rakefile +8 -0
  14. data/example/opal/user.rb +11 -0
  15. data/example/spec/user_spec.rb +15 -0
  16. data/lib/opal-rspec.rb +2 -0
  17. data/lib/opal/rspec.rb +20 -0
  18. data/lib/opal/rspec/rake_task.rb +63 -0
  19. data/lib/opal/rspec/version.rb +5 -0
  20. data/opal-rspec.gemspec +21 -0
  21. data/opal/opal-rspec.rb +1 -0
  22. data/opal/opal/rspec.rb +25 -0
  23. data/opal/opal/rspec/async.rb +289 -0
  24. data/opal/opal/rspec/browser_formatter.rb +188 -0
  25. data/opal/opal/rspec/fixes.rb +116 -0
  26. data/opal/opal/rspec/requires.rb +45 -0
  27. data/opal/opal/rspec/runner.rb +69 -0
  28. data/opal/opal/rspec/sprockets_runner.rb.erb +11 -0
  29. data/opal/opal/rspec/text_formatter.rb +74 -0
  30. data/spec/async_spec.rb +38 -0
  31. data/spec/example_spec.rb +163 -0
  32. data/spec/matchers_spec.rb +201 -0
  33. data/spec/mock_spec.rb +63 -0
  34. data/spec/named_subject_spec.rb +11 -0
  35. data/spec/should_syntax_spec.rb +17 -0
  36. data/vendor/spec_runner.js +50 -0
  37. data/vendor_lib/rspec-expectations.rb +1 -0
  38. data/vendor_lib/rspec.rb +3 -0
  39. data/vendor_lib/rspec/autorun.rb +2 -0
  40. data/vendor_lib/rspec/core.rb +203 -0
  41. data/vendor_lib/rspec/core/backport_random.rb +302 -0
  42. data/vendor_lib/rspec/core/backtrace_formatter.rb +65 -0
  43. data/vendor_lib/rspec/core/command_line.rb +36 -0
  44. data/vendor_lib/rspec/core/configuration.rb +1129 -0
  45. data/vendor_lib/rspec/core/configuration_options.rb +143 -0
  46. data/vendor_lib/rspec/core/drb_command_line.rb +26 -0
  47. data/vendor_lib/rspec/core/drb_options.rb +87 -0
  48. data/vendor_lib/rspec/core/dsl.rb +26 -0
  49. data/vendor_lib/rspec/core/example.rb +312 -0
  50. data/vendor_lib/rspec/core/example_group.rb +540 -0
  51. data/vendor_lib/rspec/core/filter_manager.rb +224 -0
  52. data/vendor_lib/rspec/core/flat_map.rb +17 -0
  53. data/vendor_lib/rspec/core/formatters.rb +54 -0
  54. data/vendor_lib/rspec/core/formatters/base_formatter.rb +291 -0
  55. data/vendor_lib/rspec/core/formatters/base_text_formatter.rb +307 -0
  56. data/vendor_lib/rspec/core/formatters/deprecation_formatter.rb +193 -0
  57. data/vendor_lib/rspec/core/formatters/documentation_formatter.rb +67 -0
  58. data/vendor_lib/rspec/core/formatters/helpers.rb +82 -0
  59. data/vendor_lib/rspec/core/formatters/html_formatter.rb +155 -0
  60. data/vendor_lib/rspec/core/formatters/html_printer.rb +408 -0
  61. data/vendor_lib/rspec/core/formatters/json_formatter.rb +99 -0
  62. data/vendor_lib/rspec/core/formatters/progress_formatter.rb +32 -0
  63. data/vendor_lib/rspec/core/formatters/snippet_extractor.rb +101 -0
  64. data/vendor_lib/rspec/core/hooks.rb +535 -0
  65. data/vendor_lib/rspec/core/memoized_helpers.rb +431 -0
  66. data/vendor_lib/rspec/core/metadata.rb +313 -0
  67. data/vendor_lib/rspec/core/mocking/with_absolutely_nothing.rb +11 -0
  68. data/vendor_lib/rspec/core/mocking/with_flexmock.rb +27 -0
  69. data/vendor_lib/rspec/core/mocking/with_mocha.rb +52 -0
  70. data/vendor_lib/rspec/core/mocking/with_rr.rb +27 -0
  71. data/vendor_lib/rspec/core/mocking/with_rspec.rb +27 -0
  72. data/vendor_lib/rspec/core/option_parser.rb +234 -0
  73. data/vendor_lib/rspec/core/ordering.rb +154 -0
  74. data/vendor_lib/rspec/core/pending.rb +110 -0
  75. data/vendor_lib/rspec/core/project_initializer.rb +88 -0
  76. data/vendor_lib/rspec/core/rake_task.rb +128 -0
  77. data/vendor_lib/rspec/core/reporter.rb +132 -0
  78. data/vendor_lib/rspec/core/ruby_project.rb +44 -0
  79. data/vendor_lib/rspec/core/runner.rb +97 -0
  80. data/vendor_lib/rspec/core/shared_context.rb +53 -0
  81. data/vendor_lib/rspec/core/shared_example_group.rb +146 -0
  82. data/vendor_lib/rspec/core/shared_example_group/collection.rb +27 -0
  83. data/vendor_lib/rspec/core/version.rb +7 -0
  84. data/vendor_lib/rspec/core/warnings.rb +22 -0
  85. data/vendor_lib/rspec/core/world.rb +131 -0
  86. data/vendor_lib/rspec/expectations.rb +75 -0
  87. data/vendor_lib/rspec/expectations/differ.rb +154 -0
  88. data/vendor_lib/rspec/expectations/errors.rb +9 -0
  89. data/vendor_lib/rspec/expectations/expectation_target.rb +87 -0
  90. data/vendor_lib/rspec/expectations/extensions.rb +1 -0
  91. data/vendor_lib/rspec/expectations/extensions/object.rb +29 -0
  92. data/vendor_lib/rspec/expectations/fail_with.rb +79 -0
  93. data/vendor_lib/rspec/expectations/handler.rb +68 -0
  94. data/vendor_lib/rspec/expectations/syntax.rb +182 -0
  95. data/vendor_lib/rspec/expectations/version.rb +8 -0
  96. data/vendor_lib/rspec/matchers.rb +633 -0
  97. data/vendor_lib/rspec/matchers/built_in.rb +39 -0
  98. data/vendor_lib/rspec/matchers/built_in/base_matcher.rb +68 -0
  99. data/vendor_lib/rspec/matchers/built_in/be.rb +213 -0
  100. data/vendor_lib/rspec/matchers/built_in/be_instance_of.rb +15 -0
  101. data/vendor_lib/rspec/matchers/built_in/be_kind_of.rb +11 -0
  102. data/vendor_lib/rspec/matchers/built_in/be_within.rb +55 -0
  103. data/vendor_lib/rspec/matchers/built_in/change.rb +141 -0
  104. data/vendor_lib/rspec/matchers/built_in/cover.rb +21 -0
  105. data/vendor_lib/rspec/matchers/built_in/eq.rb +22 -0
  106. data/vendor_lib/rspec/matchers/built_in/eql.rb +23 -0
  107. data/vendor_lib/rspec/matchers/built_in/equal.rb +48 -0
  108. data/vendor_lib/rspec/matchers/built_in/exist.rb +26 -0
  109. data/vendor_lib/rspec/matchers/built_in/has.rb +48 -0
  110. data/vendor_lib/rspec/matchers/built_in/include.rb +61 -0
  111. data/vendor_lib/rspec/matchers/built_in/match.rb +17 -0
  112. data/vendor_lib/rspec/matchers/built_in/match_array.rb +51 -0
  113. data/vendor_lib/rspec/matchers/built_in/raise_error.rb +154 -0
  114. data/vendor_lib/rspec/matchers/built_in/respond_to.rb +74 -0
  115. data/vendor_lib/rspec/matchers/built_in/satisfy.rb +30 -0
  116. data/vendor_lib/rspec/matchers/built_in/start_and_end_with.rb +48 -0
  117. data/vendor_lib/rspec/matchers/built_in/throw_symbol.rb +94 -0
  118. data/vendor_lib/rspec/matchers/built_in/yield.rb +297 -0
  119. data/vendor_lib/rspec/matchers/compatibility.rb +14 -0
  120. data/vendor_lib/rspec/matchers/configuration.rb +113 -0
  121. data/vendor_lib/rspec/matchers/dsl.rb +23 -0
  122. data/vendor_lib/rspec/matchers/generated_descriptions.rb +35 -0
  123. data/vendor_lib/rspec/matchers/matcher.rb +301 -0
  124. data/vendor_lib/rspec/matchers/method_missing.rb +12 -0
  125. data/vendor_lib/rspec/matchers/operator_matcher.rb +99 -0
  126. data/vendor_lib/rspec/matchers/pretty.rb +70 -0
  127. data/vendor_lib/rspec/matchers/test_unit_integration.rb +11 -0
  128. data/vendor_lib/rspec/mocks.rb +100 -0
  129. data/vendor_lib/rspec/mocks/any_instance/chain.rb +92 -0
  130. data/vendor_lib/rspec/mocks/any_instance/expectation_chain.rb +47 -0
  131. data/vendor_lib/rspec/mocks/any_instance/message_chains.rb +75 -0
  132. data/vendor_lib/rspec/mocks/any_instance/recorder.rb +200 -0
  133. data/vendor_lib/rspec/mocks/any_instance/stub_chain.rb +45 -0
  134. data/vendor_lib/rspec/mocks/any_instance/stub_chain_chain.rb +23 -0
  135. data/vendor_lib/rspec/mocks/argument_list_matcher.rb +104 -0
  136. data/vendor_lib/rspec/mocks/argument_matchers.rb +264 -0
  137. data/vendor_lib/rspec/mocks/arity_calculator.rb +66 -0
  138. data/vendor_lib/rspec/mocks/configuration.rb +111 -0
  139. data/vendor_lib/rspec/mocks/error_generator.rb +203 -0
  140. data/vendor_lib/rspec/mocks/errors.rb +12 -0
  141. data/vendor_lib/rspec/mocks/example_methods.rb +201 -0
  142. data/vendor_lib/rspec/mocks/extensions/marshal.rb +17 -0
  143. data/vendor_lib/rspec/mocks/framework.rb +36 -0
  144. data/vendor_lib/rspec/mocks/instance_method_stasher.rb +112 -0
  145. data/vendor_lib/rspec/mocks/matchers/have_received.rb +99 -0
  146. data/vendor_lib/rspec/mocks/matchers/receive.rb +112 -0
  147. data/vendor_lib/rspec/mocks/matchers/receive_messages.rb +72 -0
  148. data/vendor_lib/rspec/mocks/message_expectation.rb +643 -0
  149. data/vendor_lib/rspec/mocks/method_double.rb +209 -0
  150. data/vendor_lib/rspec/mocks/method_reference.rb +95 -0
  151. data/vendor_lib/rspec/mocks/mock.rb +7 -0
  152. data/vendor_lib/rspec/mocks/mutate_const.rb +406 -0
  153. data/vendor_lib/rspec/mocks/object_reference.rb +90 -0
  154. data/vendor_lib/rspec/mocks/order_group.rb +82 -0
  155. data/vendor_lib/rspec/mocks/proxy.rb +269 -0
  156. data/vendor_lib/rspec/mocks/proxy_for_nil.rb +37 -0
  157. data/vendor_lib/rspec/mocks/space.rb +95 -0
  158. data/vendor_lib/rspec/mocks/standalone.rb +3 -0
  159. data/vendor_lib/rspec/mocks/stub_chain.rb +51 -0
  160. data/vendor_lib/rspec/mocks/syntax.rb +374 -0
  161. data/vendor_lib/rspec/mocks/targets.rb +90 -0
  162. data/vendor_lib/rspec/mocks/test_double.rb +109 -0
  163. data/vendor_lib/rspec/mocks/verifying_double.rb +77 -0
  164. data/vendor_lib/rspec/mocks/verifying_message_expecation.rb +60 -0
  165. data/vendor_lib/rspec/mocks/verifying_proxy.rb +151 -0
  166. data/vendor_lib/rspec/mocks/version.rb +7 -0
  167. data/vendor_lib/rspec/support.rb +6 -0
  168. data/vendor_lib/rspec/support/caller_filter.rb +56 -0
  169. data/vendor_lib/rspec/support/spec.rb +14 -0
  170. data/vendor_lib/rspec/support/spec/deprecation_helpers.rb +29 -0
  171. data/vendor_lib/rspec/support/spec/in_sub_process.rb +40 -0
  172. data/vendor_lib/rspec/support/spec/stderr_splitter.rb +50 -0
  173. data/vendor_lib/rspec/support/version.rb +7 -0
  174. data/vendor_lib/rspec/support/warnings.rb +41 -0
  175. data/vendor_lib/rspec/version.rb +5 -0
  176. metadata +268 -0
@@ -0,0 +1,39 @@
1
+ module RSpec
2
+ module Matchers
3
+ module BuiltIn
4
+ require 'rspec/matchers/built_in/base_matcher'
5
+ autoload :BeAnInstanceOf, 'rspec/matchers/built_in/be_instance_of'
6
+ autoload :Be, 'rspec/matchers/built_in/be'
7
+ autoload :BeTruthy, 'rspec/matchers/built_in/be'
8
+ autoload :BeFalsey, 'rspec/matchers/built_in/be'
9
+ autoload :BeNil, 'rspec/matchers/built_in/be'
10
+ autoload :BeComparedTo, 'rspec/matchers/built_in/be'
11
+ autoload :BePredicate, 'rspec/matchers/built_in/be'
12
+ autoload :BeAKindOf, 'rspec/matchers/built_in/be_kind_of'
13
+ autoload :BeWithin, 'rspec/matchers/built_in/be_within'
14
+ autoload :Change, 'rspec/matchers/built_in/change'
15
+ autoload :Cover, 'rspec/matchers/built_in/cover' if (1..2).respond_to?(:cover?)
16
+ autoload :Eq, 'rspec/matchers/built_in/eq'
17
+ autoload :Eql, 'rspec/matchers/built_in/eql'
18
+ autoload :Equal, 'rspec/matchers/built_in/equal'
19
+ autoload :Exist, 'rspec/matchers/built_in/exist'
20
+ autoload :Has, 'rspec/matchers/built_in/has'
21
+ autoload :Have, 'rspec/matchers/built_in/have'
22
+ autoload :Include, 'rspec/matchers/built_in/include'
23
+ autoload :Match, 'rspec/matchers/built_in/match'
24
+ autoload :MatchArray, 'rspec/matchers/built_in/match_array'
25
+ autoload :RaiseError, 'rspec/matchers/built_in/raise_error'
26
+ autoload :RespondTo, 'rspec/matchers/built_in/respond_to'
27
+ autoload :StartWith, 'rspec/matchers/built_in/start_and_end_with'
28
+ autoload :EndWith, 'rspec/matchers/built_in/start_and_end_with'
29
+ autoload :Satisfy, 'rspec/matchers/built_in/satisfy'
30
+ autoload :ThrowSymbol, 'rspec/matchers/built_in/throw_symbol'
31
+ autoload :YieldControl, 'rspec/matchers/built_in/yield'
32
+ autoload :YieldWithArgs, 'rspec/matchers/built_in/yield'
33
+ autoload :YieldWithNoArgs, 'rspec/matchers/built_in/yield'
34
+ autoload :YieldSuccessiveArgs, 'rspec/matchers/built_in/yield'
35
+ end
36
+ end
37
+ end
38
+
39
+
@@ -0,0 +1,68 @@
1
+ module RSpec
2
+ module Matchers
3
+ module BuiltIn
4
+ # @api private
5
+ #
6
+ # Used _internally_ as a base class for matchers that ship with
7
+ # rspec-expectations.
8
+ #
9
+ # ### Warning:
10
+ #
11
+ # This class is for internal use, and subject to change without notice. We
12
+ # strongly recommend that you do not base your custom matchers on this
13
+ # class. If/when this changes, we will announce it and remove this warning.
14
+ class BaseMatcher
15
+ include RSpec::Matchers::Pretty
16
+
17
+ attr_reader :actual, :expected, :rescued_exception
18
+
19
+ def initialize(expected = nil)
20
+ @expected = expected
21
+ end
22
+
23
+ def matches?(actual)
24
+ @actual = actual
25
+ match(expected, actual)
26
+ end
27
+
28
+ def match_unless_raises(*exceptions)
29
+ exceptions.unshift Exception if exceptions.empty?
30
+ begin
31
+ yield
32
+ true
33
+ rescue *exceptions => @rescued_exception
34
+ false
35
+ end
36
+ end
37
+
38
+ def failure_message_for_should
39
+ assert_ivars :@actual, :@expected
40
+ "expected #{@actual.inspect} to #{name_to_sentence}#{expected_to_sentence}"
41
+ end
42
+
43
+ def failure_message_for_should_not
44
+ assert_ivars :@actual, :@expected
45
+ "expected #{@actual.inspect} not to #{name_to_sentence}#{expected_to_sentence}"
46
+ end
47
+
48
+ def description
49
+ expected ? "#{name_to_sentence} #{@expected.inspect}" : name_to_sentence
50
+ end
51
+
52
+ def diffable?
53
+ false
54
+ end
55
+
56
+ def ==(other)
57
+ matches?(other)
58
+ end
59
+
60
+ private
61
+
62
+ def assert_ivars *ivars
63
+ raise "#{self.class.name} needs to supply #{to_sentence ivars}" unless ivars.all? { |v| instance_variables.map(&:intern).include? v }
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,213 @@
1
+ require 'rspec/matchers/dsl'
2
+
3
+ module RSpec
4
+ module Matchers
5
+ module BuiltIn
6
+ class BeTruthy < BaseMatcher
7
+ def match(_, actual)
8
+ !!actual
9
+ end
10
+
11
+ def failure_message_for_should
12
+ "expected: truthy value\n got: #{actual.inspect}"
13
+ end
14
+
15
+ def failure_message_for_should_not
16
+ "expected: falsey value\n got: #{actual.inspect}"
17
+ end
18
+ end
19
+
20
+ class BeFalsey < BaseMatcher
21
+ def match(_, actual)
22
+ !actual
23
+ end
24
+
25
+ def failure_message_for_should
26
+ "expected: falsey value\n got: #{actual.inspect}"
27
+ end
28
+
29
+ def failure_message_for_should_not
30
+ "expected: truthy value\n got: #{actual.inspect}"
31
+ end
32
+ end
33
+
34
+ class BeNil < BaseMatcher
35
+ def match(_, actual)
36
+ actual.nil?
37
+ end
38
+
39
+ def failure_message_for_should
40
+ "expected: nil\n got: #{actual.inspect}"
41
+ end
42
+
43
+ def failure_message_for_should_not
44
+ "expected: not nil\n got: nil"
45
+ end
46
+ end
47
+
48
+ module BeHelpers
49
+ private
50
+
51
+ def args_to_s
52
+ @args.empty? ? "" : parenthesize(inspected_args.join(', '))
53
+ end
54
+
55
+ def parenthesize(string)
56
+ "(#{string})"
57
+ end
58
+
59
+ def inspected_args
60
+ @args.collect{|a| a.inspect}
61
+ end
62
+
63
+ def expected_to_sentence
64
+ split_words(@expected)
65
+ end
66
+
67
+ def args_to_sentence
68
+ to_sentence(@args)
69
+ end
70
+ end
71
+
72
+ class Be < BaseMatcher
73
+ include BeHelpers
74
+
75
+ def initialize(*args, &block)
76
+ @args = args
77
+ end
78
+
79
+ def match(_, actual)
80
+ !!actual
81
+ end
82
+
83
+ def failure_message_for_should
84
+ "expected #{@actual.inspect} to evaluate to true"
85
+ end
86
+
87
+ def failure_message_for_should_not
88
+ "expected #{@actual.inspect} to evaluate to false"
89
+ end
90
+
91
+ [:==, :<, :<=, :>=, :>, :===, :=~].each do |operator|
92
+ define_method operator do |operand|
93
+ BeComparedTo.new(operand, operator)
94
+ end
95
+ end
96
+ end
97
+
98
+ class BeComparedTo < Be
99
+ def initialize(operand, operator)
100
+ @expected, @operator = operand, operator
101
+ @args = []
102
+ end
103
+
104
+ def matches?(actual)
105
+ @actual = actual
106
+ @actual.__send__ @operator, @expected
107
+ end
108
+
109
+ def failure_message_for_should
110
+ "expected: #{@operator} #{@expected.inspect}\n got: #{@operator.to_s.gsub(/./, ' ')} #{@actual.inspect}"
111
+ end
112
+
113
+ def failure_message_for_should_not
114
+ message = <<-MESSAGE
115
+ `#{negative_expectation_expression}` not only FAILED,
116
+ it is a bit confusing.
117
+ MESSAGE
118
+
119
+ raise message << ([:===,:==].include?(@operator) ?
120
+ "It might be more clearly expressed without the \"be\"?" :
121
+ "It might be more clearly expressed in the positive?")
122
+ end
123
+
124
+ def description
125
+ "be #{@operator} #{expected_to_sentence}#{args_to_sentence}"
126
+ end
127
+
128
+ def negative_expectation_expression
129
+ Expectations::Syntax.negative_expression("actual", "be #{@operator} #{@expected}")
130
+ end
131
+ end
132
+
133
+ class BePredicate < BaseMatcher
134
+ include BeHelpers
135
+
136
+ def initialize(*args, &block)
137
+ @expected = parse_expected(args.shift)
138
+ @args = args
139
+ @block = block
140
+ end
141
+
142
+ def matches?(actual)
143
+ @actual = actual
144
+
145
+ if is_private_on?( @actual )
146
+ raise Expectations::ExpectationNotMetError.new("expectation set on private method `#{predicate}`")
147
+ end
148
+
149
+ begin
150
+ return @result = actual.__send__(predicate, *@args, &@block)
151
+ rescue NameError => predicate_missing_error
152
+ "this needs to be here or rcov will not count this branch even though it's executed in a code example"
153
+ end
154
+
155
+ begin
156
+ return @result = actual.__send__(present_tense_predicate, *@args, &@block)
157
+ rescue NameError
158
+ raise predicate_missing_error
159
+ end
160
+ end
161
+
162
+ alias === matches?
163
+
164
+ def failure_message_for_should
165
+ "expected #{predicate}#{args_to_s} to return true, got #{@result.inspect}"
166
+ end
167
+
168
+ def failure_message_for_should_not
169
+ "expected #{predicate}#{args_to_s} to return false, got #{@result.inspect}"
170
+ end
171
+
172
+ def description
173
+ "#{prefix_to_sentence}#{expected_to_sentence}#{args_to_sentence}"
174
+ end
175
+
176
+ private
177
+
178
+ # support 1.8.7
179
+ if methods.first.is_a? String
180
+ def is_private_on? actual
181
+ actual.private_methods.include? predicate.to_s
182
+ end
183
+ else
184
+ def is_private_on? actual
185
+ actual.private_methods.include? predicate
186
+ end
187
+ end
188
+
189
+ def predicate
190
+ "#{@expected}?".to_sym
191
+ end
192
+
193
+ def present_tense_predicate
194
+ "#{@expected}s?".to_sym
195
+ end
196
+
197
+ def parse_expected(expected)
198
+ @prefix, expected = prefix_and_expected(expected)
199
+ expected
200
+ end
201
+
202
+ def prefix_and_expected(symbol)
203
+ symbol.to_s =~ /^(be_(an?_)?)(.*)/
204
+ return $1, $3
205
+ end
206
+
207
+ def prefix_to_sentence
208
+ split_words(@prefix)
209
+ end
210
+ end
211
+ end
212
+ end
213
+ end
@@ -0,0 +1,15 @@
1
+ module RSpec
2
+ module Matchers
3
+ module BuiltIn
4
+ class BeAnInstanceOf < BaseMatcher
5
+ def match(expected, actual)
6
+ actual.instance_of? expected
7
+ end
8
+
9
+ def description
10
+ "be an instance of #{expected}"
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ module RSpec
2
+ module Matchers
3
+ module BuiltIn
4
+ class BeAKindOf < BaseMatcher
5
+ def match(expected, actual)
6
+ actual.kind_of? expected
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,55 @@
1
+ module RSpec
2
+ module Matchers
3
+ module BuiltIn
4
+ class BeWithin
5
+ def initialize(delta)
6
+ @delta = delta
7
+ end
8
+
9
+ def matches?(actual)
10
+ @actual = actual
11
+ raise needs_expected unless defined? @expected
12
+ raise needs_subtractable unless @actual.respond_to? :-
13
+ (@actual - @expected).abs <= @tolerance
14
+ end
15
+ alias == matches?
16
+
17
+ def of(expected)
18
+ @expected = expected
19
+ @tolerance = @delta
20
+ @unit = ''
21
+ self
22
+ end
23
+
24
+ def percent_of(expected)
25
+ @expected = expected
26
+ @tolerance = @delta * @expected.abs / 100.0
27
+ @unit = '%'
28
+ self
29
+ end
30
+
31
+ def failure_message_for_should
32
+ "expected #{@actual} to #{description}"
33
+ end
34
+
35
+ def failure_message_for_should_not
36
+ "expected #{@actual} not to #{description}"
37
+ end
38
+
39
+ def description
40
+ "be within #{@delta}#{@unit} of #{@expected}"
41
+ end
42
+
43
+ private
44
+
45
+ def needs_subtractable
46
+ ArgumentError.new "The actual value (#{@actual.inspect}) must respond to `-`"
47
+ end
48
+
49
+ def needs_expected
50
+ ArgumentError.new "You must set an expected value using #of: be_within(#{@delta}).of(expected_value)"
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,141 @@
1
+ module RSpec
2
+ module Matchers
3
+ module BuiltIn
4
+ class Change
5
+ def initialize(receiver=nil, message=nil, &block)
6
+ @message = message
7
+ @value_proc = block || lambda {receiver.__send__(message)}
8
+ @expected_after = @expected_before = @minimum = @maximum = @expected_delta = nil
9
+ @eval_before = @eval_after = false
10
+ end
11
+
12
+ def matches?(event_proc)
13
+ raise_block_syntax_error if block_given?
14
+
15
+ @actual_before = evaluate_value_proc
16
+ event_proc.call
17
+ @actual_after = evaluate_value_proc
18
+
19
+ (!change_expected? || changed?) && matches_before? && matches_after? && matches_expected_delta? && matches_min? && matches_max?
20
+ end
21
+ alias == matches?
22
+
23
+ def raise_block_syntax_error
24
+ raise SyntaxError.new(<<-MESSAGE)
25
+ block passed to should or should_not change must use {} instead of do/end
26
+ MESSAGE
27
+ end
28
+
29
+ def evaluate_value_proc
30
+ case val = @value_proc.call
31
+ when Enumerable, String
32
+ val.dup
33
+ else
34
+ val
35
+ end
36
+ end
37
+
38
+ def failure_message_for_should
39
+ if @eval_before && !expected_matches_actual?(@expected_before, @actual_before)
40
+ "#{message} should have initially been #{@expected_before.inspect}, but was #{@actual_before.inspect}"
41
+ elsif @eval_after && !expected_matches_actual?(@expected_after, @actual_after)
42
+ "#{message} should have been changed to #{failure_message_for_expected_after}, but is now #{@actual_after.inspect}"
43
+ elsif @expected_delta
44
+ "#{message} should have been changed by #{@expected_delta.inspect}, but was changed by #{actual_delta.inspect}"
45
+ elsif @minimum
46
+ "#{message} should have been changed by at least #{@minimum.inspect}, but was changed by #{actual_delta.inspect}"
47
+ elsif @maximum
48
+ "#{message} should have been changed by at most #{@maximum.inspect}, but was changed by #{actual_delta.inspect}"
49
+ else
50
+ "#{message} should have changed, but is still #{@actual_before.inspect}"
51
+ end
52
+ end
53
+
54
+ def actual_delta
55
+ @actual_after - @actual_before
56
+ end
57
+
58
+ def failure_message_for_should_not
59
+ "#{message} should not have changed, but did change from #{@actual_before.inspect} to #{@actual_after.inspect}"
60
+ end
61
+
62
+ def by(expected_delta)
63
+ @expected_delta = expected_delta
64
+ self
65
+ end
66
+
67
+ def by_at_least(minimum)
68
+ @minimum = minimum
69
+ self
70
+ end
71
+
72
+ def by_at_most(maximum)
73
+ @maximum = maximum
74
+ self
75
+ end
76
+
77
+ def to(to)
78
+ @eval_after = true
79
+ @expected_after = to
80
+ self
81
+ end
82
+
83
+ def from (before)
84
+ @eval_before = true
85
+ @expected_before = before
86
+ self
87
+ end
88
+
89
+ def description
90
+ "change ##{message}"
91
+ end
92
+
93
+ private
94
+
95
+ def failure_message_for_expected_after
96
+ if RSpec::Matchers.is_a_matcher?(@expected_after)
97
+ @expected_after.description
98
+ else
99
+ @expected_after.inspect
100
+ end
101
+ end
102
+
103
+ def message
104
+ @message || "result"
105
+ end
106
+
107
+ def change_expected?
108
+ @expected_delta != 0
109
+ end
110
+
111
+ def changed?
112
+ @actual_before != @actual_after
113
+ end
114
+
115
+ def matches_before?
116
+ @eval_before ? expected_matches_actual?(@expected_before, @actual_before) : true
117
+ end
118
+
119
+ def matches_after?
120
+ @eval_after ? expected_matches_actual?(@expected_after, @actual_after) : true
121
+ end
122
+
123
+ def matches_expected_delta?
124
+ @expected_delta ? (@actual_before + @expected_delta == @actual_after) : true
125
+ end
126
+
127
+ def matches_min?
128
+ @minimum ? (@actual_after - @actual_before >= @minimum) : true
129
+ end
130
+
131
+ def matches_max?
132
+ @maximum ? (@actual_after - @actual_before <= @maximum) : true
133
+ end
134
+
135
+ def expected_matches_actual?(expected, actual)
136
+ expected === actual || actual == expected
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end