From db89a995622c87bfff5c4c6db13c0673bd673e40 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Thu, 17 Jan 2019 14:14:10 -0700 Subject: [PATCH] Add specs for ExampleConditions --- spec/example_conditions_spec.cr | 36 ++++++++++++++++++++++++++++ spec/helpers/example_hooks_helper.cr | 25 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 spec/example_conditions_spec.cr diff --git a/spec/example_conditions_spec.cr b/spec/example_conditions_spec.cr new file mode 100644 index 0000000..5fbc51e --- /dev/null +++ b/spec/example_conditions_spec.cr @@ -0,0 +1,36 @@ +require "./spec_helper" + +describe Spectator::ExampleConditions do + {% for condition in %i[pre_condition post_condition] %} + describe "#run_{{condition.id}}s" do + it "calls a proc" do + called = false + conditions = new_conditions({{condition.id}}: ->{ called = true; nil }) + conditions.run_{{condition.id}}s + called.should be_true + end + + it "calls multiple procs" do + call_count = 0 + conditions = new_conditions({{condition.id}}s: [ + ->{ call_count += 1; nil }, + ->{ call_count += 2; nil }, + ->{ call_count += 3; nil }, + ]) + conditions.run_{{condition.id}}s + call_count.should eq(6) + end + + it "calls procs in the correct order" do + calls = [] of Symbol + conditions = new_conditions({{condition.id}}s: [ + ->{ calls << :a; nil }, + ->{ calls << :b; nil }, + ->{ calls << :c; nil }, + ]) + conditions.run_{{condition.id}}s + calls.should eq(\%i[a b c]) + end + end + {% end %} +end diff --git a/spec/helpers/example_hooks_helper.cr b/spec/helpers/example_hooks_helper.cr index dce5caa..d68a39b 100644 --- a/spec/helpers/example_hooks_helper.cr +++ b/spec/helpers/example_hooks_helper.cr @@ -32,3 +32,28 @@ def new_hooks( around_each ? [around_each] : [] of Proc(Nil) -> ) end + +# Creates a new `Spectator::ExampleConditions` instance. +# All arguments are optional, +# only specify the sets of conditions that are needed. +# The conditions that aren't specified will be left empty. +def new_conditions( + pre_conditions = [] of ->, + post_conditions = [] of -> +) + Spectator::ExampleConditions.new(pre_conditions, post_conditions) +end + +# Creates a new `Spectator::ExampleConditions` instance. +# All arguments are optional, +# only specify a condition for the types that are needed. +# The conditions that aren't specified will be left empty. +def new_conditions( + pre_condition : Proc(Nil)? = nil, + post_condition : Proc(Nil)? = nil +) + new_conditions( + pre_condition ? [pre_condition] : [] of ->, + post_condition ? [post_condition] : [] of -> + ) +end