spec/lib/danica/function_spec.rb in danica-2.3.1 vs spec/lib/danica/function_spec.rb in danica-2.4.0

- old
+ new

@@ -1,41 +1,46 @@ require 'spec_helper' -describe Danica::Function do - describe '.build' do - let(:variables) { %i(x y) } - let(:function_class) do - described_class.build(*variables) do - Danica::Operator::Power.new(x, y) - end - end - let(:function) do - function_class.new - end +shared_examples 'a generically generated function' do + it 'returns a function class' do + expect(function.class.superclass).to eq(described_class) + end - it 'returns a function class' do - expect(function_class.superclass).to eq(described_class) + it 'returns a class whose instance responds to the variables' do + variables.each do |variable| + expect(function).to respond_to(variable) end + end - it 'returns a class whose instance responds to the variables' do - variables.each do |variable| - expect(function).to respond_to(variable) - end - end + it 'returns a function that uses the block to process to_tex' do + expect(function.to_tex).to eq('x^{y}') + end - it 'returns a function that uses the block to process to_tex' do - expect(function.to_tex).to eq('x^{y}') - end + it 'returns a function that uses the block to process to_gnu' do + expect(function.to_gnu).to eq('x**(y)') + end - it 'returns a function that uses the block to process to_gnu' do - expect(function.to_gnu).to eq('x**(y)') + it 'returns a function thtat knows how to calculate' do + expect(function.calculate(x: 2, y: 3)).to eq(8) + end +end + +describe Danica::Function do + let(:variables) { %i(x y) } + let(:function_class) do + described_class.build(*variables) do + Danica::Operator::Power.new(x, y) end + end - it 'returns a function thtat knows how to calculate' do - expect(function.calculate(x: 2, y: 3)).to eq(8) + describe '.build' do + let(:function) do + function_class.new end + it_behaves_like 'a generically generated function' + context 'when no block is given' do let(:function_class) do described_class.build(*variables) end @@ -50,11 +55,11 @@ it 'has the defined variables on class definition' do expect(function_class.variables_names).to eq([:x]) end it 'has the defined variables' do - expect(function.variables_hash).to eq(x: nil) + expect(function.variables_hash).to eq(x: Danica::Wrapper::Variable.new(name: :x)) end context 'when calling to_tex' do it 'build function from block' do expect(function.to_tex).to eq('x^{2}') @@ -74,11 +79,14 @@ it 'has the defined variables on class definition' do expect(function_class.variables_names).to eq([:x, :y]) end it 'has the defined variables' do - expect(function.variables_hash).to eq(x: nil, y: nil) + expect(function.variables_hash).to eq( + x: Danica::Wrapper::Variable.new(name: :x), + y: Danica::Wrapper::Variable.new(name: :y) + ) end context 'when calling to_tex' do it 'build function from block' do expect(function.to_tex).to eq('x^{2} -y^{2}') @@ -91,10 +99,43 @@ end end end end + describe '.create' do + let(:function) do + described_class.create(*variables) do + Danica::Operator::Power.new(x, y) + end + end + it_behaves_like 'a generically generated function' + end + + describe '#describe_tex' do + context 'when function has a name' do + let(:function) do + function_class.new(name: :f) + end + + it 'returns the full function description' do + expect(function.describe_tex).to eq('f(x, y) = x^{y}') + end + end + end + + describe '#describe_gnu' do + context 'when function has a name' do + let(:function) do + function_class.new(name: :f) + end + + it 'returns the full function description' do + expect(function.describe_gnu).to eq('f(x, y) = x**(y)') + end + end + end + describe 'spatial' do let(:variables) do { time: :t, acceleration: 'a', @@ -217,21 +258,26 @@ subject { described_class::Spatial.new } let(:time) { Danica::Wrapper::Variable.new(name: :t) } context 'when initialized with an empty variable set' do it do - expect(subject.variables.compact).to be_empty + expect(subject.variables.compact).not_to be_empty end end context 'when changing a variable' do before do subject.time = time end - it 'returns the list of variables' do - expect(subject.variables.compact).to eq([ time ]) + it 'returns the list of variables merged default and new variables' do + expect(subject.variables.compact).to eq([ + time, + Danica::Wrapper::Variable.new(name: :acceleration), + Danica::Wrapper::Variable.new(name: :initial_space), + Danica::Wrapper::Variable.new(name: :initial_velocity) + ]) end end context 'when initializing with a variable set' do let(:names) { [ :t, :a, :s0, :v0 ] } @@ -302,62 +348,9 @@ expect do subject.calculate(time: time_value) end.not_to change(subject.time, :valued?) end end - end - end - end - end - - describe 'gauss' do - let(:variables) do - { - x: :x, - median: :u, - variance_root: { latex: '\theta', gnu: :v } - } - end - - subject { described_class::Gauss.new(variables) } - it_behaves_like 'an object that respond to basic_methods' - - describe '#to_tex' do - context 'when creating the spatial operator for constantly accelerated movement' do - let(:expected) { '\frac{1}{\sqrt{2 \cdot \pi \cdot \theta^{2}}} \cdot e^{-\frac{\left(x -u\right)^{2}}{2 \cdot \theta^{2}}}' } - - it 'return the latex format CAM' do - expect(subject.to_tex).to eq(expected) - end - end - end - - describe '#to_gnu' do - context 'when creating the spatial operator for constantly accelerated movement' do - let(:expected) { '(1)/(sqrt(2 * pi * v**(2))) * exp(-((x -u)**(2))/(2 * v**(2)))' } - - it 'return the gnu format CAM' do - expect(subject.to_gnu).to eq(expected) - end - end - end - - context 'when not passing variables' do - subject { described_class::Gauss.new } - - describe '#to_tex' do - let(:expected) { '\frac{1}{\sqrt{2 \cdot \pi \cdot \theta^{2}}} \cdot e^{-\frac{\left(x -u\right)^{2}}{2 \cdot \theta^{2}}}' } - - it 'rely on default variables definition' do - expect(subject.to_tex).to eq(expected) - end - end - - describe '#to_gnu' do - let(:expected) { '(1)/(sqrt(2 * pi * v**(2))) * exp(-((x -u)**(2))/(2 * v**(2)))' } - - it 'rely on default variables definition' do - expect(subject.to_gnu).to eq(expected) end end end end