parsecs 0.5.1 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4f491689b423601f13d531240c70349c9af27d4ba39bcf0194771b415ffd567d
4
- data.tar.gz: 5d051aea5ed0a8538397084ca40c231e8fbc1a48297960d2598a3d21d1adf017
3
+ metadata.gz: 794032417267277d5b12e9ef7d76b3bbcbc4e2590fa1230c1d4600388a74aa55
4
+ data.tar.gz: 7005495e663ef1656090b582bba5738cc2ccf8075bc182c672a5b87db6a034bd
5
5
  SHA512:
6
- metadata.gz: b0925569b881e6d3d6ac92b19024b4f4e065248fd049c85516fb1e535627b0b267f940e26ef449b39441a0d3b894cf64b02f94fdc53040b80d3039ca65939acc
7
- data.tar.gz: 4dff210da8d09ace5f08df556ce49eaf48e2f511e395974853694276c18e7e044b233010ed26ae31c398664c8ff4edddaef486ab8084ee2e0904f1141d348a30
6
+ metadata.gz: 01edc288484e0c48e71afa19d9bca1a93e329f39c9af0d1efcba725e3783c9ba7c3d09bccd347f014f185460154b456d99dce2df7ec8caadbd89ec5e8a5e0b9d
7
+ data.tar.gz: db1b0a6a59331065e8d279d9be948164cc103ba8c29331f94143f109c6b72e018bc92ebd74cc492cf047a5aa51cce0c434b7600f8c062379cd061c6a62427a55
@@ -38,7 +38,7 @@ libs.each do |lib|
38
38
  end
39
39
 
40
40
  GIT_REPOSITORY = 'https://github.com/niltonvasques/equations-parser.git'.freeze
41
- COMMIT = '65ff99e53961d86f14d214e49154ae26c94ec221'.freeze
41
+ COMMIT = 'eca316d8ad99d4f2e68539a1efd7524288817006'.freeze
42
42
 
43
43
  Dir.chdir(BASEDIR) do
44
44
  system('git init')
@@ -6,7 +6,7 @@ module Parsec
6
6
  class Parsec
7
7
  using StringToBooleanRefinements
8
8
 
9
- VERSION = '0.5.1'.freeze
9
+ VERSION = '0.8.0'.freeze
10
10
 
11
11
  # evaluates the equation and returns only the result
12
12
  def self.eval_equation(equation)
@@ -15,7 +15,7 @@ module Parsec
15
15
 
16
16
  # evaluates the equation and returns the result and its data type
17
17
  def self.eval_equation_with_type(equation)
18
- remove(equation, true)
18
+ equation = sanitize(equation, true)
19
19
 
20
20
  result = Libnativemath.native_eval(equation)
21
21
  { value: convert(result), type: result['type'].to_sym }
@@ -23,20 +23,20 @@ module Parsec
23
23
 
24
24
  # returns true or raise an error
25
25
  def self.validate_syntax!(equation)
26
- remove(equation)
26
+ equation = sanitize(equation)
27
27
 
28
28
  validate(Libnativemath.native_eval(equation), true)
29
29
  end
30
30
 
31
31
  # returns true or an error string
32
32
  def self.validate_syntax(equation)
33
- remove(equation)
33
+ equation = sanitize(equation)
34
34
 
35
35
  validate(Libnativemath.native_eval(equation), false)
36
36
  end
37
37
 
38
38
  def self.get_result_type(equation)
39
- remove(equation, true)
39
+ equation = sanitize(equation, true)
40
40
 
41
41
  result = Libnativemath.native_eval(equation)
42
42
  result['type'].to_sym if validate(result, true)
@@ -44,19 +44,21 @@ module Parsec
44
44
 
45
45
  private_class_method
46
46
 
47
- def self.remove(equation, new_line = false)
48
- equation.gsub!(/[\n\t\r]/, ' ')
47
+ def self.sanitize(equation, new_line = false)
48
+ equation = equation.gsub(/[\n\t\r]/, ' ')
49
49
 
50
50
  # The following regex remove all spaces that are not between quot marks
51
51
  # https://tinyurl.com/ybc7bng3
52
- equation.gsub!(/( |(".*?"))/, '\\2') if new_line == true
52
+ equation = equation.gsub(/( |(".*?"))/, '\\2') if new_line == true
53
+
54
+ equation
53
55
  end
54
56
 
55
57
  def self.convert(ans)
56
58
  case ans['value']
57
59
  when 'inf' then return 'Infinity'
58
60
  when '-inf' then return '-Infinity'
59
- when 'nan' then return ans['value']
61
+ when 'nan', '-nan' then return 'nan'
60
62
  end
61
63
 
62
64
  case ans['type']
@@ -115,7 +115,8 @@ class TestParsec < Minitest::Test
115
115
 
116
116
  def test_date_functions
117
117
  parsec = Parsec::Parsec
118
- assert_equal(Date.today, Date.parse(parsec.eval_equation("current_date()")))
118
+ assert_equal(Date.today, Date.parse(parsec.eval_equation('current_date()')))
119
+ assert_match(/^\d{4}-\d{2}-\d{2}$/, parsec.eval_equation('current_date()'))
119
120
  assert_equal(364, parsec.eval_equation('daysdiff("2018-01-01", "2018-12-31")'))
120
121
  assert_equal(365, parsec.eval_equation('daysdiff("2016-01-01", "2016-12-31")'))
121
122
  assert_equal(365, parsec.eval_equation('daysdiff("2000-01-01", "2000-12-31")'))
@@ -123,6 +124,91 @@ class TestParsec < Minitest::Test
123
124
  assert_equal(1, parsec.eval_equation('daysdiff("2018-01-01", "2017-12-31")'))
124
125
  end
125
126
 
127
+ def test_datetime_functions
128
+ parsec = Parsec::Parsec
129
+ assert_equal(24, parsec.eval_equation('hoursdiff("2018-01-01", "2018-01-02")'))
130
+ assert_equal(24, parsec.eval_equation('hoursdiff("2018-01-01", "2018-1-02")'))
131
+ assert_equal(24, parsec.eval_equation('hoursdiff("2018-1-01", "2018-01-02")'))
132
+ assert_equal(24, parsec.eval_equation('hoursdiff("2018-1-01", "2018-1-02")'))
133
+ assert_equal(24, parsec.eval_equation('hoursdiff("2018-01-01", "2018-01-2")'))
134
+ assert_equal(24, parsec.eval_equation('hoursdiff("2018-01-1", "2018-01-02")'))
135
+ assert_equal(24, parsec.eval_equation('hoursdiff("2018-01-1", "2018-01-2")'))
136
+ assert_equal(24, parsec.eval_equation('hoursdiff("2018-1-1", "2018-1-2")'))
137
+ assert_equal(288, parsec.eval_equation('hoursdiff("2019-02-01", "2019-02-13")'))
138
+ assert_equal(4, parsec.eval_equation('hoursdiff("2019-02-01T08:00", "2019-02-01T12:00")'))
139
+ assert_equal(28, parsec.eval_equation('hoursdiff("2019-02-01T08:00", "2019-02-02T12:00")'))
140
+ assert_equal(3.67, parsec.eval_equation('hoursdiff("2019-02-01T08:20", "2019-02-01T12:00")'))
141
+ assert_equal(0, parsec.eval_equation('hoursdiff(current_date(), current_date())'))
142
+ assert_operator(parsec.eval_equation('hoursdiff(current_date(), "2000-01-01")'), :<, 0)
143
+ assert_equal(0, parsec.eval_equation('hoursdiff("2018-01-01", "2018-01-01")'))
144
+ assert_equal(-20, parsec.eval_equation('hoursdiff("2018-01-02T08:00", "2018-01-01T12:00")'))
145
+ assert_raises(SyntaxError) { parsec.eval_equation('hoursdiff("2018-01-01", "INVALID2")') }
146
+ assert_raises(SyntaxError) { parsec.eval_equation('hoursdiff("INVALID1", "2018-01-01")') }
147
+ assert_raises(SyntaxError) { parsec.eval_equation('hoursdiff("INVALID1", "INVALID2")') }
148
+ assert_raises(SyntaxError) { parsec.eval_equation('hoursdiff(2, 3)') }
149
+ assert_raises(SyntaxError) { parsec.eval_equation('hoursdiff("2018-01-01", "2018-01-01T12:00")') }
150
+ assert_raises(SyntaxError) { parsec.eval_equation('hoursdiff("2018-01-01T12:00", "2018-01-01")') }
151
+ assert_raises(SyntaxError) { parsec.eval_equation('hoursdiff(current_date(), "2010-01-01T08:30")') }
152
+ end
153
+
154
+ def test_add_days
155
+ parsec = Parsec::Parsec
156
+
157
+ # With Dates
158
+ assert_equal('2019-01-01', parsec.eval_equation('add_days("2019-01-01", 0)'))
159
+ assert_equal('2019-01-02', parsec.eval_equation('add_days("2019-01-01", 1)'))
160
+ assert_equal('2018-12-31', parsec.eval_equation('add_days("2019-01-01", -1)'))
161
+ assert_equal('2019-01-04', parsec.eval_equation('add_days("2019-01-01", 3)'))
162
+
163
+ # # With DateTimes
164
+ assert_equal('2019-01-01T08:30', parsec.eval_equation('add_days("2019-01-01T08:30", 0)'))
165
+ assert_equal('2019-02-01T12:30', parsec.eval_equation('add_days("2019-01-01T12:30", 31)'))
166
+ assert_equal('2019-01-02T15:30', parsec.eval_equation('add_days("2019-01-01T15:30", 1)'))
167
+ assert_equal('2019-01-02T20:30', parsec.eval_equation('add_days("2019-01-01T08:30", 1.5)'))
168
+ assert_equal('2018-12-31T08:30', parsec.eval_equation('add_days("2019-01-01T08:30", -1)'))
169
+
170
+ # With Errors
171
+ assert_raises(SyntaxError) { parsec.eval_equation_with_type('add_days("2019-01-33", 0)') }
172
+ # assert_raises(SyntaxError) { parsec.eval_equation_with_type('add_days("2019-01-01T08:61", 0)') } # Dont work on Linux
173
+ assert_raises(SyntaxError) { parsec.eval_equation_with_type('add_days()') }
174
+ assert_raises(SyntaxError) { parsec.eval_equation_with_type('add_days(1, 2, 3)') }
175
+ assert_raises(SyntaxError) { parsec.eval_equation_with_type('add_days(1, 2)') }
176
+ end
177
+
178
+ def test_mask
179
+ parsec = Parsec::Parsec
180
+ assert_equal('123-456', parsec.eval_equation('mask("000-000", 123456)'))
181
+ assert_equal('00014', parsec.eval_equation('mask("00000", 14)'))
182
+ assert_equal('000 14', parsec.eval_equation('mask("000 00", 14)'))
183
+ assert_equal('#123', parsec.eval_equation('concat("#", mask("000", 123))'))
184
+ assert_equal('12345', parsec.eval_equation('mask("0000", 12345)'))
185
+ assert_equal('123 45', parsec.eval_equation('mask("00 00", 12345)'))
186
+ assert_equal('3-5591-1801', parsec.eval_equation('mask("0-0000-0000", 355911801)'))
187
+ assert_equal('35-5591-1801', parsec.eval_equation('mask("00-0000-0000", 3555911801)'))
188
+ assert_equal('12-1234-1234-1234', parsec.eval_equation('mask("00-0000-0000-0000", 12123412341234)'))
189
+ assert_equal('1-1234-1234-1234-1234', parsec.eval_equation('mask("0-0000-0000-0000-0000", 11234123412341234)'))
190
+ end
191
+
192
+ def test_default_value
193
+ parsec = Parsec::Parsec
194
+
195
+ # Success Scenarios
196
+ assert_equal(10, parsec.eval_equation('default_value(10, 1)'))
197
+ assert_equal(1, parsec.eval_equation('default_value(NULL, 1)'))
198
+ assert_equal(10.4, parsec.eval_equation('default_value(10.4, 1.01)'))
199
+ assert_equal(1.01, parsec.eval_equation('default_value(NULL, 1.01)'))
200
+ assert_equal('filled', parsec.eval_equation('default_value("filled", "default")'))
201
+ assert_equal('default', parsec.eval_equation('default_value(NULL, "default")'))
202
+ assert_equal(false, parsec.eval_equation('default_value(false, true)'))
203
+ assert_equal(true, parsec.eval_equation('default_value(NULL, true)'))
204
+
205
+ # Error Scenarios
206
+ assert_raises(SyntaxError) { parsec.eval_equation('default_value(1, 4.5)') }
207
+ assert_raises(SyntaxError) { parsec.eval_equation('default_value(4.5, "string")') }
208
+ assert_raises(SyntaxError) { parsec.eval_equation('default_value("string", true)') }
209
+ assert_raises(SyntaxError) { parsec.eval_equation('default_value(true, 1)') }
210
+ end
211
+
126
212
  def test_eval_equation_with_type
127
213
  parsec = Parsec::Parsec
128
214
  assert_equal({ value: 10, type: :int }, parsec.eval_equation_with_type('(5 + 1) + (6 - 2)'))
@@ -132,8 +218,8 @@ class TestParsec < Minitest::Test
132
218
  assert_equal({ value: 40.6853, type: :float }, parsec.eval_equation_with_type('10^log(3+2)'))
133
219
  assert_equal({ value: 5.1, type: :float }, parsec.eval_equation_with_type('number("5.1")'))
134
220
  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)'))
221
+ assert_equal({ value: 'bigger', type: :string }, parsec.eval_equation_with_type(equation_if))
222
+ assert_equal({ value: '5.123', type: :string }, parsec.eval_equation_with_type('string(5.123)'))
137
223
  equation_boolean = '2 == 2 ? true : false'
138
224
  assert_equal({ value: true, type: :boolean }, parsec.eval_equation_with_type(equation_boolean))
139
225
  equation_boolean = '(3==3) and (3!=3)'
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.5.1
4
+ version: 0.8.0
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-03-01 00:00:00.000000000 Z
13
+ date: 2020-06-29 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.3
85
+ rubygems_version: 2.7.9
86
86
  signing_key:
87
87
  specification_version: 4
88
88
  summary: A gem to evaluate equations using muparserx C++ library