Skip to content

Commit

Permalink
add tests for multi-line expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekg committed Aug 14, 2024
1 parent 4c8d118 commit c157ec7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
8 changes: 5 additions & 3 deletions lib/transition_through/matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down
20 changes: 18 additions & 2 deletions spec/transition_through_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit c157ec7

Please sign in to comment.