Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
A
arpeggio-gm
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
backend
arpeggio-gm
Commits
dd0c4d2a
Commit
dd0c4d2a
authored
Jul 27, 2014
by
Igor Dejanovic
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Migrating tests to pytest
parent
5a959c36
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
106 additions
and
103 deletions
+106
-103
test_decorator_combine.py
tests/unit/test_decorator_combine.py
+6
-10
test_exporter.py
tests/unit/test_exporter.py
+11
-13
test_parsing_expressions.py
tests/unit/test_parsing_expressions.py
+57
-42
test_peg_parser.py
tests/unit/test_peg_parser.py
+14
-15
test_python_parser.py
tests/unit/test_python_parser.py
+13
-15
test_rulename_lookup.py
tests/unit/test_rulename_lookup.py
+5
-8
No files found.
tests/unit/test_decorator_combine.py
View file @
dd0c4d2a
...
@@ -8,14 +8,12 @@
...
@@ -8,14 +8,12 @@
# Copyright: (c) 2014 Igor R. Dejanović <igor DOT dejanovic AT gmail DOT com>
# Copyright: (c) 2014 Igor R. Dejanović <igor DOT dejanovic AT gmail DOT com>
# License: MIT License
# License: MIT License
#######################################################################
#######################################################################
from
unittest
import
TestCase
import
pytest
from
arpeggio
import
ParserPython
,
ZeroOrMore
,
OneOrMore
,
NonTerminal
,
Terminal
,
NoMatch
,
Combine
from
arpeggio
import
ParserPython
,
ZeroOrMore
,
OneOrMore
,
NonTerminal
,
Terminal
,
NoMatch
,
Combine
from
arpeggio.peg
import
ParserPEG
from
arpeggio.peg
import
ParserPEG
class
TestDecoratorCombine
(
TestCase
):
def
test_combine_python
():
def
test_combine_python
(
self
):
# This will result in NonTerminal node
# This will result in NonTerminal node
def
root
():
return
my_rule
(),
"."
def
root
():
return
my_rule
(),
"."
...
@@ -32,13 +30,11 @@ class TestDecoratorCombine(TestCase):
...
@@ -32,13 +30,11 @@ class TestDecoratorCombine(TestCase):
ptree1
=
parser
.
parse
(
input1
)
ptree1
=
parser
.
parse
(
input1
)
def
fail_nm
(
):
with
pytest
.
raises
(
NoMatch
):
ptree2
=
parser
.
parse
(
input2
)
ptree2
=
parser
.
parse
(
input2
)
self
.
assertRaises
(
NoMatch
,
fail_nm
)
assert
isinstance
(
ptree1
,
NonTerminal
)
assert
isinstance
(
ptree1
[
0
],
Terminal
)
self
.
assertIsInstance
(
ptree1
,
NonTerminal
)
assert
ptree1
[
0
]
.
value
==
"abbb"
self
.
assertIsInstance
(
ptree1
[
0
],
Terminal
)
self
.
assertEqual
(
ptree1
[
0
]
.
value
,
"abbb"
)
tests/unit/test_exporter.py
View file @
dd0c4d2a
...
@@ -7,8 +7,8 @@
...
@@ -7,8 +7,8 @@
# License: MIT License
# License: MIT License
#######################################################################
#######################################################################
import
pytest
import
os
import
os
from
unittest
import
TestCase
from
arpeggio.export
import
PMDOTExporter
,
PTDOTExporter
from
arpeggio.export
import
PMDOTExporter
,
PTDOTExporter
# Grammar
# Grammar
...
@@ -24,32 +24,30 @@ def expression(): return term, ZeroOrMore(["+", "-"], term)
...
@@ -24,32 +24,30 @@ def expression(): return term, ZeroOrMore(["+", "-"], term)
def
calc
():
return
OneOrMore
(
expression
),
EOF
def
calc
():
return
OneOrMore
(
expression
),
EOF
class
TestPythonParser
(
TestCase
):
@pytest.fixture
def
parser
():
return
ParserPython
(
calc
)
def
setUp
(
self
):
"""
Create parser
"""
self
.
parser
=
ParserPython
(
calc
)
def
test_export_parser_model
(
self
):
def
test_export_parser_model
(
parser
):
"""
"""
Testing parser model export
Testing parser model export
"""
"""
PMDOTExporter
()
.
exportFile
(
self
.
parser
.
parser_model
,
PMDOTExporter
()
.
exportFile
(
parser
.
parser_model
,
"test_exporter_parser_model.dot"
)
"test_exporter_parser_model.dot"
)
self
.
assertTrue
(
os
.
path
.
exists
(
"test_exporter_parser_model.dot"
)
)
assert
os
.
path
.
exists
(
"test_exporter_parser_model.dot"
)
def
test_export_parse_tree
(
self
):
def
test_export_parse_tree
(
parser
):
"""
"""
Testing parse tree export.
Testing parse tree export.
"""
"""
parse_tree
=
self
.
parser
.
parse
(
"-(4-1)*5+(2+4.67)+5.89/(.2+7)"
)
parse_tree
=
parser
.
parse
(
"-(4-1)*5+(2+4.67)+5.89/(.2+7)"
)
PTDOTExporter
()
.
exportFile
(
parse_tree
,
PTDOTExporter
()
.
exportFile
(
parse_tree
,
"test_exporter_parse_tree.dot"
)
"test_exporter_parse_tree.dot"
)
self
.
assertTrue
(
os
.
path
.
exists
(
"test_exporter_parse_tree.dot"
))
assert
os
.
path
.
exists
(
"test_exporter_parse_tree.dot"
)
tests/unit/test_parsing_expressions.py
View file @
dd0c4d2a
...
@@ -7,13 +7,11 @@
...
@@ -7,13 +7,11 @@
# License: MIT License
# License: MIT License
#######################################################################
#######################################################################
from
unittest
import
TestCase
import
pytest
from
arpeggio
import
ParserPython
,
ZeroOrMore
,
OneOrMore
,
NoMatch
,
EOF
,
Optional
,
And
,
Not
from
arpeggio
import
ParserPython
,
ZeroOrMore
,
OneOrMore
,
NoMatch
,
EOF
,
Optional
,
And
,
Not
from
arpeggio
import
RegExMatch
as
_
from
arpeggio
import
RegExMatch
as
_
class
TestParsingExpression
(
TestCase
):
def
test_sequence
():
def
test_sequence
(
self
):
def
grammar
():
return
(
"a"
,
"b"
,
"c"
)
def
grammar
():
return
(
"a"
,
"b"
,
"c"
)
...
@@ -21,10 +19,10 @@ class TestParsingExpression(TestCase):
...
@@ -21,10 +19,10 @@ class TestParsingExpression(TestCase):
parsed
=
parser
.
parse
(
"a b c"
)
parsed
=
parser
.
parse
(
"a b c"
)
self
.
assertEqual
(
str
(
parsed
),
"a | b | c"
)
assert
str
(
parsed
)
==
"a | b | c"
self
.
assertEqual
(
repr
(
parsed
),
"[ 'a' [0], 'b' [2], 'c' [4] ]"
)
assert
repr
(
parsed
)
==
"[ 'a' [0], 'b' [2], 'c' [4] ]"
def
test_ordered_choice
(
self
):
def
test_ordered_choice
(
):
def
grammar
():
return
[
"a"
,
"b"
,
"c"
],
EOF
def
grammar
():
return
[
"a"
,
"b"
,
"c"
],
EOF
...
@@ -32,17 +30,20 @@ class TestParsingExpression(TestCase):
...
@@ -32,17 +30,20 @@ class TestParsingExpression(TestCase):
parsed
=
parser
.
parse
(
"b"
)
parsed
=
parser
.
parse
(
"b"
)
self
.
assertEqual
(
str
(
parsed
),
"b | "
)
assert
str
(
parsed
)
==
"b | "
self
.
assertEqual
(
repr
(
parsed
),
"[ 'b' [0], EOF [1] ]"
)
assert
repr
(
parsed
)
==
"[ 'b' [0], EOF [1] ]"
parsed
=
parser
.
parse
(
"c"
)
parsed
=
parser
.
parse
(
"c"
)
self
.
assertEqual
(
str
(
parsed
),
"c | "
)
assert
str
(
parsed
)
==
"c | "
self
.
assertEqual
(
repr
(
parsed
),
"[ 'c' [0], EOF [1] ]"
)
assert
repr
(
parsed
)
==
"[ 'c' [0], EOF [1] ]"
with
pytest
.
raises
(
NoMatch
):
parser
.
parse
(
"ab"
)
self
.
assertRaises
(
NoMatch
,
lambda
:
parser
.
parse
(
"ab"
))
with
pytest
.
raises
(
NoMatch
):
self
.
assertRaises
(
NoMatch
,
lambda
:
parser
.
parse
(
"bb"
)
)
parser
.
parse
(
"bb"
)
def
test_zero_or_more
(
self
):
def
test_zero_or_more
(
):
def
grammar
():
return
ZeroOrMore
(
"a"
),
EOF
def
grammar
():
return
ZeroOrMore
(
"a"
),
EOF
...
@@ -50,17 +51,18 @@ class TestParsingExpression(TestCase):
...
@@ -50,17 +51,18 @@ class TestParsingExpression(TestCase):
parsed
=
parser
.
parse
(
"aaaaaaa"
)
parsed
=
parser
.
parse
(
"aaaaaaa"
)
self
.
assertEqual
(
str
(
parsed
),
"a | a | a | a | a | a | a | "
)
assert
str
(
parsed
)
==
"a | a | a | a | a | a | a | "
self
.
assertEqual
(
repr
(
parsed
),
"[ 'a' [0], 'a' [1], 'a' [2], 'a' [3], 'a' [4], 'a' [5], 'a' [6], EOF [7] ]"
)
assert
repr
(
parsed
)
==
"[ 'a' [0], 'a' [1], 'a' [2], 'a' [3], 'a' [4], 'a' [5], 'a' [6], EOF [7] ]"
parsed
=
parser
.
parse
(
""
)
parsed
=
parser
.
parse
(
""
)
self
.
assertEqual
(
str
(
parsed
),
""
)
assert
str
(
parsed
)
==
""
self
.
assertEqual
(
repr
(
parsed
),
"[ EOF [0] ]"
)
assert
repr
(
parsed
)
==
"[ EOF [0] ]"
self
.
assertRaises
(
NoMatch
,
lambda
:
parser
.
parse
(
"bbb"
))
with
pytest
.
raises
(
NoMatch
):
parser
.
parse
(
"bbb"
)
def
test_one_or_more
(
self
):
def
test_one_or_more
(
):
def
grammar
():
return
OneOrMore
(
"a"
)
def
grammar
():
return
OneOrMore
(
"a"
)
...
@@ -68,13 +70,17 @@ class TestParsingExpression(TestCase):
...
@@ -68,13 +70,17 @@ class TestParsingExpression(TestCase):
parsed
=
parser
.
parse
(
"aaaaaaa"
)
parsed
=
parser
.
parse
(
"aaaaaaa"
)
self
.
assertEqual
(
str
(
parsed
),
"a | a | a | a | a | a | a"
)
assert
str
(
parsed
)
==
"a | a | a | a | a | a | a"
self
.
assertEqual
(
repr
(
parsed
),
"[ 'a' [0], 'a' [1], 'a' [2], 'a' [3], 'a' [4], 'a' [5], 'a' [6] ]"
)
assert
repr
(
parsed
)
==
"[ 'a' [0], 'a' [1], 'a' [2], 'a' [3], 'a' [4], 'a' [5], 'a' [6] ]"
with
pytest
.
raises
(
NoMatch
):
parser
.
parse
(
""
)
self
.
assertRaises
(
NoMatch
,
lambda
:
parser
.
parse
(
""
))
with
pytest
.
raises
(
NoMatch
):
self
.
assertRaises
(
NoMatch
,
lambda
:
parser
.
parse
(
"bbb"
)
)
parser
.
parse
(
"bbb"
)
def
test_optional
(
self
):
def
test_optional
(
):
def
grammar
():
return
Optional
(
"a"
),
"b"
,
EOF
def
grammar
():
return
Optional
(
"a"
),
"b"
,
EOF
...
@@ -82,36 +88,42 @@ class TestParsingExpression(TestCase):
...
@@ -82,36 +88,42 @@ class TestParsingExpression(TestCase):
parsed
=
parser
.
parse
(
"ab"
)
parsed
=
parser
.
parse
(
"ab"
)
self
.
assertEqual
(
str
(
parsed
),
"a | b | "
)
assert
str
(
parsed
)
==
"a | b | "
self
.
assertEqual
(
repr
(
parsed
),
"[ 'a' [0], 'b' [1], EOF [2] ]"
)
assert
repr
(
parsed
)
==
"[ 'a' [0], 'b' [1], EOF [2] ]"
parsed
=
parser
.
parse
(
"b"
)
parsed
=
parser
.
parse
(
"b"
)
self
.
assertEqual
(
str
(
parsed
),
"b | "
)
assert
str
(
parsed
)
==
"b | "
self
.
assertEqual
(
repr
(
parsed
),
"[ 'b' [0], EOF [1] ]"
)
assert
repr
(
parsed
)
==
"[ 'b' [0], EOF [1] ]"
self
.
assertRaises
(
NoMatch
,
lambda
:
parser
.
parse
(
"aab"
))
with
pytest
.
raises
(
NoMatch
):
self
.
assertRaises
(
NoMatch
,
lambda
:
parser
.
parse
(
""
)
)
parser
.
parse
(
"aab"
)
with
pytest
.
raises
(
NoMatch
):
parser
.
parse
(
""
)
# Syntax predicates
def
test_and
(
self
):
# Syntax predicates
def
test_and
():
def
grammar
():
return
"a"
,
And
(
"b"
),
[
"c"
,
"b"
],
EOF
def
grammar
():
return
"a"
,
And
(
"b"
),
[
"c"
,
"b"
],
EOF
parser
=
ParserPython
(
grammar
)
parser
=
ParserPython
(
grammar
)
parsed
=
parser
.
parse
(
"ab"
)
parsed
=
parser
.
parse
(
"ab"
)
self
.
assertEqual
(
str
(
parsed
),
"a | b | "
)
assert
str
(
parsed
)
==
"a | b | "
self
.
assertEqual
(
repr
(
parsed
),
"[ 'a' [0], 'b' [1], EOF [2] ]"
)
assert
repr
(
parsed
)
==
"[ 'a' [0], 'b' [1], EOF [2] ]"
# 'And' will try to match 'b' and fail so 'c' will never get matched
# 'And' will try to match 'b' and fail so 'c' will never get matched
self
.
assertRaises
(
NoMatch
,
lambda
:
parser
.
parse
(
"ac"
))
with
pytest
.
raises
(
NoMatch
):
parser
.
parse
(
"ac"
)
# 'And' will not consume 'b' from the input so second 'b' will never match
# 'And' will not consume 'b' from the input so second 'b' will never match
self
.
assertRaises
(
NoMatch
,
lambda
:
parser
.
parse
(
"abb"
))
with
pytest
.
raises
(
NoMatch
):
parser
.
parse
(
"abb"
)
def
test_not
(
self
):
def
test_not
(
):
def
grammar
():
return
"a"
,
Not
(
"b"
),
[
"b"
,
"c"
],
EOF
def
grammar
():
return
"a"
,
Not
(
"b"
),
[
"b"
,
"c"
],
EOF
...
@@ -119,11 +131,14 @@ class TestParsingExpression(TestCase):
...
@@ -119,11 +131,14 @@ class TestParsingExpression(TestCase):
parsed
=
parser
.
parse
(
"ac"
)
parsed
=
parser
.
parse
(
"ac"
)
self
.
assertEqual
(
str
(
parsed
),
"a | c | "
)
assert
str
(
parsed
)
==
"a | c | "
self
.
assertEqual
(
repr
(
parsed
),
"[ 'a' [0], 'c' [1], EOF [2] ]"
)
assert
repr
(
parsed
)
==
"[ 'a' [0], 'c' [1], EOF [2] ]"
# Not will will fail on 'b'
# Not will will fail on 'b'
self
.
assertRaises
(
NoMatch
,
lambda
:
parser
.
parse
(
"ab"
))
with
pytest
.
raises
(
NoMatch
):
parser
.
parse
(
"ab"
)
# And will not consume 'c' from the input so 'b' will never match
# And will not consume 'c' from the input so 'b' will never match
self
.
assertRaises
(
NoMatch
,
lambda
:
parser
.
parse
(
"acb"
))
with
pytest
.
raises
(
NoMatch
):
parser
.
parse
(
"acb"
)
tests/unit/test_peg_parser.py
View file @
dd0c4d2a
...
@@ -7,7 +7,7 @@
...
@@ -7,7 +7,7 @@
# License: MIT License
# License: MIT License
#######################################################################
#######################################################################
from
unittest
import
TestCase
import
pytest
from
arpeggio
import
Sequence
,
NonTerminal
from
arpeggio
import
Sequence
,
NonTerminal
from
arpeggio.peg
import
ParserPEG
from
arpeggio.peg
import
ParserPEG
...
@@ -20,33 +20,32 @@ grammar = '''
...
@@ -20,33 +20,32 @@ grammar = '''
calc <- expression+ EOF;
calc <- expression+ EOF;
'''
'''
class
TestPEGParser
(
TestCase
):
def
test_construct_parser
():
def
test_construct_parser
(
self
):
parser
=
ParserPEG
(
grammar
,
'calc'
)
parser
=
ParserPEG
(
grammar
,
'calc'
)
self
.
assertEqual
(
parser
.
parser_model
.
rule
,
'calc'
)
assert
parser
.
parser_model
.
rule
==
'calc'
self
.
assertTrue
(
isinstance
(
parser
.
parser_model
,
Sequence
)
)
assert
isinstance
(
parser
.
parser_model
,
Sequence
)
self
.
assertEqual
(
parser
.
parser_model
.
nodes
[
0
]
.
name
,
'OneOrMore'
)
assert
parser
.
parser_model
.
nodes
[
0
]
.
name
==
'OneOrMore'
def
test_parse_input
(
self
):
def
test_parse_input
(
):
parser
=
ParserPEG
(
grammar
,
'calc'
)
parser
=
ParserPEG
(
grammar
,
'calc'
)
input
=
"4+5*7/3.45*-45*(2.56+32)/-56*(2-1.34)"
input
=
"4+5*7/3.45*-45*(2.56+32)/-56*(2-1.34)"
result
=
parser
.
parse
(
input
)
result
=
parser
.
parse
(
input
)
self
.
assertTrue
(
isinstance
(
result
,
NonTerminal
)
)
assert
isinstance
(
result
,
NonTerminal
)
self
.
assertEqual
(
str
(
result
),
"4 | + | 5 | * | 7 | / | 3.45 | * | - | 45 | * | ( | 2.56 | + | 32 | ) | / | - | 56 | * | ( | 2 | - | 1.34 | ) | "
)
assert
str
(
result
)
==
"4 | + | 5 | * | 7 | / | 3.45 | * | - | 45 | * | ( | 2.56 | + | 32 | ) | / | - | 56 | * | ( | 2 | - | 1.34 | ) | "
self
.
assertEqual
(
repr
(
result
),
"[ [ [ [ number '4' [0] ] ], '+' [1], [ [ number '5' [2] ], '*' [3], [ number '7' [4] ], '/' [5], [ number '3.45' [6] ], '*' [10], [ '-' [11], number '45' [12] ], '*' [14], [ '(' [15], [ [ [ number '2.56' [16] ] ], '+' [20], [ [ number '32' [21] ] ] ], ')' [23] ], '/' [24], [ '-' [25], number '56' [26] ], '*' [28], [ '(' [29], [ [ [ number '2' [30] ] ], '-' [31], [ [ number '1.34' [32] ] ] ], ')' [36] ] ] ], EOF [37] ]"
)
assert
repr
(
result
)
==
"[ [ [ [ number '4' [0] ] ], '+' [1], [ [ number '5' [2] ], '*' [3], [ number '7' [4] ], '/' [5], [ number '3.45' [6] ], '*' [10], [ '-' [11], number '45' [12] ], '*' [14], [ '(' [15], [ [ [ number '2.56' [16] ] ], '+' [20], [ [ number '32' [21] ] ] ], ')' [23] ], '/' [24], [ '-' [25], number '56' [26] ], '*' [28], [ '(' [29], [ [ [ number '2' [30] ] ], '-' [31], [ [ number '1.34' [32] ] ] ], ')' [36] ] ] ], EOF [37] ]"
def
test_reduce_tree
(
self
):
def
test_reduce_tree
(
):
parser
=
ParserPEG
(
grammar
,
'calc'
,
reduce_tree
=
True
)
parser
=
ParserPEG
(
grammar
,
'calc'
,
reduce_tree
=
True
)
input
=
"4+5*7/3.45*-45*(2.56+32)/-56*(2-1.34)"
input
=
"4+5*7/3.45*-45*(2.56+32)/-56*(2-1.34)"
result
=
parser
.
parse
(
input
)
result
=
parser
.
parse
(
input
)
self
.
assertTrue
(
isinstance
(
result
,
NonTerminal
))
assert
isinstance
(
result
,
NonTerminal
)
assert
str
(
result
)
==
"4 | + | 5 | * | 7 | / | 3.45 | * | - | 45 | * | ( | 2.56 | + | 32 | ) | / | - | 56 | * | ( | 2 | - | 1.34 | ) | "
assert
repr
(
result
)
==
"[ [ number '4' [0], '+' [1], [ number '5' [2], '*' [3], number '7' [4], '/' [5], number '3.45' [6], '*' [10], [ '-' [11], number '45' [12] ], '*' [14], [ '(' [15], [ number '2.56' [16], '+' [20], number '32' [21] ], ')' [23] ], '/' [24], [ '-' [25], number '56' [26] ], '*' [28], [ '(' [29], [ number '2' [30], '-' [31], number '1.34' [32] ], ')' [36] ] ] ], EOF [37] ]"
self
.
assertEqual
(
str
(
result
),
"4 | + | 5 | * | 7 | / | 3.45 | * | - | 45 | * | ( | 2.56 | + | 32 | ) | / | - | 56 | * | ( | 2 | - | 1.34 | ) | "
)
self
.
assertEqual
(
repr
(
result
),
"[ [ number '4' [0], '+' [1], [ number '5' [2], '*' [3], number '7' [4], '/' [5], number '3.45' [6], '*' [10], [ '-' [11], number '45' [12] ], '*' [14], [ '(' [15], [ number '2.56' [16], '+' [20], number '32' [21] ], ')' [23] ], '/' [24], [ '-' [25], number '56' [26] ], '*' [28], [ '(' [29], [ number '2' [30], '-' [31], number '1.34' [32] ], ')' [36] ] ] ], EOF [37] ]"
)
tests/unit/test_python_parser.py
View file @
dd0c4d2a
...
@@ -7,7 +7,7 @@
...
@@ -7,7 +7,7 @@
# License: MIT License
# License: MIT License
#######################################################################
#######################################################################
from
unittest
import
TestCase
import
pytest
# Grammar
# Grammar
from
arpeggio
import
Optional
,
ZeroOrMore
,
OneOrMore
,
EOF
,
ParserPython
,
\
from
arpeggio
import
Optional
,
ZeroOrMore
,
OneOrMore
,
EOF
,
ParserPython
,
\
...
@@ -23,36 +23,34 @@ def expression(): return term, ZeroOrMore(["+", "-"], term)
...
@@ -23,36 +23,34 @@ def expression(): return term, ZeroOrMore(["+", "-"], term)
def
calc
():
return
OneOrMore
(
expression
),
EOF
def
calc
():
return
OneOrMore
(
expression
),
EOF
class
TestPythonParser
(
TestCase
):
def
test_pp_construction
():
def
test_pp_construction
(
self
):
'''
'''
Tests parser construction from python internal DSL description.
Tests parser construction from python internal DSL description.
'''
'''
parser
=
ParserPython
(
calc
)
parser
=
ParserPython
(
calc
)
self
.
assertEqual
(
parser
.
parser_model
.
rule
,
'calc'
)
assert
parser
.
parser_model
.
rule
==
'calc'
self
.
assertTrue
(
isinstance
(
parser
.
parser_model
,
Sequence
)
)
assert
isinstance
(
parser
.
parser_model
,
Sequence
)
self
.
assertEqual
(
parser
.
parser_model
.
nodes
[
0
]
.
desc
,
'OneOrMore'
)
assert
parser
.
parser_model
.
nodes
[
0
]
.
desc
==
'OneOrMore'
def
test_parse_input
(
self
):
def
test_parse_input
(
):
parser
=
ParserPython
(
calc
)
parser
=
ParserPython
(
calc
)
input
=
"4+5*7/3.45*-45*(2.56+32)/-56*(2-1.34)"
input
=
"4+5*7/3.45*-45*(2.56+32)/-56*(2-1.34)"
result
=
parser
.
parse
(
input
)
result
=
parser
.
parse
(
input
)
self
.
assertTrue
(
isinstance
(
result
,
NonTerminal
)
)
assert
isinstance
(
result
,
NonTerminal
)
self
.
assertEqual
(
str
(
result
),
"4 | + | 5 | * | 7 | / | 3.45 | * | - | 45 | * | ( | 2.56 | + | 32 | ) | / | - | 56 | * | ( | 2 | - | 1.34 | ) | "
)
assert
str
(
result
)
==
"4 | + | 5 | * | 7 | / | 3.45 | * | - | 45 | * | ( | 2.56 | + | 32 | ) | / | - | 56 | * | ( | 2 | - | 1.34 | ) | "
self
.
assertEqual
(
repr
(
result
),
"[ [ [ [ number '4' [0] ] ], '+' [1], [ [ number '5' [2] ], '*' [3], [ number '7' [4] ], '/' [5], [ number '3.45' [6] ], '*' [10], [ '-' [11], number '45' [12] ], '*' [14], [ '(' [15], [ [ [ number '2.56' [16] ] ], '+' [20], [ [ number '32' [21] ] ] ], ')' [23] ], '/' [24], [ '-' [25], number '56' [26] ], '*' [28], [ '(' [29], [ [ [ number '2' [30] ] ], '-' [31], [ [ number '1.34' [32] ] ] ], ')' [36] ] ] ], EOF [37] ]"
)
assert
repr
(
result
)
==
"[ [ [ [ number '4' [0] ] ], '+' [1], [ [ number '5' [2] ], '*' [3], [ number '7' [4] ], '/' [5], [ number '3.45' [6] ], '*' [10], [ '-' [11], number '45' [12] ], '*' [14], [ '(' [15], [ [ [ number '2.56' [16] ] ], '+' [20], [ [ number '32' [21] ] ] ], ')' [23] ], '/' [24], [ '-' [25], number '56' [26] ], '*' [28], [ '(' [29], [ [ [ number '2' [30] ] ], '-' [31], [ [ number '1.34' [32] ] ] ], ')' [36] ] ] ], EOF [37] ]"
def
test_reduce_tree
(
self
):
def
test_reduce_tree
(
):
parser
=
ParserPython
(
calc
,
reduce_tree
=
True
)
parser
=
ParserPython
(
calc
,
reduce_tree
=
True
)
input
=
"4+5*7/3.45*-45*(2.56+32)/-56*(2-1.34)"
input
=
"4+5*7/3.45*-45*(2.56+32)/-56*(2-1.34)"
result
=
parser
.
parse
(
input
)
result
=
parser
.
parse
(
input
)
self
.
assertTrue
(
isinstance
(
result
,
NonTerminal
)
)
assert
isinstance
(
result
,
NonTerminal
)
self
.
assertEqual
(
str
(
result
),
"4 | + | 5 | * | 7 | / | 3.45 | * | - | 45 | * | ( | 2.56 | + | 32 | ) | / | - | 56 | * | ( | 2 | - | 1.34 | ) | "
)
assert
str
(
result
)
==
"4 | + | 5 | * | 7 | / | 3.45 | * | - | 45 | * | ( | 2.56 | + | 32 | ) | / | - | 56 | * | ( | 2 | - | 1.34 | ) | "
self
.
assertEqual
(
repr
(
result
),
"[ [ number '4' [0], '+' [1], [ number '5' [2], '*' [3], number '7' [4], '/' [5], number '3.45' [6], '*' [10], [ '-' [11], number '45' [12] ], '*' [14], [ '(' [15], [ number '2.56' [16], '+' [20], number '32' [21] ], ')' [23] ], '/' [24], [ '-' [25], number '56' [26] ], '*' [28], [ '(' [29], [ number '2' [30], '-' [31], number '1.34' [32] ], ')' [36] ] ] ], EOF [37] ]"
)
assert
repr
(
result
)
==
"[ [ number '4' [0], '+' [1], [ number '5' [2], '*' [3], number '7' [4], '/' [5], number '3.45' [6], '*' [10], [ '-' [11], number '45' [12] ], '*' [14], [ '(' [15], [ number '2.56' [16], '+' [20], number '32' [21] ], ')' [23] ], '/' [24], [ '-' [25], number '56' [26] ], '*' [28], [ '(' [29], [ number '2' [30], '-' [31], number '1.34' [32] ], ')' [36] ] ] ], EOF [37] ]"
tests/unit/test_rulename_lookup.py
View file @
dd0c4d2a
...
@@ -7,7 +7,7 @@
...
@@ -7,7 +7,7 @@
# License: MIT License
# License: MIT License
#######################################################################
#######################################################################
from
unittest
import
TestCase
import
pytest
# Grammar
# Grammar
from
arpeggio
import
ParserPython
,
ZeroOrMore
from
arpeggio
import
ParserPython
,
ZeroOrMore
...
@@ -18,16 +18,13 @@ def bar(): return "c"
...
@@ -18,16 +18,13 @@ def bar(): return "c"
def
baz
():
return
"d"
def
baz
():
return
"d"
def
test_lookup_single
():
class
TestPEGParser
(
TestCase
):
def
test_lookup_single
(
self
):
parser
=
ParserPython
(
foo
)
parser
=
ParserPython
(
foo
)
result
=
parser
.
parse
(
"a c b d"
)
result
=
parser
.
parse
(
"a c b d"
)
self
.
assertTrue
(
hasattr
(
result
,
"bar"
)
)
assert
hasattr
(
result
,
"bar"
)
self
.
assertTrue
(
hasattr
(
result
,
"baz"
)
)
assert
hasattr
(
result
,
"baz"
)
self
.
assertTrue
(
not
hasattr
(
result
,
"unexisting"
)
)
assert
not
hasattr
(
result
,
"unexisting"
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment