From 4af23751bca89f418c539de5a408050ebf49ba0c Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 13 Feb 2021 13:30:05 -0700 Subject: [PATCH] Add specs for value types --- spec/spectator/lazy_spec.cr | 15 +++++++++++++++ spec/spectator/wrapper_spec.cr | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 spec/spectator/lazy_spec.cr create mode 100644 spec/spectator/wrapper_spec.cr diff --git a/spec/spectator/lazy_spec.cr b/spec/spectator/lazy_spec.cr new file mode 100644 index 0000000..8458a80 --- /dev/null +++ b/spec/spectator/lazy_spec.cr @@ -0,0 +1,15 @@ +require "../spec_helper" + +Spectator.describe Spectator::Lazy do + subject { Spectator::Lazy(Int32).new } + + it "returns the value of the block" do + expect { subject.get { 42 } }.to eq(42) + end + + it "caches the value" do + count = 0 + expect { subject.get { count += 1 } }.to change { count }.from(0).to(1) + expect { subject.get { count += 1 } }.to_not change { count } + end +end diff --git a/spec/spectator/wrapper_spec.cr b/spec/spectator/wrapper_spec.cr new file mode 100644 index 0000000..aa2351c --- /dev/null +++ b/spec/spectator/wrapper_spec.cr @@ -0,0 +1,18 @@ +require "../spec_helper" + +Spectator.describe Spectator::Wrapper do + it "stores a value" do + wrapper = described_class.new(42) + expect(wrapper.get(Int32)).to eq(42) + end + + it "retrieves a value using the block trick" do + wrapper = described_class.new(Int32) + expect(wrapper.get { Int32 }).to eq(Int32) + end + + it "raises on invalid cast" do + wrapper = described_class.new(42) + expect { wrapper.get(String) }.to raise_error(TypeCastError) + end +end