spec/lib/danica/function_spec.rb in danica-1.1.0 vs spec/lib/danica/function_spec.rb in danica-1.2.0
- old
+ new
@@ -2,11 +2,11 @@
describe Danica::Function do
class Danica::Function
class Spatial < Danica::Function
variables :time, :acceleration, :initial_space, :initial_velocity
- delegate :to_tex, :to_gnu, to: :sum
+ delegate :to_f, :to_tex, :to_gnu, to: :sum
private
def sum
@sum ||= Sum.new(parcels)
@@ -191,9 +191,57 @@
end
let(:subject) { described_class::Spatial.new variables }
it 'returns the variables given on initialization' do
expect(subject.variables.map(&:name)).to eq(names)
+ end
+ end
+ end
+ end
+ end
+
+ describe '#calculate' do
+ context 'when all variables have value' do
+ let(:time_value) { 2 }
+ let(:time) { time_value }
+ let(:acceleration) { 3 }
+ let(:initial_space) { 1 }
+ let(:initial_velocity) { 1 }
+ let(:subject) { described_class::Spatial.new(time, acceleration, initial_space, initial_velocity) }
+ let(:expected) { initial_space + initial_velocity * time_value + acceleration * (time_value ** 2) / 2.0 }
+
+ it 'retuirns the calculated value' do
+ expect(subject.calculate).to eq(expected)
+ end
+
+ context 'when not all variables have value' do
+ let(:time) { :t }
+
+ it do
+ expect { subject.calculate }.to raise_error(Danica::Exception::NotDefined)
+ end
+
+ context 'but calling calculate with a value for the variables' do
+ it 'calculate using the given value' do
+ expect(subject.calculate(time_value)).to eq(expected)
+ end
+
+ it 'does not change the values of then valued variables' do
+ expect do
+ subject.calculate(time_value)
+ end.not_to change(subject.time, :valued?)
+ end
+ end
+
+ context 'when calling with a hash for the values' do
+ it 'calculate using the given value' do
+ expect(subject.calculate(time: time_value)).to eq(expected)
+ end
+
+ it 'does not change the values of then valued variables' do
+ expect do
+ subject.calculate(time: time_value)
+ end.not_to change(subject.time, :valued?)
end
end
end
end
end