Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

Commit

Permalink
Add tests for type expressions and classes (#4)
Browse files Browse the repository at this point in the history
* Fix bareword string, add type expression tests

* Add class/enum tests
  • Loading branch information
rjmholt committed Aug 2, 2019
1 parent 5f76a32 commit 281fc95
Show file tree
Hide file tree
Showing 3 changed files with 315 additions and 1 deletion.
127 changes: 127 additions & 0 deletions corpus/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -299,3 +299,130 @@ Multi-key hashtable
(simple_variable)))
(variable
(simple_variable))))))

===
Simple type expression #1
===

[string]

---

(program
(pipeline_statement
(type_expr)))

===
Simple type expression #2
===

[System.Collections.ArrayList]

---

(program
(pipeline_statement
(type_expr)))

===
Array type expression #1
===

[int[]]

---

(program
(pipeline_statement
(type_expr)))

===
Array type expression #1
===

[System.DateTime[]]

---

(program
(pipeline_statement
(type_expr)))

===
Generic type expression #1
===

[MyType[psobject]]

---

(program
(pipeline_statement
(type_expr)))

===
Generic type expression #2
===

[System.Collections.Generic.Dictionary[string, System.IO.FileInfo]]

---

(program
(pipeline_statement
(type_expr)))

===
Complex type expression
===

[System.Collections.Generic.Dictionary[int[], System.Collections.Generic.List[string[]]]]

---

(program
(pipeline_statement
(type_expr)))

===
Type-attributed variable
===

[string]$str

---

(program
(pipeline_statement
(type_expr)
(variable
(simple_variable))))

===
Non-type attributed variable
===

[MyAttribute()]$var

---

(program
(pipeline_statement
(non_type_attribute)
(variable
(simple_variable))))

===
Double attributed variable
===

[MyAttribute()][string]$var

---

(program
(pipeline_statement
(non_type_attribute)
(type_expr)
(variable
(simple_variable))))
185 changes: 185 additions & 0 deletions corpus/type_definitions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
===
Simple enum
===

enum MyEnum
{
One
}

---

(program
(enum_definition
(bareword_string)))

===
More complex enum
===

enum Enum2
{
Part1; Two

Three



Four
}

---

(program
(enum_definition
(bareword_string)
(bareword_string)
(bareword_string)
(bareword_string)))

===
Basic class
===

class MyClass {}

---

(program
(class_definition))

===
Class with property
===

class Class2
{
[string]$Name
}

---

(program
(class_definition
(class_property
(type_expr)
(simple_variable))))

===
Class with method
===

class Class2
{
[void]SayHi()
{
Write-Host "Hi"
}
}

---

(program
(class_definition
(class_method
(type_expr)
(bareword_string)
(class_method_body
(pipeline_statement
(command_expression
(bareword_string)
(command_argument
(double_quote_string))))))))

===
Class with parametered method
===

class Class2
{
[void]SayHi($Greeting, $Title)
{
Write-Host "$Greeting, $Title"
}
}

---

(program
(class_definition
(class_method
(type_expr)
(bareword_string)
(class_parameter
(simple_variable))
(class_parameter
(simple_variable))
(class_method_body
(pipeline_statement
(command_expression
(bareword_string)
(command_argument
(double_quote_string
(variable
(simple_variable))
(variable
(simple_variable))))))))))


===
Complex class
===

class Class2
{
[string]$Word

[void]AnotherMethod()
{
Write-Host "Nothing"
}

[int]$Number

[void]SayHi($Greeting, $Title)
{
Write-Host "$Greeting, $Title"
}
}

---

(program
(class_definition
(class_property
(type_expr)
(simple_variable))
(class_method
(type_expr)
(bareword_string)
(class_method_body
(pipeline_statement
(command_expression
(bareword_string)
(command_argument
(double_quote_string))))))
(class_property
(type_expr)
(simple_variable))
(class_method
(type_expr)
(bareword_string)
(class_parameter
(simple_variable))
(class_parameter
(simple_variable))
(class_method_body
(pipeline_statement
(command_expression
(bareword_string)
(command_argument
(double_quote_string
(variable
(simple_variable))
(variable
(simple_variable))))))))))
4 changes: 3 additions & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ module.exports = grammar({

class_definition: $ => seq(
caseInsensitive('class'),
repeat($._newline),
/[a-zA-Z_][a-zA-Z0-9_]+/,
repeat($._newline),
'{',
repeat(
seq(
Expand Down Expand Up @@ -423,7 +425,7 @@ module.exports = grammar({
)
),

bareword_string: $ => /[^0-9'"$^&|()@\-%{}\s][^'"$^&|()@!%{}\s]*/,
bareword_string: $ => /[^0-9'"$^&|;()@\-%{}\[\]\s][^'"$^&|;()@!%{}\s]*/,

comment: $ => token(prec(PREC.COMMENT,
choice(
Expand Down

0 comments on commit 281fc95

Please sign in to comment.