-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lexer testing #75
Merged
Abdur-rahmaanJ
merged 8 commits into
Abdur-rahmaanJ:master
from
CodeScratcher:Lexer-testing
Oct 14, 2021
Merged
Lexer testing #75
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7928b6c
Good standards: Self as the first argument of lex
thisusernameisavailable 94eb2c1
Reverted broken change and added lex tests
thisusernameisavailable a6beecb
Finished testing functions.
thisusernameisavailable 34ab864
Added some tests, including PEMDAS and for loops
thisusernameisavailable b726f40
Variable testing
thisusernameisavailable 78c595d
Better standards
thisusernameisavailable c4a35e5
No import goes unused
thisusernameisavailable 6ec21b7
OK now no import goes unused
thisusernameisavailable File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from gb_utils.greenberry_lex import GreenBerryLex | ||
from symbols import * | ||
from debug_cp import * | ||
import inspect | ||
MATH_OPS = ["+", "-", "*", "/"] | ||
BOOLS = [S.TRUE, S.FALSE] | ||
BOOL_OPS = [S.GREATER, S.LESS] | ||
EOS = [S.NL, S.EOF] | ||
|
||
|
||
def greenberry_lex_test(x, expected): | ||
KWDs = [ | ||
getattr(S, i) | ||
for i in [ | ||
b[0] | ||
for b in [ | ||
a | ||
for a in inspect.getmembers(S, lambda a: not inspect.isroutine(a)) | ||
if not (a[0].startswith("__") and a[0].endswith("__")) | ||
] | ||
] | ||
] | ||
words = GreenBerryLex.lex(x, KWDs, add_eof=1) | ||
print("\033[1mWords:\033[0m") | ||
is_correct = True | ||
j = 0 | ||
for i in words: | ||
print(i) | ||
if not i == expected[j]: | ||
print("\x1b[31m This token is unexpected.\x1b[39m") | ||
is_correct = False | ||
j += 1 | ||
return is_correct | ||
|
||
|
||
def greenberry_lex_tester(to_lex, *args): | ||
l_args = list(args) | ||
l_args.append("{***end-of-file***}") | ||
result = greenberry_lex_test(to_lex, l_args) | ||
if result: | ||
print("\x1b[32m Test passed \x1b[39m") | ||
else: | ||
print("\x1b[31m Test failed \x1b[39m") | ||
return result | ||
def greenberry_multi_tests(*args): | ||
result = True | ||
for i in args: | ||
cur = greenberry_lex_tester(i["test"], *i["expected"]) | ||
if not cur: | ||
result = False | ||
if result: | ||
print("\x1b[32m All tests passed. \x1b[39m") | ||
else: | ||
print("\x1b[31m A test failed. \x1b[39m") | ||
|
||
|
||
greenberry_multi_tests({ | ||
"test": "print \"hi\"", | ||
"expected": ["print", "\"hi\""] | ||
}, | ||
{ | ||
"test": "print string hi", | ||
"expected": ["print", "string", "hi"] | ||
}, | ||
{ | ||
"test": "5 * 3 + (3 / 1)", | ||
"expected": ["5", "*", "3", "+", "(", "3", "/", "1", ")"] | ||
}, | ||
{ | ||
"test": "for 3 times: print greenBerry", | ||
"expected": ["for", "3", "times", ":", "print", "greenBerry"] | ||
}, | ||
{ | ||
"test": "var y = @ x", | ||
"expected": ["var", "y", "=", "@", "x"] | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though a work of art, those comprehensions will give me nightmares while reading the source again. Could you use normal loops? Thank You!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@thisusernameisavailable