Skip to content

Commit 9feae1b

Browse files
author
Claudiu Popa
committed
Add support for case insensitivity in grammar constructs.
1 parent 232479b commit 9feae1b

File tree

2 files changed

+160
-62
lines changed

2 files changed

+160
-62
lines changed

tests/test_grammar.py

Lines changed: 88 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ def test_keyword(self):
142142
"in", "param", "process", "return",
143143
"switch", "throw", "trap", "try",
144144
"until", "using", "var", "while")
145+
literals = chain(literals,
146+
[literal.swapcase() for literal in literals])
145147
self._test_expected(Keyword, literals)
146148

147149
def test_expandable_string_part(self):
@@ -547,7 +549,8 @@ def test_single_line_comment(self):
547549
self._parse(SingleLineComment, "t#rrop")
548550

549551
def test_numeric_multiplier(self):
550-
multipliers = ["kb", "mb", "gb", "tb", "pb"]
552+
multipliers = ["kb", "mb", "gb", "tb", "pb",
553+
"Kb", "MB", "gB", "TB", "Pb"]
551554
self._test_expected(NumericMultiplier, multipliers)
552555

553556
with self.assertRaises(ParseError):
@@ -689,6 +692,8 @@ def test_comparison_operator(self):
689692
"split"
690693
]
691694
literals = ["-{}".format(op) for op in ops]
695+
literals = chain(literals,
696+
[literal.swapcase() for literal in literals])
692697
self._test_expected(ComparisonOperator, literals)
693698

694699
def test_operator_or_punctuator(self):
@@ -699,10 +704,10 @@ def test_operator_or_punctuator(self):
699704
"-", "--",
700705
"-and", "-band", "-bnot",
701706
"-bor", "-bxor", "-not",
702-
"-or", "-xor",
707+
"-or", "-xor", "-AND", "-BAND", "-BNOT",
708+
"-BOR", "-Bxor", "-nOt",
703709
"+=", "*=",
704-
">>",
705-
"-inotlike",
710+
">>", "-INOTliKe", "-inotlike",
706711
"-f"
707712
]
708713
self._test_expected(OperatorOrPunctuator, literals)
@@ -722,9 +727,9 @@ def test_variable_namespace(self):
722727
self._parse(VariableNamespace, ":a")
723728

724729
def test_variable_scope(self):
725-
scopes = ["globe:", "local:", "private:", "script:"]
730+
scopes = ["globe:", "global:", "local:", "private:", "script:",
731+
"GLOBE:", "GLOBAL:", "Local:", "Private:", "SCRIPT:"]
726732
self._test_expected(VariableScope, scopes)
727-
728733
self._test_expected(VariableScope, ["abc:"])
729734

730735
def test_braced_variable(self):
@@ -1019,12 +1024,15 @@ def test_statement_terminators(self):
10191024
self._test_expected(StatementTerminators, [";;", "\n\n"])
10201025

10211026
def test_block_name(self):
1022-
names = ["dynamicparam", "begin", "process", "end"]
1027+
names = ["dynamicparam", "begin", "process", "end",
1028+
"DynamicParam", "BEGIN", "PrOcEss", "End"]
10231029
self._test_expected(BlockName, names)
10241030

10251031
def test_switch_parameters(self):
10261032
params = ["-regex", "-wildcard", "-exact", "-casesensitive"]
10271033
self._test_expected(SwitchParameter, params)
1034+
self._test_expected(SwitchParameter,
1035+
[param.upper() for param in params])
10281036

10291037
params = [param + " " + param
10301038
for param in params]
@@ -1048,20 +1056,25 @@ def test_flow_control_statement(self):
10481056
"break",
10491057
"break $lab",
10501058
"break labelA",
1059+
"Break $lab",
10511060

10521061
"continue",
10531062
"continue $lab",
10541063
"continue labelA",
1064+
"CONTINUE labelB",
10551065

10561066
"throw",
10571067
"throw 100",
10581068
'throw "No such record in file"',
1069+
"Throw 42",
10591070

10601071
"return 1",
10611072
"return $4",
1073+
"Return $52",
10621074

10631075
"exit",
10641076
"exit $4",
1077+
"EXIT $42",
10651078
]
10661079
self._test_expected(FlowControlStatement, parts)
10671080

@@ -1227,6 +1240,9 @@ def test_bitwise_expression(self):
12271240
"0x0F0F -bxor 0xFEL",
12281241
"0x0F0F -bxor 14.40D",
12291242
"0x0F0F -bxor 14.6",
1243+
"0x0F0F -BXOR 14",
1244+
"0x0F0F -BOR 24",
1245+
"0x0F0F -BaND 23",
12301246
]
12311247
self._test_expected(BitwiseExpression, parts)
12321248

@@ -1265,6 +1281,9 @@ def test_logical_expression(self):
12651281
"($j -eq 5) -and (++$k -gt 15)",
12661282
"($j++ -gt 5) -or (++$k -lt 15)",
12671283
"($j -eq 10) -or ($k -gt 15)",
1284+
"($j -eq 10) -OR ($k -gt 15)",
1285+
"($j -eq 10) -XoR ($k -gt 15)",
1286+
"($j -eq 10) -AND ($k -gt 15)",
12681287
]
12691288
self._test_expected(LogicalExpression, parts)
12701289

@@ -1431,6 +1450,8 @@ def test_if_statement(self):
14311450
else { "Grade F" }'''),
14321451
dedent('''if ($grade -ge 90) { "Grade A" }'''),
14331452
dedent('''if($grade -ge 90){"Grade A"}'''),
1453+
dedent('''IF($grade -ge 90){"Grade A"}'''),
1454+
dedent('''If($grade -ge 90){"Grade A"}'''),
14341455
]
14351456
self._test_expected(IfStatement, parts)
14361457

@@ -1439,6 +1460,8 @@ def test_else_clause(self):
14391460
'else { "grade f" }',
14401461
'else{"grafe f"\n}',
14411462
'else{"giraffe"}',
1463+
'ELSE{"giraffe"}',
1464+
'Else{"giraffe"}',
14421465
]
14431466
self._test_expected(ElseClause, parts)
14441467

@@ -1447,6 +1470,8 @@ def test_else_if_clause(self):
14471470
'elseif ($grade -ge 80) { "Grade B" }',
14481471
'elseif($grade -ge 70){ "Grade C" }',
14491472
'elseif($grade -ge 60)\n{"Grade D"}',
1473+
'ELSEIF($grade -ge 60)\n{"Grade D"}',
1474+
'ElseIf($grade -ge 60)\n{"Grade D"}',
14501475
]
14511476
self._test_expected(ElseIfClause, parts)
14521477

@@ -1470,6 +1495,8 @@ def test_for_statement(self):
14701495
'for ($i = 1;)\n{ "$i" }',
14711496
'for ()\n{ "$i" }',
14721497
'for (){ "$i" }',
1498+
'For (){ "$i" }',
1499+
'FOR (){ "$i" }',
14731500
]
14741501
self._test_expected(ForStatement, parts)
14751502

@@ -1491,7 +1518,9 @@ def test_while_statement(self):
14911518
'while ($i++ -lt 2) { $i }',
14921519
'while ($i++ -lt 2)\n{ $i }',
14931520
'while (\n$i++ -lt 2\n) { $i }',
1494-
'while (1) { $i }'
1521+
'while (1) { $i }',
1522+
'WHILE (1) { $i }',
1523+
'While (1) { $i }',
14951524
]
14961525
self._test_expected(WhileStatement, parts)
14971526

@@ -1500,7 +1529,9 @@ def test_do_statement(self):
15001529
'do\n{\n$i;\n}\nwhile (++$i -le 5)',
15011530
'do\n{\n$i;}\nuntil (++$i -gt 5)',
15021531
'do { $i } while ($i)',
1503-
'do { $i } until ($i -le 85)'
1532+
'do { $i } until ($i -le 85)',
1533+
'DO { $i } Until ($i -le 85)',
1534+
'Do { $i } WHILE ($i)',
15041535
]
15051536
self._test_expected(DoStatement, parts)
15061537

@@ -1511,12 +1542,14 @@ def test_foreach_statement(self):
15111542
'foreach ($t in [byte],[int],[long]) {$t::MaxValue}',
15121543
'foreach ($f in dir *.txt)\n\n{}',
15131544
'foreach ($e in $h1.Keys) {}',
1545+
'Foreach ($e In $h1.Keys) {}',
1546+
'FOREACH ($e IN $h1.Keys) {}',
15141547
]
15151548
self._test_expected(ForeachStatement, parts)
15161549

15171550
def test_foreach_parameter(self):
1518-
parsed = self._parse(ForeachParameter, "-parallel")
1519-
self.assertEqual(str(parsed), "-parallel")
1551+
params = ["-parallel", "-Parallel", "-PARALLEL"]
1552+
self._test_expected(ForeachParameter, params)
15201553

15211554
def test_switch_clause(self):
15221555
parts = [
@@ -1600,6 +1633,14 @@ def test_switch_statement(self):
16001633
{
16011634
a* { ++$lineCount }
16021635
}'''),
1636+
dedent('''Switch -wildcard ("abc")
1637+
{
1638+
a* { ++$lineCount }
1639+
}'''),
1640+
dedent('''SWITCH -wildcard ("abc")
1641+
{
1642+
a* { ++$lineCount }
1643+
}'''),
16031644

16041645
dedent('''switch -regex -casesensitive ("abc")
16051646
{
@@ -1631,14 +1672,17 @@ def test_expression_with_unary_operator(self):
16311672
for operator in
16321673
(",", "-bnot", "-not", "-split", "-join", "!", "+")
16331674
]
1634-
parts += [
1635-
"[bool]-10",
1636-
"[int]-10.70D",
1637-
"[int]10.7",
1638-
'[long]"+2.3e+3"',
1639-
'[char[]]"Hello"',
1640-
'++$k', '++${k}',
1641-
]
1675+
parts = chain(
1676+
parts,
1677+
[literal.swapcase() for literal in parts],
1678+
[
1679+
"[bool]-10",
1680+
"[int]-10.70D",
1681+
"[int]10.7",
1682+
'[long]"+2.3e+3"',
1683+
'[char[]]"Hello"',
1684+
'++$k', '++${k}',
1685+
])
16421686
self._test_expected(ExpressionWithUnaryOperator, parts)
16431687

16441688
def test_merging_redirection_operator(self):
@@ -1701,19 +1745,25 @@ def test_trap_statement(self):
17011745
'trap { $j =2; continue }',
17021746
'trap {$j =2; break }',
17031747
'trap{}',
1748+
'Trap{}',
1749+
'TRAP{}',
17041750
]
17051751
self._test_expected(TrapStatement, stmts)
17061752

17071753
def test_finally_clause(self):
17081754
clauses = [
17091755
'finally\n{ "Tobi is a good boy" }',
17101756
'finally{$a=4}',
1757+
'Finally{$a=4}',
1758+
'FINALLY{$a=4}',
17111759
]
17121760
self._test_expected(FinallyClause, clauses)
17131761

17141762
def test_catch_clause(self):
17151763
clauses = [
17161764
'catch\n{"Caught unexpected exception"}',
1765+
'Catch\n{"Caught unexpected exception"}',
1766+
'CATCH\n{"Caught unexpected exception"}',
17171767
dedent('''catch [IndexOutOfRangeException]
17181768
{
17191769
"Handling out-of-bounds index, >$_<`n"
@@ -1852,6 +1902,10 @@ def test_function_statement(self):
18521902
{
18531903
$_ * $_
18541904
}'''),
1905+
dedent('''FILTER Get-Square2
1906+
{
1907+
$_ * $_
1908+
}'''),
18551909
dedent('''function Get-Power ([long]$base, [int]$exponent)
18561910
{
18571911
$result = 1
@@ -1862,6 +1916,7 @@ def test_function_statement(self):
18621916
return $result
18631917
}'''),
18641918
'function Find-Str ([string]$str, [int]$start_pos = 0) { $str }',
1919+
'Function Find-Str ([string]$str, [int]$start_pos = 0) { $str }',
18651920
dedent('''function Get-Square1
18661921
{
18671922
foreach ($i in $input)
@@ -1882,6 +1937,9 @@ def test_function_statement(self):
18821937
dedent('''workflow paralleltest {
18831938
parallel {
18841939
Get-Service -Name s*}}'''),
1940+
dedent('''WORKflow paralleltest {
1941+
parallel {
1942+
Get-Service -Name s*}}'''),
18851943
]
18861944
self._test_expected(FunctionStatement, funcs)
18871945

@@ -1968,27 +2026,34 @@ def test_inlinescript_statement(self):
19682026
stmts = [
19692027
'inlinescript {"Inline A0 = $a"}',
19702028
'inlinescript{$a = $Using:a+1; $a}',
2029+
'InlineScript{$a = $Using:a+1; $a}',
2030+
19712031
]
19722032
self._test_expected(InlinescriptStatement, stmts)
19732033

19742034
def test_parallel_statement(self):
19752035
stmts = [
19762036
'parallel {"Inline A0 = $a"}',
19772037
'parallel{$a = $Using:a+1; $a}',
2038+
'Parallel{$a = $Using:a+1; $a}',
2039+
'PARALLEL{$a = $Using:a+1; $a}',
19782040
]
19792041
self._test_expected(ParallelStatement, stmts)
19802042

19812043
def test_sequence_statement(self):
19822044
stmts = [
19832045
'sequence {"Inline A0 = $a"}',
19842046
'sequence {$a = $Using:a+1; $a}',
2047+
'Sequence {$a = $Using:a+1; $a}',
19852048
]
19862049
self._test_expected(SequenceStatement, stmts)
19872050

19882051
def test_param_block(self):
19892052
blocks = [
19902053
dedent('''param ([Parameter(Mandatory = $true)]
19912054
[string[]] $ComputerName )'''),
2055+
dedent('''PARAM ([Parameter(Mandatory = $true)]
2056+
[string[]] $ComputerName )'''),
19922057
dedent('''param (
19932058
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
19942059
[string[]]$ComputerName )'''),
@@ -2096,6 +2161,7 @@ def test_data_commands_allowed(self):
20962161
commands = [
20972162
"-supportedcommand ConvertFromString",
20982163
"-supportedcommand TobiIsObito",
2164+
"-SupportedCommand TobiIsObito",
20992165
]
21002166
self._test_expected(DataCommandsAllowed, commands)
21012167

@@ -2104,7 +2170,7 @@ def test_data_statement(self):
21042170
# TODO: pathological case
21052171
# "data -supportedcommand Format-XML { "
21062172
# "Format-XML -strings string1, string2, string3}",
2107-
2173+
"DATA {}",
21082174
"data -supportedcommand Format-XML { "
21092175
"Format-XML -strings string1}",
21102176

@@ -2130,6 +2196,8 @@ def test_data_statement(self):
21302196
def test_try_statement(self):
21312197
statement = [
21322198
'try { $value / 10 }\ncatch { Break }',
2199+
'Try { $value / 10 }\nCatch { Break }',
2200+
'TRY { $value / 10 }\ncatch { Break }',
21332201
'try { $value / 10 }\nfinally { $status=1 }',
21342202

21352203
'try\n{\n$value / 10\n}\ncatch\n{\nBreak\n}',

0 commit comments

Comments
 (0)