Skip to content

Commit

Permalink
Simplify Trial
Browse files Browse the repository at this point in the history
  • Loading branch information
dessy committed Dec 1, 2014
1 parent 34b9802 commit 747112f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
4 changes: 2 additions & 2 deletions pedometer/models/trial.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class Trial
def initialize(name = nil, method = nil, rate = nil, steps = nil)
@name = name.to_s.delete(' ')
@method = method.to_s.delete(' ')
@rate = Integer(rate.to_s) unless rate.to_s.empty? rescue raise('Invalid rate')
@steps = Integer(steps.to_s) unless steps.to_s.empty? rescue raise('Invalid steps')
@rate = Integer(rate.to_s) unless rate.to_s.empty?
@steps = Integer(steps.to_s) unless steps.to_s.empty?

raise('Invalid rate') if @rate && (@rate <= 0)
raise('Invalid steps') if @steps && (@steps < 0)
Expand Down
16 changes: 14 additions & 2 deletions pedometer/test/unit/trial_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@ def test_create_with_rate
assert_equal 100, Trial.new(nil, nil, nil).rate
assert_equal 100, Trial.new(nil, nil, '').rate

['invalid rate', 0, '0', -1, '-1', 2.5, '2.5'].each do |rate|
[0, '0', -1, '-1'].each do |rate|
assert_raise_with_message(RuntimeError, 'Invalid rate') do
Trial.new(nil, nil, rate)
end
end

['invalid rate', 2.5, '2.5'].each do |rate|
assert_raises(ArgumentError) do
Trial.new(nil, nil, rate)
end
end

assert_equal 1, Trial.new(nil, nil, 1).rate
assert_equal 1, Trial.new(nil, nil, '1').rate
assert_equal 100, Trial.new(nil, nil, 100).rate
Expand All @@ -41,12 +47,18 @@ def test_create_with_steps
assert_nil Trial.new(nil, nil, nil, nil).steps
assert_nil Trial.new(nil, nil, nil, '').steps

['invalid steps', -1, '-1', 2.5, '2.5'].each do |steps|
[-1, '-1'].each do |steps|
assert_raise_with_message(RuntimeError, 'Invalid steps') do
Trial.new(nil, nil, nil, steps)
end
end

['invalid steps', 2.5, '2.5'].each do |steps|
assert_raises(ArgumentError) do
Trial.new(nil, nil, nil, steps)
end
end

assert_equal 0, Trial.new(nil, nil, nil, 0).steps
assert_equal 0, Trial.new(nil, nil, nil, '0').steps
assert_equal 1, Trial.new(nil, nil, nil, 1).steps
Expand Down

0 comments on commit 747112f

Please sign in to comment.