From 2538f3a9a4eb975350c1e9f5c930057835c77b5f Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 4 Jan 2020 09:58:53 -0700 Subject: [PATCH] Add RSpec predicate matchers spec --- .../expectations/predicate_matchers_spec.cr | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 spec/rspec/expectations/predicate_matchers_spec.cr diff --git a/spec/rspec/expectations/predicate_matchers_spec.cr b/spec/rspec/expectations/predicate_matchers_spec.cr new file mode 100644 index 0000000..f2dac5c --- /dev/null +++ b/spec/rspec/expectations/predicate_matchers_spec.cr @@ -0,0 +1,86 @@ +require "../../spec_helper" + +# Examples taken from: +# https://relishapp.com/rspec/rspec-expectations/v/3-8/docs/built-in-matchers/predicate-matchers +# and modified to fit Spectator and Crystal. +Spectator.describe "Predicate matchers" do + context "should be_zero (based on Int#zero?)" do + describe 0 do + it { is_expected.to be_zero } + end + + describe 7 do + # deliberate failure + # TODO: Add support for expected failures. + xit { is_expected.to be_zero } + end + end + + context "should_not be_empty (based on Array#empty?)" do + describe [1, 2, 3] do + it { is_expected.not_to be_empty } + end + + describe [] of Int32 do + # deliberate failure + # TODO: Add support for expected failures. + xit { is_expected.not_to be_empty } + end + end + + context "should have_key (based on Hash#has_key?)" do + describe Hash do + subject { {:foo => 7} } + + it { is_expected.to have_key(:foo) } + + # deliberate failure + # TODO: Add support for expected failures. + xit { is_expected.to have_key(:bar) } + end + end + + context "should_not have_all_string_keys (based on custom #has_all_string_keys? method)" do + class ::Hash(K, V) + def has_all_string_keys? + keys.all? { |k| String === k } + end + end + + describe Hash do + context "with symbol keys" do + subject { {:foo => 7, :bar => 5} } + + it { is_expected.not_to have_all_string_keys } + end + + context "with string keys" do + subject { {"foo" => 7, "bar" => 5} } + + # deliberate failure + # TODO: Add support for expected failures. + xit { is_expected.not_to have_all_string_keys } + end + end + end + + context "matcher arguments are passed on to the predicate method" do + struct ::Int + def multiple_of?(x) + (self % x).zero? + end + end + + describe 12 do + it { is_expected.to be_multiple_of(3) } + it { is_expected.not_to be_multiple_of(7) } + + # deliberate failures + # TODO: Add support for expected failures. + xit { is_expected.not_to be_multiple_of(4) } + xit { is_expected.to be_multiple_of(5) } + end + end + + # The examples using private methods cause a compilation error in Crystal, and can't be used here. +end