json-schema-rspec 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 78897a15eee63f17fbe327166c1cc79a4cab7d98
4
- data.tar.gz: 365ee5bf15d49a0d4a8c534122570babd2669613
3
+ metadata.gz: cf7ece58f4187556a3ef1ec8d53ba2154518a57b
4
+ data.tar.gz: c05ef489dab2371dca40d13c4dba36cc623e2968
5
5
  SHA512:
6
- metadata.gz: 7bda5f7a71289c987070eef0212c03e12915cf8b5ac318a7c96c235a4f436a004279a1371dd1653f0695fb8f30a5c4d8ff4f00f6fd68dc9c1c892440538c564e
7
- data.tar.gz: 23f7b5a39d5c14036eb49063736cb0dc941b9097e5e0568a134411184850c439b5bfaf885beda9101e0d526b773faed99672ca7bd1464d44eb830fe3c5ca01bd
6
+ metadata.gz: 45bafafc48779ae37292c05209f497ce1be413acfac7888d9eb9b9b9918b89ef285a70450bd8de2c5bbc6cb691eba1f6a7a667b934a5a85c1efc2a9e7b633ca5
7
+ data.tar.gz: 74dcb86009906ac8edc7fdd72248f9c64be523b9d2107285a1a2db89358cca74d0ba601dc63c75f923616a721d612783d1dbebaed5c7ed7fa025c7a73eab9128
@@ -1,3 +1,9 @@
1
+ 0.0.3 / 2015-09-14
2
+ ==================
3
+
4
+ * Allow matcher class to be used as an argument matcher
5
+ * Update diff output to include helpful error information
6
+
1
7
  0.0.2 / 2015-05-06
2
8
  ==================
3
9
 
data/README.md CHANGED
@@ -28,15 +28,26 @@ In your `spec_helper.rb`:
28
28
  config.json_schemas[:my_schema] = "path/to/schema.json"
29
29
  #inline
30
30
  config.json_schemas[:inline_schema] = '{"type": "string"}'
31
-
31
+
32
32
  You can then write tests such as:
33
33
 
34
34
  #passing spec
35
35
  expect('"hello world"').to match_json_schema(:inline_schema)
36
-
36
+
37
37
  #failing spec
38
38
  expect('[1, 2, 3]').to match_json_schema(:inline_schema)
39
39
 
40
+ It also works as an argument matcher:
41
+
42
+ expect(some_object).to receive(:some_method).with(object_matching_schema(:inline_schema))
43
+
44
+ The argument matcher will match anything that the underlying JSON-Schema validator knows how to validate, so if you want to
45
+ limit your argument to a certain class, you'll need to add a compound matcher for that.
46
+
47
+ expect(some_object).to receive(:some_method)
48
+ .with(an_instance_of(String).and(object_matching_schema(:inline_schema)))
49
+
50
+
40
51
  #### Strict schema validation
41
52
 
42
53
  If you wish to use strict schema validation you can do so by passing an additional argument to the matcher.
@@ -46,7 +57,7 @@ those explicitly defined in your schema
46
57
  ```
47
58
  expect(response.body).to match_json_schema(:my_schema, strict: true)
48
59
  ```
49
-
60
+
50
61
  ### Schema in a file
51
62
  You can also use rails path utilities such as `Rails.root.join("spec/support/schemas/my_business_object.schema.json").to_s` when defining schema locations. This gem is backed by the [json-schema](http://github.com/hoxworth/json-schema) gem, so whatever that validator accepts for paths should work.
52
63
 
@@ -15,15 +15,10 @@ module JSON
15
15
 
16
16
  def matches?(actual)
17
17
  @actual = actual
18
+
18
19
  schema = schema_for_name(@schema_name)
19
20
  if schema.nil?
20
- @errors = [
21
- "No schema defined for #{@schema_name}",
22
- "Add a line to your RSpec.configure block to define the schema:",
23
- " RSpec.configure do |config|",
24
- " config.json_schemas[:my_remote_schema] = 'path/to/schema'",
25
- " config.json_schemas[:my_inline_schema] = '{\"json\": \"schema\"}'",
26
- " end"]
21
+ @errors = [ "No schema defined for #{@schema_name}. Available schemas are #{RSpec.configuration.json_schemas.keys}." ]
27
22
  return false
28
23
  end
29
24
  @errors = JSON::Validator.fully_validate(schema_for_name(@schema_name), @actual, @validation_opts)
@@ -36,6 +31,20 @@ module JSON
36
31
  end
37
32
  end
38
33
 
34
+ def inspect
35
+ if @errors.any?
36
+ failure_message
37
+ else
38
+ super
39
+ end
40
+ end
41
+
42
+ alias_method :to_s, :inspect
43
+
44
+ def ===(other)
45
+ matches?(other)
46
+ end
47
+
39
48
  def failure_message
40
49
  @errors.join("\n")
41
50
  end
@@ -45,7 +54,12 @@ module JSON
45
54
  end
46
55
 
47
56
  def description
48
- "match JSON schema identified by #{@schema_name}"
57
+ if @errors.any?
58
+ # ignore the preamble in a diff
59
+ @errors[1..-1].join("\n")
60
+ else
61
+ "match JSON schema identified by #{@schema_name}"
62
+ end
49
63
  end
50
64
 
51
65
  def schema_for_name(schema)
@@ -56,5 +70,7 @@ module JSON
56
70
  def match_json_schema(schema_name, validation_opts = {})
57
71
  MatchJsonSchemaMatcher.new(schema_name, validation_opts)
58
72
  end
73
+
74
+ alias_method :object_matching_schema, :match_json_schema
59
75
  end
60
76
  end
@@ -1,7 +1,7 @@
1
1
  module Json
2
2
  module Schema
3
3
  module Rspec
4
- VERSION = "0.0.2"
4
+ VERSION = "0.0.3"
5
5
  end
6
6
  end
7
7
  end
@@ -18,7 +18,57 @@ describe JSON::SchemaMatchers::MatchJsonSchemaMatcher do
18
18
  specify 'assigns a failure message' do
19
19
  matcher = match_json_schema(unconfigured_schema)
20
20
  expect(matcher.matches?(valid_json)).to eq(false)
21
- expect(matcher.failure_message).to match(/^No schema defined for #{unconfigured_schema}/)
21
+ expect(matcher.failure_message)
22
+ .to match(/^No schema defined for #{unconfigured_schema}/)
23
+ .and match(/Available schemas are/)
24
+ end
25
+ end
26
+
27
+ context 'when being used as a argument matcher' do
28
+ before :each do
29
+ RSpec.configuration.json_schemas[:inline_schema] = inline_schema
30
+ end
31
+
32
+ let(:dummy) { double }
33
+
34
+ # prevent the mock verify from saying we didn't call the method
35
+ # BECAUSE IT LIES
36
+ after(:each) { RSpec::Mocks.space.proxy_for(dummy).reset }
37
+
38
+ context 'when the argument is a hash' do
39
+ let(:inline_schema) { '{"type": "object", "properties": { "name" : { "type" : "string" } } }' }
40
+ let(:valid_hash) { { name: 'bob' } }
41
+
42
+ it 'still succeeds' do
43
+ expect(dummy).to receive(:method_call).with(object_matching_schema(:inline_schema))
44
+ dummy.method_call(valid_hash)
45
+ end
46
+
47
+ it 'can filter with compound matchers' do
48
+ expect {
49
+ expect(dummy).to receive(:method_call).with(an_instance_of(String).and(object_matching_schema(:inline_schema)))
50
+ dummy.method_call(valid_hash)
51
+ }.to fail_including("unexpected arguments")
52
+ end
53
+ end
54
+
55
+ it 'works as an argument matcher' do
56
+ expect(dummy).to receive(:method_call).with(object_matching_schema(:inline_schema))
57
+ dummy.method_call(valid_json)
58
+ end
59
+
60
+ it 'does something useful as an argument matcher when it does not match' do
61
+ expect {
62
+ expect(dummy).to receive(:method_call).with(object_matching_schema(:inline_schema))
63
+ dummy.method_call(invalid_json)
64
+ }.to fail_including("The property '#/' of type Hash did not match the following type: string in schema")
65
+ end
66
+
67
+ it 'can be used as a deeply nested matcher' do
68
+ expect {
69
+ expect(dummy).to receive(:method_call).with({a: 1, b: 2, c: object_matching_schema(:inline_schema)})
70
+ dummy.method_call(a:1, b:2, c: invalid_json)
71
+ }.to fail_including("The property '#/' of type Hash did not match the following type: string in schema")
22
72
  end
23
73
  end
24
74
 
@@ -1,5 +1,28 @@
1
1
  require 'json-schema-rspec'
2
2
 
3
+ # Copyright (c) 2012 David Chelimsky, Myron Marston
4
+ # Copyright (c) 2006 David Chelimsky, The RSpec Development Team
5
+ # Copyright (c) 2005 Steven Baker
6
+ # Shamelessly stolen from rspec-mocks
7
+ module RSpec
8
+ module ExpectationFailMatchers
9
+ def fail(&block)
10
+ raise_error(RSpec::Mocks::MockExpectationError, &block)
11
+ end
12
+
13
+ def fail_with(*args, &block)
14
+ raise_error(RSpec::Mocks::MockExpectationError, *args, &block)
15
+ end
16
+
17
+ def fail_including(*snippets)
18
+ raise_error(
19
+ RSpec::Mocks::MockExpectationError,
20
+ a_string_including(*snippets)
21
+ )
22
+ end
23
+ end
24
+ end
25
+
3
26
  Dir[File.expand_path("../../spec/support/**/*.rb",__FILE__)].each { |f| require f }
4
27
 
5
28
  RSpec.configure do |config|
@@ -11,4 +34,5 @@ RSpec.configure do |config|
11
34
  config.filter_run_excluding wip: true
12
35
 
13
36
  config.include JSON::SchemaMatchers
37
+ config.include RSpec::ExpectationFailMatchers
14
38
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-schema-rspec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sharethrough Engineering
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-05-06 00:00:00.000000000 Z
12
+ date: 2015-09-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -78,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
78
  version: '0'
79
79
  requirements: []
80
80
  rubyforge_project:
81
- rubygems_version: 2.2.2
81
+ rubygems_version: 2.4.8
82
82
  signing_key:
83
83
  specification_version: 4
84
84
  summary: JSON Schema RSpec matchers