danica 0.3.0 → 1.0.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/Gemfile.lock +4 -1
- data/README.md +20 -4
- data/danica.gemspec +1 -0
- data/lib/danica.rb +1 -1
- data/lib/danica/exception.rb +4 -0
- data/lib/danica/exception/not_defined.rb +2 -0
- data/lib/danica/function.rb +55 -37
- data/lib/danica/function/chained.rb +6 -2
- data/lib/danica/function/division.rb +10 -6
- data/lib/danica/function/power.rb +10 -6
- data/lib/danica/function/product.rb +4 -0
- data/lib/danica/function/square_root.rb +9 -6
- data/lib/danica/function/sum.rb +4 -0
- data/lib/danica/number.rb +2 -0
- data/lib/danica/variable.rb +13 -3
- data/lib/danica/version.rb +1 -1
- data/spec/lib/danica/function/division_spec.rb +14 -83
- data/spec/lib/danica/function/power_spec.rb +14 -83
- data/spec/lib/danica/function/product_spec.rb +11 -4
- data/spec/lib/danica/function/square_root_spec.rb +21 -3
- data/spec/lib/danica/function/sum_spec.rb +11 -4
- data/spec/lib/danica/function_spec.rb +22 -11
- data/spec/lib/danica/number_spec.rb +50 -0
- data/spec/lib/danica/variable_spec.rb +7 -19
- data/spec/spec_helper.rb +5 -0
- data/spec/support/shared_examples/{function.rb → function/chained.rb} +24 -14
- data/spec/support/shared_examples/function/dual_term.rb +116 -0
- data/spec/support/shared_examples/variable.rb +28 -0
- metadata +26 -5
- data/lib/danica/not_defined.rb +0 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d3f27550f7488c80de59bfa28553dd0922778607
|
4
|
+
data.tar.gz: 9dfbb7cb1c84689e5798305c9cc255294ee1938f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cfb395f94ecd24d165a833cd4c88991538378a8789ae892aadc34335e51cfdb52bc4a35f17b4b3a3898797e14bb9d81be6d685c6835448ecef0ac964641a4883
|
7
|
+
data.tar.gz: 57bb42340df18ee9e09233c89917452f65bf069f2ca93ee926bcf40296300db2a8fb11604a265fa09ff6ff9325f2d536350529ee15607e61041861ba5426e177
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
danica (0.
|
4
|
+
danica (1.0.0)
|
5
5
|
activemodel
|
6
6
|
activesupport
|
7
7
|
|
@@ -15,6 +15,8 @@ GEM
|
|
15
15
|
i18n (~> 0.7)
|
16
16
|
minitest (~> 5.1)
|
17
17
|
tzinfo (~> 1.1)
|
18
|
+
codeclimate-test-reporter (0.5.0)
|
19
|
+
simplecov (>= 0.7.1, < 1.0.0)
|
18
20
|
coderay (1.1.1)
|
19
21
|
concurrent-ruby (1.0.2)
|
20
22
|
diff-lcs (1.2.5)
|
@@ -53,6 +55,7 @@ PLATFORMS
|
|
53
55
|
|
54
56
|
DEPENDENCIES
|
55
57
|
bundler (~> 1.6)
|
58
|
+
codeclimate-test-reporter
|
56
59
|
danica!
|
57
60
|
pry-nav
|
58
61
|
rake
|
data/README.md
CHANGED
@@ -1,4 +1,7 @@
|
|
1
1
|
# danica
|
2
|
+
[](https://codeclimate.com/github/darthjee/danica)
|
3
|
+
[](https://codeclimate.com/github/darthjee/danica/coverage)
|
4
|
+
|
2
5
|
A tool for math formulation on docs
|
3
6
|
|
4
7
|
## How to use
|
@@ -60,12 +63,16 @@ class Danica::Function
|
|
60
63
|
end
|
61
64
|
end
|
62
65
|
|
63
|
-
Danica::Function.new(
|
66
|
+
fx = Danica::Function.new(
|
64
67
|
time: :t,
|
65
68
|
acceleration: 'a',
|
66
|
-
initial_space: { name: :S0, latex: 'S_0' },
|
67
|
-
initial_velocity: { name: :V0, latex: 'V_0' }
|
68
|
-
)
|
69
|
+
initial_space: { name: :S0, latex: 'S_0', gnu: 'S0' },
|
70
|
+
initial_velocity: { name: :V0, latex: 'V_0', gnu: 'V0' }
|
71
|
+
)
|
72
|
+
```
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
fx.to_tex
|
69
76
|
```
|
70
77
|
|
71
78
|
returns
|
@@ -73,3 +80,12 @@ returns
|
|
73
80
|
S_0 + V_0 \cdot t + \frac{a \cdot t^2}{2}
|
74
81
|
```
|
75
82
|
|
83
|
+
```ruby
|
84
|
+
fx.to_gnu
|
85
|
+
```
|
86
|
+
|
87
|
+
returns
|
88
|
+
```string
|
89
|
+
S0 + V0 * t + a * t**2/2
|
90
|
+
```
|
91
|
+
|
data/danica.gemspec
CHANGED
data/lib/danica.rb
CHANGED
data/lib/danica/function.rb
CHANGED
@@ -1,39 +1,57 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
1
|
+
module Danica
|
2
|
+
class Function
|
3
|
+
include ActiveModel::Model
|
4
|
+
|
5
|
+
require 'danica/function/chained'
|
6
|
+
require 'danica/function/product'
|
7
|
+
require 'danica/function/sum'
|
8
|
+
require 'danica/function/division'
|
9
|
+
require 'danica/function/power'
|
10
|
+
require 'danica/function/square_root'
|
11
|
+
|
12
|
+
attr_accessor :name, :variables
|
13
|
+
|
14
|
+
def to_f
|
15
|
+
raise 'Not IMplemented yet'
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_tex
|
19
|
+
Number.new(to_f).to_tex
|
20
|
+
rescue Exception::NotDefined
|
21
|
+
tex_string
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_gnu
|
25
|
+
Number.new(to_f).to_gnu
|
26
|
+
rescue Exception::NotDefined
|
27
|
+
gnu_string
|
28
|
+
end
|
29
|
+
|
30
|
+
def variables=(variables)
|
31
|
+
@variables = variables.map { |v| wrap_value(v) }
|
32
|
+
end
|
33
|
+
|
34
|
+
def valued?
|
35
|
+
to_f.presend?
|
36
|
+
rescue Exception::NotDefined
|
37
|
+
false
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def tex_string
|
43
|
+
raise 'Not IMplemented yet'
|
44
|
+
end
|
45
|
+
|
46
|
+
def gnu_string
|
47
|
+
raise 'Not IMplemented yet'
|
48
|
+
end
|
49
|
+
|
50
|
+
def wrap_value(value)
|
51
|
+
return Number.new(value) if value.is_a?(Numeric)
|
52
|
+
return Variable.new(value) if value.is_a?(Hash)
|
53
|
+
return Variable.new(name: value) if [ String, Symbol ].any? { |c| value.is_a?(c) }
|
54
|
+
value
|
55
|
+
end
|
38
56
|
end
|
39
57
|
end
|
@@ -5,11 +5,15 @@ module Danica
|
|
5
5
|
chain(variables.map(&:to_f))
|
6
6
|
end
|
7
7
|
|
8
|
-
|
8
|
+
private
|
9
|
+
|
10
|
+
def tex_string
|
9
11
|
(numeric_to_tex + non_numeric_variables.map(&:to_tex)).join(" #{tex_symbol} ")
|
10
12
|
end
|
11
13
|
|
12
|
-
|
14
|
+
def gnu_string
|
15
|
+
(numeric_to_tex + non_numeric_variables.map(&:to_gnu)).join(" #{gnu_symbol} ")
|
16
|
+
end
|
13
17
|
|
14
18
|
def numeric_variables
|
15
19
|
variables.select { |v| v.valued? }
|
@@ -7,12 +7,6 @@ module Danica
|
|
7
7
|
numerator.to_f / denominator.to_f
|
8
8
|
end
|
9
9
|
|
10
|
-
def to_tex
|
11
|
-
Number.new(to_f).to_tex
|
12
|
-
rescue NotDefined
|
13
|
-
"\\frac{#{numerator.to_tex}}{#{denominator.to_tex}}"
|
14
|
-
end
|
15
|
-
|
16
10
|
def numerator=(value)
|
17
11
|
@numerator = wrap_value(value)
|
18
12
|
end
|
@@ -20,6 +14,16 @@ module Danica
|
|
20
14
|
def denominator=(value)
|
21
15
|
@denominator = wrap_value(value)
|
22
16
|
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def tex_string
|
21
|
+
"\\frac{#{numerator.to_tex}}{#{denominator.to_tex}}"
|
22
|
+
end
|
23
|
+
|
24
|
+
def gnu_string
|
25
|
+
"#{numerator.to_gnu}/#{denominator.to_gnu}"
|
26
|
+
end
|
23
27
|
end
|
24
28
|
end
|
25
29
|
end
|
@@ -7,12 +7,6 @@ module Danica
|
|
7
7
|
base.to_f ** exponent.to_f
|
8
8
|
end
|
9
9
|
|
10
|
-
def to_tex
|
11
|
-
Number.new(to_f).to_tex
|
12
|
-
rescue NotDefined
|
13
|
-
"#{base.to_tex}^{#{exponent.to_tex}}"
|
14
|
-
end
|
15
|
-
|
16
10
|
def base=(value)
|
17
11
|
@base = wrap_value(value)
|
18
12
|
end
|
@@ -20,6 +14,16 @@ module Danica
|
|
20
14
|
def exponent=(value)
|
21
15
|
@exponent = wrap_value(value)
|
22
16
|
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def tex_string
|
21
|
+
"#{base.to_tex}^{#{exponent.to_tex}}"
|
22
|
+
end
|
23
|
+
|
24
|
+
def gnu_string
|
25
|
+
"#{base.to_gnu}**#{exponent.to_gnu}"
|
26
|
+
end
|
23
27
|
end
|
24
28
|
end
|
25
29
|
end
|
@@ -7,16 +7,19 @@ module Danica
|
|
7
7
|
Math.sqrt(variable.to_f)
|
8
8
|
end
|
9
9
|
|
10
|
-
def to_tex
|
11
|
-
Number.new(to_f).to_tex
|
12
|
-
rescue NotDefined
|
13
|
-
"\\sqrt{#{variable.to_tex}}"
|
14
|
-
end
|
15
|
-
|
16
10
|
def variable=(value)
|
17
11
|
@variable = wrap_value(value)
|
18
12
|
end
|
19
13
|
|
14
|
+
private
|
15
|
+
|
16
|
+
def tex_string
|
17
|
+
"\\sqrt{#{variable.to_tex}}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def gnu_string
|
21
|
+
"sqrt(#{variable.to_gnu})"
|
22
|
+
end
|
20
23
|
end
|
21
24
|
end
|
22
25
|
end
|
data/lib/danica/function/sum.rb
CHANGED
data/lib/danica/number.rb
CHANGED
data/lib/danica/variable.rb
CHANGED
@@ -1,19 +1,29 @@
|
|
1
1
|
module Danica
|
2
2
|
class Variable
|
3
3
|
include ActiveModel::Model
|
4
|
-
attr_accessor :value, :name, :latex
|
4
|
+
attr_accessor :value, :name, :latex, :gnu
|
5
5
|
|
6
6
|
def to_f
|
7
|
-
value.nil? ? raise(NotDefined) : value.to_f
|
7
|
+
value.nil? ? raise(Exception::NotDefined) : value.to_f
|
8
8
|
end
|
9
9
|
|
10
10
|
def to_tex
|
11
|
-
|
11
|
+
return value.to_tex if value
|
12
|
+
(latex || name).to_s
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_gnu
|
16
|
+
return value.to_gnu if value
|
17
|
+
(gnu || name).to_s
|
12
18
|
end
|
13
19
|
|
14
20
|
def valued?
|
15
21
|
value.present?
|
16
22
|
end
|
23
|
+
|
24
|
+
def value=(value)
|
25
|
+
@value = value.is_a?(Numeric) ? Number.new(value) : value
|
26
|
+
end
|
17
27
|
end
|
18
28
|
end
|
19
29
|
|
data/lib/danica/version.rb
CHANGED
@@ -1,92 +1,23 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Danica::Function::Division do
|
4
|
-
let(:variables_number) { 4 }
|
5
|
-
let(:values) { [ 2, 4 ] }
|
6
|
-
let(:variables) do
|
7
|
-
[ 1, 2 ].map do |i|
|
8
|
-
{ name: "X#{i}", value: values[i-1] }
|
9
|
-
end
|
10
|
-
end
|
11
4
|
let(:subject) do
|
12
5
|
described_class.new(numerator: variables[0], denominator: variables[1])
|
13
6
|
end
|
14
7
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
it 'returns the division of the values' do
|
30
|
-
expect(subject.to_f).to eq(1.0 / 2.0)
|
31
|
-
end
|
32
|
-
|
33
|
-
it do
|
34
|
-
expect(subject.to_f).to be_a(Float)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
context 'when one the variables are numbers' do
|
39
|
-
before do
|
40
|
-
variables[0] = 2
|
41
|
-
end
|
42
|
-
|
43
|
-
it 'returns the division of the values' do
|
44
|
-
expect(subject.to_f).to eq(1.0 / 2.0)
|
45
|
-
end
|
46
|
-
|
47
|
-
it do
|
48
|
-
expect(subject.to_f).to be_a(Float)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
describe 'to_tex' do
|
54
|
-
context 'when variables have no value' do
|
55
|
-
let(:variables) do
|
56
|
-
[ 1,2 ].map { |i| "X#{i}" }
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'returns a latex format fraction' do
|
60
|
-
expect(subject.to_tex).to eq('\frac{X1}{X2}')
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
context 'when one of the variables has value' do
|
65
|
-
before do
|
66
|
-
subject.denominator.value = nil
|
67
|
-
end
|
68
|
-
|
69
|
-
it 'returns the number instead of the value' do
|
70
|
-
expect(subject.to_tex).to eq('\frac{2}{X2}')
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
context 'when both variables are numeric' do
|
75
|
-
it 'prints the result of the division' do
|
76
|
-
expect(subject.to_tex).to eq('0.5')
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
context 'when one of the variables is a number' do
|
81
|
-
before do
|
82
|
-
variables[0] = 1
|
83
|
-
subject.denominator.value = nil
|
84
|
-
end
|
85
|
-
|
86
|
-
it 'prints both numbers' do
|
87
|
-
expect(subject.to_tex).to eq('\frac{1}{X2}')
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
8
|
+
it_behaves_like 'a function that has two terms', :division, {
|
9
|
+
values: [ 2, 4 ],
|
10
|
+
calculated: 1.0 / 2.0,
|
11
|
+
to_tex: {
|
12
|
+
string_expected: '\frac{X1}{X2}',
|
13
|
+
numeric_string_expected: '0.5',
|
14
|
+
partial_string_expected: '\frac{2}{X2}'
|
15
|
+
},
|
16
|
+
to_gnu: {
|
17
|
+
string_expected: 'X1/X2',
|
18
|
+
numeric_string_expected: '0.5',
|
19
|
+
partial_string_expected: '2/X2'
|
20
|
+
}
|
21
|
+
}
|
91
22
|
end
|
92
23
|
|
@@ -1,92 +1,23 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Danica::Function::Power do
|
4
|
-
let(:variables_number) { 4 }
|
5
|
-
let(:values) { [ 3, 2 ] }
|
6
|
-
let(:variables) do
|
7
|
-
[ 1, 2 ].map do |i|
|
8
|
-
{ name: "X#{i}", value: values[i-1] }
|
9
|
-
end
|
10
|
-
end
|
11
4
|
let(:subject) do
|
12
5
|
described_class.new(base: variables[0], exponent: variables[1])
|
13
6
|
end
|
14
7
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
it 'returns the power of the values' do
|
30
|
-
expect(subject.to_f).to eq(9.0)
|
31
|
-
end
|
32
|
-
|
33
|
-
it do
|
34
|
-
expect(subject.to_f).to be_a(Float)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
context 'when one the variables are numbers' do
|
39
|
-
before do
|
40
|
-
variables[0] = 3
|
41
|
-
end
|
42
|
-
|
43
|
-
it 'returns the power of the values' do
|
44
|
-
expect(subject.to_f).to eq(9.0)
|
45
|
-
end
|
46
|
-
|
47
|
-
it do
|
48
|
-
expect(subject.to_f).to be_a(Float)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
describe 'to_tex' do
|
54
|
-
context 'when variables have no value' do
|
55
|
-
let(:variables) do
|
56
|
-
[ 1,2 ].map { |i| "X#{i}" }
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'returns a latex format fraction' do
|
60
|
-
expect(subject.to_tex).to eq('X1^{X2}')
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
context 'when one of the variables has value' do
|
65
|
-
before do
|
66
|
-
subject.exponent.value = nil
|
67
|
-
end
|
68
|
-
|
69
|
-
it 'returns both variables' do
|
70
|
-
expect(subject.to_tex).to eq('3^{X2}')
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
context 'when both variables are numeric' do
|
75
|
-
it 'prints the final calculation' do
|
76
|
-
expect(subject.to_tex).to eq('9')
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
context 'when one of the variables is a number' do
|
81
|
-
before do
|
82
|
-
variables[0] = 1
|
83
|
-
subject.exponent.value = nil
|
84
|
-
end
|
85
|
-
|
86
|
-
it 'prints both variables' do
|
87
|
-
expect(subject.to_tex).to eq('1^{X2}')
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
8
|
+
it_behaves_like 'a function that has two terms', :power, {
|
9
|
+
values: [ 3, 2 ],
|
10
|
+
calculated: 9.0,
|
11
|
+
to_tex: {
|
12
|
+
string_expected: 'X1^{X2}',
|
13
|
+
numeric_string_expected: '9',
|
14
|
+
partial_string_expected: '3^{X2}'
|
15
|
+
},
|
16
|
+
to_gnu: {
|
17
|
+
string_expected: 'X1**X2',
|
18
|
+
numeric_string_expected: '9',
|
19
|
+
partial_string_expected: '3**X2'
|
20
|
+
}
|
21
|
+
}
|
91
22
|
end
|
92
23
|
|
@@ -3,9 +3,16 @@ require 'spec_helper'
|
|
3
3
|
describe Danica::Function::Product do
|
4
4
|
it_behaves_like 'a function that joins many variables with same operation', {
|
5
5
|
calculated: 24,
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
numeric_variables: [ 1.5, 2, 3.5 ],
|
7
|
+
to_tex: {
|
8
|
+
string_expected: %w(X1 X2 X3 X4).join(' \cdot '),
|
9
|
+
integer_expected: %w(3 X3 X4).join(' \cdot '),
|
10
|
+
float_expected: '10.5 \cdot X4'
|
11
|
+
},
|
12
|
+
to_gnu: {
|
13
|
+
string_expected: %w(X1 X2 X3 X4).join(' * '),
|
14
|
+
integer_expected: %w(3 X3 X4).join(' * '),
|
15
|
+
float_expected: '10.5 * X4'
|
16
|
+
}
|
10
17
|
}
|
11
18
|
end
|
@@ -7,7 +7,7 @@ describe Danica::Function::SquareRoot do
|
|
7
7
|
described_class.new(variable: variable)
|
8
8
|
end
|
9
9
|
|
10
|
-
describe 'to_f' do
|
10
|
+
describe '#to_f' do
|
11
11
|
context 'when variables are not numbers but have value' do
|
12
12
|
it 'returns the division of the values' do
|
13
13
|
expect(subject.to_f).to eq(3.0)
|
@@ -31,7 +31,7 @@ describe Danica::Function::SquareRoot do
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
-
describe 'to_tex' do
|
34
|
+
describe '#to_tex' do
|
35
35
|
context 'when variables have no value' do
|
36
36
|
let(:variable) { :X }
|
37
37
|
|
@@ -49,5 +49,23 @@ describe Danica::Function::SquareRoot do
|
|
49
49
|
end
|
50
50
|
end
|
51
51
|
end
|
52
|
-
end
|
53
52
|
|
53
|
+
describe '#to_gnu' do
|
54
|
+
context 'when variables have no value' do
|
55
|
+
let(:variable) { :X }
|
56
|
+
|
57
|
+
it 'returns a latex format fraction' do
|
58
|
+
expect(subject.to_gnu).to eq('sqrt(X)')
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'when the variable is numeric' do
|
63
|
+
before do
|
64
|
+
subject.variable.value = 9
|
65
|
+
end
|
66
|
+
it 'prints both numbers' do
|
67
|
+
expect(subject.to_gnu).to eq('3')
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -3,9 +3,16 @@ require 'spec_helper'
|
|
3
3
|
describe Danica::Function::Sum do
|
4
4
|
it_behaves_like 'a function that joins many variables with same operation', {
|
5
5
|
calculated: 10,
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
numeric_variables: [ 1.5, 2.5, 3.5 ],
|
7
|
+
to_tex: {
|
8
|
+
string_expected: 'X1 + X2 + X3 + X4',
|
9
|
+
integer_expected: '4 + X3 + X4',
|
10
|
+
float_expected: '7.5 + X4'
|
11
|
+
},
|
12
|
+
to_gnu: {
|
13
|
+
string_expected: 'X1 + X2 + X3 + X4',
|
14
|
+
integer_expected: '4 + X3 + X4',
|
15
|
+
float_expected: '7.5 + X4'
|
16
|
+
}
|
10
17
|
}
|
11
18
|
end
|
@@ -4,7 +4,7 @@ describe Danica::Function do
|
|
4
4
|
class Danica::Function
|
5
5
|
class Spatial < Danica::Function
|
6
6
|
attr_accessor :time, :acceleration, :initial_space, :initial_velocity
|
7
|
-
delegate :to_tex, to: :sum
|
7
|
+
delegate :to_tex, :to_gnu, to: :sum
|
8
8
|
|
9
9
|
private
|
10
10
|
|
@@ -34,18 +34,19 @@ describe Danica::Function do
|
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
|
+
let(:variables) do
|
38
|
+
{
|
39
|
+
time: :t,
|
40
|
+
acceleration: 'a',
|
41
|
+
initial_space: { name: :S0, latex: 'S_0' },
|
42
|
+
initial_velocity: { name: :V0, latex: 'V_0' }
|
43
|
+
}
|
44
|
+
end
|
45
|
+
|
46
|
+
let(:subject) { described_class::Spatial.new(variables) }
|
47
|
+
|
37
48
|
describe '#to_tex' do
|
38
49
|
context 'when creating the spatial function for constantly accelerated movement' do
|
39
|
-
let(:variables) do
|
40
|
-
{
|
41
|
-
time: :t,
|
42
|
-
acceleration: 'a',
|
43
|
-
initial_space: { name: :S0, latex: 'S_0' },
|
44
|
-
initial_velocity: { name: :V0, latex: 'V_0' }
|
45
|
-
}
|
46
|
-
end
|
47
|
-
|
48
|
-
let(:subject) { described_class::Spatial.new(variables) }
|
49
50
|
let(:expected) { 'S_0 + V_0 \cdot t + \frac{a \cdot t^{2}}{2}' }
|
50
51
|
|
51
52
|
it 'return the latex format CAM' do
|
@@ -53,4 +54,14 @@ describe Danica::Function do
|
|
53
54
|
end
|
54
55
|
end
|
55
56
|
end
|
57
|
+
|
58
|
+
describe '#to_gnu' do
|
59
|
+
context 'when creating the spatial function for constantly accelerated movement' do
|
60
|
+
let(:expected) { 'S0 + V0 * t + a * t**2/2' }
|
61
|
+
|
62
|
+
it 'return the latex format CAM' do
|
63
|
+
expect(subject.to_gnu).to eq(expected)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
56
67
|
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Danica::Number do
|
4
|
+
let(:value) { 10 }
|
5
|
+
let(:subject) { described_class.new(value) }
|
6
|
+
|
7
|
+
describe '#to_f' do
|
8
|
+
it 'returns the float of value' do
|
9
|
+
expect(subject.to_f).to eq(10)
|
10
|
+
end
|
11
|
+
|
12
|
+
it { expect(subject.to_f).to be_a(Float) }
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#to_tex' do
|
16
|
+
context 'when value should be integer' do
|
17
|
+
let(:value) { 10.0 }
|
18
|
+
|
19
|
+
it 'returns the value integer string' do
|
20
|
+
expect(subject.to_tex).to eq('10')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'when value should be integer' do
|
25
|
+
let(:value) { 10.5 }
|
26
|
+
|
27
|
+
it 'returns the value integer string' do
|
28
|
+
expect(subject.to_tex).to eq('10.5')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '#to_gnu' do
|
34
|
+
context 'when value should be integer' do
|
35
|
+
let(:value) { 10.0 }
|
36
|
+
|
37
|
+
it 'returns the value integer string' do
|
38
|
+
expect(subject.to_gnu).to eq('10')
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'when value should be integer' do
|
43
|
+
let(:value) { 10.5 }
|
44
|
+
|
45
|
+
it 'returns the value integer string' do
|
46
|
+
expect(subject.to_gnu).to eq('10.5')
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -1,9 +1,9 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Danica::Variable do
|
4
|
-
describe 'to_f' do
|
4
|
+
describe '#to_f' do
|
5
5
|
context 'when variable has no value' do
|
6
|
-
it { expect { subject.to_f }.to raise_error(Danica::NotDefined) }
|
6
|
+
it { expect { subject.to_f }.to raise_error(Danica::Exception::NotDefined) }
|
7
7
|
end
|
8
8
|
|
9
9
|
context 'when variable has value' do
|
@@ -16,23 +16,11 @@ describe Danica::Variable do
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
describe 'to_tex' do
|
20
|
-
|
21
|
-
|
22
|
-
context 'when latex is not defined ' do
|
23
|
-
let(:subject) { described_class.new name: name }
|
24
|
-
|
25
|
-
it 'returns name' do
|
26
|
-
expect(subject.to_tex).to eq('delta')
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
context 'when latex has been defined' do
|
31
|
-
let(:subject) { described_class.new name: name, latex: '\delta' }
|
19
|
+
describe '#to_tex' do
|
20
|
+
it_behaves_like 'a variable method to formated string', :to_tex, :latex
|
21
|
+
end
|
32
22
|
|
33
|
-
|
34
|
-
|
35
|
-
end
|
36
|
-
end
|
23
|
+
describe '#to_gnu' do
|
24
|
+
it_behaves_like 'a variable method to formated string', :to_gnu, :gnu
|
37
25
|
end
|
38
26
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
shared_examples 'a function that joins many variables with same operation' do |arguments|
|
2
2
|
it_behaves_like 'a function that knows how to calculate', arguments
|
3
3
|
it_behaves_like 'a function that knows how to write to tex', arguments
|
4
|
+
it_behaves_like 'a function that knows how to write to gnu', arguments
|
4
5
|
end
|
5
6
|
|
6
7
|
shared_examples 'a function that knows how to calculate' do |arguments|
|
@@ -41,21 +42,30 @@ shared_examples 'a function that knows how to calculate' do |arguments|
|
|
41
42
|
end
|
42
43
|
|
43
44
|
shared_examples 'a function that knows how to write to tex' do |arguments|
|
44
|
-
|
45
|
-
|
45
|
+
it_behaves_like 'a function that knows how to write to a string', :to_tex, arguments
|
46
|
+
end
|
47
|
+
|
48
|
+
shared_examples 'a function that knows how to write to gnu' do |arguments|
|
49
|
+
it_behaves_like 'a function that knows how to write to a string', :to_gnu, arguments
|
50
|
+
end
|
51
|
+
|
52
|
+
shared_examples 'a function that knows how to write to a string' do |command, arguments|
|
53
|
+
let(:numeric_variables) { arguments[:numeric_variables] }
|
54
|
+
%w(integer_expected string_expected float_expected).each do |key|
|
55
|
+
let(key) { arguments[command][key.to_sym] }
|
46
56
|
end
|
47
57
|
let(:subject) do
|
48
58
|
described_class.new(variables: variables)
|
49
59
|
end
|
50
60
|
|
51
|
-
describe
|
61
|
+
describe "#{command}" do
|
52
62
|
let(:variables) do
|
53
63
|
(1..4).map { |i| "X#{i}" }
|
54
64
|
end
|
55
65
|
|
56
66
|
context 'when variables have no value' do
|
57
|
-
it 'outputs a
|
58
|
-
expect(subject.
|
67
|
+
it 'outputs a text format' do
|
68
|
+
expect(subject.public_send(command)).to eq(string_expected)
|
59
69
|
end
|
60
70
|
end
|
61
71
|
|
@@ -68,14 +78,14 @@ shared_examples 'a function that knows how to write to tex' do |arguments|
|
|
68
78
|
end
|
69
79
|
|
70
80
|
it 'outputs a latex format with colapsed numbers' do
|
71
|
-
expect(subject.
|
81
|
+
expect(subject.public_send(command)).to eq(integer_expected)
|
72
82
|
end
|
73
83
|
|
74
|
-
context 'when numeric variables
|
84
|
+
context 'when numeric variables calculated is a float value' do
|
75
85
|
let(:numeric_variables_index) { 2 }
|
76
86
|
|
77
|
-
it 'outputs a
|
78
|
-
expect(subject.
|
87
|
+
it 'outputs a text format with colapsed numbers' do
|
88
|
+
expect(subject.public_send(command)).to eq(float_expected)
|
79
89
|
end
|
80
90
|
end
|
81
91
|
end
|
@@ -88,15 +98,15 @@ shared_examples 'a function that knows how to write to tex' do |arguments|
|
|
88
98
|
end
|
89
99
|
end
|
90
100
|
|
91
|
-
it 'outputs a
|
92
|
-
expect(subject.
|
101
|
+
it 'outputs a text format with colapsed numbers' do
|
102
|
+
expect(subject.public_send(command)).to eq(integer_expected)
|
93
103
|
end
|
94
104
|
|
95
|
-
context 'when numeric variables
|
105
|
+
context 'when numeric variables calculated is a float value' do
|
96
106
|
let(:numeric_variables_index) { 2 }
|
97
107
|
|
98
|
-
it 'outputs a
|
99
|
-
expect(subject.
|
108
|
+
it 'outputs a text format with colapsed numbers' do
|
109
|
+
expect(subject.public_send(command)).to eq(float_expected)
|
100
110
|
end
|
101
111
|
end
|
102
112
|
end
|
@@ -0,0 +1,116 @@
|
|
1
|
+
shared_examples 'a function that has two terms' do |name, arguments|
|
2
|
+
it_behaves_like 'a function that has two terms and knows how to calculate it', name, arguments
|
3
|
+
it_behaves_like 'a function that has two terms and knows how to call to_tex', arguments
|
4
|
+
it_behaves_like 'a function that has two terms and knows how to call to_gnu', arguments
|
5
|
+
end
|
6
|
+
|
7
|
+
shared_examples 'a function that has two terms and knows how to calculate it' do |name, arguments|
|
8
|
+
%w(values calculated).each do |key|
|
9
|
+
let(key) { arguments[key.to_sym] }
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:variables) do
|
13
|
+
[ 1, 2 ].map do |i|
|
14
|
+
{ name: "X#{i}", value: values[i-1] }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#to_f' do
|
19
|
+
context 'when variables are not numbers but have value' do
|
20
|
+
it "returns the #{name} of the values" do
|
21
|
+
expect(subject.to_f).to eq(calculated)
|
22
|
+
end
|
23
|
+
|
24
|
+
it do
|
25
|
+
expect(subject.to_f).to be_a(Float)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'when all the variables are numbers' do
|
30
|
+
let(:variables) { values }
|
31
|
+
|
32
|
+
it "returns the #{name} of the values" do
|
33
|
+
expect(subject.to_f).to eq(calculated)
|
34
|
+
end
|
35
|
+
|
36
|
+
it do
|
37
|
+
expect(subject.to_f).to be_a(Float)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'when one the variables are numbers' do
|
42
|
+
before do
|
43
|
+
variables[0] = values[0]
|
44
|
+
end
|
45
|
+
|
46
|
+
it "returns the #{name} of the values" do
|
47
|
+
expect(subject.to_f).to eq(calculated)
|
48
|
+
end
|
49
|
+
|
50
|
+
it do
|
51
|
+
expect(subject.to_f).to be_a(Float)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
shared_examples 'a function that has two terms and knows how to call to_tex' do |arguments|
|
58
|
+
it_behaves_like 'a function that has two terms and knows how to return a string out of it', :to_tex, arguments
|
59
|
+
end
|
60
|
+
|
61
|
+
shared_examples 'a function that has two terms and knows how to call to_gnu' do |arguments|
|
62
|
+
it_behaves_like 'a function that has two terms and knows how to return a string out of it', :to_gnu, arguments
|
63
|
+
end
|
64
|
+
|
65
|
+
shared_examples 'a function that has two terms and knows how to return a string out of it' do |command, arguments|
|
66
|
+
let(:values) { arguments[:values] }
|
67
|
+
|
68
|
+
%w(string_expected numeric_string_expected partial_string_expected).each do |key|
|
69
|
+
let(key) { arguments[command][key.to_sym] }
|
70
|
+
end
|
71
|
+
describe "##{command}" do
|
72
|
+
let(:variables) do
|
73
|
+
[ 1, 2 ].map do |i|
|
74
|
+
{ name: "X#{i}", value: values[i-1] }
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
context 'when variables have no value' do
|
79
|
+
let(:variables) do
|
80
|
+
[ 1,2 ].map { |i| "X#{i}" }
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'returns a text format fraction' do
|
84
|
+
expect(subject.public_send(command)).to eq(string_expected)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'when one of the variables has value' do
|
89
|
+
before do
|
90
|
+
variables[1][:value] = nil
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'returns the number instead of the value' do
|
94
|
+
expect(subject.public_send(command)).to eq(partial_string_expected)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context 'when both variables are numeric' do
|
99
|
+
it 'prints the result of the calculation' do
|
100
|
+
expect(subject.public_send(command)).to eq(numeric_string_expected)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
context 'when one of the variables is a number' do
|
105
|
+
before do
|
106
|
+
variables[0] = values[0]
|
107
|
+
variables[1][:value] = nil
|
108
|
+
end
|
109
|
+
|
110
|
+
it 'prints both parts' do
|
111
|
+
expect(subject.public_send(command)).to eq(partial_string_expected)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
shared_examples 'a variable method to formated string' do |method, format|
|
2
|
+
let(:name) { :delta }
|
3
|
+
let(:value) { 10.0 }
|
4
|
+
let(:arguments) { { name: name, latex: '\delta', gnu: 'del' } }
|
5
|
+
let(:subject) { described_class.new(arguments) }
|
6
|
+
|
7
|
+
context "when #{format} is not defined" do
|
8
|
+
before { arguments.delete(format) }
|
9
|
+
|
10
|
+
it 'returns name' do
|
11
|
+
expect(subject.public_send(method)).to eq('delta')
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'when value is defined' do
|
15
|
+
before { arguments[:value] = value }
|
16
|
+
|
17
|
+
it 'returns the value' do
|
18
|
+
expect(subject.public_send(method)).to eq('10')
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context "when #{format} has been defined" do
|
24
|
+
it "returns #{format}" do
|
25
|
+
expect(subject.public_send(method)).to eq(arguments[format])
|
26
|
+
end
|
27
|
+
end
|
28
|
+
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: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Darthjee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-09-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -122,6 +122,20 @@ dependencies:
|
|
122
122
|
- - ">="
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: codeclimate-test-reporter
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
125
139
|
description:
|
126
140
|
email:
|
127
141
|
- darthjee@gmail.com
|
@@ -137,6 +151,8 @@ files:
|
|
137
151
|
- Rakefile
|
138
152
|
- danica.gemspec
|
139
153
|
- lib/danica.rb
|
154
|
+
- lib/danica/exception.rb
|
155
|
+
- lib/danica/exception/not_defined.rb
|
140
156
|
- lib/danica/function.rb
|
141
157
|
- lib/danica/function/chained.rb
|
142
158
|
- lib/danica/function/division.rb
|
@@ -144,7 +160,6 @@ files:
|
|
144
160
|
- lib/danica/function/product.rb
|
145
161
|
- lib/danica/function/square_root.rb
|
146
162
|
- lib/danica/function/sum.rb
|
147
|
-
- lib/danica/not_defined.rb
|
148
163
|
- lib/danica/number.rb
|
149
164
|
- lib/danica/variable.rb
|
150
165
|
- lib/danica/version.rb
|
@@ -154,9 +169,12 @@ files:
|
|
154
169
|
- spec/lib/danica/function/square_root_spec.rb
|
155
170
|
- spec/lib/danica/function/sum_spec.rb
|
156
171
|
- spec/lib/danica/function_spec.rb
|
172
|
+
- spec/lib/danica/number_spec.rb
|
157
173
|
- spec/lib/danica/variable_spec.rb
|
158
174
|
- spec/spec_helper.rb
|
159
|
-
- spec/support/shared_examples/function.rb
|
175
|
+
- spec/support/shared_examples/function/chained.rb
|
176
|
+
- spec/support/shared_examples/function/dual_term.rb
|
177
|
+
- spec/support/shared_examples/variable.rb
|
160
178
|
homepage:
|
161
179
|
licenses: []
|
162
180
|
metadata: {}
|
@@ -187,6 +205,9 @@ test_files:
|
|
187
205
|
- spec/lib/danica/function/square_root_spec.rb
|
188
206
|
- spec/lib/danica/function/sum_spec.rb
|
189
207
|
- spec/lib/danica/function_spec.rb
|
208
|
+
- spec/lib/danica/number_spec.rb
|
190
209
|
- spec/lib/danica/variable_spec.rb
|
191
210
|
- spec/spec_helper.rb
|
192
|
-
- spec/support/shared_examples/function.rb
|
211
|
+
- spec/support/shared_examples/function/chained.rb
|
212
|
+
- spec/support/shared_examples/function/dual_term.rb
|
213
|
+
- spec/support/shared_examples/variable.rb
|
data/lib/danica/not_defined.rb
DELETED