Module: RSpec::ComposableJSONMatchers
- Defined in:
- lib/rspec/composable_json_matchers.rb,
lib/rspec/composable_json_matchers/be_json.rb,
lib/rspec/composable_json_matchers/version.rb,
lib/rspec/composable_json_matchers/configuration.rb
Overview
Mix-in module for the be_json
matcher.
Defined Under Namespace
Modules: Version Classes: BeJSON, Configuration
Class Method Summary collapse
-
.configuration ⇒ RSpec::ComposableJSONMatchers::Configuration
Returns the current configuration.
-
.configure {|config| ... }
Provides a block for configuring RSpec::ComposableJSONMatchers.
- .json_value?(object) ⇒ Boolean private
- .matcher?(object) ⇒ Boolean private
- .reset! ⇒ Object private
Instance Method Summary collapse
-
#be_json(matcher_or_json_value) ⇒ Object
Passes if actual string can be decoded as JSON and the decoded value passes the given matcher.
Class Method Details
.configuration ⇒ RSpec::ComposableJSONMatchers::Configuration
Returns the current configuration.
29 30 31 |
# File 'lib/rspec/composable_json_matchers.rb', line 29 def configuration @configuration ||= Configuration.new end |
.configure {|config| ... }
This method returns an undefined value.
Provides a block for configuring RSpec::ComposableJSONMatchers.
48 49 50 |
# File 'lib/rspec/composable_json_matchers.rb', line 48 def configure yield configuration end |
.json_value?(object) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/rspec/composable_json_matchers.rb', line 69 def json_value?(object) # value = false / null / true / object / array / number / string # https://www.rfc-editor.org/rfc/rfc8259.txt case object when Hash, Array, Numeric, String, TrueClass, FalseClass, NilClass true else false end end |
.matcher?(object) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
58 59 60 61 62 63 64 65 66 |
# File 'lib/rspec/composable_json_matchers.rb', line 58 def matcher?(object) begin return false if object.respond_to?(:i_respond_to_everything_so_im_not_really_a_matcher) rescue NoMethodError return false end object.respond_to?(:matches?) end |
.reset! ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
53 54 55 |
# File 'lib/rspec/composable_json_matchers.rb', line 53 def reset! @configuration = nil end |
Instance Method Details
#be_json(matcher_or_json_value) ⇒ Object
Passes if actual string can be decoded as JSON and the decoded value passes the given
matcher. When a JSON value is given, it's handled as be_json matching(value)
(matching
is an alias of the match
matcher).
99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rspec/composable_json_matchers.rb', line 99 def be_json(matcher_or_json_value) if ComposableJSONMatchers.matcher?(matcher_or_json_value) BeJSON.new(matcher_or_json_value, ComposableJSONMatchers.configuration) elsif ComposableJSONMatchers.json_value?(matcher_or_json_value) matcher = matching(matcher_or_json_value) BeJSON.new(matcher, ComposableJSONMatchers.configuration) else raise ArgumentError, 'You must pass a matcher or a JSON value ' \ '(hash, array, numeric, string, true, false, or nil) ' \ 'to `be_json` matcher.' end end |