danica 2.3.1 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/danica.rb +2 -0
- data/lib/danica/common.rb +1 -8
- data/lib/danica/function.rb +14 -0
- data/{spec/support/models/functions → lib/danica/function}/gauss.rb +5 -4
- data/lib/danica/operator/chained.rb +6 -0
- data/lib/danica/variables_holder.rb +14 -10
- data/lib/danica/variables_holder/variables_builder.rb +13 -9
- data/lib/danica/version.rb +1 -1
- data/lib/danica/wrapper.rb +8 -0
- data/spec/lib/danica/dsl_spec.rb +2 -2
- data/spec/lib/danica/function/gauss_spec.rb +54 -0
- data/spec/lib/danica/function_spec.rb +76 -83
- data/spec/lib/danica/operator/addition_spec.rb +6 -0
- data/spec/lib/danica/operator/division_spec.rb +12 -0
- data/spec/lib/danica/operator/multiplication_spec.rb +7 -0
- data/spec/lib/danica/operator_spec.rb +27 -0
- data/spec/lib/danica/variables_holder_spec.rb +150 -0
- metadata +9 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 68db92a2ab4f0816a2678a44e458b7fcbeb87eda
|
4
|
+
data.tar.gz: b83bd4b12f815aae2c7d9eb1f00da008e886e8a5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 819529160a878cbc4b49d5c2323891cd3ddddabbacb68b764d8d5909fe26bcf84c1b1586294ef2449fe9331376893c103b6e4f600b6ea8871f3c82e232544725
|
7
|
+
data.tar.gz: 51d0237f2037afa9c1d4ac1af254d2a93ed9eb0a68341df0fce2830a19c2eb1dc0f5a29f047de83568206883e0d3f06c563b08acdbb24e432f811b8f96ddecf5
|
data/lib/danica.rb
CHANGED
data/lib/danica/common.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
module Danica
|
2
2
|
module Common extend ::ActiveSupport::Concern
|
3
3
|
included do
|
4
|
+
include Wrapper
|
4
5
|
include DSL
|
5
6
|
|
6
7
|
class << self
|
@@ -22,14 +23,6 @@ module Danica
|
|
22
23
|
|
23
24
|
private
|
24
25
|
|
25
|
-
def wrap_value(value)
|
26
|
-
return wrap_value(number(value)) if value.is_a?(Numeric)
|
27
|
-
return wrap_value(variable(value)) if value.is_a?(Hash)
|
28
|
-
return wrap_value(variable(name: value)) if [ String, Symbol ].any? { |c| value.is_a?(c) }
|
29
|
-
return wrap_value(variable) if value.nil?
|
30
|
-
value
|
31
|
-
end
|
32
|
-
|
33
26
|
def wrap_as_group(value)
|
34
27
|
return value if is_grouped? || value.priority >= priority
|
35
28
|
group(value)
|
data/lib/danica/function.rb
CHANGED
@@ -23,6 +23,10 @@ module Danica
|
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
+
def self.create(*vars, &block)
|
27
|
+
build(*vars, &block).new
|
28
|
+
end
|
29
|
+
|
26
30
|
def initialize(*args)
|
27
31
|
options = args.extract_options!
|
28
32
|
|
@@ -40,5 +44,15 @@ module Danica
|
|
40
44
|
|
41
45
|
self.class.new(vars_map).to_f
|
42
46
|
end
|
47
|
+
|
48
|
+
def describe_tex
|
49
|
+
"#{name}(#{variables.map(&:to_tex).join(', ')}) = #{to_tex}"
|
50
|
+
end
|
51
|
+
|
52
|
+
def describe_gnu
|
53
|
+
"#{name}(#{variables.map(&:to_gnu).join(', ')}) = #{to_gnu}"
|
54
|
+
end
|
55
|
+
|
56
|
+
autoload :Gauss, 'danica/function/gauss'
|
43
57
|
end
|
44
58
|
end
|
@@ -1,9 +1,9 @@
|
|
1
1
|
module Danica
|
2
|
-
class Function::Gauss < Function.build(:x,
|
2
|
+
class Function::Gauss < Function.build(:x, average: { latex: '\mu', gnu: :u }, variance_root: { latex: '\sigma', gnu: :v }) { multiplication(elements) }
|
3
3
|
|
4
4
|
private
|
5
5
|
|
6
|
-
def
|
6
|
+
def elements
|
7
7
|
[
|
8
8
|
division(1, denominator),
|
9
9
|
exponential(exp)
|
@@ -11,8 +11,9 @@ module Danica
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def denominator
|
14
|
+
variance_root *
|
14
15
|
squared_root(
|
15
|
-
multiplication(2, PI
|
16
|
+
multiplication(2, PI),
|
16
17
|
)
|
17
18
|
end
|
18
19
|
|
@@ -20,7 +21,7 @@ module Danica
|
|
20
21
|
negative(
|
21
22
|
division(
|
22
23
|
power(group(
|
23
|
-
addition(x, negative(
|
24
|
+
addition(x, negative(average))
|
24
25
|
), 2),
|
25
26
|
multiplication(2, variance)
|
26
27
|
)
|
@@ -9,29 +9,33 @@ module Danica
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def variables_names
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
variables_hash.keys
|
13
|
+
end
|
14
|
+
|
15
|
+
def variables_hash
|
16
|
+
@variables_hash ||= (superclass.try(:variables_hash) || {}).dup
|
15
17
|
end
|
16
18
|
end
|
17
19
|
end
|
18
20
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
@
|
21
|
+
def variables=(vars)
|
22
|
+
vars = vars.as_hash(self.class.variables_names).compact unless vars.is_a? Hash
|
23
|
+
vars = vars.change_values(skip_inner: false) { |v| wrap_value(v) }
|
24
|
+
@variables_hash = self.class.variables_hash.merge(vars)
|
23
25
|
end
|
24
26
|
|
25
27
|
def variables
|
26
|
-
|
28
|
+
variables_hash.values
|
27
29
|
end
|
28
30
|
|
29
31
|
def variables_hash
|
30
|
-
@
|
32
|
+
@variables_hash ||= {}.merge(self.class.variables_hash)
|
31
33
|
end
|
32
34
|
|
33
35
|
def variables_value_hash
|
34
|
-
variables.map
|
36
|
+
variables.map do |var|
|
37
|
+
var.try(:value)
|
38
|
+
end.as_hash(self.class.variables_names)
|
35
39
|
end
|
36
40
|
|
37
41
|
private
|
@@ -1,5 +1,8 @@
|
|
1
1
|
module Danica::VariablesHolder
|
2
2
|
class VariablesBuilder
|
3
|
+
include Danica::DSL
|
4
|
+
include Danica::Wrapper
|
5
|
+
|
3
6
|
attr_reader :instance, :attr_names
|
4
7
|
|
5
8
|
def initialize(attr_names, instance)
|
@@ -8,16 +11,18 @@ module Danica::VariablesHolder
|
|
8
11
|
end
|
9
12
|
|
10
13
|
def build
|
11
|
-
attr_names.extract_options
|
14
|
+
names_hash = attr_names.extract_options!
|
15
|
+
|
16
|
+
attr_names.each do |name|
|
12
17
|
add_setter(name)
|
13
|
-
add_reader(name
|
14
|
-
instance.
|
18
|
+
add_reader(name)
|
19
|
+
instance.variables_hash[name.to_sym] = wrap_value(name)
|
15
20
|
end
|
16
21
|
|
17
|
-
|
22
|
+
names_hash.each do |name, default|
|
18
23
|
add_setter(name)
|
19
|
-
add_reader(name
|
20
|
-
instance.
|
24
|
+
add_reader(name)
|
25
|
+
instance.variables_hash[name.to_sym] = wrap_value(default)
|
21
26
|
end
|
22
27
|
end
|
23
28
|
|
@@ -26,13 +31,12 @@ module Danica::VariablesHolder
|
|
26
31
|
def add_setter(name)
|
27
32
|
instance.send(:define_method, "#{name}=") do |value|
|
28
33
|
variables_hash[name.to_sym] = wrap_value(value)
|
29
|
-
@variables = variables_hash.values
|
30
34
|
end
|
31
35
|
end
|
32
36
|
|
33
|
-
def add_reader(name
|
37
|
+
def add_reader(name)
|
34
38
|
instance.send(:define_method, name) do
|
35
|
-
variables_hash[name.to_sym]
|
39
|
+
variables_hash[name.to_sym]
|
36
40
|
end
|
37
41
|
end
|
38
42
|
end
|
data/lib/danica/version.rb
CHANGED
data/lib/danica/wrapper.rb
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
module Danica
|
2
2
|
module Wrapper
|
3
|
+
def wrap_value(value)
|
4
|
+
return wrap_value(number(value)) if value.is_a?(Numeric)
|
5
|
+
return wrap_value(variable(value)) if value.is_a?(Hash)
|
6
|
+
return wrap_value(variable(name: value)) if [ String, Symbol ].any? { |c| value.is_a?(c) }
|
7
|
+
return wrap_value(variable) if value.nil?
|
8
|
+
value
|
9
|
+
end
|
10
|
+
|
3
11
|
autoload :Number, 'danica/wrapper/number'
|
4
12
|
autoload :Group, 'danica/wrapper/group'
|
5
13
|
autoload :Negative, 'danica/wrapper/negative'
|
data/spec/lib/danica/dsl_spec.rb
CHANGED
@@ -17,8 +17,6 @@ shared_context 'a class with mapped dsl' do
|
|
17
17
|
multiplication: Danica::Operator::Multiplication,
|
18
18
|
product: Danica::Operator::Multiplication,
|
19
19
|
division: Danica::Operator::Division,
|
20
|
-
sin: Danica::Operator::Sin,
|
21
|
-
cos: Danica::Operator::Cos,
|
22
20
|
power: Danica::Operator::Power
|
23
21
|
}.each do |aliaz, clazz|
|
24
22
|
it_behaves_like 'a class with alias to a clazz', aliaz, clazz, 2, 3
|
@@ -26,6 +24,8 @@ shared_context 'a class with mapped dsl' do
|
|
26
24
|
{
|
27
25
|
squared_root: Danica::Operator::SquaredRoot,
|
28
26
|
exponential: Danica::Operator::Exponential,
|
27
|
+
sin: Danica::Operator::Sin,
|
28
|
+
cos: Danica::Operator::Cos,
|
29
29
|
group: Danica::Wrapper::Group,
|
30
30
|
negative: Danica::Wrapper::Negative,
|
31
31
|
number: Danica::Wrapper::Number,
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Danica::Function::Gauss do
|
4
|
+
let(:variables) do
|
5
|
+
{
|
6
|
+
x: :x,
|
7
|
+
average: :u,
|
8
|
+
variance_root: { latex: '\theta', gnu: :v }
|
9
|
+
}
|
10
|
+
end
|
11
|
+
|
12
|
+
subject { described_class::Gauss.new(variables) }
|
13
|
+
it_behaves_like 'an object that respond to basic_methods'
|
14
|
+
|
15
|
+
describe '#to_tex' do
|
16
|
+
context 'when creating the spatial operator for constantly accelerated movement' do
|
17
|
+
let(:expected) { '\frac{1}{\theta \cdot \sqrt{2 \cdot \pi}} \cdot e^{-\frac{\left(x -u\right)^{2}}{2 \cdot \theta^{2}}}' }
|
18
|
+
|
19
|
+
it 'return the latex format CAM' do
|
20
|
+
expect(subject.to_tex).to eq(expected)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#to_gnu' do
|
26
|
+
context 'when creating the spatial operator for constantly accelerated movement' do
|
27
|
+
let(:expected) { '(1)/(v * sqrt(2 * pi)) * exp(-((x -u)**(2))/(2 * v**(2)))' }
|
28
|
+
|
29
|
+
it 'return the gnu format CAM' do
|
30
|
+
expect(subject.to_gnu).to eq(expected)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'when not passing variables' do
|
36
|
+
subject { described_class::Gauss.new }
|
37
|
+
|
38
|
+
describe '#to_tex' do
|
39
|
+
let(:expected) { '\frac{1}{\sigma \cdot \sqrt{2 \cdot \pi}} \cdot e^{-\frac{\left(x -\mu\right)^{2}}{2 \cdot \sigma^{2}}}' }
|
40
|
+
|
41
|
+
it 'rely on default variables definition' do
|
42
|
+
expect(subject.to_tex).to eq(expected)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe '#to_gnu' do
|
47
|
+
let(:expected) { '(1)/(v * sqrt(2 * pi)) * exp(-((x -u)**(2))/(2 * v**(2)))' }
|
48
|
+
|
49
|
+
it 'rely on default variables definition' do
|
50
|
+
expect(subject.to_gnu).to eq(expected)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -1,39 +1,44 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
described_class.build(*variables) do
|
8
|
-
Danica::Operator::Power.new(x, y)
|
9
|
-
end
|
10
|
-
end
|
11
|
-
let(:function) do
|
12
|
-
function_class.new
|
13
|
-
end
|
3
|
+
shared_examples 'a generically generated function' do
|
4
|
+
it 'returns a function class' do
|
5
|
+
expect(function.class.superclass).to eq(described_class)
|
6
|
+
end
|
14
7
|
|
15
|
-
|
16
|
-
|
8
|
+
it 'returns a class whose instance responds to the variables' do
|
9
|
+
variables.each do |variable|
|
10
|
+
expect(function).to respond_to(variable)
|
17
11
|
end
|
12
|
+
end
|
18
13
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
end
|
23
|
-
end
|
14
|
+
it 'returns a function that uses the block to process to_tex' do
|
15
|
+
expect(function.to_tex).to eq('x^{y}')
|
16
|
+
end
|
24
17
|
|
25
|
-
|
26
|
-
|
27
|
-
|
18
|
+
it 'returns a function that uses the block to process to_gnu' do
|
19
|
+
expect(function.to_gnu).to eq('x**(y)')
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'returns a function thtat knows how to calculate' do
|
23
|
+
expect(function.calculate(x: 2, y: 3)).to eq(8)
|
24
|
+
end
|
25
|
+
end
|
28
26
|
|
29
|
-
|
30
|
-
|
27
|
+
describe Danica::Function do
|
28
|
+
let(:variables) { %i(x y) }
|
29
|
+
let(:function_class) do
|
30
|
+
described_class.build(*variables) do
|
31
|
+
Danica::Operator::Power.new(x, y)
|
31
32
|
end
|
33
|
+
end
|
32
34
|
|
33
|
-
|
34
|
-
|
35
|
+
describe '.build' do
|
36
|
+
let(:function) do
|
37
|
+
function_class.new
|
35
38
|
end
|
36
39
|
|
40
|
+
it_behaves_like 'a generically generated function'
|
41
|
+
|
37
42
|
context 'when no block is given' do
|
38
43
|
let(:function_class) do
|
39
44
|
described_class.build(*variables)
|
@@ -52,7 +57,7 @@ describe Danica::Function do
|
|
52
57
|
end
|
53
58
|
|
54
59
|
it 'has the defined variables' do
|
55
|
-
expect(function.variables_hash).to eq(x:
|
60
|
+
expect(function.variables_hash).to eq(x: Danica::Wrapper::Variable.new(name: :x))
|
56
61
|
end
|
57
62
|
|
58
63
|
context 'when calling to_tex' do
|
@@ -76,7 +81,10 @@ describe Danica::Function do
|
|
76
81
|
end
|
77
82
|
|
78
83
|
it 'has the defined variables' do
|
79
|
-
expect(function.variables_hash).to eq(
|
84
|
+
expect(function.variables_hash).to eq(
|
85
|
+
x: Danica::Wrapper::Variable.new(name: :x),
|
86
|
+
y: Danica::Wrapper::Variable.new(name: :y)
|
87
|
+
)
|
80
88
|
end
|
81
89
|
|
82
90
|
context 'when calling to_tex' do
|
@@ -93,6 +101,39 @@ describe Danica::Function do
|
|
93
101
|
end
|
94
102
|
end
|
95
103
|
|
104
|
+
describe '.create' do
|
105
|
+
let(:function) do
|
106
|
+
described_class.create(*variables) do
|
107
|
+
Danica::Operator::Power.new(x, y)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
it_behaves_like 'a generically generated function'
|
111
|
+
end
|
112
|
+
|
113
|
+
describe '#describe_tex' do
|
114
|
+
context 'when function has a name' do
|
115
|
+
let(:function) do
|
116
|
+
function_class.new(name: :f)
|
117
|
+
end
|
118
|
+
|
119
|
+
it 'returns the full function description' do
|
120
|
+
expect(function.describe_tex).to eq('f(x, y) = x^{y}')
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
describe '#describe_gnu' do
|
126
|
+
context 'when function has a name' do
|
127
|
+
let(:function) do
|
128
|
+
function_class.new(name: :f)
|
129
|
+
end
|
130
|
+
|
131
|
+
it 'returns the full function description' do
|
132
|
+
expect(function.describe_gnu).to eq('f(x, y) = x**(y)')
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
96
137
|
describe 'spatial' do
|
97
138
|
let(:variables) do
|
98
139
|
{
|
@@ -219,7 +260,7 @@ describe Danica::Function do
|
|
219
260
|
|
220
261
|
context 'when initialized with an empty variable set' do
|
221
262
|
it do
|
222
|
-
expect(subject.variables.compact).
|
263
|
+
expect(subject.variables.compact).not_to be_empty
|
223
264
|
end
|
224
265
|
end
|
225
266
|
|
@@ -228,8 +269,13 @@ describe Danica::Function do
|
|
228
269
|
subject.time = time
|
229
270
|
end
|
230
271
|
|
231
|
-
it 'returns the list of variables' do
|
232
|
-
expect(subject.variables.compact).to eq([
|
272
|
+
it 'returns the list of variables merged default and new variables' do
|
273
|
+
expect(subject.variables.compact).to eq([
|
274
|
+
time,
|
275
|
+
Danica::Wrapper::Variable.new(name: :acceleration),
|
276
|
+
Danica::Wrapper::Variable.new(name: :initial_space),
|
277
|
+
Danica::Wrapper::Variable.new(name: :initial_velocity)
|
278
|
+
])
|
233
279
|
end
|
234
280
|
end
|
235
281
|
|
@@ -309,59 +355,6 @@ describe Danica::Function do
|
|
309
355
|
end
|
310
356
|
end
|
311
357
|
|
312
|
-
describe 'gauss' do
|
313
|
-
let(:variables) do
|
314
|
-
{
|
315
|
-
x: :x,
|
316
|
-
median: :u,
|
317
|
-
variance_root: { latex: '\theta', gnu: :v }
|
318
|
-
}
|
319
|
-
end
|
320
|
-
|
321
|
-
subject { described_class::Gauss.new(variables) }
|
322
|
-
it_behaves_like 'an object that respond to basic_methods'
|
323
|
-
|
324
|
-
describe '#to_tex' do
|
325
|
-
context 'when creating the spatial operator for constantly accelerated movement' do
|
326
|
-
let(:expected) { '\frac{1}{\sqrt{2 \cdot \pi \cdot \theta^{2}}} \cdot e^{-\frac{\left(x -u\right)^{2}}{2 \cdot \theta^{2}}}' }
|
327
|
-
|
328
|
-
it 'return the latex format CAM' do
|
329
|
-
expect(subject.to_tex).to eq(expected)
|
330
|
-
end
|
331
|
-
end
|
332
|
-
end
|
333
|
-
|
334
|
-
describe '#to_gnu' do
|
335
|
-
context 'when creating the spatial operator for constantly accelerated movement' do
|
336
|
-
let(:expected) { '(1)/(sqrt(2 * pi * v**(2))) * exp(-((x -u)**(2))/(2 * v**(2)))' }
|
337
|
-
|
338
|
-
it 'return the gnu format CAM' do
|
339
|
-
expect(subject.to_gnu).to eq(expected)
|
340
|
-
end
|
341
|
-
end
|
342
|
-
end
|
343
|
-
|
344
|
-
context 'when not passing variables' do
|
345
|
-
subject { described_class::Gauss.new }
|
346
|
-
|
347
|
-
describe '#to_tex' do
|
348
|
-
let(:expected) { '\frac{1}{\sqrt{2 \cdot \pi \cdot \theta^{2}}} \cdot e^{-\frac{\left(x -u\right)^{2}}{2 \cdot \theta^{2}}}' }
|
349
|
-
|
350
|
-
it 'rely on default variables definition' do
|
351
|
-
expect(subject.to_tex).to eq(expected)
|
352
|
-
end
|
353
|
-
end
|
354
|
-
|
355
|
-
describe '#to_gnu' do
|
356
|
-
let(:expected) { '(1)/(sqrt(2 * pi * v**(2))) * exp(-((x -u)**(2))/(2 * v**(2)))' }
|
357
|
-
|
358
|
-
it 'rely on default variables definition' do
|
359
|
-
expect(subject.to_gnu).to eq(expected)
|
360
|
-
end
|
361
|
-
end
|
362
|
-
end
|
363
|
-
end
|
364
|
-
|
365
358
|
describe 'baskara' do
|
366
359
|
context 'when using the default value for variables' do
|
367
360
|
subject { described_class::Baskara.new }
|
@@ -3,6 +3,12 @@ require 'spec_helper'
|
|
3
3
|
describe Danica::Operator::Addition do
|
4
4
|
subject { described_class.new(10, 2) }
|
5
5
|
|
6
|
+
it 'initializes from array' do
|
7
|
+
expect do
|
8
|
+
described_class.new(10, 2)
|
9
|
+
end.not_to raise_error
|
10
|
+
end
|
11
|
+
|
6
12
|
it_behaves_like 'an object that respond to basic_methods'
|
7
13
|
|
8
14
|
it_behaves_like 'an object with basic operation', ignore: %i(+ -)
|
@@ -4,6 +4,18 @@ describe Danica::Operator::Division do
|
|
4
4
|
let(:variables) { [2, 4] }
|
5
5
|
subject { described_class.new(*variables) }
|
6
6
|
|
7
|
+
describe '#numerator' do
|
8
|
+
it 'returns the numerator' do
|
9
|
+
expect(subject.numerator).to eq(Danica::Wrapper::Number.new(2))
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#denominator' do
|
14
|
+
it 'returns the denominator' do
|
15
|
+
expect(subject.denominator).to eq(Danica::Wrapper::Number.new(4))
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
7
19
|
it_behaves_like 'an object that respond to basic_methods'
|
8
20
|
it_behaves_like 'an object with basic operation'
|
9
21
|
|
@@ -2,6 +2,13 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Danica::Operator::Multiplication do
|
4
4
|
subject { described_class.new(2,4) }
|
5
|
+
|
6
|
+
it 'initializes from array' do
|
7
|
+
expect do
|
8
|
+
described_class.new(10, 2)
|
9
|
+
end.not_to raise_error
|
10
|
+
end
|
11
|
+
|
5
12
|
it_behaves_like 'an object that respond to basic_methods'
|
6
13
|
|
7
14
|
it_behaves_like 'an object with * operation' do
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Danica::Operator::Dummy < Danica::Operator
|
4
|
+
variables :a, :b
|
5
|
+
end
|
6
|
+
|
7
|
+
describe Danica::Operator do
|
8
|
+
let(:variables) { [2, 4] }
|
9
|
+
let(:clazz) { described_class::Dummy }
|
10
|
+
subject { clazz.new(*variables) }
|
11
|
+
|
12
|
+
describe 'variables assignment' do
|
13
|
+
it 'assignes the variables tpo its places' do
|
14
|
+
expect(subject.a).to eq(Danica::Wrapper::Number.new(2))
|
15
|
+
expect(subject.b).to eq(Danica::Wrapper::Number.new(4))
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'when initializing with a hash' do
|
19
|
+
let(:variables) { [{ name: :A, value: 2 }, { name: :B, value: 4 }] }
|
20
|
+
|
21
|
+
it 'assignes the variables tpo its places' do
|
22
|
+
expect(subject.a).to eq(Danica::Wrapper::Variable.new(name: :A, value: 2))
|
23
|
+
expect(subject.b).to eq(Danica::Wrapper::Variable.new(name: :B, value: 4))
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,150 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
shared_examples 'an object that returns the default variables hash' do
|
4
|
+
it 'returns the default variables hash' do
|
5
|
+
expect(subject.variables_hash).to eq(
|
6
|
+
x: Danica::Wrapper::Variable.new(name: :x),
|
7
|
+
y: Danica::Wrapper::Variable.new(latex: '\y'),
|
8
|
+
z: Danica::Wrapper::Number.new(10)
|
9
|
+
)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
shared_examples 'an object that returns the default variables' do
|
14
|
+
it 'returns the default variables hash' do
|
15
|
+
expect(subject.variables).to eq([
|
16
|
+
Danica::Wrapper::Variable.new(name: :x),
|
17
|
+
Danica::Wrapper::Variable.new(latex: '\y'),
|
18
|
+
Danica::Wrapper::Number.new(10)
|
19
|
+
])
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe Danica::VariablesHolder do
|
24
|
+
class Danica::VariablesHolder::Dummy
|
25
|
+
include Danica::Common
|
26
|
+
include Danica::VariablesHolder
|
27
|
+
|
28
|
+
variables :x, y: { latex: '\y' }, z: 10
|
29
|
+
end
|
30
|
+
|
31
|
+
let(:clazz) { described_class::Dummy }
|
32
|
+
subject do
|
33
|
+
clazz.new
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'creates setters and getters for the variables' do
|
37
|
+
%i(x y z).each do |var|
|
38
|
+
expect(subject).to respond_to(var)
|
39
|
+
expect(subject).to respond_to("#{var}=")
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe '.variables_names' do
|
44
|
+
it 'returns the list of variables pre configured' do
|
45
|
+
expect(clazz.variables_names).to eq(%i(x y z))
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe '#variables=' do
|
50
|
+
before do
|
51
|
+
subject.variables = variables
|
52
|
+
end
|
53
|
+
|
54
|
+
context 'when setting the variables using a list' do
|
55
|
+
let(:variables) { [1, 2, 3] }
|
56
|
+
|
57
|
+
it 'changes the values of the variables' do
|
58
|
+
expect(subject.x).to eq(Danica::Wrapper::Number.new(1))
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'but the array is empty' do
|
62
|
+
let(:variables) { [] }
|
63
|
+
|
64
|
+
it_behaves_like 'an object that returns the default variables hash'
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'when setting the variales using a hash' do
|
69
|
+
let(:variables) { { x: 1, y: 2, z: 3 } }
|
70
|
+
|
71
|
+
it 'changes the values of the variables' do
|
72
|
+
expect(subject.x).to eq(Danica::Wrapper::Number.new(1))
|
73
|
+
end
|
74
|
+
|
75
|
+
context 'but the hash is empty' do
|
76
|
+
let(:variables) { {} }
|
77
|
+
|
78
|
+
it_behaves_like 'an object that returns the default variables hash'
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe '#variables' do
|
84
|
+
context 'when instance has no variables defined' do
|
85
|
+
it do
|
86
|
+
expect(subject.variables).not_to be_empty
|
87
|
+
end
|
88
|
+
|
89
|
+
it_behaves_like 'an object that returns the default variables'
|
90
|
+
end
|
91
|
+
|
92
|
+
context 'when some variables where defined' do
|
93
|
+
before do
|
94
|
+
subject.y = 1
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'returns the default variables and the new set one' do
|
98
|
+
expect(subject.variables).to eq([
|
99
|
+
Danica::Wrapper::Variable.new(name: :x),
|
100
|
+
Danica::Wrapper::Number.new(1),
|
101
|
+
Danica::Wrapper::Number.new(10)
|
102
|
+
])
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'does not change the class variables' do
|
106
|
+
expect do
|
107
|
+
subject.z = 2
|
108
|
+
end.not_to change { clazz.variables_hash }
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
describe '#variables_hash' do
|
114
|
+
context 'when instance has no variables defined' do
|
115
|
+
it_behaves_like 'an object that returns the default variables hash'
|
116
|
+
end
|
117
|
+
|
118
|
+
context 'when some variables where defined' do
|
119
|
+
before do
|
120
|
+
subject.y = 1
|
121
|
+
end
|
122
|
+
|
123
|
+
it 'returns the default hash with the set value' do
|
124
|
+
expect(subject.variables_hash).to eq(
|
125
|
+
x: Danica::Wrapper::Variable.new(name: :x),
|
126
|
+
y: Danica::Wrapper::Number.new(1),
|
127
|
+
z: Danica::Wrapper::Number.new(10)
|
128
|
+
)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
describe '#variables_value_hash' do
|
134
|
+
context 'when instance has no variables defined' do
|
135
|
+
it 'returns the default value' do
|
136
|
+
expect(subject.variables_value_hash).to eq({x: nil, y: nil, z: 10})
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
context 'when some of the variables have been valued' do
|
141
|
+
before do
|
142
|
+
subject.y = 1
|
143
|
+
end
|
144
|
+
|
145
|
+
it 'returns the values' do
|
146
|
+
expect(subject.variables_value_hash).to eq({x: nil, y: 1, z: 10})
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: danica
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Darthjee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-10-
|
11
|
+
date: 2017-10-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -159,6 +159,7 @@ files:
|
|
159
159
|
- lib/danica/dsl.rb
|
160
160
|
- lib/danica/exception.rb
|
161
161
|
- lib/danica/function.rb
|
162
|
+
- lib/danica/function/gauss.rb
|
162
163
|
- lib/danica/operator.rb
|
163
164
|
- lib/danica/operator/addition.rb
|
164
165
|
- lib/danica/operator/chained.rb
|
@@ -186,6 +187,7 @@ files:
|
|
186
187
|
- spec/integration/plus_minus_spec.rb
|
187
188
|
- spec/integration/power_spec.rb
|
188
189
|
- spec/lib/danica/dsl_spec.rb
|
190
|
+
- spec/lib/danica/function/gauss_spec.rb
|
189
191
|
- spec/lib/danica/function_spec.rb
|
190
192
|
- spec/lib/danica/operator/addition_spec.rb
|
191
193
|
- spec/lib/danica/operator/cos_spec.rb
|
@@ -195,6 +197,8 @@ files:
|
|
195
197
|
- spec/lib/danica/operator/power_spec.rb
|
196
198
|
- spec/lib/danica/operator/sin_spec.rb
|
197
199
|
- spec/lib/danica/operator/squared_root_spec.rb
|
200
|
+
- spec/lib/danica/operator_spec.rb
|
201
|
+
- spec/lib/danica/variables_holder_spec.rb
|
198
202
|
- spec/lib/danica/wrapper/constant_spec.rb
|
199
203
|
- spec/lib/danica/wrapper/group_spec.rb
|
200
204
|
- spec/lib/danica/wrapper/negative_spec.rb
|
@@ -204,7 +208,6 @@ files:
|
|
204
208
|
- spec/lib/danica_spec.rb
|
205
209
|
- spec/spec_helper.rb
|
206
210
|
- spec/support/models/functions/baskara.rb
|
207
|
-
- spec/support/models/functions/gauss.rb
|
208
211
|
- spec/support/models/functions/hyperbole.rb
|
209
212
|
- spec/support/models/functions/spatial.rb
|
210
213
|
- spec/support/shared_contexts/common.rb
|
@@ -244,6 +247,7 @@ test_files:
|
|
244
247
|
- spec/integration/plus_minus_spec.rb
|
245
248
|
- spec/integration/power_spec.rb
|
246
249
|
- spec/lib/danica/dsl_spec.rb
|
250
|
+
- spec/lib/danica/function/gauss_spec.rb
|
247
251
|
- spec/lib/danica/function_spec.rb
|
248
252
|
- spec/lib/danica/operator/addition_spec.rb
|
249
253
|
- spec/lib/danica/operator/cos_spec.rb
|
@@ -253,6 +257,8 @@ test_files:
|
|
253
257
|
- spec/lib/danica/operator/power_spec.rb
|
254
258
|
- spec/lib/danica/operator/sin_spec.rb
|
255
259
|
- spec/lib/danica/operator/squared_root_spec.rb
|
260
|
+
- spec/lib/danica/operator_spec.rb
|
261
|
+
- spec/lib/danica/variables_holder_spec.rb
|
256
262
|
- spec/lib/danica/wrapper/constant_spec.rb
|
257
263
|
- spec/lib/danica/wrapper/group_spec.rb
|
258
264
|
- spec/lib/danica/wrapper/negative_spec.rb
|
@@ -262,7 +268,6 @@ test_files:
|
|
262
268
|
- spec/lib/danica_spec.rb
|
263
269
|
- spec/spec_helper.rb
|
264
270
|
- spec/support/models/functions/baskara.rb
|
265
|
-
- spec/support/models/functions/gauss.rb
|
266
271
|
- spec/support/models/functions/hyperbole.rb
|
267
272
|
- spec/support/models/functions/spatial.rb
|
268
273
|
- spec/support/shared_contexts/common.rb
|