From 2f8b4761de39349fd3a0d499b51337cc8466766c Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 23 Mar 2019 21:40:13 -0600 Subject: [PATCH] Make Example comparable against names and sources This will be needed for filtering examples. --- spec/pending_example_spec.cr | 62 +++++++++++++++++++++++++++++++++++ spec/runnable_example_spec.cr | 62 +++++++++++++++++++++++++++++++++++ src/spectator/example.cr | 16 +++++++++ 3 files changed, 140 insertions(+) diff --git a/spec/pending_example_spec.cr b/spec/pending_example_spec.cr index 6809816..845ed76 100644 --- a/spec/pending_example_spec.cr +++ b/spec/pending_example_spec.cr @@ -119,4 +119,66 @@ describe Spectator::PendingExample do example.to_s.should contain(group.what.to_s) end end + + describe "#===" do + context "with a matching Regex" do + it "is true" do + example = new_pending_example + regex = Regex.new(Regex.escape(example.what)) + (example === regex).should be_true + end + end + + context "with a non-matching Regex" do + it "is false" do + example = new_pending_example + regex = /BOGUS/ + (example === regex).should be_false + end + end + + context "with a String equal to the name" do + it "is true" do + example = new_pending_example + (example === example.to_s).should be_true + end + end + + context "with a String different than the name" do + it "is false" do + example = new_pending_example + (example === "BOGUS").should be_false + end + end + + context "with a matching source location" do + it "is true" do + example = new_pending_example + (example === example.source).should be_true + end + end + + context "with a non-matching source location" do + it "is false" do + example = new_pending_example + source = Spectator::Source.new(__FILE__, __LINE__) + (example === source).should be_false + end + end + + context "with a matching source line" do + it "is true" do + example = new_pending_example + (example === example.source.line).should be_true + end + end + + context "with a non-matching source line" do + it "is false" do + example = new_pending_example + line = example.source.line + 5 + (example === line).should be_false + end + end + end end diff --git a/spec/runnable_example_spec.cr b/spec/runnable_example_spec.cr index 602dc38..8fb7b1f 100644 --- a/spec/runnable_example_spec.cr +++ b/spec/runnable_example_spec.cr @@ -1565,4 +1565,66 @@ describe Spectator::RunnableExample do end end end + + describe "#===" do + context "with a matching Regex" do + it "is true" do + example = new_runnable_example + regex = Regex.new(Regex.escape(example.what)) + (example === regex).should be_true + end + end + + context "with a non-matching Regex" do + it "is false" do + example = new_runnable_example + regex = /BOGUS/ + (example === regex).should be_false + end + end + + context "with a String equal to the name" do + it "is true" do + example = new_runnable_example + (example === example.to_s).should be_true + end + end + + context "with a String different than the name" do + it "is false" do + example = new_runnable_example + (example === "BOGUS").should be_false + end + end + + context "with a matching source location" do + it "is true" do + example = new_runnable_example + (example === example.source).should be_true + end + end + + context "with a non-matching source location" do + it "is false" do + example = new_runnable_example + source = Spectator::Source.new(__FILE__, __LINE__) + (example === source).should be_false + end + end + + context "with a matching source line" do + it "is true" do + example = new_runnable_example + (example === example.source.line).should be_true + end + end + + context "with a non-matching source line" do + it "is false" do + example = new_runnable_example + line = example.source.line + 5 + (example === line).should be_false + end + end + end end diff --git a/src/spectator/example.cr b/src/spectator/example.cr index d12363e..faa5673 100644 --- a/src/spectator/example.cr +++ b/src/spectator/example.cr @@ -59,5 +59,21 @@ module Spectator def to_json(json : ::JSON::Builder) json.string(to_s) end + + # Checks if this example matches some criteria. + # This is used to filter examples. + def ===(other) + other === to_s + end + + # Checks if this example is at the specified source. + def ===(other : Source) + source == other + end + + # Checks if this example is at the specified line number. + def ===(other : Int32) + source.line === other + end end end