parsecs 0.6.2 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/libnativemath/extconf.rb +1 -1
- data/lib/parsec.rb +10 -8
- data/test/test_parsec.rb +87 -3
- metadata +6 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 60a45f7bed4f8e37362535038632693c74a4aad9d4efd7294d65a2455e218e05
|
4
|
+
data.tar.gz: 91f3e5ef94961ad9a7020f425f9cd4ce5a295d2b5c17215e3dffeaf7a252498c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a507bc067a5cd679c909fe2f582962f8d52050b0a869ce4de18f177a5724d985f807864867df3ac3ebab3e595da243227b84aeb5e211654b5137ae688c05463c
|
7
|
+
data.tar.gz: 81dad90965c2e7c822844762c27053b1f6615bc8d049ab0d6589222bbd59ca21ce0e9b788cf7ddf1b0616488889ee89e5c04d64fb05fdd0d97f21328cd0d5001
|
@@ -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 = '
|
41
|
+
COMMIT = '8acd8d964531f1b78ef905ccdace7b7b038e9d8d'.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.
|
9
|
+
VERSION = '0.9.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
|
-
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
|
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
|
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
|
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,12 @@ 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
|
+
assert_equal('<a href=\"http://foo.bar\">Title</a>', parser.eval_equation('link("Title", "http://foo.bar")'))
|
64
|
+
assert_equal('<a href=\"#\">Title</a>', parser.eval_equation('link("Title", "#")'))
|
65
|
+
assert_equal('<a href=\"/test\">Test title</a>', parser.eval_equation('link("Test title", "/test")'))
|
66
|
+
assert_raises(SyntaxError) { parser.eval_equation_with_type('link()') }
|
67
|
+
assert_raises(SyntaxError) { parser.eval_equation_with_type('link(1, 2, 3)') }
|
68
|
+
assert_raises(SyntaxError) { parser.eval_equation_with_type('link(1, "2")') }
|
63
69
|
end
|
64
70
|
|
65
71
|
def test_complex_string_manipulation
|
@@ -115,7 +121,8 @@ class TestParsec < Minitest::Test
|
|
115
121
|
|
116
122
|
def test_date_functions
|
117
123
|
parsec = Parsec::Parsec
|
118
|
-
assert_equal(Date.today, Date.parse(parsec.eval_equation(
|
124
|
+
assert_equal(Date.today, Date.parse(parsec.eval_equation('current_date()')))
|
125
|
+
assert_match(/^\d{4}-\d{2}-\d{2}$/, parsec.eval_equation('current_date()'))
|
119
126
|
assert_equal(364, parsec.eval_equation('daysdiff("2018-01-01", "2018-12-31")'))
|
120
127
|
assert_equal(365, parsec.eval_equation('daysdiff("2016-01-01", "2016-12-31")'))
|
121
128
|
assert_equal(365, parsec.eval_equation('daysdiff("2000-01-01", "2000-12-31")'))
|
@@ -126,10 +133,19 @@ class TestParsec < Minitest::Test
|
|
126
133
|
def test_datetime_functions
|
127
134
|
parsec = Parsec::Parsec
|
128
135
|
assert_equal(24, parsec.eval_equation('hoursdiff("2018-01-01", "2018-01-02")'))
|
136
|
+
assert_equal(24, parsec.eval_equation('hoursdiff("2018-01-01", "2018-1-02")'))
|
137
|
+
assert_equal(24, parsec.eval_equation('hoursdiff("2018-1-01", "2018-01-02")'))
|
138
|
+
assert_equal(24, parsec.eval_equation('hoursdiff("2018-1-01", "2018-1-02")'))
|
139
|
+
assert_equal(24, parsec.eval_equation('hoursdiff("2018-01-01", "2018-01-2")'))
|
140
|
+
assert_equal(24, parsec.eval_equation('hoursdiff("2018-01-1", "2018-01-02")'))
|
141
|
+
assert_equal(24, parsec.eval_equation('hoursdiff("2018-01-1", "2018-01-2")'))
|
142
|
+
assert_equal(24, parsec.eval_equation('hoursdiff("2018-1-1", "2018-1-2")'))
|
129
143
|
assert_equal(288, parsec.eval_equation('hoursdiff("2019-02-01", "2019-02-13")'))
|
130
144
|
assert_equal(4, parsec.eval_equation('hoursdiff("2019-02-01T08:00", "2019-02-01T12:00")'))
|
131
145
|
assert_equal(28, parsec.eval_equation('hoursdiff("2019-02-01T08:00", "2019-02-02T12:00")'))
|
132
146
|
assert_equal(3.67, parsec.eval_equation('hoursdiff("2019-02-01T08:20", "2019-02-01T12:00")'))
|
147
|
+
assert_equal(0, parsec.eval_equation('hoursdiff(current_date(), current_date())'))
|
148
|
+
assert_operator(parsec.eval_equation('hoursdiff(current_date(), "2000-01-01")'), :<, 0)
|
133
149
|
assert_equal(0, parsec.eval_equation('hoursdiff("2018-01-01", "2018-01-01")'))
|
134
150
|
assert_equal(-20, parsec.eval_equation('hoursdiff("2018-01-02T08:00", "2018-01-01T12:00")'))
|
135
151
|
assert_raises(SyntaxError) { parsec.eval_equation('hoursdiff("2018-01-01", "INVALID2")') }
|
@@ -138,6 +154,74 @@ class TestParsec < Minitest::Test
|
|
138
154
|
assert_raises(SyntaxError) { parsec.eval_equation('hoursdiff(2, 3)') }
|
139
155
|
assert_raises(SyntaxError) { parsec.eval_equation('hoursdiff("2018-01-01", "2018-01-01T12:00")') }
|
140
156
|
assert_raises(SyntaxError) { parsec.eval_equation('hoursdiff("2018-01-01T12:00", "2018-01-01")') }
|
157
|
+
assert_raises(SyntaxError) { parsec.eval_equation('hoursdiff(current_date(), "2010-01-01T08:30")') }
|
158
|
+
end
|
159
|
+
|
160
|
+
def test_add_days
|
161
|
+
parsec = Parsec::Parsec
|
162
|
+
|
163
|
+
# With Dates
|
164
|
+
assert_equal('2019-01-01', parsec.eval_equation('add_days("2019-01-01", 0)'))
|
165
|
+
assert_equal('2019-01-02', parsec.eval_equation('add_days("2019-01-01", 1)'))
|
166
|
+
assert_equal('2018-12-31', parsec.eval_equation('add_days("2019-01-01", -1)'))
|
167
|
+
assert_equal('2019-01-04', parsec.eval_equation('add_days("2019-01-01", 3)'))
|
168
|
+
|
169
|
+
# # With DateTimes
|
170
|
+
assert_equal('2019-01-01T08:30', parsec.eval_equation('add_days("2019-01-01T08:30", 0)'))
|
171
|
+
assert_equal('2019-02-01T12:30', parsec.eval_equation('add_days("2019-01-01T12:30", 31)'))
|
172
|
+
assert_equal('2019-01-02T15:30', parsec.eval_equation('add_days("2019-01-01T15:30", 1)'))
|
173
|
+
assert_equal('2019-01-02T20:30', parsec.eval_equation('add_days("2019-01-01T08:30", 1.5)'))
|
174
|
+
assert_equal('2018-12-31T08:30', parsec.eval_equation('add_days("2019-01-01T08:30", -1)'))
|
175
|
+
|
176
|
+
# With Errors
|
177
|
+
assert_raises(SyntaxError) { parsec.eval_equation_with_type('add_days("2019-01-33", 0)') }
|
178
|
+
# assert_raises(SyntaxError) { parsec.eval_equation_with_type('add_days("2019-01-01T08:61", 0)') } # Dont work on Linux
|
179
|
+
assert_raises(SyntaxError) { parsec.eval_equation_with_type('add_days()') }
|
180
|
+
assert_raises(SyntaxError) { parsec.eval_equation_with_type('add_days(1, 2, 3)') }
|
181
|
+
assert_raises(SyntaxError) { parsec.eval_equation_with_type('add_days(1, 2)') }
|
182
|
+
end
|
183
|
+
|
184
|
+
def test_mask
|
185
|
+
parsec = Parsec::Parsec
|
186
|
+
assert_equal('123-456', parsec.eval_equation('mask("000-000", 123456)'))
|
187
|
+
assert_equal('00014', parsec.eval_equation('mask("00000", 14)'))
|
188
|
+
assert_equal('000 14', parsec.eval_equation('mask("000 00", 14)'))
|
189
|
+
assert_equal('#123', parsec.eval_equation('concat("#", mask("000", 123))'))
|
190
|
+
assert_equal('12345', parsec.eval_equation('mask("0000", 12345)'))
|
191
|
+
assert_equal('123 45', parsec.eval_equation('mask("00 00", 12345)'))
|
192
|
+
assert_equal('3-5591-1801', parsec.eval_equation('mask("0-0000-0000", 355911801)'))
|
193
|
+
assert_equal('35-5591-1801', parsec.eval_equation('mask("00-0000-0000", 3555911801)'))
|
194
|
+
assert_equal('12-1234-1234-1234', parsec.eval_equation('mask("00-0000-0000-0000", 12123412341234)'))
|
195
|
+
assert_equal('1-1234-1234-1234-1234', parsec.eval_equation('mask("0-0000-0000-0000-0000", 11234123412341234)'))
|
196
|
+
end
|
197
|
+
|
198
|
+
def test_default_value
|
199
|
+
parsec = Parsec::Parsec
|
200
|
+
|
201
|
+
# Success Scenarios
|
202
|
+
assert_equal(10, parsec.eval_equation('default_value(10, 1)'))
|
203
|
+
assert_equal(1, parsec.eval_equation('default_value(NULL, 1)'))
|
204
|
+
assert_equal(10.4, parsec.eval_equation('default_value(10.4, 1.01)'))
|
205
|
+
assert_equal(1.01, parsec.eval_equation('default_value(NULL, 1.01)'))
|
206
|
+
assert_equal('filled', parsec.eval_equation('default_value("filled", "default")'))
|
207
|
+
assert_equal('default', parsec.eval_equation('default_value(NULL, "default")'))
|
208
|
+
assert_equal(false, parsec.eval_equation('default_value(false, true)'))
|
209
|
+
assert_equal(true, parsec.eval_equation('default_value(NULL, true)'))
|
210
|
+
|
211
|
+
# Mixing number types
|
212
|
+
assert_equal(1, parsec.eval_equation('default_value(1, 4.5)'))
|
213
|
+
assert_equal(1, parsec.eval_equation('default_value(1, 10)'))
|
214
|
+
assert_equal(1, parsec.eval_equation('default_value(1, 10.0)'))
|
215
|
+
assert_equal(1, parsec.eval_equation('default_value(1.0, 10)'))
|
216
|
+
assert_equal(1, parsec.eval_equation('default_value(1.0, 10.0)'))
|
217
|
+
assert_equal(1.5, parsec.eval_equation('default_value(1.5, 10)'))
|
218
|
+
assert_equal(1.5, parsec.eval_equation('default_value(1.5, 10.0)'))
|
219
|
+
assert_equal(1.5, parsec.eval_equation('default_value(1.5, 10.5)'))
|
220
|
+
|
221
|
+
# Error Scenarios
|
222
|
+
assert_raises(SyntaxError) { parsec.eval_equation('default_value(4.5, "string")') }
|
223
|
+
assert_raises(SyntaxError) { parsec.eval_equation('default_value("string", true)') }
|
224
|
+
assert_raises(SyntaxError) { parsec.eval_equation('default_value(true, 1)') }
|
141
225
|
end
|
142
226
|
|
143
227
|
def test_eval_equation_with_type
|
@@ -149,8 +233,8 @@ class TestParsec < Minitest::Test
|
|
149
233
|
assert_equal({ value: 40.6853, type: :float }, parsec.eval_equation_with_type('10^log(3+2)'))
|
150
234
|
assert_equal({ value: 5.1, type: :float }, parsec.eval_equation_with_type('number("5.1")'))
|
151
235
|
equation_if = '4 > 2 ? "bigger" : "smaller"'
|
152
|
-
assert_equal({ value:
|
153
|
-
assert_equal({ value:
|
236
|
+
assert_equal({ value: 'bigger', type: :string }, parsec.eval_equation_with_type(equation_if))
|
237
|
+
assert_equal({ value: '5.123', type: :string }, parsec.eval_equation_with_type('string(5.123)'))
|
154
238
|
equation_boolean = '2 == 2 ? true : false'
|
155
239
|
assert_equal({ value: true, type: :boolean }, parsec.eval_equation_with_type(equation_boolean))
|
156
240
|
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.
|
4
|
+
version: 0.9.0
|
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:
|
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
|
-
|
85
|
-
|
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:
|