# frozen_string_literal: true require "spec_helpers" describe Wayfarer::Routing::DSL do subject(:root) { Wayfarer::Routing::RootRoute.new } let(:child) { root.children.first } describe "#to" do it "adds a child with an always-matching matcher" do expect(root.to(:foobar)).to be_a(Wayfarer::Routing::Route) expect(child.action).to be(:foobar) expect(child.matcher).to be_a(Wayfarer::Routing::Matchers::Custom) expect(child.parent).to be(root) %w[http://example.com http://w3c.org http://google.com].each do |url| expect(child.matcher.match(url)).to be(true) end end end describe "#url" do it "adds a child with an URL matcher" do expect(root.url("http://example.com")).to be_a(Wayfarer::Routing::Route) expect(child.matcher).to be_a(Wayfarer::Routing::Matchers::URL) expect(child.matcher.url).to eq("http://example.com") expect(child.parent).to be(root) end end describe "#host" do it "adds a child with a host matcher" do expect(root.host("example.com")).to be_a(Wayfarer::Routing::Route) expect(child.matcher).to be_a(Wayfarer::Routing::Matchers::Host) expect(child.matcher.host).to eq("example.com") expect(child.parent).to be(root) end end describe "#path" do it "adds a child with a path matcher" do expect(root.path("/foo/bar")).to be_a(Wayfarer::Routing::Route) expect(child.matcher).to be_a(Wayfarer::Routing::Matchers::Path) expect(child.matcher.path).to eq("/foo/bar") expect(child.parent).to be(root) end end describe "#query" do it "adds a child with a query matcher" do expect(root.query(foo: "bar")).to be_a(Wayfarer::Routing::Route) expect(child.matcher).to be_a(Wayfarer::Routing::Matchers::Query) expect(child.matcher.fields).to eq({ foo: "bar" }) expect(child.parent).to be(root) end end describe "#scheme" do it "adds a child with a scheme matcher" do expect(root.scheme(:https)).to be_a(Wayfarer::Routing::Route) expect(child.matcher).to be_a(Wayfarer::Routing::Matchers::Scheme) expect(child.matcher.scheme).to be(:https) expect(child.parent).to be(root) end end describe "#suffix" do it "adds a child with a suffix matcher" do expect(root.suffix(".png")).to be_a(Wayfarer::Routing::Route) expect(child.matcher).to be_a(Wayfarer::Routing::Matchers::Suffix) expect(child.matcher.suffix).to eq(".png") expect(child.parent).to be(root) end end describe "#custom" do it "adds a child with a custom matcher" do expect(root.custom(->(_) { true })).to be_a(Wayfarer::Routing::Route) expect(child.matcher).to be_a(Wayfarer::Routing::Matchers::Custom) expect(child.matcher.delegate).to be_a(Proc) expect(child.parent).to be(root) end end describe "Complex routing tree" do it "constructs the expected tree" do root.host "example.com", path: "/foo" do query foo: 4, bar: 2 end expect(root.children.count).to be(1) expect(root.children.first.matcher).to be_a(Wayfarer::Routing::Matchers::Host) expect(root.children.first.children.count).to be(1) expect(root.children.first.children.first.matcher).to be_a(Wayfarer::Routing::Matchers::Path) end end end