parsecs 0.7.0 → 0.9.2
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 +9 -7
- data/test/test_parsec.rb +44 -0
- 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: 872f4306482aab0ddea0dcc751284936637fe5693c3910f75a59ad8504f21114
|
4
|
+
data.tar.gz: e218fdce7f8449d8366429d45075f99e29acf2fef37a42b78bb383366c980b39
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c6852a76f55edeace33df216bcaf65e6491c7f6ef40dee3ff2d2c144d08ec659bc7b2e682ef0ca3589d75302a7ceff5b153b0d7950842391faeaa86b95d1c9d5
|
7
|
+
data.tar.gz: 37358e63b4cfb396dccca6da93172ebc6f097f74b0f85bf12b7083d0e0cf449e25afc93cf323f8e8b7360d4d303a3989835ef19af8095f93ba74f857032b9d7d
|
@@ -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 = '8942cecb71bec402c0487ab77fec97759ff82fdb'.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.2'.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,11 +45,13 @@ 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)
|
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
|
@@ -189,6 +195,44 @@ class TestParsec < Minitest::Test
|
|
189
195
|
assert_equal('1-1234-1234-1234-1234', parsec.eval_equation('mask("0-0000-0000-0000-0000", 11234123412341234)'))
|
190
196
|
end
|
191
197
|
|
198
|
+
def test_string_cast
|
199
|
+
parsec = Parsec::Parsec
|
200
|
+
assert_equal('4', parsec.eval_equation('string(4)'))
|
201
|
+
assert_equal('4.5', parsec.eval_equation('string(4.5)'))
|
202
|
+
assert_equal('true', parsec.eval_equation('string(true)'))
|
203
|
+
assert_equal('false', parsec.eval_equation('string(false)'))
|
204
|
+
assert_equal('4', parsec.eval_equation('string("4")'))
|
205
|
+
end
|
206
|
+
|
207
|
+
def test_default_value
|
208
|
+
parsec = Parsec::Parsec
|
209
|
+
|
210
|
+
# Success Scenarios
|
211
|
+
assert_equal(10, parsec.eval_equation('default_value(10, 1)'))
|
212
|
+
assert_equal(1, parsec.eval_equation('default_value(NULL, 1)'))
|
213
|
+
assert_equal(10.4, parsec.eval_equation('default_value(10.4, 1.01)'))
|
214
|
+
assert_equal(1.01, parsec.eval_equation('default_value(NULL, 1.01)'))
|
215
|
+
assert_equal('filled', parsec.eval_equation('default_value("filled", "default")'))
|
216
|
+
assert_equal('default', parsec.eval_equation('default_value(NULL, "default")'))
|
217
|
+
assert_equal(false, parsec.eval_equation('default_value(false, true)'))
|
218
|
+
assert_equal(true, parsec.eval_equation('default_value(NULL, true)'))
|
219
|
+
|
220
|
+
# Mixing number types
|
221
|
+
assert_equal(1, parsec.eval_equation('default_value(1, 4.5)'))
|
222
|
+
assert_equal(1, parsec.eval_equation('default_value(1, 10)'))
|
223
|
+
assert_equal(1, parsec.eval_equation('default_value(1, 10.0)'))
|
224
|
+
assert_equal(1, parsec.eval_equation('default_value(1.0, 10)'))
|
225
|
+
assert_equal(1, parsec.eval_equation('default_value(1.0, 10.0)'))
|
226
|
+
assert_equal(1.5, parsec.eval_equation('default_value(1.5, 10)'))
|
227
|
+
assert_equal(1.5, parsec.eval_equation('default_value(1.5, 10.0)'))
|
228
|
+
assert_equal(1.5, parsec.eval_equation('default_value(1.5, 10.5)'))
|
229
|
+
|
230
|
+
# Error Scenarios
|
231
|
+
assert_raises(SyntaxError) { parsec.eval_equation('default_value(4.5, "string")') }
|
232
|
+
assert_raises(SyntaxError) { parsec.eval_equation('default_value("string", true)') }
|
233
|
+
assert_raises(SyntaxError) { parsec.eval_equation('default_value(true, 1)') }
|
234
|
+
end
|
235
|
+
|
192
236
|
def test_eval_equation_with_type
|
193
237
|
parsec = Parsec::Parsec
|
194
238
|
assert_equal({ value: 10, type: :int }, parsec.eval_equation_with_type('(5 + 1) + (6 - 2)'))
|
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.2
|
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-19 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:
|