-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_parser.py
147 lines (106 loc) · 4.98 KB
/
test_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""Tests for the Python version of the PeopleCode parser."""
import os.path
from antlr4 import CommonTokenStream, FileStream, ParseTreeWalker
from peoplecodeparser.PeopleCodeLexer import PeopleCodeLexer
from peoplecodeparser.PeopleCodeParser import PeopleCodeParser
from peoplecodeparser.PeopleCodeParserListener import PeopleCodeParserListener
_TEST_DIR = os.path.dirname(__file__)
class SQLExecListener(PeopleCodeParserListener):
"""Rule to check for SQLExec calls with literal SQL statements."""
def __init__(self):
"""Initialize the listener."""
self.lines_with_sqlexec_literals = []
def enterSimpleFunctionCall(
self,
ctx: PeopleCodeParser.SimpleFunctionCallContext):
"""Enter a parse tree for a simpleFunctionCall parser rule.
This will count the number of times a SQLExec call is found with
a string literal or an expression (as opposed to a SQL
Definition reference). This could be enhanced to exclude "at"
references (e.g., @("SQL." | &someVariable)), but the current
implementation is sufficient for this test.
"""
line = ctx.start.line
function_name = ctx.genericID().allowableFunctionName()
if function_name and function_name.getText().upper() == 'SQLEXEC':
args = ctx.functionCallArguments()
if args:
expr = args.expression(i=0)
if hasattr(expr, 'literal') or hasattr(expr, 'expression'):
self.lines_with_sqlexec_literals.append(line)
else:
# SQLExec with no arguments, should never happen in valid
# PeopleCode
assert False
def _parse_file(file_name, app_class=False):
file_path = os.path.join(_TEST_DIR, file_name)
file_stream = FileStream(file_path, encoding='utf-8')
lexer = PeopleCodeLexer(file_stream)
token_stream = CommonTokenStream(lexer)
parser = PeopleCodeParser(token_stream)
if app_class:
tree = parser.appClass()
else:
tree = parser.program()
return tree
def _validate_parse(file_name, capfd, app_class=False, error_text=''):
tree = _parse_file(file_name, app_class=app_class)
captured = capfd.readouterr()
assert captured.err == error_text
return tree
def test_app_class_1(capfd):
"""Validate parsing of an Application Class.
In addition to simple parsing validation, this test will also use a
custom listener to verify that the Application Class in question
includes two SQLExec calls with string literals. Note that the
Application Class has a third such call commented out, which should
not be detected.
"""
tree = _validate_parse('EOCF_FILTER.FilterForm.ppl', capfd, app_class=True)
walker = ParseTreeWalker()
listener = SQLExecListener()
walker.walk(listener, tree)
assert set(listener.lines_with_sqlexec_literals) == {2528, 2713}
def test_app_class_2(capfd):
"""Validate parsing of an Application Class."""
_validate_parse('EP_FUNCTIONS.WorkCenterUI.ppl', capfd, app_class=True)
def test_app_class_3(capfd):
"""Validate parsing of an Application Class."""
_validate_parse('HRMH_SETUP.HRMHServices.ppl', capfd, app_class=True)
def test_app_class_4(capfd):
"""Validate parsing of an Application Class."""
_validate_parse('HRS_CANDIDATE_MANAGER.CMP_CAND_EXP.UI.PageLayout.ppl',
capfd, app_class=True)
def test_app_class_5(capfd):
"""Validate parsing of an Application Class."""
_validate_parse('PSXP_RPTDEFNMANAGER.ReportDefn.ppl', capfd,
app_class=True)
def test_program_1(capfd):
"""Validate parsing of a PeopleCode program."""
_validate_parse('FUNCLIB_EP.EP_CHKPT.FieldFormula.ppl', capfd)
def test_program_2(capfd):
"""Validate parsing of a PeopleCode program."""
_validate_parse('FUNCLIB_HR_SS.HR_SS_CONFIG_FUNC.FieldFormula.ppl', capfd)
def test_program_3(capfd):
"""Validate parsing of a PeopleCode program."""
_validate_parse('FUNCLIB_W3EBENR.PLAN_TYPE_2X.FieldFormula.ppl', capfd)
def test_program_4(capfd):
"""Validate parsing of a PeopleCode program."""
_validate_parse('PSIBLOGICAL_WRK.TREECTLEVENT.FieldFormula.ppl', capfd)
def test_program_5(capfd):
"""Validate parsing of a PeopleCode program."""
_validate_parse('PTPG_WORKREC.FUNCLIB.FieldFormula.ppl', capfd)
def test_error_1(capfd):
"""Verify the expected error in parsing of a PeopleCode program.
See README.md for details.
"""
_validate_parse('HR_MSS_CT_CONF_FL.Activate.ppl', capfd,
error_text=("line 15:0 mismatched input '&Confirmation' "
"expecting {<EOF>, ';'}\n"))
def test_error_2(capfd):
"""Verify the expected error in parsing of a PeopleCode program.
See README.md for details.
"""
_validate_parse('PT_HEADERPAGE.Activate.ppl', capfd,
error_text=("line 376:0 mismatched input '<EOF>' "
'expecting {ELSE, END_IF}\n'))