From c157ec7b68bc1853b3ce1a61a6624cf663616dcc Mon Sep 17 00:00:00 2001 From: Zeke Gabrielse Date: Wed, 14 Aug 2024 09:35:08 -0500 Subject: [PATCH] add tests for multi-line expressions --- lib/transition_through/matcher.rb | 8 +++++--- spec/transition_through_spec.rb | 20 ++++++++++++++++++-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/transition_through/matcher.rb b/lib/transition_through/matcher.rb index a9621c6..99dd11e 100644 --- a/lib/transition_through/matcher.rb +++ b/lib/transition_through/matcher.rb @@ -19,6 +19,8 @@ def initialize(state_block) def supports_block_expectations? = true def matches?(expect_block) + raise InvalidExpressionError, 'transition state block is required' if state_block.nil? + path, start_line = state_block.source_location # walk the ast until we find our transition expression @@ -27,14 +29,14 @@ def matches?(expect_block) ast.value.accept(exp) - # raise if the expression is empty - raise InvalidExpressionError if + # raise if the expression is too complex or empty + raise InvalidExpressionError, 'complex or empty transition expressions are not supported' if exp.result.nil? || exp.result.receiver.nil? || exp.result.method_name.nil? # get the actual transitioning object from the state block's binding receiver = state_block.binding.eval(exp.result.receiver.name.to_s) - raise InvalidExpressionError unless + raise InvalidExpressionError, "expected accessor #{receiver.class}##{exp.result.method_name} but it's missing" unless receiver.respond_to?(:"#{exp.result.method_name}=") && receiver.respond_to?(exp.result.method_name) diff --git a/spec/transition_through_spec.rb b/spec/transition_through_spec.rb index 11760da..e8e700d 100644 --- a/spec/transition_through_spec.rb +++ b/spec/transition_through_spec.rb @@ -13,13 +13,23 @@ def initialize = self.count = 0 def increment(n = 1) = n.times { self.count += 1 } end - it 'should support simple expressions' do + it 'should support inline expression' do expect { expect { counter.increment }.to transition { counter.count } }.to_not raise_error end - it 'should raise on complex expressions' do + it 'should support multi-line expression' do + expect { + expect { counter.increment }.to( + transition do + counter.count + end + ) + }.to_not raise_error + end + + it 'should raise on complex expression' do expect { expect { counter.increment }.to transition { counter.itself.count } }.to raise_error TransitionThrough::InvalidExpressionError @@ -31,6 +41,12 @@ def increment(n = 1) = n.times { self.count += 1 } }.to raise_error TransitionThrough::InvalidExpressionError end + it 'should raise on no expression' do + expect { + expect { counter.increment }.to transition + }.to raise_error TransitionThrough::InvalidExpressionError + end + it 'should support arrays' do expect { counter.increment(3) }.to transition { counter.count }.through [0, 1, 2, 3] end