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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8d8ae393046bb15fcbcd46af8e9430aa50bbbad6
4
- data.tar.gz: 835a4b71b6348e1a3da11a34771d4b4864a37894
3
+ metadata.gz: d3f27550f7488c80de59bfa28553dd0922778607
4
+ data.tar.gz: 9dfbb7cb1c84689e5798305c9cc255294ee1938f
5
5
  SHA512:
6
- metadata.gz: 74eb602d0265187cc3505c25b0870d9564a05bb82e6ae072efdca3cba8be6c9a5b04c243c179a68e8fc94f5f5e73fdfca5c941e547ff1d24e4197f14c78e02ba
7
- data.tar.gz: a389e884fa35861559a76b78ee907b6525a42a3274ddb6ee25d41a896361b5df5003328b2186fdc6d4a1382b9944b80318c022256e22861b27bd551ec7850425
6
+ metadata.gz: cfb395f94ecd24d165a833cd4c88991538378a8789ae892aadc34335e51cfdb52bc4a35f17b4b3a3898797e14bb9d81be6d685c6835448ecef0ac964641a4883
7
+ data.tar.gz: 57bb42340df18ee9e09233c89917452f65bf069f2ca93ee926bcf40296300db2a8fb11604a265fa09ff6ff9325f2d536350529ee15607e61041861ba5426e177
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- danica (0.3.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
+ [![Code Climate](https://codeclimate.com/github/darthjee/danica/badges/gpa.svg)](https://codeclimate.com/github/darthjee/danica)
3
+ [![Test Coverage](https://codeclimate.com/github/darthjee/danica/badges/coverage.svg)](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
- ).to_tex
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
+
@@ -24,5 +24,6 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency 'rspec-mocks'
25
25
  spec.add_development_dependency 'pry-nav'
26
26
  spec.add_development_dependency 'simplecov'
27
+ spec.add_development_dependency 'codeclimate-test-reporter'
27
28
 
28
29
  end
@@ -4,6 +4,6 @@ module Danica
4
4
  require 'danica/number'
5
5
  require 'danica/variable'
6
6
  require 'danica/function'
7
- require 'danica/not_defined'
7
+ require 'danica/exception'
8
8
  end
9
9
 
@@ -0,0 +1,4 @@
1
+ class Danica::Exception < ::Exception
2
+ require 'danica/exception/not_defined'
3
+ end
4
+
@@ -0,0 +1,2 @@
1
+ class Danica::Exception::NotDefined < Danica::Exception
2
+ end
@@ -1,39 +1,57 @@
1
- class Danica::Function
2
- include ActiveModel::Model
3
-
4
- require 'danica/function/chained'
5
- require 'danica/function/product'
6
- require 'danica/function/sum'
7
- require 'danica/function/division'
8
- require 'danica/function/power'
9
- require 'danica/function/square_root'
10
-
11
- attr_accessor :name, :variables
12
-
13
- def to_f
14
- raise 'Not IMplemented yet'
15
- end
16
-
17
- def to_tex
18
- raise 'Not IMplemented yet'
19
- end
20
-
21
- def variables=(variables)
22
- @variables = variables.map { |v| wrap_value(v) }
23
- end
24
-
25
- def valued?
26
- to_f.presend?
27
- rescue Danica::NotDefined
28
- false
29
- end
30
-
31
- private
32
-
33
- def wrap_value(value)
34
- return Danica::Number.new(value) if value.is_a?(Numeric)
35
- return Danica::Variable.new(value) if value.is_a?(Hash)
36
- return Danica::Variable.new(name: value) if [ String, Symbol ].any? { |c| value.is_a?(c) }
37
- value
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
- def to_tex
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
- private
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,6 +7,10 @@ module Danica
7
7
  '\cdot'
8
8
  end
9
9
 
10
+ def gnu_symbol
11
+ '*'
12
+ end
13
+
10
14
  def chain_operation(a, b)
11
15
  a * b
12
16
  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
@@ -7,6 +7,10 @@ module Danica
7
7
  '+'
8
8
  end
9
9
 
10
+ def gnu_symbol
11
+ '+'
12
+ end
13
+
10
14
  def chain_operation(a, b)
11
15
  a + b
12
16
  end
@@ -18,6 +18,8 @@ module Danica
18
18
  def valued?
19
19
  value.present?
20
20
  end
21
+
22
+ alias_method :to_gnu, :to_tex
21
23
  end
22
24
  end
23
25
 
@@ -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
- (value || latex || name).to_s
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
 
@@ -1,3 +1,3 @@
1
1
  module Danica
2
- VERSION = '0.3.0'
2
+ VERSION = '1.0.0'
3
3
  end
@@ -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
- describe 'to_f' do
16
- context 'when variables are not numbers but have value' do
17
- it 'returns the division of the values' do
18
- expect(subject.to_f).to eq(1.0 / 2.0)
19
- end
20
-
21
- it do
22
- expect(subject.to_f).to be_a(Float)
23
- end
24
- end
25
-
26
- context 'when all the variables are numbers' do
27
- let(:variables) { [ 2, 4 ] }
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
- describe 'to_f' do
16
- context 'when variables are not numbers but have value' do
17
- it 'returns the power of the values' do
18
- expect(subject.to_f).to eq(9.0)
19
- end
20
-
21
- it do
22
- expect(subject.to_f).to be_a(Float)
23
- end
24
- end
25
-
26
- context 'when all the variables are numbers' do
27
- let(:variables) { [ 3, 2 ] }
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
- tex_expected: %w(X1 X2 X3 X4).join(' \cdot '),
7
- tex_integer_expected: %w(3 X3 X4).join(' \cdot '),
8
- tex_float_expected: '10.5 \cdot X4',
9
- numeric_variables: [ 1.5, 2, 3.5 ]
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
- tex_expected: 'X1 + X2 + X3 + X4',
7
- tex_integer_expected: '4 + X3 + X4',
8
- tex_float_expected: '7.5 + X4',
9
- numeric_variables: [ 1.5, 2.5, 3.5 ]
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
- let(:name) { :delta }
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
- it 'returns latex' do
34
- expect(subject.to_tex).to eq('\delta')
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
@@ -4,6 +4,11 @@ SimpleCov.profiles.define 'gem' do
4
4
  add_filter '/spec/'
5
5
  end
6
6
 
7
+ if ENV['CODECLIMATE_REPO_TOKEN']
8
+ require 'codeclimate-test-reporter'
9
+ CodeClimate::TestReporter.start
10
+ end
11
+
7
12
  SimpleCov.start 'gem'
8
13
  require 'pry-nav'
9
14
  require 'danica'
@@ -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
- %w(numeric_variables tex_integer_expected tex_expected tex_float_expected).each do |key|
45
- let(key) { arguments[key.to_sym] }
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 'to_tex' do
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 latex format' do
58
- expect(subject.to_tex).to eq(tex_expected)
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.to_tex).to eq(tex_integer_expected)
81
+ expect(subject.public_send(command)).to eq(integer_expected)
72
82
  end
73
83
 
74
- context 'when numeric variables sum is a float value' do
84
+ context 'when numeric variables calculated is a float value' do
75
85
  let(:numeric_variables_index) { 2 }
76
86
 
77
- it 'outputs a latex format with colapsed numbers' do
78
- expect(subject.to_tex).to eq(tex_float_expected)
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 latex format with colapsed numbers' do
92
- expect(subject.to_tex).to eq(tex_integer_expected)
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 sum is a float value' do
105
+ context 'when numeric variables calculated is a float value' do
96
106
  let(:numeric_variables_index) { 2 }
97
107
 
98
- it 'outputs a latex format with colapsed numbers' do
99
- expect(subject.to_tex).to eq(tex_float_expected)
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.3.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-08-28 00:00:00.000000000 Z
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
@@ -1,2 +0,0 @@
1
- class Danica::NotDefined < Exception
2
- end