wildcard_matchers 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.1.5
2
+ * ENHANCEMENT
3
+ * add without_query! modifier to with_uri_template matcher
4
+
1
5
  ## 0.1.4
2
6
  * ENHANCEMENT
3
7
  * with_uri_template matcher
data/README.md CHANGED
@@ -33,6 +33,8 @@ See specs, for more detail.
33
33
  * with_uri_template
34
34
  * with_uri_template("http://example.com/users/{id}", "id" => "1") === "http://example.com/users/1" #=> true
35
35
  * with_uri_template("http://example.com/users{?id}", "id" => "1") === "http://example.com/users?id=1" #=> true
36
+ * with_uri_template and witout_query!
37
+ * with_uri_template("http://example.com/users", "id" => "1").without_query! === "http://example.com/users?id=1" #=> true
36
38
  * hash_includes
37
39
  * hash_includes(:a) === { :a => 1 } #=> true
38
40
  * hash_includes(:b) === { :a => 1 } #=> false
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.4
1
+ 0.1.5
@@ -1,3 +1,5 @@
1
+ require "addressable/uri"
2
+
1
3
  module WildcardMatchers
2
4
  module Matchers
3
5
  def is_uri(hash = {})
@@ -8,18 +10,10 @@ module WildcardMatchers
8
10
  protected
9
11
  def wildcard_match(actual)
10
12
  unless actual
11
- errors.push "#{position}: expect uri but nil"
12
- return
13
+ return errors.push "#{position}: expect URI but nil"
13
14
  end
14
15
 
15
- uri = nil
16
- begin
17
- require "addressable/uri"
18
- uri = ::Addressable::URI.parse(actual) # if actual is ::URI re-parse
19
- rescue LoadError
20
- require "uri"
21
- uri = actual.is_a?(::URI) ? actual : ::URI.parse(actual)
22
- end
16
+ uri = ::Addressable::URI.parse(actual) # if actual is ::URI re-parse
23
17
 
24
18
  expectation.each do |key, value|
25
19
  errors.push(*self.class.superclass.check_errors(uri.__send__(key), value, position + "[#{key.inspect}]"))
@@ -14,11 +14,40 @@ module WildcardMatchers
14
14
  @position = position
15
15
  end
16
16
 
17
+ def without_query!
18
+ @without_query = true
19
+
20
+ # umhhh
21
+ uri = ::Addressable::URI.parse(@template.pattern.split("{?").first)
22
+
23
+ @template_without_query = ::Addressable::Template.new(uri.omit(:query).to_s)
24
+
25
+ self
26
+ end
27
+
17
28
  protected
18
29
  def wildcard_match(actual)
19
- extracted = @template.extract(actual)
30
+ unless actual
31
+ return errors.push("#{position}: expect URI but nil")
32
+ end
33
+
34
+ if @without_query
35
+ uri = ::Addressable::URI.parse(actual)
36
+
37
+ params = uri.query_values || {}
38
+
39
+ unless extracted = @template_without_query.extract(uri.omit!(:query).to_s)
40
+ return errors.push("#{position}: expect #{uri.to_s} to match #{@template_without_query.pattern}")
41
+ end
42
+
43
+ errors.push(*self.class.superclass.check_errors(extracted.merge(params), expectation, position))
44
+ else
45
+ unless extracted = @template.extract(actual)
46
+ return errors.push("#{position}: expect #{actual} to match #{@template.pattern}")
47
+ end
20
48
 
21
- errors.push(*self.class.superclass.check_errors(extracted, expectation, position))
49
+ errors.push(*self.class.superclass.check_errors(extracted, expectation, position))
50
+ end
22
51
  end
23
52
  end
24
53
  end
data/spec/spec_helper.rb CHANGED
@@ -15,4 +15,6 @@ end
15
15
  # global debug function
16
16
  # usage:
17
17
  # wildcard_match?(actual, expected, &$debug)
18
- $debug = proc {|errors| puts errors }
18
+ def debugger
19
+ ENV["DEBUG"] ? proc {|errors| puts errors } : nil
20
+ end
@@ -1,25 +1,25 @@
1
1
  shared_examples_for "wildcard match" do |actual, matcher, *args|
2
- expected = matcher.inspect + (args.size > 0 ? "(#{args.map(&:inspect).join(", ")})" : "")
2
+ expected = matcher.to_s + (args.size > 0 ? "(#{args.map(&:inspect).join(", ")})" : "")
3
3
 
4
4
  it "#{actual.inspect} with #{expected}" do
5
5
  if matcher.is_a?(Symbol) and WildcardMatchers.respond_to?(matcher)
6
6
  # Note: some symbol comes here and may fail
7
- wildcard_match?(actual, send(matcher, *args)).should be_true
7
+ wildcard_match?(actual, send(matcher, *args), &debugger).should be_true
8
8
  else
9
- wildcard_match?(actual, matcher).should be_true
9
+ wildcard_match?(actual, matcher, &debugger).should be_true
10
10
  end
11
11
  end
12
12
  end
13
13
 
14
14
  shared_examples_for "not wildcard match" do |actual, matcher, *args|
15
- expected = matcher.inspect + (args.size > 0 ? "(#{args.map(&:inspect).join(", ")})" : "")
15
+ expected = matcher.to_s + (args.size > 0 ? "(#{args.map(&:inspect).join(", ")})" : "")
16
16
 
17
17
  it "#{actual.inspect} with #{expected}" do
18
18
  if matcher.is_a?(Symbol) and WildcardMatchers.respond_to?(matcher)
19
19
  # Note: some symbol comes here and may fail
20
- wildcard_match?(actual, send(matcher, *args)).should be_false
20
+ wildcard_match?(actual, send(matcher, *args), &debugger).should be_false
21
21
  else
22
- wildcard_match?(actual, matcher).should be_false
22
+ wildcard_match?(actual, matcher, &debugger).should be_false
23
23
  end
24
24
  end
25
25
  end
@@ -38,9 +38,9 @@ shared_examples_for "wildcard match with helper" do |actual, helper, matcher, *a
38
38
  it "#{actual.inspect} with #{expected}" do
39
39
  if matcher.is_a?(Symbol) and WildcardMatchers.respond_to?(matcher)
40
40
  # Note: some symbol comes here and may fail
41
- wildcard_match?(actual, send(helper, send(matcher, *args))).should be_true
41
+ wildcard_match?(actual, send(helper, send(matcher, *args)), &debugger).should be_true
42
42
  else
43
- wildcard_match?(actual, send(helper, matcher)).should be_true
43
+ wildcard_match?(actual, send(helper, matcher), &debugger).should be_true
44
44
  end
45
45
  end
46
46
  end
@@ -1,14 +1,46 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe WildcardMatchers::Matchers::WithUriTemplate do
4
- [ [ "http://example.com/hoge/fuga", :with_uri_template, "http://example.com/hoge/{fuga}", "fuga" => "fuga" ],
4
+ extend WildcardMatchers::Matchers
5
+
6
+ [ [ "http://example.com/hoge/fuga", :with_uri_template, "http://example.com/{hoge}/{fuga}",
7
+ "hoge" => "hoge", "fuga" => "fuga" ],
8
+ [ "http://example.com/hoge/fuga", :with_uri_template, "http://example.com{/hoge,fuga}",
9
+ "hoge" => "hoge", "fuga" => "fuga" ],
10
+ [ "http://example.com/?hoge=fuga&fuga=ugu", :with_uri_template, "http://example.com/{?hoge,fuga}",
11
+ "hoge" => "fuga", "fuga" => "ugu" ],
12
+ [ "http://example.com/?hoge=fuga&fuga=ugu", :with_uri_template, "http://example.com/{?hoge,fuga}",
13
+ hash_includes("hoge" => "fuga") ],
5
14
  ].each do |actual, matcher, *args|
6
15
  it_behaves_like "wildcard match", actual, matcher, *args
7
16
  end
8
17
 
9
- [ [ "http://example.com/hoge/fuga", :with_uri_template, "http://example.com/hoge/{fuga}", "fuga" => "hoge" ],
10
- [ "http://example.com/hoge/fuga", :with_uri_template, "http://example.com/hoge/{fuga}", "hoge" => "fuga" ],
18
+ [ [ "http://example.com/hoge/fuga", :with_uri_template, "http://example.com/hoge/{fuga}", "fuga" => "hoge" ],
19
+ [ "http://example.com/hoge/fuga", :with_uri_template, "http://example.com/{hoge}/{fuga}", "hoge" => "fuga" ],
20
+ [ "http://example.com/", :with_uri_template, "http://example.com/{fuga}", {} ],
21
+ [ "http://example.com/?hoge=fuga", :with_uri_template, "http://example.com/{?hoge}", "hoge" => "ugu" ],
22
+
23
+ [ "http://example.com/?hoge=fuga&fuga=ugu", :with_uri_template, "http://example.com/{?hoge}",
24
+ hash_includes("hoge" => "fuga") ],
11
25
  ].each do |actual, matcher, *args|
12
26
  it_behaves_like "not wildcard match", actual, matcher, *args
13
27
  end
28
+
29
+ context "with without_query!", :focused do
30
+ it "works" do
31
+ wildcard_match?(
32
+ "http://example.com/?hoge=fuga&fuga=ugu",
33
+ with_uri_template("http://example.com/{?hoge}", hash_includes("hoge" => "fuga")).without_query!,
34
+ &debugger
35
+ ).should be_true
36
+ end
37
+
38
+ it "works" do
39
+ wildcard_match?(
40
+ "http://example.com/ugu?hoge=fuga&fuga=ugu",
41
+ with_uri_template("http://example.com/{piyo}{?hoge}", hash_includes("hoge" => "fuga", "piyo" => "ugu")).without_query!,
42
+ &debugger
43
+ ).should be_true
44
+ end
45
+ end
14
46
  end
@@ -15,14 +15,12 @@ Gem::Specification.new do |gem|
15
15
  gem.version = File.read(File.join(File.dirname(__FILE__), "VERSION")).chomp
16
16
 
17
17
  gem.add_dependency "facets"
18
+ gem.add_dependency "addressable", "~> 2.3"
18
19
 
19
20
  gem.add_development_dependency "rake"
20
21
  gem.add_development_dependency "rspec"
21
22
  gem.add_development_dependency "autowatchr"
22
23
 
23
- # your choice
24
- gem.add_development_dependency "addressable"
25
-
26
24
  # for debug
27
25
  gem.add_development_dependency "pry"
28
26
  gem.add_development_dependency "tapp"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wildcard_matchers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-07 00:00:00.000000000 Z
12
+ date: 2013-03-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: facets
@@ -28,23 +28,23 @@ dependencies:
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
30
  - !ruby/object:Gem::Dependency
31
- name: rake
31
+ name: addressable
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
- - - ! '>='
35
+ - - ~>
36
36
  - !ruby/object:Gem::Version
37
- version: '0'
38
- type: :development
37
+ version: '2.3'
38
+ type: :runtime
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  none: false
42
42
  requirements:
43
- - - ! '>='
43
+ - - ~>
44
44
  - !ruby/object:Gem::Version
45
- version: '0'
45
+ version: '2.3'
46
46
  - !ruby/object:Gem::Dependency
47
- name: rspec
47
+ name: rake
48
48
  requirement: !ruby/object:Gem::Requirement
49
49
  none: false
50
50
  requirements:
@@ -60,7 +60,7 @@ dependencies:
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  - !ruby/object:Gem::Dependency
63
- name: autowatchr
63
+ name: rspec
64
64
  requirement: !ruby/object:Gem::Requirement
65
65
  none: false
66
66
  requirements:
@@ -76,7 +76,7 @@ dependencies:
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
78
  - !ruby/object:Gem::Dependency
79
- name: addressable
79
+ name: autowatchr
80
80
  requirement: !ruby/object:Gem::Requirement
81
81
  none: false
82
82
  requirements: