Skip to content

Commit

Permalink
add tests for readme example
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekg committed Aug 14, 2024
1 parent 52d8b7f commit fbd1424
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@ $ gem install transition_through

```ruby
it 'should transition through' do
expect {
counter = Counter.new
count = -> {
counter.count = 0
counter.count += 1
counter.count = counter.count + 3
counter.count -= 2
}.to transition { counter.count }.through [0, 1, 4, 2]
}

expect { count.call }.to transition { counter.count }.through [0, 1, 4, 2]
end
```

Expand Down
38 changes: 26 additions & 12 deletions spec/transition_through_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@
RSpec.describe TransitionThrough do
let(:counter) { Counter.new }

context 'using ivars' do
temporary_model :counter, table_name: nil, base_class: nil do
attr_accessor :count

def initialize = @count = 0
def increment(n = 1) = n.times { @count += 1 }
end

# TODO(ezekg) not sure this is possible since you can't redefine an ivar setter
it 'should not track transitions' do
expect { counter.increment(3) }.to transition { counter.count }.through [0]
end
end

context 'using methods' do
temporary_model :counter, table_name: nil, base_class: nil do
attr_accessor :count
Expand Down Expand Up @@ -67,19 +81,19 @@ def increment(n = 1) = n.times { self.count += 1 }
counter.count -= 2
}.to transition { counter.count }.through [0, 1, 4, 2]
end
end

context 'using ivars' do
temporary_model :counter, table_name: nil, base_class: nil do
attr_accessor :count

def initialize = @count = 0
def increment(n = 1) = n.times { @count += 1 }
end

# TODO(ezekg) not sure this is possible since you can't redefine an ivar setter
it 'should not track transitions' do
expect { counter.increment(3) }.to transition { counter.count }.through [0]
describe 'README' do
it 'should transition through' do
counter = Counter.new
count = -> {
counter.count = 0
counter.count += 1
counter.count = counter.count + 3
counter.count -= 2
}

expect { count.call }.to transition { counter.count }.through [0, 1, 4, 2]
end
end
end
end

0 comments on commit fbd1424

Please sign in to comment.