-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Docstring to EvaluateExpression() function
Add recursive parsing function to help evaluate ast trees Fix unexpected behaviour with EvaluateExpression() resulting in successfully parsing constant strings Improve test completeness for EvaluateExpression and StringFormat methods
- Loading branch information
1 parent
2eaa9c8
commit 90c30d8
Showing
2 changed files
with
71 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,44 @@ | ||
import pytest | ||
|
||
from objdictgen.node import EvaluateExpression | ||
from objdictgen.node import StringFormat, EvaluateExpression | ||
|
||
def test_string_format(): | ||
assert StringFormat('Additional Server SDO %d Parameter[(idx)]', 5, 0) == 'Additional Server SDO 5 Parameter' | ||
assert StringFormat('Restore Manufacturer Defined Default Parameters %d[(sub - 3)]', 1, 5) == 'Restore Manufacturer Defined Default Parameters 2' | ||
assert StringFormat('This doesn\'t match the regex', 1, 2) == 'This doesn\'t match the regex' | ||
|
||
assert StringFormat('%s %.3f[(idx,sub)]', 1, 2) == '1 2.000' | ||
assert StringFormat('%s %.3f[( idx , sub )]', 1, 2) == '1 2.000' | ||
|
||
with pytest.raises(TypeError): | ||
StringFormat('What are these %s[("tests")]', 0, 1) | ||
|
||
with pytest.raises(TypeError): | ||
StringFormat('There is nothing to format[(idx, sub)]', 1, 2) | ||
|
||
with pytest.raises(Exception): | ||
StringFormat('Unhandled arithmatic[(idx*sub)]', 2, 4) | ||
|
||
|
||
def test_evaluate_expression(): | ||
|
||
assert EvaluateExpression('4+3') == 7 | ||
assert EvaluateExpression('4-3') == 1 | ||
assert EvaluateExpression('11') == 11 | ||
assert EvaluateExpression('4+3+2') == 9 | ||
assert EvaluateExpression('4+3-2') == 5 | ||
|
||
with pytest.raises(TypeError): | ||
EvaluateExpression('3-"tests"') | ||
|
||
with pytest.raises(SyntaxError): | ||
EvaluateExpression('4+3+2') | ||
EvaluateExpression('4+3') | ||
EvaluateExpression('4*3') | ||
|
||
with pytest.raises(TypeError): | ||
EvaluateExpression('str') | ||
|
||
with pytest.raises(TypeError): | ||
EvaluateExpression('"str"') | ||
|
||
with pytest.raises(SyntaxError): | ||
EvaluateExpression('$NODEID+12') |