like-structure 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +8 -0
- data/Gemfile +4 -0
- data/README +0 -0
- data/Rakefile +2 -0
- data/features/like_method.feature +106 -0
- data/features/step_definitions/copied_from_rspec.rb +5 -0
- data/features/support/env.rb +6 -0
- data/lib/like_structure.rb +2 -0
- data/lib/like_structure/extensions.rb +32 -0
- data/lib/like_structure/version.rb +3 -0
- data/lib/like_structure/wildcard.rb +50 -0
- data/like_structure.gemspec +25 -0
- data/spec/spec_helper.rb +29 -0
- metadata +98 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README
ADDED
File without changes
|
data/Rakefile
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
Feature: Be Like Matcher
|
2
|
+
In order to check whether data structure matches given criteria
|
3
|
+
I want to test it against some reference structure
|
4
|
+
Which may contain keywords like anything, any_array and so on.
|
5
|
+
|
6
|
+
Scenario: Very Boring Example, Testing Against Exact Simple Object
|
7
|
+
Given a file named "simple_spec.rb" with:
|
8
|
+
"""
|
9
|
+
require "spec_helper"
|
10
|
+
|
11
|
+
describe "nil" do
|
12
|
+
subject{ nil }
|
13
|
+
it{ should be_like nil }
|
14
|
+
it{ should_not be_like [] }
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "A string which is a simple element" do
|
18
|
+
subject{ "Once upon a midnight dreary, while I pondered weak and weary," }
|
19
|
+
it{ should be_like "Once upon a midnight dreary, while I pondered weak and weary," }
|
20
|
+
it{ should_not be_like nil }
|
21
|
+
end
|
22
|
+
"""
|
23
|
+
When I run `rspec simple_spec.rb`
|
24
|
+
Then the examples should all pass
|
25
|
+
|
26
|
+
Scenario: Using any* matchers
|
27
|
+
Given a file named "anystar_spec.rb" with:
|
28
|
+
"""
|
29
|
+
require "spec_helper"
|
30
|
+
|
31
|
+
describe "A string" do
|
32
|
+
subject{ "Quoth the raven, 'Nevermore.'" }
|
33
|
+
it{ should be_like Wildcard.any_string }
|
34
|
+
it{ should_not be_like "any_string" }
|
35
|
+
it{ should_not be_like Wildcard.any_array }
|
36
|
+
it{ should be_like Wildcard.anything }
|
37
|
+
it{ should be_like Wildcard.not_nil }
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "nil" do
|
41
|
+
subject{ nil }
|
42
|
+
it{ should_not be_like Wildcard.any_array }
|
43
|
+
it{ should be_like Wildcard.anything }
|
44
|
+
it{ should_not be_like Wildcard.not_nil }
|
45
|
+
end
|
46
|
+
"""
|
47
|
+
When I run `rspec anystar_spec.rb`
|
48
|
+
Then the examples should all pass
|
49
|
+
|
50
|
+
Scenario: A Complicated Structure Of Arrays
|
51
|
+
Given a file named "arrays_spec.rb" with:
|
52
|
+
"""
|
53
|
+
require "spec_helper"
|
54
|
+
|
55
|
+
describe "An array containing strings and arrays" do
|
56
|
+
subject{ [["black", "cat"], nil, [["edgar", "alan"], "poe"]] }
|
57
|
+
it{ should be_like Wildcard.any_array }
|
58
|
+
it{ should be_like Wildcard.anything }
|
59
|
+
it{ should_not be_like Wildcard.any_hash }
|
60
|
+
it{ should be_like [Wildcard.anything, Wildcard.anything, Wildcard.anything] }
|
61
|
+
it{ should_not be_like [Wildcard.anything, Wildcard.anything, Wildcard.anything, Wildcard.anything] }
|
62
|
+
it{ should be_like [Wildcard.not_nil, nil, Wildcard.not_nil] }
|
63
|
+
it{ should be_like [Wildcard.any_array, nil, Wildcard.any_array] }
|
64
|
+
it{ should_not be_like [["cat", "black"], nil, Wildcard.any_array] }
|
65
|
+
it{ should be_like [Wildcard.anything, nil, [Wildcard.any_array, Wildcard.any_string]] }
|
66
|
+
it{ should be_like [["black", "cat"], nil, [Wildcard.any_array, "poe"]] }
|
67
|
+
it{ should be_like [["black", "cat"], nil, [["edgar", "alan"], "poe"]] }
|
68
|
+
it{ should_not be_like [["black", "cat"], nil, ["edgar", "alan", "poe"]] }
|
69
|
+
end
|
70
|
+
"""
|
71
|
+
When I run `rspec arrays_spec.rb`
|
72
|
+
Then the examples should all pass
|
73
|
+
|
74
|
+
Scenario: A Complicated Structure Of Hashes
|
75
|
+
Given a file named "hashes_spec.rb" with:
|
76
|
+
"""
|
77
|
+
require "spec_helper"
|
78
|
+
|
79
|
+
describe "A hash of hashes" do
|
80
|
+
subject{ {"edgar" => {"alan" => "poe"}, {"black" => "cat"} => {"is" => {"one of" => "works"}}, "this should point to" => nil} }
|
81
|
+
it{ should be_like Wildcard.anything }
|
82
|
+
it{ should be_like Wildcard.any_hash }
|
83
|
+
it{ should be_like({"edgar" => Wildcard.anything, {"black" => "cat"} => Wildcard.anything, "this should point to" => Wildcard.anything})}
|
84
|
+
it{ should be_like({"edgar" => Wildcard.not_nil, {"black" => "cat"} => Wildcard.not_nil, "this should point to" => nil})}
|
85
|
+
it{ should be_like({"edgar" => Wildcard.any_hash, {"black" => "cat"} => {"is" => Wildcard.any_hash}, "this should point to" => nil})}
|
86
|
+
it{ should be_like({"edgar" => Wildcard.any_hash, {"black" => "cat"} => {"is" => Wildcard.any_hash}, "this should point to" => nil})}
|
87
|
+
it{ should be_like({"edgar" => {"alan" => "poe"}, {"black" => "cat"} => {"is" => {"one of" => "works"}}, "this should point to" => nil})}
|
88
|
+
end
|
89
|
+
"""
|
90
|
+
When I run `rspec hashes_spec.rb`
|
91
|
+
Then the examples should all pass
|
92
|
+
|
93
|
+
Scenario: Wildcards In Hash Keys Does Not Work (Yet)
|
94
|
+
Given a file named "wildcards_in_hashes_spec.rb" with:
|
95
|
+
"""
|
96
|
+
require "spec_helper"
|
97
|
+
|
98
|
+
describe "A hash with wildcard in key" do
|
99
|
+
subject{ {"edgar" => "raven"} }
|
100
|
+
it{ should_not be_like({Wildcard.anything => "raven"}) }
|
101
|
+
it{ should be_like({"edgar" => Wildcard.anything}) }
|
102
|
+
it{ should_not be_like({Wildcard.anything => Wildcard.anything}) }
|
103
|
+
end
|
104
|
+
"""
|
105
|
+
When I run `rspec wildcards_in_hashes_spec.rb`
|
106
|
+
Then the examples should all pass
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'set'
|
2
|
+
class Object
|
3
|
+
def like?(pattern)
|
4
|
+
pattern.reverse_like?(self)
|
5
|
+
end
|
6
|
+
def reverse_like?(actual)
|
7
|
+
actual == self
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class Array
|
12
|
+
def reverse_like?(actual)
|
13
|
+
return false unless actual.kind_of?(Array) && actual.size == self.size
|
14
|
+
[actual, self].transpose.each do |actual_item, expected_item|
|
15
|
+
return false unless actual_item.like?(expected_item)
|
16
|
+
end
|
17
|
+
true
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class Hash
|
22
|
+
def reverse_like?(actual)
|
23
|
+
# expected_keys_wildcards, expected_keys_exact = self.keys.partition{ |k| k.kind_of?(Wildcard) } #TODO wildcards in keys
|
24
|
+
expected_keys_exact = self.keys
|
25
|
+
actual_keys = Set[*actual.keys]
|
26
|
+
return false unless expected_keys_exact.size == actual_keys.size
|
27
|
+
expected_keys_exact.each do |key|
|
28
|
+
return false unless actual_keys.delete?(key) && actual[key].like?(self[key])
|
29
|
+
end
|
30
|
+
true
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Wildcard
|
2
|
+
class Wildcard ; end
|
3
|
+
|
4
|
+
class TypeWildcard < Wildcard
|
5
|
+
def initialize(type)
|
6
|
+
@type = type
|
7
|
+
end
|
8
|
+
|
9
|
+
def reverse_like?(actual)
|
10
|
+
actual.kind_of?(@type)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class AnythingWildcard < Wildcard
|
15
|
+
def initialize(allow_nil)
|
16
|
+
@allow_nil = allow_nil
|
17
|
+
end
|
18
|
+
def reverse_like?(actual)
|
19
|
+
@allow_nil || !actual.nil?
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.anything
|
24
|
+
AnythingWildcard.new(true)
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.not_nil
|
28
|
+
AnythingWildcard.new(false)
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.respond_to? name, include_private = false
|
32
|
+
analyze_method_name(name) ? true : super
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.method_missing name, *args, &block
|
36
|
+
args.empty? && block.nil? && analyze_method_name(name) or super
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.analyze_method_name(name)
|
40
|
+
case name.to_s
|
41
|
+
when /\Aany_(\w+)\Z/
|
42
|
+
type_name = $1.capitalize
|
43
|
+
return false unless Object.const_defined?(type_name)
|
44
|
+
TypeWildcard.new(Object.const_get(type_name))
|
45
|
+
else
|
46
|
+
false
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "like_structure/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "like-structure"
|
7
|
+
s.version = LikeStructure::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Sebastian Skalacki"]
|
10
|
+
s.email = ["skalee@gmail.com"]
|
11
|
+
s.homepage = "https://github.com/skalee/be_like"
|
12
|
+
s.summary = %q{Like RegExp but for arrays, hashes.}
|
13
|
+
s.description = %q{Allows for matching data structures against given criteria. Very useful for testing JSON responses but may be used anywhere.}
|
14
|
+
|
15
|
+
# s.rubyforge_project = "be_like"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
|
22
|
+
s.add_development_dependency 'aruba', "~> 0.3.5"
|
23
|
+
s.add_development_dependency 'rspec-core', "~>2.0"
|
24
|
+
s.add_development_dependency 'cucumber', "~>0.10.2"
|
25
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#copied from https://github.com/rspec/rspec-expectations/blob/master/spec/spec_helper.rb
|
2
|
+
def add_to_load_path(path, prepend=false)
|
3
|
+
path = File.expand_path("../../#{path}/lib", __FILE__)
|
4
|
+
if prepend
|
5
|
+
$LOAD_PATH.unshift(path) unless $LOAD_PATH.include?(path)
|
6
|
+
else
|
7
|
+
$LOAD_PATH << path unless $LOAD_PATH.include?(path)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
require 'test/unit'
|
12
|
+
|
13
|
+
add_to_load_path("rspec-expectations", :prepend)
|
14
|
+
add_to_load_path("rspec-core")
|
15
|
+
add_to_load_path("rspec-mocks")
|
16
|
+
|
17
|
+
require 'rspec/expectations'
|
18
|
+
require 'rspec/core'
|
19
|
+
require 'rspec/mocks'
|
20
|
+
|
21
|
+
Dir['./spec/support/**/*'].each {|f| require f}
|
22
|
+
|
23
|
+
RSpec::configure do |config|
|
24
|
+
config.color_enabled = true
|
25
|
+
config.filter_run :focused => true
|
26
|
+
config.run_all_when_everything_filtered = true
|
27
|
+
end
|
28
|
+
|
29
|
+
require 'like_structure'
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: like-structure
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Sebastian Skalacki
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-04-18 00:00:00.000000000 +02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: aruba
|
17
|
+
requirement: &2153366740 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ~>
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 0.3.5
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *2153366740
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rspec-core
|
28
|
+
requirement: &2153366240 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *2153366240
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: cucumber
|
39
|
+
requirement: &2164540680 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: 0.10.2
|
45
|
+
type: :development
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *2164540680
|
48
|
+
description: Allows for matching data structures against given criteria. Very useful
|
49
|
+
for testing JSON responses but may be used anywhere.
|
50
|
+
email:
|
51
|
+
- skalee@gmail.com
|
52
|
+
executables: []
|
53
|
+
extensions: []
|
54
|
+
extra_rdoc_files: []
|
55
|
+
files:
|
56
|
+
- .gitignore
|
57
|
+
- Gemfile
|
58
|
+
- README
|
59
|
+
- Rakefile
|
60
|
+
- features/like_method.feature
|
61
|
+
- features/step_definitions/copied_from_rspec.rb
|
62
|
+
- features/support/env.rb
|
63
|
+
- lib/like_structure.rb
|
64
|
+
- lib/like_structure/extensions.rb
|
65
|
+
- lib/like_structure/version.rb
|
66
|
+
- lib/like_structure/wildcard.rb
|
67
|
+
- like_structure.gemspec
|
68
|
+
- spec/spec_helper.rb
|
69
|
+
has_rdoc: true
|
70
|
+
homepage: https://github.com/skalee/be_like
|
71
|
+
licenses: []
|
72
|
+
post_install_message:
|
73
|
+
rdoc_options: []
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
requirements: []
|
89
|
+
rubyforge_project:
|
90
|
+
rubygems_version: 1.5.2
|
91
|
+
signing_key:
|
92
|
+
specification_version: 3
|
93
|
+
summary: Like RegExp but for arrays, hashes.
|
94
|
+
test_files:
|
95
|
+
- features/like_method.feature
|
96
|
+
- features/step_definitions/copied_from_rspec.rb
|
97
|
+
- features/support/env.rb
|
98
|
+
- spec/spec_helper.rb
|