Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
uceme
mjtest
Commits
c0757d6c
Commit
c0757d6c
authored
Nov 09, 2016
by
Johannes Bechberger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Modify ast mode checker algorithm
parent
fec32835
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
28 additions
and
8 deletions
+28
-8
README.mdwn
README.mdwn
+1
-1
mjtest/test/ast_tests.py
mjtest/test/ast_tests.py
+27
-7
No files found.
README.mdwn
View file @
c0757d6c
...
...
@@ -75,7 +75,7 @@ Test types for the ast mode
<tr>
<td><code>.valid.mj</code> <code>.mj</code>
<td>Pretty printing the source file should result in the same output as pretty printing the already pretty printed file.
The sorted
lexer output for the
last should be the same as the sorted
lexer output f
or
the
source
file.</td>
All lines in the
lexer output for the
source file should be present in the
lexer output
o
f the
pretty printed
file.</td>
</tr>
</table>
...
...
mjtest/test/ast_tests.py
View file @
c0757d6c
import
difflib
import
os
import
shutil
,
logging
from
typing
import
Tuple
from
typing
import
Tuple
,
Dict
from
mjtest.environment
import
Environment
,
TestMode
from
mjtest.test.syntax_tests
import
BasicSyntaxTest
from
mjtest.test.tests
import
TestCase
,
BasicDiffTestResult
,
BasicTestResult
...
...
@@ -36,22 +36,23 @@ class ASTPrettyPrintTest(BasicSyntaxTest):
rtcode_lex
,
out_lex
,
err_lex
=
self
.
env
.
run_mj_command
(
TestMode
.
lexer
,
self
.
file
)
rtcode_lex2
,
out_lex2
,
err_lex2
=
self
.
env
.
run_mj_command
(
TestMode
.
lexer
,
tmp_file2
)
os
.
remove
(
tmp_file2
)
out_lex
=
self
.
_sort_lexed
(
out_lex
.
decode
())
out_lex2
=
self
.
_sort_lexed
(
out_lex2
.
decode
())
out_lex
=
self
.
_line_count
(
out_lex
.
decode
())
out_lex2
=
self
.
_line_count
(
out_lex2
.
decode
())
comp
=
self
.
_comp_dicts
(
out_lex
,
out_lex2
)
incorrect_msg
,
rtcode
=
""
,
0
if
rtcode_lex
+
rtcode_lex2
:
incorrect_msg
,
rtcode
=
"Lexing failed"
,
1
elif
out
!=
out2
:
incorrect_msg
,
rtcode
=
"Not idempotent"
,
1
elif
out_lex
!=
out_lex2
:
elif
comp
[
0
]
:
incorrect_msg
,
rtcode
=
"Sorted and lexed second pretty print differs from original"
,
1
btr
=
BasicTestResult
(
self
,
rtcode
,
incorrect_msg
=
incorrect_msg
)
btr
.
add_additional_text
(
"First round output"
,
out
)
btr
.
add_additional_text
(
"Second round output"
,
out2
)
btr
.
add_additional_text
(
"Diff"
,
self
.
_diff
(
out
,
out2
))
btr
.
add_additional_text
(
"Original file, sorted and lexed"
,
out_lex
)
btr
.
add_additional_text
(
"Second round output, sorted and lexed"
,
out_lex2
)
btr
.
add_additional_text
(
"Diff"
,
self
.
_diff
(
out_lex
,
out_lex2
)
)
btr
.
add_additional_text
(
"Original file, sorted and lexed"
,
self
.
_dict_to_str
(
out_lex
)
)
btr
.
add_additional_text
(
"Second round output, sorted and lexed"
,
self
.
_dict_to_str
(
out_lex2
)
)
btr
.
add_additional_text
(
"Diff"
,
comp
[
1
]
)
return
btr
def
_diff
(
self
,
first
:
str
,
second
:
str
)
->
str
:
...
...
@@ -61,7 +62,26 @@ class ASTPrettyPrintTest(BasicSyntaxTest):
#return "".join(difflib.Differ().compare(self.expected_output.splitlines(True), self.output.splitlines(True)))
return
""
.
join
(
sorted
(
lexed
.
splitlines
(
True
)))
def
_line_count
(
self
,
lexed
:
str
)
->
Dict
[
str
,
int
]:
d
=
{}
for
l
in
lexed
.
splitlines
():
if
l
not
in
d
:
d
[
l
]
=
1
else
:
d
[
l
]
+=
1
return
d
def
_dict_to_str
(
self
,
d
:
Dict
[
str
,
int
])
->
str
:
return
"
\n
"
.
join
(
"{} {}"
.
format
(
k
,
d
[
k
])
for
k
in
sorted
(
d
.
keys
()))
def
_comp_dicts
(
self
,
d_orig
:
Dict
[
str
,
int
],
d_second
:
Dict
[
str
,
int
])
->
(
bool
,
str
):
strs
=
[]
for
k
in
sorted
(
d_orig
.
keys
()):
c_orig
=
d_orig
[
k
]
c_second
=
d_second
[
k
]
if
k
in
d_second
else
0
if
c_orig
>
c_second
:
strs
.
append
(
"{} [original file contained {}, pretty printed only {}]"
.
format
(
c_orig
,
c_second
))
return
len
(
strs
)
==
0
,
"
\n
"
.
join
(
strs
)
def
_pretty_print
(
self
,
input_file
:
str
,
output_file
:
str
)
->
Tuple
[
int
,
str
,
str
]:
out
,
err
,
rtcode
=
self
.
env
.
run_mj_command
(
TestMode
.
ast
,
input_file
)
...
...
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