rspec-json_matcher 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: aea2e2c2395eca489ac7a672f7cbdfdc22db91f6
4
+ data.tar.gz: d5bc11c37a503f0ff4b8db0c1e456c162825bb02
5
+ SHA512:
6
+ metadata.gz: 4ebf3089fd90e425dedfd883287ad02ffc94615312ec8fca156128a575a9d7a151f4f0416ff933082667fa2f0a595c5a4e91f6084f4ee35dac6b0b66f58e975f
7
+ data.tar.gz: 57b8cd3c74f36fb6f936e5244bd9bab12b8c179c01f49ae8d997661fabdfdc9e1d8c9647646fbd6802854af18d1f62ce9e2ce8eda57c6bf47be756418a1a5c29
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rspec-json_matcher.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Ryo Nakamura
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,63 @@
1
+ # RSpec::JsonMatcher
2
+ This library provides RSpec matcher for testing JSON string.
3
+
4
+ ## Usage
5
+ ```ruby
6
+ # spec/spec_helper.rb
7
+ RSpec.configuration.include RSpec::JsonMatcher
8
+ ```
9
+
10
+ ### Features
11
+ * parsing a given value as JSON string
12
+ * handy pattern matching with `#===` method like case-when statement
13
+ * supporting nested pattern matching
14
+
15
+ ### Example
16
+ ```ruby
17
+ [
18
+ {
19
+ "url" => "https://api.github.com/gists/17a07d0a27fd3f708f5f",
20
+ "id" => "1",
21
+ "description" => "description of gist",
22
+ "public" => true,
23
+ "user" => {
24
+ "login" => "octocat",
25
+ "id" => 1,
26
+ "avatar_url" => "https://github.com/images/error/octocat_happy.gif",
27
+ "gravatar_id" => "somehexcode",
28
+ "url" => "https://api.github.com/users/octocat"
29
+ },
30
+ "files" => {
31
+ "ring.erl" => {
32
+ "size" => 932,
33
+ "filename" => "ring.erl",
34
+ "raw_url" => "https://gist.github.com/raw/365370/8c4d2d43d178df44f4c03a7f2ac0ff512853564e/ring.erl"
35
+ }
36
+ },
37
+ "comments" => 0,
38
+ "comments_url" => "https://api.github.com/gists/6fb6af8cb6e26dbbc327/comments/",
39
+ "html_url" => "https://gist.github.com/1",
40
+ "git_pull_url" => "git://gist.github.com/1.git",
41
+ "git_push_url" => "git@gist.github.com:1.git",
42
+ "created_at" => "2010-04-14T02:15:15Z"
43
+ }
44
+ ].to_json.should be_json([
45
+ {
46
+ "url" => /^invalid pattern$/,
47
+ "id" => /^\d+$/,
48
+ "description" => /gist/,
49
+ "public" => true,
50
+ "user" => Hash,
51
+ "files" => Hash,
52
+ "comments" => Fixnum,
53
+ "comments_url" => /^https:/,
54
+ "html_url" => /^https:/,
55
+ "git_pull_url" => /^git:/,
56
+ "git_push_url" => /^git@/,
57
+ "created_at" => /^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\dZ/,
58
+ }
59
+ ])
60
+ ```
61
+
62
+ ### Failure message
63
+ ![](http://dl.dropbox.com/u/5978869/image/20130426_005849.png)
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,125 @@
1
+ require "rspec/json_matcher/version"
2
+ require "json"
3
+ require "awesome_print"
4
+
5
+ module RSpec
6
+ module JsonMatcher
7
+ def be_json(expected = nil)
8
+ Matcher.new(expected)
9
+ end
10
+
11
+ class Matcher
12
+ attr_reader :expected
13
+
14
+ def initialize(expected = nil)
15
+ @expected = expected
16
+ end
17
+
18
+ def matches?(json)
19
+ @parsed = JSON.parse(json)
20
+ if has_expectation?
21
+ Comparer.compare(@parsed, expected)
22
+ else
23
+ true
24
+ end
25
+ rescue JSON::ParserError
26
+ @parser_error = true
27
+ false
28
+ end
29
+
30
+ def description
31
+ "be JSON"
32
+ end
33
+
34
+ def failure_message_for_should
35
+ if has_parser_error?
36
+ "expected value to be parsed as JSON, but failed"
37
+ else
38
+ inspection
39
+ end
40
+ end
41
+
42
+ def failure_message_for_should_not
43
+ if has_parser_error?
44
+ "expected value not to be parsed as JSON, but succeeded"
45
+ else
46
+ inspection("not ")
47
+ end
48
+ end
49
+
50
+ private
51
+
52
+ def has_parser_error?
53
+ !!@parser_error
54
+ end
55
+
56
+ def has_expectation?
57
+ !!expected
58
+ end
59
+
60
+ def inspection(prefix = nil)
61
+ ["expected #{prefix}to match:", @expected.ai, "", "actual:", @parsed.ai].join("\n")
62
+ end
63
+ end
64
+
65
+ class Comparer
66
+ attr_reader :actual, :expected
67
+
68
+ def self.compare(*args)
69
+ new(*args).compare
70
+ end
71
+
72
+ def initialize(actual, expected)
73
+ @actual = actual
74
+ @expected = expected
75
+ end
76
+
77
+ def compare
78
+ has_same_value?
79
+ end
80
+
81
+ private
82
+
83
+ def has_same_class?
84
+ actual.class == expected.class
85
+ end
86
+
87
+ def has_same_size?
88
+ actual.size == expected.size
89
+ end
90
+
91
+ def has_same_value?
92
+ has_equal_value? || has_same_collection?
93
+ end
94
+
95
+ def has_equal_value?
96
+ if expected.respond_to?(:call)
97
+ expected.call(actual)
98
+ else
99
+ expected === actual
100
+ end
101
+ end
102
+
103
+ def has_same_collection?
104
+ collection? && has_same_class? && has_same_size? && has_same_key_values?
105
+ end
106
+
107
+ def has_same_key_values?
108
+ keys.all? {|key| self.class.compare(actual[key], expected[key]) }
109
+ end
110
+
111
+ def collection?
112
+ actual.is_a?(Array) || actual.is_a?(Hash)
113
+ end
114
+
115
+ def keys
116
+ case actual
117
+ when Array
118
+ actual.each_index
119
+ when Hash
120
+ actual.keys
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,5 @@
1
+ module RSpec
2
+ module JsonMatcher
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "rspec/json_matcher/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rspec-json_matcher"
8
+ spec.version = RSpec::JsonMatcher::VERSION
9
+ spec.authors = ["Ryo Nakamura"]
10
+ spec.email = ["r7kamura@gmail.com"]
11
+ spec.description = "This library provides RSpec matcher for testing JSON string."
12
+ spec.summary = "RSpec matcher for testing JSON string"
13
+ spec.homepage = "https://github.com/r7kamura/rspec-json_matcher"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "json"
22
+ spec.add_dependency "awesome_print"
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ end
@@ -0,0 +1,57 @@
1
+ require "spec_helper"
2
+
3
+ describe RSpec::JsonMatcher do
4
+ context "with invalid JSON" do
5
+ it "does match" do
6
+ "".should_not be_json
7
+ end
8
+ end
9
+
10
+ context "with valid JSON" do
11
+ it "matches with handy patterns" do
12
+ [
13
+ {
14
+ "url" => "https://api.github.com/gists/17a07d0a27fd3f708f5f",
15
+ "id" => "1",
16
+ "description" => "description of gist",
17
+ "public" => true,
18
+ "user" => {
19
+ "login" => "octocat",
20
+ "id" => 1,
21
+ "avatar_url" => "https://github.com/images/error/octocat_happy.gif",
22
+ "gravatar_id" => "somehexcode",
23
+ "url" => "https://api.github.com/users/octocat"
24
+ },
25
+ "files" => {
26
+ "ring.erl" => {
27
+ "size" => 932,
28
+ "filename" => "ring.erl",
29
+ "raw_url" => "https://gist.github.com/raw/365370/8c4d2d43d178df44f4c03a7f2ac0ff512853564e/ring.erl"
30
+ }
31
+ },
32
+ "comments" => 0,
33
+ "comments_url" => "https://api.github.com/gists/6fb6af8cb6e26dbbc327/comments/",
34
+ "html_url" => "https://gist.github.com/1",
35
+ "git_pull_url" => "git://gist.github.com/1.git",
36
+ "git_push_url" => "git@gist.github.com:1.git",
37
+ "created_at" => "2010-04-14T02:15:15Z"
38
+ }
39
+ ].to_json.should be_json([
40
+ {
41
+ "url" => /^invalid pattern$/,
42
+ "id" => /^\d+$/,
43
+ "description" => /gist/,
44
+ "public" => true,
45
+ "user" => Hash,
46
+ "files" => Hash,
47
+ "comments" => Fixnum,
48
+ "comments_url" => /^https:/,
49
+ "html_url" => /^https:/,
50
+ "git_pull_url" => /^git:/,
51
+ "git_push_url" => /^git@/,
52
+ "created_at" => /^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\dZ/,
53
+ }
54
+ ])
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,8 @@
1
+ require File.expand_path("../../lib/rspec/json_matcher", __FILE__)
2
+
3
+ RSpec.configure do |config|
4
+ config.treat_symbols_as_metadata_keys_with_true_values = true
5
+ config.run_all_when_everything_filtered = true
6
+ config.filter_run :focus
7
+ config.include RSpec::JsonMatcher
8
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-json_matcher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ryo Nakamura
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-04-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: awesome_print
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: This library provides RSpec matcher for testing JSON string.
84
+ email:
85
+ - r7kamura@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - lib/rspec/json_matcher.rb
96
+ - lib/rspec/json_matcher/version.rb
97
+ - rspec-json_matcher.gemspec
98
+ - spec/rspec/json_matcher_spec.rb
99
+ - spec/spec_helper.rb
100
+ homepage: https://github.com/r7kamura/rspec-json_matcher
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.0.0
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: RSpec matcher for testing JSON string
124
+ test_files:
125
+ - spec/rspec/json_matcher_spec.rb
126
+ - spec/spec_helper.rb
127
+ has_rdoc: