# frozen_string_literal: true require "spec_helpers" describe Wayfarer::Routing::Matchers::Query do subject(:matcher) { Wayfarer::Routing::Matchers::Query.new(constraints) } describe "String constraints" do let(:constraints) { Hash[arg: "foo"] } context "with matching query field value" do let(:url) { Addressable::URI.parse("http://example.com?arg=foo") } it "returns true" do expect(matcher).to match(url) end end context "with mismatching query field value" do let(:url) { Addressable::URI.parse("http://example.com?arg=bar") } it "returns false" do expect(matcher).not_to match(url) end end end describe "Integer constraints" do let(:constraints) { Hash[arg: 0] } context "with matching query field value" do let(:url) { Addressable::URI.parse("http://example.com?arg=0") } it "returns true" do expect(matcher).to match(url) end end context "with mismatching query field value" do let(:url) { Addressable::URI.parse("http://example.com?arg=1") } it "returns false" do expect(matcher).not_to match(url) end end context "with non-numeric query field value" do let(:url) { Addressable::URI.parse("http://example.com?arg=foo") } it "returns false" do expect(matcher).not_to match(url) end end end describe "Regexp constraints" do let(:constraints) { Hash[arg: /foo/] } context "with matching query field value" do let(:url) { Addressable::URI.parse("http://example.com?arg=foo") } it "returns true" do expect(matcher).to match(url) end end context "with mismatching query field value" do let(:url) { Addressable::URI.parse("http://example.com?arg=bar") } it "returns false" do expect(matcher).not_to match(url) end end end describe "Range constraints" do let(:constraints) { Hash[arg: 1..10] } context "with matching query field value" do let(:url) { Addressable::URI.parse("http://example.com?arg=5") } it "returns true" do expect(matcher).to match(url) end end context "with mismatching query field value" do let(:url) { Addressable::URI.parse("http://example.com?arg=11") } it "returns false" do expect(matcher).not_to match(url) end end context "with non-numeric query field value" do let(:url) { Addressable::URI.parse("http://example.com?arg=foo") } it "returns false" do expect(matcher).not_to match(url) end end end describe "Compound constraints" do let(:constraints) do Hash[foo: 1..5, bar: /baz/, qux: "zot", toto: 2] end context "with matching query field values" do let(:url) { Addressable::URI.parse("http://example.com?foo=4&bar=bazqux&qux=zot&toto=2") } it "returns true" do expect(matcher).to match(url) end end context "with mismatching query field values" do let(:url) { Addressable::URI.parse("http://example.com?foo=bar&bar=qux&qux=6toto=0") } it "returns false" do expect(matcher).not_to match(url) end end end describe "#params" do let(:constraints) do Hash[foo: 1..5, bar: /baz/, qux: "zot", toto: 2] end it "returns the correct parameters" do url = Addressable::URI.parse("http://example.com?foo=4&bar=bazqux&qux=zot&toto=2") expect(matcher.params(url)).to \ eq("foo" => "4", "bar" => "bazqux", "qux" => "zot", "toto" => "2") end end end