diff --git a/spec/spectator/matchers/built_in/be_nan_matcher_spec.cr b/spec/spectator/matchers/built_in/be_nan_matcher_spec.cr new file mode 100644 index 00000000..2de0d3a7 --- /dev/null +++ b/spec/spectator/matchers/built_in/be_nan_matcher_spec.cr @@ -0,0 +1,186 @@ +require "../../../spec_helper" + +struct NaNObject + getter? nan : Bool + + def initialize(@nan : Bool = true) + end +end + +alias BeNaNMatcher = Spectator::Matchers::BuiltIn::BeNaNMatcher + +Spectator.describe BeNaNMatcher do + describe "#matches?" do + it "returns true if the object is NaN" do + matcher = BeNaNMatcher.new + expect(matcher.matches?(NaNObject.new)).to be_true + end + + it "returns false if the object is not NaN" do + matcher = BeNaNMatcher.new + expect(matcher.matches?(NaNObject.new(false))).to be_false + end + + context "with a Float" do + it "returns true if the value is NaN" do + matcher = BeNaNMatcher.new + expect(matcher.matches?(Float64::NAN)).to be_true + end + + it "returns false if the value is not NaN" do + matcher = BeNaNMatcher.new + expect(matcher.matches?(1.0)).to be_false + end + + it "returns false if the value is zero" do + matcher = BeNaNMatcher.new + expect(matcher.matches?(0.0)).to be_false + end + + it "returns false if the value is positive infinity" do + matcher = BeNaNMatcher.new + expect(matcher.matches?(Float64::INFINITY)).to be_false + end + + it "returns false if the value is negative infinity" do + matcher = BeNaNMatcher.new + expect(matcher.matches?(-Float64::INFINITY)).to be_false + end + end + + context "with an Int" do + it "returns false" do + matcher = BeNaNMatcher.new + expect(matcher.matches?(1)).to be_false + end + end + end + + describe "#failure_message" do + it "returns the failure message" do + matcher = BeNaNMatcher.new + expect(matcher.failure_message(1.0)).to eq("Expected 1.0 to be NaN") + end + end + + describe "#negated_failure_message" do + it "returns the negated failure message" do + matcher = BeNaNMatcher.new + expect(matcher.negated_failure_message(Float64::NAN)).to eq("Expected NaN not to be NaN") + end + end + + describe "DSL" do + describe "`be_nan`" do + context "with `.to`" do + it "matches if the object is NaN" do + expect do + expect(NaNObject.new).to be_nan + end.to pass_check + end + + it "does not match if the object is not NaN" do + object = NaNObject.new(false) + expect do + expect(object).to be_nan + end.to fail_check("Expected #{object.pretty_inspect} to be NaN") + end + + context "with a Float" do + it "matches if the value is NaN" do + expect do + expect(Float64::NAN).to be_nan + end.to pass_check + end + + it "does not match if the value is not NaN" do + expect do + expect(1.0).to be_nan + end.to fail_check("Expected 1.0 to be NaN") + end + + it "does not match if the value is zero" do + expect do + expect(0.0).to be_nan + end.to fail_check("Expected 0.0 to be NaN") + end + + it "does not match if the value is positive infinity" do + expect do + expect(Float64::INFINITY).to be_nan + end.to fail_check("Expected Infinity to be NaN") + end + + it "does not match if the value is negative infinity" do + expect do + expect(-Float64::INFINITY).to be_nan + end.to fail_check("Expected -Infinity to be NaN") + end + end + + context "with an Int" do + it "does not match" do + expect do + expect(1).to be_nan + end.to fail_check("Expected 1 to be NaN") + end + end + end + + context "with `.not_to`" do + it "does not match if the object is NaN" do + object = NaNObject.new + expect do + expect(object).not_to be_nan + end.to fail_check("Expected #{object.pretty_inspect} not to be NaN") + end + + it "matches if the object is not NaN" do + expect do + expect(NaNObject.new(false)).not_to be_nan + end.to pass_check + end + + context "with a Float" do + it "does not match if the value is NaN" do + expect do + expect(Float64::NAN).not_to be_nan + end.to fail_check("Expected NaN not to be NaN") + end + + it "matches if the value is not NaN" do + expect do + expect(1.0).not_to be_nan + end.to pass_check + end + + it "matches if the value is zero" do + expect do + expect(0.0).not_to be_nan + end.to pass_check + end + + it "matches if the value is positive infinity" do + expect do + expect(Float64::INFINITY).not_to be_nan + end.to pass_check + end + + it "matches if the value is negative infinity" do + expect do + expect(-Float64::INFINITY).not_to be_nan + end.to pass_check + end + end + + context "with an Int" do + it "matches" do + expect do + expect(1).not_to be_nan + end.to pass_check + end + end + end + end + end +end diff --git a/spec/spectator/matchers/built_in/be_negative_matcher_spec.cr b/spec/spectator/matchers/built_in/be_negative_matcher_spec.cr new file mode 100644 index 00000000..3e92ac94 --- /dev/null +++ b/spec/spectator/matchers/built_in/be_negative_matcher_spec.cr @@ -0,0 +1,237 @@ +require "../../../spec_helper" + +struct NegativeObject + getter? negative : Bool + + def initialize(@negative : Bool = true) + end +end + +alias BeNegativeMatcher = Spectator::Matchers::BuiltIn::BeNegativeMatcher + +Spectator.describe BeNegativeMatcher do + describe "#matches?" do + it "returns true if the object is negative" do + matcher = BeNegativeMatcher.new + expect(matcher.matches?(NegativeObject.new)).to be_true + end + + it "returns false if the object is not negative" do + matcher = BeNegativeMatcher.new + expect(matcher.matches?(NegativeObject.new(false))).to be_false + end + + context "with a Float" do + it "returns true if the value is negative" do + matcher = BeNegativeMatcher.new + expect(matcher.matches?(-1.0)).to be_true + end + + it "returns false if the value is positive" do + matcher = BeNegativeMatcher.new + expect(matcher.matches?(1.0)).to be_false + end + + it "returns false if the value is zero" do + matcher = BeNegativeMatcher.new + expect(matcher.matches?(0.0)).to be_false + end + + it "returns false if the value is positive infinity" do + matcher = BeNegativeMatcher.new + expect(matcher.matches?(Float64::INFINITY)).to be_false + end + + it "returns true if the value is negative infinity" do + matcher = BeNegativeMatcher.new + expect(matcher.matches?(-Float64::INFINITY)).to be_true + end + + it "returns false if the value is NaN" do + matcher = BeNegativeMatcher.new + expect(matcher.matches?(Float64::NAN)).to be_false + end + end + + context "with an Int" do + it "returns true if the value is negative" do + matcher = BeNegativeMatcher.new + expect(matcher.matches?(-1)).to be_true + end + + it "returns false if the value is positive" do + matcher = BeNegativeMatcher.new + expect(matcher.matches?(1)).to be_false + end + + it "returns false if the value is zero" do + matcher = BeNegativeMatcher.new + expect(matcher.matches?(0)).to be_false + end + end + end + + describe "#failure_message" do + it "returns the failure message" do + matcher = BeNegativeMatcher.new + expect(matcher.failure_message(1.0)).to eq("Expected 1.0 to be negative") + end + end + + describe "#negated_failure_message" do + it "returns the negated failure message" do + matcher = BeNegativeMatcher.new + expect(matcher.negated_failure_message(-1.0)).to eq("Expected -1.0 not to be negative") + end + end + + describe "DSL" do + describe "`be_negative`" do + context "with `.to`" do + it "matches if the object is negative" do + expect do + expect(NegativeObject.new).to be_negative + end.to pass_check + end + + it "does not match if the object is not negative" do + object = NegativeObject.new(false) + expect do + expect(object).to be_negative + end.to fail_check("Expected #{object.pretty_inspect} to be negative") + end + + context "with a Float" do + it "matches if the value is negative" do + expect do + expect(-1.0).to be_negative + end.to pass_check + end + + it "does not match if the value is positive" do + expect do + expect(1.0).to be_negative + end.to fail_check("Expected 1.0 to be negative") + end + + it "does not match if the value is zero" do + expect do + expect(0.0).to be_negative + end.to fail_check("Expected 0.0 to be negative") + end + + it "does not match if the value is positive infinity" do + expect do + expect(Float64::INFINITY).to be_negative + end.to fail_check("Expected Infinity to be negative") + end + + it "matches if the value is negative infinity" do + expect do + expect(-Float64::INFINITY).to be_negative + end.to pass_check + end + + it "does not match if the value is NaN" do + expect do + expect(Float64::NAN).to be_negative + end.to fail_check("Expected NaN to be negative") + end + end + + context "with an Int" do + it "matches if the value is negative" do + expect do + expect(-1).to be_negative + end.to pass_check + end + + it "does not match if the value is positive" do + expect do + expect(1).to be_negative + end.to fail_check("Expected 1 to be negative") + end + + it "does not match if the value is zero" do + expect do + expect(0).to be_negative + end.to fail_check("Expected 0 to be negative") + end + end + end + + context "with `.not_to`" do + it "matches if the object is negative" do + object = NegativeObject.new + expect do + expect(object).not_to be_negative + end.to fail_check("Expected #{object.pretty_inspect} not to be negative") + end + + it "does not match if the object is not negative" do + expect do + expect(NegativeObject.new(false)).not_to be_negative + end.to pass_check + end + + context "with a Float" do + it "does not match if the value is negative" do + expect do + expect(-1.0).not_to be_negative + end.to fail_check("Expected -1.0 not to be negative") + end + + it "matches if the value is positive" do + expect do + expect(1.0).not_to be_negative + end.to pass_check + end + + it "matches if the value is zero" do + expect do + expect(0.0).not_to be_negative + end.to pass_check + end + + it "matches if the value is positive infinity" do + expect do + expect(Float64::INFINITY).not_to be_negative + end.to pass_check + end + + it "does not match if the value is negative infinity" do + expect do + expect(-Float64::INFINITY).not_to be_negative + end.to fail_check("Expected -Infinity not to be negative") + end + + it "matches if the value is NaN" do + expect do + expect(Float64::NAN).not_to be_negative + end.to pass_check + end + end + + context "with an Int" do + it "does not match if the value is negative" do + expect do + expect(-1).not_to be_negative + end.to fail_check("Expected -1 not to be negative") + end + + it "matches if the value is positive" do + expect do + expect(1).not_to be_negative + end.to pass_check + end + + it "matches if the value is zero" do + expect do + expect(0).not_to be_negative + end.to pass_check + end + end + end + end + end +end diff --git a/spec/spectator/matchers/built_in/be_positive_matcher_spec.cr b/spec/spectator/matchers/built_in/be_positive_matcher_spec.cr new file mode 100644 index 00000000..b87c8994 --- /dev/null +++ b/spec/spectator/matchers/built_in/be_positive_matcher_spec.cr @@ -0,0 +1,237 @@ +require "../../../spec_helper" + +struct PositiveObject + getter? positive : Bool + + def initialize(@positive : Bool = true) + end +end + +alias BePositiveMatcher = Spectator::Matchers::BuiltIn::BePositiveMatcher + +Spectator.describe BePositiveMatcher do + describe "#matches?" do + it "returns true if the object is positive" do + matcher = BePositiveMatcher.new + expect(matcher.matches?(PositiveObject.new)).to be_true + end + + it "returns false if the object is not positive" do + matcher = BePositiveMatcher.new + expect(matcher.matches?(PositiveObject.new(false))).to be_false + end + + context "with a Float" do + it "returns true if the value is positive" do + matcher = BePositiveMatcher.new + expect(matcher.matches?(1.0)).to be_true + end + + it "returns false if the value is negative" do + matcher = BePositiveMatcher.new + expect(matcher.matches?(-1.0)).to be_false + end + + it "returns false if the value is zero" do + matcher = BePositiveMatcher.new + expect(matcher.matches?(0.0)).to be_false + end + + it "returns true if the value is positive infinity" do + matcher = BePositiveMatcher.new + expect(matcher.matches?(Float64::INFINITY)).to be_true + end + + it "returns false if the value is negative infinity" do + matcher = BePositiveMatcher.new + expect(matcher.matches?(-Float64::INFINITY)).to be_false + end + + it "returns false if the value is NaN" do + matcher = BePositiveMatcher.new + expect(matcher.matches?(Float64::NAN)).to be_false + end + end + + context "with an Int" do + it "returns true if the value is positive" do + matcher = BePositiveMatcher.new + expect(matcher.matches?(1)).to be_true + end + + it "returns false if the value is negative" do + matcher = BePositiveMatcher.new + expect(matcher.matches?(-1)).to be_false + end + + it "returns false if the value is zero" do + matcher = BePositiveMatcher.new + expect(matcher.matches?(0)).to be_false + end + end + end + + describe "#failure_message" do + it "returns the failure message" do + matcher = BePositiveMatcher.new + expect(matcher.failure_message(-1.0)).to eq("Expected -1.0 to be positive") + end + end + + describe "#negated_failure_message" do + it "returns the negated failure message" do + matcher = BePositiveMatcher.new + expect(matcher.negated_failure_message(1.0)).to eq("Expected 1.0 not to be positive") + end + end + + describe "DSL" do + describe "`be_positive`" do + context "with `.to`" do + it "matches if the object is positive" do + expect do + expect(PositiveObject.new).to be_positive + end.to pass_check + end + + it "does not match if the object is not positive" do + object = PositiveObject.new(false) + expect do + expect(object).to be_positive + end.to fail_check("Expected #{object.pretty_inspect} to be positive") + end + + context "with a Float" do + it "matches if the value is positive" do + expect do + expect(1.0).to be_positive + end.to pass_check + end + + it "does not match if the value is negative" do + expect do + expect(-1.0).to be_positive + end.to fail_check("Expected -1.0 to be positive") + end + + it "does not match if the value is zero" do + expect do + expect(0.0).to be_positive + end.to fail_check("Expected 0.0 to be positive") + end + + it "matches if the value is positive infinity" do + expect do + expect(Float64::INFINITY).to be_positive + end.to pass_check + end + + it "does not match if the value is negative infinity" do + expect do + expect(-Float64::INFINITY).to be_positive + end.to fail_check("Expected -Infinity to be positive") + end + + it "does not match if the value is NaN" do + expect do + expect(Float64::NAN).to be_positive + end.to fail_check("Expected NaN to be positive") + end + end + + context "with an Int" do + it "matches if the value is positive" do + expect do + expect(1).to be_positive + end.to pass_check + end + + it "does not match if the value is negative" do + expect do + expect(-1).to be_positive + end.to fail_check("Expected -1 to be positive") + end + + it "does not match if the value is zero" do + expect do + expect(0).to be_positive + end.to fail_check("Expected 0 to be positive") + end + end + end + + context "with `.not_to`" do + it "matches if the object is positive" do + object = PositiveObject.new + expect do + expect(object).not_to be_positive + end.to fail_check("Expected #{object.pretty_inspect} not to be positive") + end + + it "does not match if the object is not positive" do + expect do + expect(PositiveObject.new(false)).not_to be_positive + end.to pass_check + end + + context "with a Float" do + it "does not match if the value is positive" do + expect do + expect(1.0).not_to be_positive + end.to fail_check("Expected 1.0 not to be positive") + end + + it "matches if the value is negative" do + expect do + expect(-1.0).not_to be_positive + end.to pass_check + end + + it "matches if the value is zero" do + expect do + expect(0.0).not_to be_positive + end.to pass_check + end + + it "does not match if the value is positive infinity" do + expect do + expect(Float64::INFINITY).not_to be_positive + end.to fail_check("Expected Infinity not to be positive") + end + + it "matches if the value is negative infinity" do + expect do + expect(-Float64::INFINITY).not_to be_positive + end.to pass_check + end + + it "matches if the value is NaN" do + expect do + expect(Float64::NAN).not_to be_positive + end.to pass_check + end + end + + context "with an Int" do + it "does not match if the value is positive" do + expect do + expect(1).not_to be_positive + end.to fail_check("Expected 1 not to be positive") + end + + it "matches if the value is negative" do + expect do + expect(-1).not_to be_positive + end.to pass_check + end + + it "matches if the value is zero" do + expect do + expect(0).not_to be_positive + end.to pass_check + end + end + end + end + end +end diff --git a/spec/spectator/matchers/built_in/be_zero_matcher_spec.cr b/spec/spectator/matchers/built_in/be_zero_matcher_spec.cr new file mode 100644 index 00000000..e1168c15 --- /dev/null +++ b/spec/spectator/matchers/built_in/be_zero_matcher_spec.cr @@ -0,0 +1,237 @@ +require "../../../spec_helper" + +struct ZeroObject + getter? zero : Bool + + def initialize(@zero : Bool = true) + end +end + +alias BeZeroMatcher = Spectator::Matchers::BuiltIn::BeZeroMatcher + +Spectator.describe BeZeroMatcher do + describe "#matches?" do + it "returns true if the object is zero" do + matcher = BeZeroMatcher.new + expect(matcher.matches?(ZeroObject.new)).to be_true + end + + it "returns false if the object is not zero" do + matcher = BeZeroMatcher.new + expect(matcher.matches?(ZeroObject.new(false))).to be_false + end + + context "with a Float" do + it "returns true if the value is zero" do + matcher = BeZeroMatcher.new + expect(matcher.matches?(0.0)).to be_true + end + + it "returns false if the value is positive" do + matcher = BeZeroMatcher.new + expect(matcher.matches?(1.0)).to be_false + end + + it "returns false if the value is negative" do + matcher = BeZeroMatcher.new + expect(matcher.matches?(-1.0)).to be_false + end + + it "returns false if the value is positive infinity" do + matcher = BeZeroMatcher.new + expect(matcher.matches?(Float64::INFINITY)).to be_false + end + + it "returns false if the value is negative infinity" do + matcher = BeZeroMatcher.new + expect(matcher.matches?(-Float64::INFINITY)).to be_false + end + + it "returns false if the value is NaN" do + matcher = BeZeroMatcher.new + expect(matcher.matches?(Float64::NAN)).to be_false + end + end + + context "with an Int" do + it "returns true if the value is zero" do + matcher = BeZeroMatcher.new + expect(matcher.matches?(0)).to be_true + end + + it "returns false if the value is positive" do + matcher = BeZeroMatcher.new + expect(matcher.matches?(1)).to be_false + end + + it "returns false if the value is negative" do + matcher = BeZeroMatcher.new + expect(matcher.matches?(-1)).to be_false + end + end + end + + describe "#failure_message" do + it "returns the failure message" do + matcher = BeZeroMatcher.new + expect(matcher.failure_message(-1.0)).to eq("Expected -1.0 to be zero") + end + end + + describe "#negated_failure_message" do + it "returns the negated failure message" do + matcher = BeZeroMatcher.new + expect(matcher.negated_failure_message(0.0)).to eq("Expected 0.0 not to be zero") + end + end + + describe "DSL" do + describe "`be_zero`" do + context "with `.to`" do + it "matches if the object is zero" do + expect do + expect(ZeroObject.new).to be_zero + end.to pass_check + end + + it "does not match if the object is not zero" do + object = ZeroObject.new(false) + expect do + expect(object).to be_zero + end.to fail_check("Expected #{object.pretty_inspect} to be zero") + end + + context "with a Float" do + it "matches if the value is zero" do + expect do + expect(0.0).to be_zero + end.to pass_check + end + + it "does not match if the value is positive" do + expect do + expect(1.0).to be_zero + end.to fail_check("Expected 1.0 to be zero") + end + + it "does not match if the value is negative" do + expect do + expect(-1.0).to be_zero + end.to fail_check("Expected -1.0 to be zero") + end + + it "does not match if the value is positive infinity" do + expect do + expect(Float64::INFINITY).to be_zero + end.to fail_check("Expected Infinity to be zero") + end + + it "does not match if the value is negative infinity" do + expect do + expect(-Float64::INFINITY).to be_zero + end.to fail_check("Expected -Infinity to be zero") + end + + it "does not match if the value is NaN" do + expect do + expect(Float64::NAN).to be_zero + end.to fail_check("Expected NaN to be zero") + end + end + + context "with an Int" do + it "matches if the value is zero" do + expect do + expect(0).to be_zero + end.to pass_check + end + + it "does not match if the value is positive" do + expect do + expect(1).to be_zero + end.to fail_check("Expected 1 to be zero") + end + + it "does not match if the value is negative" do + expect do + expect(-1).to be_zero + end.to fail_check("Expected -1 to be zero") + end + end + end + + context "with `.not_to`" do + it "does not match if the object is zero" do + object = ZeroObject.new + expect do + expect(object).not_to be_zero + end.to fail_check("Expected #{object.pretty_inspect} not to be zero") + end + + it "matches if the object is not zero" do + expect do + expect(ZeroObject.new(false)).not_to be_zero + end.to pass_check + end + + context "with a Float" do + it "does not match if the value is zero" do + expect do + expect(0.0).not_to be_zero + end.to fail_check("Expected 0.0 not to be zero") + end + + it "matches if the value is positive" do + expect do + expect(1.0).not_to be_zero + end.to pass_check + end + + it "matches if the value is negative" do + expect do + expect(-1.0).not_to be_zero + end.to pass_check + end + + it "matches if the value is positive infinity" do + expect do + expect(Float64::INFINITY).not_to be_zero + end.to pass_check + end + + it "matches if the value is negative infinity" do + expect do + expect(-Float64::INFINITY).not_to be_zero + end.to pass_check + end + + it "matches if the value is NaN" do + expect do + expect(Float64::NAN).not_to be_zero + end.to pass_check + end + end + + context "with an Int" do + it "does not match if the value is zero" do + expect do + expect(0).not_to be_zero + end.to fail_check("Expected 0 not to be zero") + end + + it "matches if the value is positive" do + expect do + expect(1).not_to be_zero + end.to pass_check + end + + it "matches if the value is negative" do + expect do + expect(-1).not_to be_zero + end.to pass_check + end + end + end + end + end +end