peppers-ghost 0.0.0

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: 8c4990710319dbcc755fa8e9651b277ea26c94db
4
+ data.tar.gz: a39e7dca88220240fdd6be33c1210fc2baac987b
5
+ SHA512:
6
+ metadata.gz: c2fda7ce252b9959861633268deb497e3b47c86efb73c93b796ee51eea3add569f0c2f17efb94e7e90c786bf7ac81697c52cc7d872f92be0291046e3ae2886db
7
+ data.tar.gz: 78a728fe87cd8f04015531f33343d6d0486e26dccecb1b617ac5385584d392b6cd818a87d110e7ad95e6c3a008fbb46987f071ea4d0f2520176b717e0c83d71e
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,24 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ peppers-ghost (0.0.0)
5
+ rspec
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.2.4)
11
+ rspec (2.14.1)
12
+ rspec-core (~> 2.14.0)
13
+ rspec-expectations (~> 2.14.0)
14
+ rspec-mocks (~> 2.14.0)
15
+ rspec-core (2.14.4)
16
+ rspec-expectations (2.14.0)
17
+ diff-lcs (>= 1.1.3, < 2.0)
18
+ rspec-mocks (2.14.2)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ peppers-ghost!
@@ -0,0 +1,25 @@
1
+ require 'rspec'
2
+
3
+ module PeppersGhost
4
+ end
5
+
6
+ require_relative 'peppers-ghost/core'
7
+
8
+ require_relative 'peppers-ghost/ghost'
9
+ require_relative 'peppers-ghost/ghost_factory'
10
+
11
+ require_relative 'peppers-ghost/shared/json_context'
12
+ require_relative 'peppers-ghost/shared/list_example'
13
+
14
+ require_relative 'peppers-ghost/matchers'
15
+ require_relative 'peppers-ghost/matchers/match_structure_of'
16
+ require_relative 'peppers-ghost/matchers/match_resource'
17
+
18
+ module PeppersGhost
19
+ extend PeppersGhost::GhostFactory
20
+ end
21
+
22
+ RSpec.configure do |config|
23
+ config.include PeppersGhost::Core
24
+ config.include PeppersGhost::Matchers
25
+ end
@@ -0,0 +1,58 @@
1
+ module PeppersGhost::Core
2
+ def match_resource_type(json_resource, resource, attributes, strict = false)
3
+ match_structure json_resource, attributes
4
+ match_values json_resource, resource, attributes, strict
5
+ end
6
+
7
+ def match_structure(json_resource, attributes, count = nil, method = nil)
8
+ method ||= :resource
9
+ expect(json_resource).to_not be_nil
10
+
11
+ if json_resource.is_a?(Array)
12
+ expect(json_resource).to have(count).method_missing(method) if count
13
+ match_single_structure json_resource, attributes, count
14
+ else
15
+ expect(json_resource.keys).to match_array attributes.map(&:to_s)
16
+ end
17
+ end
18
+
19
+ def match_values(json_resource, resource, attributes, strict = false)
20
+ if json_resource.is_a?(Array)
21
+ expect(json_resource).to have(1).resource
22
+ json_resource = json_resource.first
23
+ end
24
+ match_attributes json_resource, resource, attributes, strict
25
+ end
26
+
27
+ private
28
+
29
+ def match_single_structure(json_resources, attributes, count)
30
+ json_resources.each do |json_resource|
31
+ match_structure json_resource, attributes
32
+ end
33
+ end
34
+
35
+ def match_attributes(json_resource, resource, attributes, strict = false)
36
+ attributes.each do |attribute|
37
+ next unless resource.respond_to?(attribute) or
38
+ (resource.is_a?(Hash) and
39
+ (resource.has_key?(attribute.to_s) or resource.has_key?(attribute.to_sym)))
40
+
41
+ value =
42
+ if resource.respond_to?(attribute)
43
+ resource.send attribute
44
+ elsif resource.is_a? Hash
45
+ resource[attribute.to_s] || resource[attribute.to_sym]
46
+ end
47
+
48
+ json_value = json_resource[attribute.to_s]
49
+
50
+ match_attribute json_value, value, strict
51
+ end
52
+ end
53
+
54
+ def match_attribute(json_value, value, strict = false)
55
+ expect(value).to_not be_nil if strict
56
+ expect(json_value).to eq value
57
+ end
58
+ end
@@ -0,0 +1,9 @@
1
+ class PeppersGhost::Ghost
2
+ def attributes
3
+ @attributes ||= []
4
+ end
5
+
6
+ def method_missing(method, *args, &block)
7
+ attributes << method
8
+ end
9
+ end
@@ -0,0 +1,21 @@
1
+ module PeppersGhost::GhostFactory
2
+ def define(&block)
3
+ instance_eval &block
4
+ end
5
+
6
+ def ghost_for(resource)
7
+ ghosts[resource]
8
+ end
9
+
10
+ def ghost(name, &block)
11
+ ghost = PeppersGhost::Ghost.new
12
+ ghost.instance_eval &block
13
+ ghosts[name] = ghost
14
+ end
15
+
16
+ private
17
+
18
+ def ghosts
19
+ @ghosts ||= {}
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ module PeppersGhost
2
+ module Matchers
3
+ extend RSpec::Matchers::DSL
4
+ end
5
+ end
@@ -0,0 +1,21 @@
1
+ module PeppersGhost::Matchers
2
+ matcher :match_resource do |expected, type|
3
+ chain :strictly do
4
+ @strict = true
5
+ end
6
+
7
+ match do |actual|
8
+ begin
9
+ resource = PeppersGhost.ghost_for(type)
10
+ match_resource_type actual, expected, resource.attributes, @strict
11
+ rescue RSpec::Expectations::ExpectationNotMetError => e
12
+ @error = e.message
13
+ false
14
+ end
15
+ end
16
+
17
+ failure_message_for_should do
18
+ @error
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,30 @@
1
+ module PeppersGhost::Matchers
2
+ matcher :match_structure_of do |expected|
3
+ chain :with do |count|
4
+ @count = count
5
+ end
6
+
7
+ match do |actual|
8
+ begin
9
+ resource = PeppersGhost.ghost_for(expected)
10
+ match_structure actual, resource.attributes, @count, @method
11
+ rescue RSpec::Expectations::ExpectationNotMetError => e
12
+ @error = e.message
13
+ false
14
+ end
15
+ end
16
+
17
+ failure_message_for_should do
18
+ @error
19
+ end
20
+
21
+ def method_missing(method, *args, &block)
22
+ if defined?(@method)
23
+ super
24
+ else
25
+ @method = method
26
+ end
27
+ self
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,5 @@
1
+ shared_context 'json' do
2
+ let(:json) { JSON last_response.body }
3
+ let(:root_key) { json.keys.reject { |k| k == 'meta' }.first }
4
+ let(:json_root) { json[root_key] }
5
+ end
@@ -0,0 +1,7 @@
1
+ shared_examples_for 'a list of' do |type, count|
2
+ include_context 'json'
3
+
4
+ it "returns a list of #{type}" do
5
+ expect(json_root).to match_structure_of(type).with(count).method_missing(type)
6
+ end
7
+ end
@@ -0,0 +1,23 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'peppers-ghost'
3
+ s.version = '0.0.0'
4
+
5
+ s.summary = 'Adds some helpers for testing api resources'
6
+ s.description = %Q{}.strip
7
+
8
+ s.author = 'Travis Herrick'
9
+ s.email = 'tthetoad@gmail.com'
10
+ s.homepage = 'http://www.bitbucket.org/ToadJamb/peppers-ghost'
11
+
12
+ s.license = 'LGPLv3'
13
+
14
+ s.extra_rdoc_files = Dir['README', 'license/*']
15
+
16
+ s.require_paths = ['lib']
17
+ s.files = Dir['*', 'lib/**/*.rb', 'license/*']
18
+ s.test_files = Dir['spec/**/*.rb']
19
+
20
+ s.add_dependency 'rspec'
21
+
22
+ s.has_rdoc = false
23
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: peppers-ghost
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Travis Herrick
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
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
+ description: ''
28
+ email: tthetoad@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - Gemfile
34
+ - Gemfile.lock
35
+ - peppers-ghost.gemspec
36
+ - lib/peppers-ghost/core.rb
37
+ - lib/peppers-ghost/ghost.rb
38
+ - lib/peppers-ghost/ghost_factory.rb
39
+ - lib/peppers-ghost/matchers/match_resource.rb
40
+ - lib/peppers-ghost/matchers/match_structure_of.rb
41
+ - lib/peppers-ghost/matchers.rb
42
+ - lib/peppers-ghost/shared/json_context.rb
43
+ - lib/peppers-ghost/shared/list_example.rb
44
+ - lib/peppers-ghost.rb
45
+ homepage: http://www.bitbucket.org/ToadJamb/peppers-ghost
46
+ licenses:
47
+ - LGPLv3
48
+ metadata: {}
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 2.0.3
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Adds some helpers for testing api resources
69
+ test_files: []