From 42ef2cc904da1350349776d279486f90e1fe9a9b Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 4 Jan 2020 12:47:23 -0700 Subject: [PATCH] Add RSpec type matchers spec --- spec/rspec/expectations/type_matchers_spec.cr | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 spec/rspec/expectations/type_matchers_spec.cr diff --git a/spec/rspec/expectations/type_matchers_spec.cr b/spec/rspec/expectations/type_matchers_spec.cr new file mode 100644 index 0000000..c2b1a2d --- /dev/null +++ b/spec/rspec/expectations/type_matchers_spec.cr @@ -0,0 +1,101 @@ +require "../../spec_helper" + +# Examples taken from: +# https://relishapp.com/rspec/rspec-expectations/v/3-8/docs/built-in-matchers/type-matchers +# and modified to fit Spectator and Crystal. +Spectator.describe "Type matchers" do + context "be_(a_)kind_of matcher" do + # The docs use Float as an example. + # This doesn't work with the Crystal compiler, + # so a custom hierarchy is used instead. + # "Error: can't use Number as generic type argument yet, use a more specific type" + + module MyModule; end + + class Base; end + + class Derived < Base + include MyModule + end + + describe Derived do + # the actual class + it { is_expected.to be_kind_of(Derived) } + it { is_expected.to be_a_kind_of(Derived) } + it { is_expected.to be_a(Derived) } + + # the superclass + it { is_expected.to be_kind_of(Base) } + it { is_expected.to be_a_kind_of(Base) } + it { is_expected.to be_an(Base) } + + # an included module + it { is_expected.to be_kind_of(MyModule) } + it { is_expected.to be_a_kind_of(MyModule) } + it { is_expected.to be_a(MyModule) } + + # negative passing case + it { is_expected.not_to be_kind_of(String) } + it { is_expected.not_to be_a_kind_of(String) } + it { is_expected.not_to be_a(String) } + + # deliberate failures + # TODO: Add support for expected failures. + xit { is_expected.not_to be_kind_of(Derived) } + xit { is_expected.not_to be_a_kind_of(Derived) } + xit { is_expected.not_to be_a(Derived) } + xit { is_expected.not_to be_kind_of(Base) } + xit { is_expected.not_to be_a_kind_of(Base) } + xit { is_expected.not_to be_an(Base) } + xit { is_expected.not_to be_kind_of(MyModule) } + xit { is_expected.not_to be_a_kind_of(MyModule) } + xit { is_expected.not_to be_a(MyModule) } + xit { is_expected.to be_kind_of(String) } + xit { is_expected.to be_a_kind_of(String) } + xit { is_expected.to be_a(String) } + end + + context "be_(an_)instance_of matcher" do + # The docs use Float as an example. + # This doesn't work with the Crystal compiler, + # so a custom hierarchy is used instead. + # "Error: can't use Number as generic type argument yet, use a more specific type" + + module MyModule; end + + class Base; end + + class Derived < Base + include MyModule + end + + describe Derived do + # the actual class + it { is_expected.to be_instance_of(Derived) } + it { is_expected.to be_an_instance_of(Derived) } + + # the superclass + it { is_expected.not_to be_instance_of(Base) } + it { is_expected.not_to be_an_instance_of(Base) } + + # an included module + it { is_expected.not_to be_instance_of(MyModule) } + it { is_expected.not_to be_an_instance_of(MyModule) } + + # another class with no relation to the subject's hierarchy + it { is_expected.not_to be_instance_of(String) } + it { is_expected.not_to be_an_instance_of(String) } + + # deliberate failures + xit { is_expected.not_to be_instance_of(Derived) } + xit { is_expected.not_to be_an_instance_of(Derived) } + xit { is_expected.to be_instance_of(Base) } + xit { is_expected.to be_an_instance_of(Base) } + xit { is_expected.to be_instance_of(MyModule) } + xit { is_expected.to be_an_instance_of(MyModule) } + xit { is_expected.to be_instance_of(String) } + xit { is_expected.to be_an_instance_of(String) } + end + end + end +end