From 35946dc99364959e2aa9fb9d714d8a6238284dba Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Tue, 9 Feb 2021 22:50:21 -0700 Subject: [PATCH] Test value types --- spec/spectator/block_spec.cr | 34 ++++++++++++++++++++++++++++ spec/spectator/lazy_wrapper_spec.cr | 28 +++++++++++++++++++++++ spec/spectator/value_spec.cr | 35 +++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 spec/spectator/block_spec.cr create mode 100644 spec/spectator/lazy_wrapper_spec.cr create mode 100644 spec/spectator/value_spec.cr diff --git a/spec/spectator/block_spec.cr b/spec/spectator/block_spec.cr new file mode 100644 index 0000000..a459b02 --- /dev/null +++ b/spec/spectator/block_spec.cr @@ -0,0 +1,34 @@ +require "../spec_helper" + +Spectator.describe Spectator::Block do + describe "#value" do + it "calls the block" do + called = false + block = described_class.new { called = true } + expect { block.value }.to change { called }.to(true) + end + + it "can be called multiple times (doesn't cache the value)" do + count = 0 + block = described_class.new { count += 1 } + block.value # Call once, count should be 1. + expect { block.value }.to change { count }.from(1).to(2) + end + end + + describe "#to_s" do + let(block) do + described_class.new("Test Label") { 42 } + end + + subject { block.to_s } + + it "contains the label" do + is_expected.to contain("Test Label") + end + + it "contains the value" do + is_expected.to contain("42") + end + end +end diff --git a/spec/spectator/lazy_wrapper_spec.cr b/spec/spectator/lazy_wrapper_spec.cr new file mode 100644 index 0000000..94782b4 --- /dev/null +++ b/spec/spectator/lazy_wrapper_spec.cr @@ -0,0 +1,28 @@ +require "../spec_helper" + +Spectator.describe Spectator::LazyWrapper do + it "returns the value of the block" do + expect { subject.get { 42 } }.to eq(42) + end + + it "caches the value" do + wrapper = described_class.new + count = 0 + expect { wrapper.get { count += 1 } }.to change { count }.from(0).to(1) + expect { wrapper.get { count += 1 } }.to_not change { count } + end + + # This type of nesting is used when `super` is called in a subject block. + # ``` + # subject { super.to_s } + # ``` + it "works with nested wrappers" do + outer = described_class.new + inner = described_class.new + value = outer.get do + inner.get { 42 }.to_s + end + expect(value).to eq("42") + expect(value).to be_a(String) + end +end diff --git a/spec/spectator/value_spec.cr b/spec/spectator/value_spec.cr new file mode 100644 index 0000000..ab7652e --- /dev/null +++ b/spec/spectator/value_spec.cr @@ -0,0 +1,35 @@ +require "../spec_helper" + +Spectator.describe Spectator::Value do + subject { described_class.new(42, "Test Label") } + + it "stores the value" do + expect(&.value).to eq(42) + end + + # TODO: Fix issue with compile-time type of `subject` being a union. + # describe "#to_s" do + # subject { super.to_s } + # + # it "contains the label" do + # is_expected.to contain("Test Label") + # end + # + # it "contains the value" do + # is_expected.to contain("42") + # end + # end + # + # describe "#inspect" do + # let(value) { described_class.new([42], "Test Label") } + # subject { value.inspect } + # + # it "contains the label" do + # is_expected.to contain("Test Label") + # end + # + # it "contains the value" do + # is_expected.to contain("[42]") + # end + # end +end