Add RSpec type matchers spec

This commit is contained in:
Michael Miller 2020-01-04 12:47:23 -07:00
parent 93c442d1e2
commit 42ef2cc904
1 changed files with 101 additions and 0 deletions

View File

@ -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