parsecs 0.6.4 → 0.9.1

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: 7e50265b6776233a62be0fc29e04ae1a74e59352fb40338b7493debc49d8debf
4
- data.tar.gz: 3272ffdb8a3f9a31b2bb74853f0f672943a4812b729058b65c92311039fc7000
3
+ metadata.gz: 787c99411c7e672564b1dafb59bc4827ec8e185d4e44c16fc6446d794f59e726
4
+ data.tar.gz: 5c060e8bc1799a012182014258f02cda546b32f0038c765a56c0c41e75049f25
5
5
  SHA512:
6
- metadata.gz: 202a869978505c0ba7f46528e6414bac4c157ff1343f766c6ead677690cb06a75c74e3c6fc29616bd5f8224d57657895295bfc832b24acd2c53fa1467da6e7db
7
- data.tar.gz: 4b1318018190be70d492686f85877a2d9cf507a836e3bc8a5818c04807567581e6241ec54f12b7cdb8a5acb67601dbf59f0b3288819495759ef238322f57f7aa
6
+ metadata.gz: e99a93bd999b2e9bb60e664ca89833e007238d8a93cdafb23f6f142b641f9893e228019f3ca8b6298f6e07b865d3370417473026ffb3f36272b44a178d4d39dc
7
+ data.tar.gz: 31c3a16a7dc6e32ebcd54be4c726070ec443100e00e663293c525721c5953f4a3ef23b579a37287db71e3f5f5cabc0e1d22ca55d7c2994444a0b1b3f56a72479
@@ -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 = '3d6c854dfd3c0ce96a4ed5464fd7139ef1f62fb9'.freeze
41
+ COMMIT = '8951b6498c465808b897457fe61fb0b52fdc1c65'.freeze
42
42
 
43
43
  Dir.chdir(BASEDIR) do
44
44
  system('git init')
data/lib/parsec.rb CHANGED
@@ -6,7 +6,7 @@ module Parsec
6
6
  class Parsec
7
7
  using StringToBooleanRefinements
8
8
 
9
- VERSION = '0.6.4'.freeze
9
+ VERSION = '0.9.1'.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
- sanitize(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
- sanitize(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
- sanitize(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
- sanitize(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)
@@ -45,18 +45,20 @@ module Parsec
45
45
  private_class_method
46
46
 
47
47
  def self.sanitize(equation, new_line = false)
48
- equation.gsub!(/[\n\t\r]/, ' ')
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']
data/test/test_parsec.rb CHANGED
@@ -60,6 +60,13 @@ class TestParsec < Minitest::Test
60
60
  assert_equal(5, parser.eval_equation('number("5")'))
61
61
  assert_equal('Hello', parser.eval_equation('left("Hello World", 5)'))
62
62
  assert_equal('World', parser.eval_equation('right("Hello World", 5)'))
63
+ puts parser.eval_equation('link("Title", "http://foo.bar")')
64
+ assert_equal('<a href="http://foo.bar">Title</a>', parser.eval_equation('link("Title", "http://foo.bar")'))
65
+ assert_equal('<a href="#">Title</a>', parser.eval_equation('link("Title", "#")'))
66
+ assert_equal('<a href="/test">Test title</a>', parser.eval_equation('link("Test title", "/test")'))
67
+ assert_raises(SyntaxError) { parser.eval_equation_with_type('link()') }
68
+ assert_raises(SyntaxError) { parser.eval_equation_with_type('link(1, 2, 3)') }
69
+ assert_raises(SyntaxError) { parser.eval_equation_with_type('link(1, "2")') }
63
70
  end
64
71
 
65
72
  def test_complex_string_manipulation
@@ -115,8 +122,8 @@ class TestParsec < Minitest::Test
115
122
 
116
123
  def test_date_functions
117
124
  parsec = Parsec::Parsec
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()"))
125
+ assert_equal(Date.today, Date.parse(parsec.eval_equation('current_date()')))
126
+ assert_match(/^\d{4}-\d{2}-\d{2}$/, parsec.eval_equation('current_date()'))
120
127
  assert_equal(364, parsec.eval_equation('daysdiff("2018-01-01", "2018-12-31")'))
121
128
  assert_equal(365, parsec.eval_equation('daysdiff("2016-01-01", "2016-12-31")'))
122
129
  assert_equal(365, parsec.eval_equation('daysdiff("2000-01-01", "2000-12-31")'))
@@ -151,6 +158,73 @@ class TestParsec < Minitest::Test
151
158
  assert_raises(SyntaxError) { parsec.eval_equation('hoursdiff(current_date(), "2010-01-01T08:30")') }
152
159
  end
153
160
 
161
+ def test_add_days
162
+ parsec = Parsec::Parsec
163
+
164
+ # With Dates
165
+ assert_equal('2019-01-01', parsec.eval_equation('add_days("2019-01-01", 0)'))
166
+ assert_equal('2019-01-02', parsec.eval_equation('add_days("2019-01-01", 1)'))
167
+ assert_equal('2018-12-31', parsec.eval_equation('add_days("2019-01-01", -1)'))
168
+ assert_equal('2019-01-04', parsec.eval_equation('add_days("2019-01-01", 3)'))
169
+
170
+ # # With DateTimes
171
+ assert_equal('2019-01-01T08:30', parsec.eval_equation('add_days("2019-01-01T08:30", 0)'))
172
+ assert_equal('2019-02-01T12:30', parsec.eval_equation('add_days("2019-01-01T12:30", 31)'))
173
+ assert_equal('2019-01-02T15:30', parsec.eval_equation('add_days("2019-01-01T15:30", 1)'))
174
+ assert_equal('2019-01-02T20:30', parsec.eval_equation('add_days("2019-01-01T08:30", 1.5)'))
175
+ assert_equal('2018-12-31T08:30', parsec.eval_equation('add_days("2019-01-01T08:30", -1)'))
176
+
177
+ # With Errors
178
+ assert_raises(SyntaxError) { parsec.eval_equation_with_type('add_days("2019-01-33", 0)') }
179
+ # assert_raises(SyntaxError) { parsec.eval_equation_with_type('add_days("2019-01-01T08:61", 0)') } # Dont work on Linux
180
+ assert_raises(SyntaxError) { parsec.eval_equation_with_type('add_days()') }
181
+ assert_raises(SyntaxError) { parsec.eval_equation_with_type('add_days(1, 2, 3)') }
182
+ assert_raises(SyntaxError) { parsec.eval_equation_with_type('add_days(1, 2)') }
183
+ end
184
+
185
+ def test_mask
186
+ parsec = Parsec::Parsec
187
+ assert_equal('123-456', parsec.eval_equation('mask("000-000", 123456)'))
188
+ assert_equal('00014', parsec.eval_equation('mask("00000", 14)'))
189
+ assert_equal('000 14', parsec.eval_equation('mask("000 00", 14)'))
190
+ assert_equal('#123', parsec.eval_equation('concat("#", mask("000", 123))'))
191
+ assert_equal('12345', parsec.eval_equation('mask("0000", 12345)'))
192
+ assert_equal('123 45', parsec.eval_equation('mask("00 00", 12345)'))
193
+ assert_equal('3-5591-1801', parsec.eval_equation('mask("0-0000-0000", 355911801)'))
194
+ assert_equal('35-5591-1801', parsec.eval_equation('mask("00-0000-0000", 3555911801)'))
195
+ assert_equal('12-1234-1234-1234', parsec.eval_equation('mask("00-0000-0000-0000", 12123412341234)'))
196
+ assert_equal('1-1234-1234-1234-1234', parsec.eval_equation('mask("0-0000-0000-0000-0000", 11234123412341234)'))
197
+ end
198
+
199
+ def test_default_value
200
+ parsec = Parsec::Parsec
201
+
202
+ # Success Scenarios
203
+ assert_equal(10, parsec.eval_equation('default_value(10, 1)'))
204
+ assert_equal(1, parsec.eval_equation('default_value(NULL, 1)'))
205
+ assert_equal(10.4, parsec.eval_equation('default_value(10.4, 1.01)'))
206
+ assert_equal(1.01, parsec.eval_equation('default_value(NULL, 1.01)'))
207
+ assert_equal('filled', parsec.eval_equation('default_value("filled", "default")'))
208
+ assert_equal('default', parsec.eval_equation('default_value(NULL, "default")'))
209
+ assert_equal(false, parsec.eval_equation('default_value(false, true)'))
210
+ assert_equal(true, parsec.eval_equation('default_value(NULL, true)'))
211
+
212
+ # Mixing number types
213
+ assert_equal(1, parsec.eval_equation('default_value(1, 4.5)'))
214
+ assert_equal(1, parsec.eval_equation('default_value(1, 10)'))
215
+ assert_equal(1, parsec.eval_equation('default_value(1, 10.0)'))
216
+ assert_equal(1, parsec.eval_equation('default_value(1.0, 10)'))
217
+ assert_equal(1, parsec.eval_equation('default_value(1.0, 10.0)'))
218
+ assert_equal(1.5, parsec.eval_equation('default_value(1.5, 10)'))
219
+ assert_equal(1.5, parsec.eval_equation('default_value(1.5, 10.0)'))
220
+ assert_equal(1.5, parsec.eval_equation('default_value(1.5, 10.5)'))
221
+
222
+ # Error Scenarios
223
+ assert_raises(SyntaxError) { parsec.eval_equation('default_value(4.5, "string")') }
224
+ assert_raises(SyntaxError) { parsec.eval_equation('default_value("string", true)') }
225
+ assert_raises(SyntaxError) { parsec.eval_equation('default_value(true, 1)') }
226
+ end
227
+
154
228
  def test_eval_equation_with_type
155
229
  parsec = Parsec::Parsec
156
230
  assert_equal({ value: 10, type: :int }, parsec.eval_equation_with_type('(5 + 1) + (6 - 2)'))
@@ -160,8 +234,8 @@ class TestParsec < Minitest::Test
160
234
  assert_equal({ value: 40.6853, type: :float }, parsec.eval_equation_with_type('10^log(3+2)'))
161
235
  assert_equal({ value: 5.1, type: :float }, parsec.eval_equation_with_type('number("5.1")'))
162
236
  equation_if = '4 > 2 ? "bigger" : "smaller"'
163
- assert_equal({ value: "bigger", type: :string }, parsec.eval_equation_with_type(equation_if))
164
- assert_equal({ value: "5.123", type: :string }, parsec.eval_equation_with_type('string(5.123)'))
237
+ assert_equal({ value: 'bigger', type: :string }, parsec.eval_equation_with_type(equation_if))
238
+ assert_equal({ value: '5.123', type: :string }, parsec.eval_equation_with_type('string(5.123)'))
165
239
  equation_boolean = '2 == 2 ? true : false'
166
240
  assert_equal({ value: true, type: :boolean }, parsec.eval_equation_with_type(equation_boolean))
167
241
  equation_boolean = '(3==3) and (3!=3)'
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: parsecs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.4
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nilton Vasques
8
8
  - Victor Cordeiro
9
9
  - Beatriz Fagundes
10
- autorequire:
10
+ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2019-03-15 00:00:00.000000000 Z
13
+ date: 2021-03-11 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: minitest
@@ -62,7 +62,7 @@ homepage: https://github.com/niltonvasques/parsec
62
62
  licenses:
63
63
  - MIT
64
64
  metadata: {}
65
- post_install_message:
65
+ post_install_message:
66
66
  rdoc_options: []
67
67
  require_paths:
68
68
  - lib
@@ -81,9 +81,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
81
  requirements:
82
82
  - swig
83
83
  - cmake
84
- rubyforge_project:
85
- rubygems_version: 2.7.7
86
- signing_key:
84
+ rubygems_version: 3.0.9
85
+ signing_key:
87
86
  specification_version: 4
88
87
  summary: A gem to evaluate equations using muparserx C++ library
89
88
  test_files: