-
-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[eggex] Encode flags to normalized string
- Loading branch information
Andy Chu
committed
Dec 13, 2023
1 parent
1893841
commit d52d495
Showing
2 changed files
with
75 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#!/usr/bin/env python2 | ||
from __future__ import print_function | ||
|
||
import unittest | ||
|
||
from _devbuild.gen.id_kind_asdl import Id | ||
from _devbuild.gen.syntax_asdl import EggexFlag, Token, source, SourceLine | ||
|
||
from asdl import runtime | ||
from ysh import regex_translate | ||
|
||
|
||
def _Name(s): | ||
# Doesn't work | ||
#return lexer.DummyToken(Id.Expr_Name, s) | ||
src = source.Stdin('') | ||
source_line = SourceLine(1, s, src) | ||
return Token(Id.Expr_Name, 0, len(s), runtime.NO_SPID, source_line, None) | ||
|
||
|
||
class RegexTranslateTest(unittest.TestCase): | ||
|
||
def testEncodeFlags(self): | ||
reg_icase = _Name('reg_icase') | ||
i = _Name('i') # abbreviation | ||
reg_newline = _Name('reg_newline') | ||
bad = _Name('bad') | ||
|
||
flags = [EggexFlag(False, reg_icase)] | ||
self.assertEqual('i', regex_translate._EncodeFlags(flags)) | ||
|
||
flags = [EggexFlag(False, i)] | ||
self.assertEqual('i', regex_translate._EncodeFlags(flags)) | ||
|
||
flags = [EggexFlag(False, bad)] | ||
try: | ||
regex_translate._EncodeFlags(flags) | ||
except Exception as e: | ||
print(e.UserErrorString()) | ||
else: | ||
self.fail('Should have failed') | ||
|
||
order1 = [EggexFlag(False, reg_icase), EggexFlag(False, reg_newline)] | ||
order2 = [EggexFlag(False, reg_newline), EggexFlag(False, reg_icase)] | ||
|
||
self.assertEqual('in', regex_translate._EncodeFlags(order1)) | ||
self.assertEqual('in', regex_translate._EncodeFlags(order2)) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |