opal-rspec-cj 0.4.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +3 -0
- data/.gitmodules +15 -0
- data/.travis.yml +13 -0
- data/.yardopts +5 -0
- data/CHANGELOG.md +25 -0
- data/Gemfile +8 -0
- data/README.md +147 -0
- data/Rakefile +26 -0
- data/config.ru +10 -0
- data/example/Gemfile +4 -0
- data/example/README.md +13 -0
- data/example/Rakefile +8 -0
- data/example/opal/user.rb +11 -0
- data/example/spec/user_spec.rb +15 -0
- data/lib/opal-rspec.rb +2 -0
- data/lib/opal/rspec.rb +20 -0
- data/lib/opal/rspec/rake_task.rb +63 -0
- data/lib/opal/rspec/version.rb +5 -0
- data/opal-rspec.gemspec +21 -0
- data/opal/opal-rspec.rb +1 -0
- data/opal/opal/rspec.rb +25 -0
- data/opal/opal/rspec/async.rb +289 -0
- data/opal/opal/rspec/browser_formatter.rb +188 -0
- data/opal/opal/rspec/fixes.rb +116 -0
- data/opal/opal/rspec/requires.rb +45 -0
- data/opal/opal/rspec/runner.rb +69 -0
- data/opal/opal/rspec/sprockets_runner.rb.erb +11 -0
- data/opal/opal/rspec/text_formatter.rb +74 -0
- data/spec/async_spec.rb +38 -0
- data/spec/example_spec.rb +163 -0
- data/spec/matchers_spec.rb +201 -0
- data/spec/mock_spec.rb +63 -0
- data/spec/named_subject_spec.rb +11 -0
- data/spec/should_syntax_spec.rb +17 -0
- data/vendor/spec_runner.js +50 -0
- data/vendor_lib/rspec-expectations.rb +1 -0
- data/vendor_lib/rspec.rb +3 -0
- data/vendor_lib/rspec/autorun.rb +2 -0
- data/vendor_lib/rspec/core.rb +203 -0
- data/vendor_lib/rspec/core/backport_random.rb +302 -0
- data/vendor_lib/rspec/core/backtrace_formatter.rb +65 -0
- data/vendor_lib/rspec/core/command_line.rb +36 -0
- data/vendor_lib/rspec/core/configuration.rb +1129 -0
- data/vendor_lib/rspec/core/configuration_options.rb +143 -0
- data/vendor_lib/rspec/core/drb_command_line.rb +26 -0
- data/vendor_lib/rspec/core/drb_options.rb +87 -0
- data/vendor_lib/rspec/core/dsl.rb +26 -0
- data/vendor_lib/rspec/core/example.rb +312 -0
- data/vendor_lib/rspec/core/example_group.rb +540 -0
- data/vendor_lib/rspec/core/filter_manager.rb +224 -0
- data/vendor_lib/rspec/core/flat_map.rb +17 -0
- data/vendor_lib/rspec/core/formatters.rb +54 -0
- data/vendor_lib/rspec/core/formatters/base_formatter.rb +291 -0
- data/vendor_lib/rspec/core/formatters/base_text_formatter.rb +307 -0
- data/vendor_lib/rspec/core/formatters/deprecation_formatter.rb +193 -0
- data/vendor_lib/rspec/core/formatters/documentation_formatter.rb +67 -0
- data/vendor_lib/rspec/core/formatters/helpers.rb +82 -0
- data/vendor_lib/rspec/core/formatters/html_formatter.rb +155 -0
- data/vendor_lib/rspec/core/formatters/html_printer.rb +408 -0
- data/vendor_lib/rspec/core/formatters/json_formatter.rb +99 -0
- data/vendor_lib/rspec/core/formatters/progress_formatter.rb +32 -0
- data/vendor_lib/rspec/core/formatters/snippet_extractor.rb +101 -0
- data/vendor_lib/rspec/core/hooks.rb +535 -0
- data/vendor_lib/rspec/core/memoized_helpers.rb +431 -0
- data/vendor_lib/rspec/core/metadata.rb +313 -0
- data/vendor_lib/rspec/core/mocking/with_absolutely_nothing.rb +11 -0
- data/vendor_lib/rspec/core/mocking/with_flexmock.rb +27 -0
- data/vendor_lib/rspec/core/mocking/with_mocha.rb +52 -0
- data/vendor_lib/rspec/core/mocking/with_rr.rb +27 -0
- data/vendor_lib/rspec/core/mocking/with_rspec.rb +27 -0
- data/vendor_lib/rspec/core/option_parser.rb +234 -0
- data/vendor_lib/rspec/core/ordering.rb +154 -0
- data/vendor_lib/rspec/core/pending.rb +110 -0
- data/vendor_lib/rspec/core/project_initializer.rb +88 -0
- data/vendor_lib/rspec/core/rake_task.rb +128 -0
- data/vendor_lib/rspec/core/reporter.rb +132 -0
- data/vendor_lib/rspec/core/ruby_project.rb +44 -0
- data/vendor_lib/rspec/core/runner.rb +97 -0
- data/vendor_lib/rspec/core/shared_context.rb +53 -0
- data/vendor_lib/rspec/core/shared_example_group.rb +146 -0
- data/vendor_lib/rspec/core/shared_example_group/collection.rb +27 -0
- data/vendor_lib/rspec/core/version.rb +7 -0
- data/vendor_lib/rspec/core/warnings.rb +22 -0
- data/vendor_lib/rspec/core/world.rb +131 -0
- data/vendor_lib/rspec/expectations.rb +75 -0
- data/vendor_lib/rspec/expectations/differ.rb +154 -0
- data/vendor_lib/rspec/expectations/errors.rb +9 -0
- data/vendor_lib/rspec/expectations/expectation_target.rb +87 -0
- data/vendor_lib/rspec/expectations/extensions.rb +1 -0
- data/vendor_lib/rspec/expectations/extensions/object.rb +29 -0
- data/vendor_lib/rspec/expectations/fail_with.rb +79 -0
- data/vendor_lib/rspec/expectations/handler.rb +68 -0
- data/vendor_lib/rspec/expectations/syntax.rb +182 -0
- data/vendor_lib/rspec/expectations/version.rb +8 -0
- data/vendor_lib/rspec/matchers.rb +633 -0
- data/vendor_lib/rspec/matchers/built_in.rb +39 -0
- data/vendor_lib/rspec/matchers/built_in/base_matcher.rb +68 -0
- data/vendor_lib/rspec/matchers/built_in/be.rb +213 -0
- data/vendor_lib/rspec/matchers/built_in/be_instance_of.rb +15 -0
- data/vendor_lib/rspec/matchers/built_in/be_kind_of.rb +11 -0
- data/vendor_lib/rspec/matchers/built_in/be_within.rb +55 -0
- data/vendor_lib/rspec/matchers/built_in/change.rb +141 -0
- data/vendor_lib/rspec/matchers/built_in/cover.rb +21 -0
- data/vendor_lib/rspec/matchers/built_in/eq.rb +22 -0
- data/vendor_lib/rspec/matchers/built_in/eql.rb +23 -0
- data/vendor_lib/rspec/matchers/built_in/equal.rb +48 -0
- data/vendor_lib/rspec/matchers/built_in/exist.rb +26 -0
- data/vendor_lib/rspec/matchers/built_in/has.rb +48 -0
- data/vendor_lib/rspec/matchers/built_in/include.rb +61 -0
- data/vendor_lib/rspec/matchers/built_in/match.rb +17 -0
- data/vendor_lib/rspec/matchers/built_in/match_array.rb +51 -0
- data/vendor_lib/rspec/matchers/built_in/raise_error.rb +154 -0
- data/vendor_lib/rspec/matchers/built_in/respond_to.rb +74 -0
- data/vendor_lib/rspec/matchers/built_in/satisfy.rb +30 -0
- data/vendor_lib/rspec/matchers/built_in/start_and_end_with.rb +48 -0
- data/vendor_lib/rspec/matchers/built_in/throw_symbol.rb +94 -0
- data/vendor_lib/rspec/matchers/built_in/yield.rb +297 -0
- data/vendor_lib/rspec/matchers/compatibility.rb +14 -0
- data/vendor_lib/rspec/matchers/configuration.rb +113 -0
- data/vendor_lib/rspec/matchers/dsl.rb +23 -0
- data/vendor_lib/rspec/matchers/generated_descriptions.rb +35 -0
- data/vendor_lib/rspec/matchers/matcher.rb +301 -0
- data/vendor_lib/rspec/matchers/method_missing.rb +12 -0
- data/vendor_lib/rspec/matchers/operator_matcher.rb +99 -0
- data/vendor_lib/rspec/matchers/pretty.rb +70 -0
- data/vendor_lib/rspec/matchers/test_unit_integration.rb +11 -0
- data/vendor_lib/rspec/mocks.rb +100 -0
- data/vendor_lib/rspec/mocks/any_instance/chain.rb +92 -0
- data/vendor_lib/rspec/mocks/any_instance/expectation_chain.rb +47 -0
- data/vendor_lib/rspec/mocks/any_instance/message_chains.rb +75 -0
- data/vendor_lib/rspec/mocks/any_instance/recorder.rb +200 -0
- data/vendor_lib/rspec/mocks/any_instance/stub_chain.rb +45 -0
- data/vendor_lib/rspec/mocks/any_instance/stub_chain_chain.rb +23 -0
- data/vendor_lib/rspec/mocks/argument_list_matcher.rb +104 -0
- data/vendor_lib/rspec/mocks/argument_matchers.rb +264 -0
- data/vendor_lib/rspec/mocks/arity_calculator.rb +66 -0
- data/vendor_lib/rspec/mocks/configuration.rb +111 -0
- data/vendor_lib/rspec/mocks/error_generator.rb +203 -0
- data/vendor_lib/rspec/mocks/errors.rb +12 -0
- data/vendor_lib/rspec/mocks/example_methods.rb +201 -0
- data/vendor_lib/rspec/mocks/extensions/marshal.rb +17 -0
- data/vendor_lib/rspec/mocks/framework.rb +36 -0
- data/vendor_lib/rspec/mocks/instance_method_stasher.rb +112 -0
- data/vendor_lib/rspec/mocks/matchers/have_received.rb +99 -0
- data/vendor_lib/rspec/mocks/matchers/receive.rb +112 -0
- data/vendor_lib/rspec/mocks/matchers/receive_messages.rb +72 -0
- data/vendor_lib/rspec/mocks/message_expectation.rb +643 -0
- data/vendor_lib/rspec/mocks/method_double.rb +209 -0
- data/vendor_lib/rspec/mocks/method_reference.rb +95 -0
- data/vendor_lib/rspec/mocks/mock.rb +7 -0
- data/vendor_lib/rspec/mocks/mutate_const.rb +406 -0
- data/vendor_lib/rspec/mocks/object_reference.rb +90 -0
- data/vendor_lib/rspec/mocks/order_group.rb +82 -0
- data/vendor_lib/rspec/mocks/proxy.rb +269 -0
- data/vendor_lib/rspec/mocks/proxy_for_nil.rb +37 -0
- data/vendor_lib/rspec/mocks/space.rb +95 -0
- data/vendor_lib/rspec/mocks/standalone.rb +3 -0
- data/vendor_lib/rspec/mocks/stub_chain.rb +51 -0
- data/vendor_lib/rspec/mocks/syntax.rb +374 -0
- data/vendor_lib/rspec/mocks/targets.rb +90 -0
- data/vendor_lib/rspec/mocks/test_double.rb +109 -0
- data/vendor_lib/rspec/mocks/verifying_double.rb +77 -0
- data/vendor_lib/rspec/mocks/verifying_message_expecation.rb +60 -0
- data/vendor_lib/rspec/mocks/verifying_proxy.rb +151 -0
- data/vendor_lib/rspec/mocks/version.rb +7 -0
- data/vendor_lib/rspec/support.rb +6 -0
- data/vendor_lib/rspec/support/caller_filter.rb +56 -0
- data/vendor_lib/rspec/support/spec.rb +14 -0
- data/vendor_lib/rspec/support/spec/deprecation_helpers.rb +29 -0
- data/vendor_lib/rspec/support/spec/in_sub_process.rb +40 -0
- data/vendor_lib/rspec/support/spec/stderr_splitter.rb +50 -0
- data/vendor_lib/rspec/support/version.rb +7 -0
- data/vendor_lib/rspec/support/warnings.rb +41 -0
- data/vendor_lib/rspec/version.rb +5 -0
- metadata +268 -0
@@ -0,0 +1,21 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Matchers
|
3
|
+
module BuiltIn
|
4
|
+
class Cover < BaseMatcher
|
5
|
+
def initialize(*expected)
|
6
|
+
@expected = expected
|
7
|
+
end
|
8
|
+
|
9
|
+
def matches?(range)
|
10
|
+
@actual = range
|
11
|
+
@expected.all? { |e| range.cover?(e) }
|
12
|
+
end
|
13
|
+
|
14
|
+
def does_not_match?(range)
|
15
|
+
@actual = range
|
16
|
+
expected.none? { |e| range.cover?(e) }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Matchers
|
3
|
+
module BuiltIn
|
4
|
+
class Eq < BaseMatcher
|
5
|
+
def match(expected, actual)
|
6
|
+
actual == expected
|
7
|
+
end
|
8
|
+
|
9
|
+
def failure_message_for_should
|
10
|
+
"\nexpected: #{expected.inspect}\n got: #{actual.inspect}\n\n(compared using ==)\n"
|
11
|
+
end
|
12
|
+
|
13
|
+
def failure_message_for_should_not
|
14
|
+
"\nexpected: value != #{expected.inspect}\n got: #{actual.inspect}\n\n(compared using ==)\n"
|
15
|
+
end
|
16
|
+
|
17
|
+
def diffable?; true; end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Matchers
|
3
|
+
module BuiltIn
|
4
|
+
class Eql < BaseMatcher
|
5
|
+
def match(expected, actual)
|
6
|
+
actual.eql? expected
|
7
|
+
end
|
8
|
+
|
9
|
+
def failure_message_for_should
|
10
|
+
"\nexpected: #{expected.inspect}\n got: #{actual.inspect}\n\n(compared using eql?)\n"
|
11
|
+
end
|
12
|
+
|
13
|
+
def failure_message_for_should_not
|
14
|
+
"\nexpected: value != #{expected.inspect}\n got: #{actual.inspect}\n\n(compared using eql?)\n"
|
15
|
+
end
|
16
|
+
|
17
|
+
def diffable?
|
18
|
+
true
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Matchers
|
3
|
+
module BuiltIn
|
4
|
+
class Equal < BaseMatcher
|
5
|
+
def match(expected, actual)
|
6
|
+
actual.equal? expected
|
7
|
+
end
|
8
|
+
|
9
|
+
def failure_message_for_should
|
10
|
+
return <<-MESSAGE
|
11
|
+
|
12
|
+
expected #{inspect_object(expected)}
|
13
|
+
got #{inspect_object(actual)}
|
14
|
+
|
15
|
+
Compared using equal?, which compares object identity,
|
16
|
+
but expected and actual are not the same object. Use
|
17
|
+
`#{eq_expression}` if you don't care about
|
18
|
+
object identity in this example.
|
19
|
+
|
20
|
+
MESSAGE
|
21
|
+
end
|
22
|
+
|
23
|
+
def failure_message_for_should_not
|
24
|
+
return <<-MESSAGE
|
25
|
+
|
26
|
+
expected not #{inspect_object(actual)}
|
27
|
+
got #{inspect_object(expected)}
|
28
|
+
|
29
|
+
Compared using equal?, which compares object identity.
|
30
|
+
|
31
|
+
MESSAGE
|
32
|
+
end
|
33
|
+
|
34
|
+
def diffable?; true; end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def inspect_object(o)
|
39
|
+
"#<#{o.class}:#{o.object_id}> => #{o.inspect}"
|
40
|
+
end
|
41
|
+
|
42
|
+
def eq_expression
|
43
|
+
Expectations::Syntax.positive_expression("actual", "eq(expected)")
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Matchers
|
3
|
+
module BuiltIn
|
4
|
+
class Exist < BaseMatcher
|
5
|
+
def initialize(*expected)
|
6
|
+
@expected = expected
|
7
|
+
end
|
8
|
+
|
9
|
+
def matches?(actual)
|
10
|
+
@actual = actual
|
11
|
+
predicates = [:exist?, :exists?].select { |p| @actual.respond_to?(p) }
|
12
|
+
existence_values = predicates.map { |p| @actual.__send__(p, *@expected) }
|
13
|
+
uniq_truthy_values = existence_values.map { |v| !!v }.uniq
|
14
|
+
|
15
|
+
case uniq_truthy_values.size
|
16
|
+
when 0; raise NoMethodError.new("#{@actual.inspect} does not respond to either #exist? or #exists?")
|
17
|
+
when 1; existence_values.first
|
18
|
+
else raise "#exist? and #exists? returned different values:\n\n" +
|
19
|
+
" exist?: #{existence_values.first}\n" +
|
20
|
+
"exists?: #{existence_values.last}"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Matchers
|
3
|
+
module BuiltIn
|
4
|
+
class Has
|
5
|
+
def initialize(expected, *args)
|
6
|
+
@expected, @args = expected, args
|
7
|
+
end
|
8
|
+
|
9
|
+
def matches?(actual)
|
10
|
+
actual.__send__(predicate(@expected), *@args)
|
11
|
+
end
|
12
|
+
alias == matches?
|
13
|
+
|
14
|
+
def failure_message_for_should
|
15
|
+
"expected ##{predicate(@expected)}#{failure_message_args_description} to return true, got false"
|
16
|
+
end
|
17
|
+
|
18
|
+
def failure_message_for_should_not
|
19
|
+
"expected ##{predicate(@expected)}#{failure_message_args_description} to return false, got true"
|
20
|
+
end
|
21
|
+
|
22
|
+
def description
|
23
|
+
[method_description(@expected), args_description].compact.join(' ')
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def predicate(sym)
|
29
|
+
"#{sym.to_s.sub("have_","has_")}?".to_sym
|
30
|
+
end
|
31
|
+
|
32
|
+
def method_description(method)
|
33
|
+
method.to_s.gsub('_', ' ')
|
34
|
+
end
|
35
|
+
|
36
|
+
def args_description
|
37
|
+
return nil if @args.empty?
|
38
|
+
@args.map { |arg| arg.inspect }.join(', ')
|
39
|
+
end
|
40
|
+
|
41
|
+
def failure_message_args_description
|
42
|
+
desc = args_description
|
43
|
+
"(#{desc})" if desc
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Matchers
|
3
|
+
module BuiltIn
|
4
|
+
class Include < BaseMatcher
|
5
|
+
def initialize(*expected)
|
6
|
+
@expected = expected
|
7
|
+
end
|
8
|
+
|
9
|
+
def matches?(actual)
|
10
|
+
@actual = actual
|
11
|
+
perform_match(:all?, :all?, @actual, @expected)
|
12
|
+
end
|
13
|
+
|
14
|
+
def does_not_match?(actual)
|
15
|
+
@actual = actual
|
16
|
+
perform_match(:none?, :any?, @actual, @expected)
|
17
|
+
end
|
18
|
+
|
19
|
+
def description
|
20
|
+
"include#{expected_to_sentence}"
|
21
|
+
end
|
22
|
+
|
23
|
+
def diffable?
|
24
|
+
# Matchers do not diff well, since diff uses their inspect
|
25
|
+
# output, which includes their instance variables and such.
|
26
|
+
@expected.none? { |e| RSpec::Matchers.is_a_matcher?(e) }
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def perform_match(predicate, hash_predicate, actuals, expecteds)
|
32
|
+
expecteds.__send__(predicate) do |expected|
|
33
|
+
if comparing_hash_values?(actuals, expected)
|
34
|
+
expected.__send__(hash_predicate) { |k,v|
|
35
|
+
actuals.has_key?(k) && actuals[k] == v
|
36
|
+
}
|
37
|
+
elsif comparing_hash_keys?(actuals, expected)
|
38
|
+
actuals.has_key?(expected)
|
39
|
+
elsif comparing_with_matcher?(actual, expected)
|
40
|
+
actual.any? { |value| expected.matches?(value) }
|
41
|
+
else
|
42
|
+
actuals.include?(expected)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def comparing_hash_keys?(actual, expected)
|
48
|
+
actual.is_a?(Hash) && !expected.is_a?(Hash)
|
49
|
+
end
|
50
|
+
|
51
|
+
def comparing_hash_values?(actual, expected)
|
52
|
+
actual.is_a?(Hash) && expected.is_a?(Hash)
|
53
|
+
end
|
54
|
+
|
55
|
+
def comparing_with_matcher?(actual, expected)
|
56
|
+
actual.is_a?(Array) && RSpec::Matchers.is_a_matcher?(expected)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Matchers
|
3
|
+
module BuiltIn
|
4
|
+
class MatchArray < BaseMatcher
|
5
|
+
def match(expected, actual)
|
6
|
+
return false unless actual.respond_to? :to_ary
|
7
|
+
@extra_items = difference_between_arrays(actual, expected)
|
8
|
+
@missing_items = difference_between_arrays(expected, actual)
|
9
|
+
@extra_items.empty? & @missing_items.empty?
|
10
|
+
end
|
11
|
+
|
12
|
+
def failure_message_for_should
|
13
|
+
if actual.respond_to? :to_ary
|
14
|
+
message = "expected collection contained: #{safe_sort(expected).inspect}\n"
|
15
|
+
message += "actual collection contained: #{safe_sort(actual).inspect}\n"
|
16
|
+
message += "the missing elements were: #{safe_sort(@missing_items).inspect}\n" unless @missing_items.empty?
|
17
|
+
message += "the extra elements were: #{safe_sort(@extra_items).inspect}\n" unless @extra_items.empty?
|
18
|
+
else
|
19
|
+
message = "expected an array, actual collection was #{actual.inspect}"
|
20
|
+
end
|
21
|
+
|
22
|
+
message
|
23
|
+
end
|
24
|
+
|
25
|
+
def failure_message_for_should_not
|
26
|
+
"Matcher does not support should_not"
|
27
|
+
end
|
28
|
+
|
29
|
+
def description
|
30
|
+
"contain exactly #{_pretty_print(expected)}"
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def safe_sort(array)
|
36
|
+
array.sort rescue array
|
37
|
+
end
|
38
|
+
|
39
|
+
def difference_between_arrays(array_1, array_2)
|
40
|
+
difference = array_1.to_ary.dup
|
41
|
+
array_2.to_ary.each do |element|
|
42
|
+
if index = difference.index(element)
|
43
|
+
difference.delete_at(index)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
difference
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,154 @@
|
|
1
|
+
module RSpec
|
2
|
+
module Matchers
|
3
|
+
module BuiltIn
|
4
|
+
class RaiseError
|
5
|
+
def initialize(expected_error_or_message=Exception, expected_message=nil, &block)
|
6
|
+
@block = block
|
7
|
+
@actual_error = nil
|
8
|
+
case expected_error_or_message
|
9
|
+
when String, Regexp
|
10
|
+
@expected_error, @expected_message = Exception, expected_error_or_message
|
11
|
+
else
|
12
|
+
@expected_error, @expected_message = expected_error_or_message, expected_message
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def with_message(expected_message)
|
17
|
+
raise_message_already_set if @expected_message
|
18
|
+
@expected_message = expected_message
|
19
|
+
self
|
20
|
+
end
|
21
|
+
|
22
|
+
def matches?(given_proc, negative_expectation = false, &block)
|
23
|
+
@block ||= block
|
24
|
+
@raised_expected_error = false
|
25
|
+
@with_expected_message = false
|
26
|
+
@eval_block = false
|
27
|
+
@eval_block_passed = false
|
28
|
+
|
29
|
+
unless given_proc.respond_to?(:call)
|
30
|
+
::Kernel.warn "`raise_error` was called with non-proc object #{given_proc.inspect}"
|
31
|
+
return false
|
32
|
+
end
|
33
|
+
|
34
|
+
begin
|
35
|
+
given_proc.call
|
36
|
+
rescue Exception => @actual_error
|
37
|
+
if @actual_error == @expected_error || @expected_error === @actual_error
|
38
|
+
@raised_expected_error = true
|
39
|
+
@with_expected_message = verify_message
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
unless negative_expectation
|
44
|
+
eval_block if @raised_expected_error && @with_expected_message && @block
|
45
|
+
end
|
46
|
+
|
47
|
+
expectation_matched?
|
48
|
+
end
|
49
|
+
|
50
|
+
alias == matches?
|
51
|
+
|
52
|
+
def expectation_matched?
|
53
|
+
error_and_message_match? && block_matches?
|
54
|
+
end
|
55
|
+
|
56
|
+
def error_and_message_match?
|
57
|
+
@raised_expected_error && @with_expected_message
|
58
|
+
end
|
59
|
+
|
60
|
+
def block_matches?
|
61
|
+
@eval_block ? @eval_block_passed : true
|
62
|
+
end
|
63
|
+
|
64
|
+
def does_not_match?(given_proc)
|
65
|
+
prevent_invalid_expectations
|
66
|
+
!matches?(given_proc, :negative_expectation)
|
67
|
+
end
|
68
|
+
|
69
|
+
def eval_block
|
70
|
+
@eval_block = true
|
71
|
+
begin
|
72
|
+
@block[@actual_error]
|
73
|
+
@eval_block_passed = true
|
74
|
+
rescue Exception => err
|
75
|
+
@actual_error = err
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def verify_message
|
80
|
+
case @expected_message
|
81
|
+
when nil
|
82
|
+
true
|
83
|
+
when Regexp
|
84
|
+
@expected_message =~ @actual_error.message
|
85
|
+
else
|
86
|
+
@expected_message == @actual_error.message
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def failure_message_for_should
|
91
|
+
@eval_block ? @actual_error.message : "expected #{expected_error}#{given_error}"
|
92
|
+
end
|
93
|
+
|
94
|
+
def failure_message_for_should_not
|
95
|
+
"expected no #{expected_error}#{given_error}"
|
96
|
+
end
|
97
|
+
|
98
|
+
def description
|
99
|
+
"raise #{expected_error}"
|
100
|
+
end
|
101
|
+
|
102
|
+
private
|
103
|
+
|
104
|
+
def prevent_invalid_expectations
|
105
|
+
if (expecting_specific_exception? || @expected_message)
|
106
|
+
what_to_raise = if expecting_specific_exception? && @expected_message
|
107
|
+
"`expect { }.not_to raise_error(SpecificErrorClass, message)`"
|
108
|
+
elsif expecting_specific_exception?
|
109
|
+
"`expect { }.not_to raise_error(SpecificErrorClass)`"
|
110
|
+
elsif @expected_message
|
111
|
+
"`expect { }.not_to raise_error(message)`"
|
112
|
+
end
|
113
|
+
specific_class_error = ArgumentError.new("#{what_to_raise} is not valid, use `expect { }.not_to raise_error` (with no args) instead")
|
114
|
+
raise specific_class_error
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def expected_error
|
119
|
+
case @expected_message
|
120
|
+
when nil
|
121
|
+
@expected_error.inspect
|
122
|
+
when Regexp
|
123
|
+
"#{@expected_error} with message matching #{@expected_message.inspect}"
|
124
|
+
else
|
125
|
+
"#{@expected_error} with #{@expected_message.inspect}"
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def format_backtrace(backtrace)
|
130
|
+
formatter = Matchers.configuration.backtrace_formatter
|
131
|
+
formatter.format_backtrace(backtrace)
|
132
|
+
end
|
133
|
+
|
134
|
+
def given_error
|
135
|
+
return " but nothing was raised" unless @actual_error
|
136
|
+
|
137
|
+
backtrace = format_backtrace(@actual_error.backtrace)
|
138
|
+
[
|
139
|
+
", got #{@actual_error.inspect} with backtrace:",
|
140
|
+
*backtrace
|
141
|
+
].join("\n # ")
|
142
|
+
end
|
143
|
+
|
144
|
+
def expecting_specific_exception?
|
145
|
+
@expected_error != Exception
|
146
|
+
end
|
147
|
+
|
148
|
+
def raise_message_already_set
|
149
|
+
raise "`expect { }.to raise_error(message).with_message(message)` is not valid. The matcher only allows the expected message to be specified once"
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|