parsecs 0.4.1 → 0.5.1

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/parsec.rb +16 -3
  3. data/test/test_parsec.rb +28 -0
  4. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 219c85823eeec9cf36a7623ce522c5059b7fb0d2b4e19fdff86bbf7a4fa0a763
4
- data.tar.gz: c2f6a125b730db44fa8aefeb507287e913389be64d18806ea5e6e237fd6d1e8b
3
+ metadata.gz: 4f491689b423601f13d531240c70349c9af27d4ba39bcf0194771b415ffd567d
4
+ data.tar.gz: 5d051aea5ed0a8538397084ca40c231e8fbc1a48297960d2598a3d21d1adf017
5
5
  SHA512:
6
- metadata.gz: 20b7f32e0dfa829c70db84c18885ff623f980045633bf8488ffcafdaa305f274d896a6a8cff01e564a3c5f969207ac8379d70010acde07255d9e5c5a76ab993a
7
- data.tar.gz: cba4799932c699f0bccf18f0db670440a757d3c721fcde1018106211ea5b25ae853d1aa43d5aef43f4ade5371468430075191509b29bdfe797cb3769e88ee99d
6
+ metadata.gz: b0925569b881e6d3d6ac92b19024b4f4e065248fd049c85516fb1e535627b0b267f940e26ef449b39441a0d3b894cf64b02f94fdc53040b80d3039ca65939acc
7
+ data.tar.gz: 4dff210da8d09ace5f08df556ce49eaf48e2f511e395974853694276c18e7e044b233010ed26ae31c398664c8ff4edddaef486ab8084ee2e0904f1141d348a30
@@ -6,13 +6,19 @@ module Parsec
6
6
  class Parsec
7
7
  using StringToBooleanRefinements
8
8
 
9
- VERSION = '0.4.1'.freeze
9
+ VERSION = '0.5.1'.freeze
10
10
 
11
- # evaluates the equation
11
+ # evaluates the equation and returns only the result
12
12
  def self.eval_equation(equation)
13
+ eval_equation_with_type(equation)[:value]
14
+ end
15
+
16
+ # evaluates the equation and returns the result and its data type
17
+ def self.eval_equation_with_type(equation)
13
18
  remove(equation, true)
14
19
 
15
- convert(Libnativemath.native_eval(equation))
20
+ result = Libnativemath.native_eval(equation)
21
+ { value: convert(result), type: result['type'].to_sym }
16
22
  end
17
23
 
18
24
  # returns true or raise an error
@@ -29,6 +35,13 @@ module Parsec
29
35
  validate(Libnativemath.native_eval(equation), false)
30
36
  end
31
37
 
38
+ def self.get_result_type(equation)
39
+ remove(equation, true)
40
+
41
+ result = Libnativemath.native_eval(equation)
42
+ result['type'].to_sym if validate(result, true)
43
+ end
44
+
32
45
  private_class_method
33
46
 
34
47
  def self.remove(equation, new_line = false)
@@ -95,6 +95,12 @@ class TestParsec < Minitest::Test
95
95
  assert_equal(true, parsec.validate_syntax("\n4\n+ \n 2\n"))
96
96
  end
97
97
 
98
+ def test_equation_with_bad_syntax
99
+ parsec = Parsec::Parsec
100
+ assert_raises(SyntaxError) { parsec.eval_equation('concat(1, 2)') }
101
+ assert_raises(SyntaxError) { parsec.eval_equation('4 > 2 ? "smaller"') }
102
+ end
103
+
98
104
  def test_validate_syntax
99
105
  parsec = Parsec::Parsec
100
106
  refute_equal(true, parsec.validate_syntax('((0.09/1.0)+2.58)-1.6+'))
@@ -116,4 +122,26 @@ class TestParsec < Minitest::Test
116
122
  assert_equal(364, parsec.eval_equation('daysdiff("2100-01-01", "2100-12-31")'))
117
123
  assert_equal(1, parsec.eval_equation('daysdiff("2018-01-01", "2017-12-31")'))
118
124
  end
125
+
126
+ def test_eval_equation_with_type
127
+ parsec = Parsec::Parsec
128
+ assert_equal({ value: 10, type: :int }, parsec.eval_equation_with_type('(5 + 1) + (6 - 2)'))
129
+ assert_equal({ value: 362_880_0, type: :int }, parsec.eval_equation_with_type('10!'))
130
+ equation_date = 'daysdiff("2018-01-01", "2017-12-31")'
131
+ assert_equal({ value: 1, type: :int }, parsec.eval_equation_with_type(equation_date))
132
+ assert_equal({ value: 40.6853, type: :float }, parsec.eval_equation_with_type('10^log(3+2)'))
133
+ assert_equal({ value: 5.1, type: :float }, parsec.eval_equation_with_type('number("5.1")'))
134
+ equation_if = '4 > 2 ? "bigger" : "smaller"'
135
+ assert_equal({ value: "bigger", type: :string }, parsec.eval_equation_with_type(equation_if))
136
+ assert_equal({ value: "5.123", type: :string }, parsec.eval_equation_with_type('string(5.123)'))
137
+ equation_boolean = '2 == 2 ? true : false'
138
+ assert_equal({ value: true, type: :boolean }, parsec.eval_equation_with_type(equation_boolean))
139
+ equation_boolean = '(3==3) and (3!=3)'
140
+ assert_equal({ value: false, type: :boolean }, parsec.eval_equation_with_type(equation_boolean))
141
+ assert_equal({ value: 'Infinity', type: :float }, parsec.eval_equation_with_type('4 / 0'))
142
+ assert_equal({ value: 'nan', type: :float }, parsec.eval_equation_with_type('0 / 0'))
143
+ # invalid equations raises an error
144
+ assert_raises(SyntaxError) { parsec.eval_equation_with_type('concat(1, 2)') }
145
+ assert_raises(SyntaxError) { parsec.eval_equation_with_type('4 > 2 ? "smaller"') }
146
+ end
119
147
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parsecs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nilton Vasques
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2019-02-26 00:00:00.000000000 Z
13
+ date: 2019-03-01 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: minitest
@@ -82,7 +82,7 @@ requirements:
82
82
  - swig
83
83
  - cmake
84
84
  rubyforge_project:
85
- rubygems_version: 2.7.6
85
+ rubygems_version: 2.7.3
86
86
  signing_key:
87
87
  specification_version: 4
88
88
  summary: A gem to evaluate equations using muparserx C++ library