-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RubyParser: Allow splatting elements in hash literals (#3221)
- Loading branch information
1 parent
5a7e75d
commit f5757f9
Showing
3 changed files
with
150 additions
and
3 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
135 changes: 135 additions & 0 deletions
135
...i/frontends/rubysrc2cpg/src/test/scala/io/joern/rubysrc2cpg/parser/HashLiteralTests.scala
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,135 @@ | ||
package io.joern.rubysrc2cpg.parser | ||
|
||
class HashLiteralTests extends RubyParserAbstractTest { | ||
|
||
"A standalone hash literal" should { | ||
|
||
"be parsed as a primary expression" when { | ||
|
||
"it contains no elements" in { | ||
val code = "{ }" | ||
printAst(_.primary(), code) shouldEqual | ||
"""HashConstructorPrimary | ||
| HashConstructor | ||
| { | ||
| WsOrNl | ||
| }""".stripMargin | ||
} | ||
|
||
"it contains a single splatting identifier" in { | ||
val code = "{ **x }" | ||
printAst(_.primary(), code) shouldEqual | ||
"""HashConstructorPrimary | ||
| HashConstructor | ||
| { | ||
| WsOrNl | ||
| HashConstructorElements | ||
| HashConstructorElement | ||
| ** | ||
| PrimaryExpression | ||
| VariableReferencePrimary | ||
| VariableIdentifierVariableReference | ||
| VariableIdentifier | ||
| x | ||
| }""".stripMargin | ||
} | ||
|
||
"it contains two consecutive splatting identifiers" in { | ||
val code = "{**x, **y}" | ||
printAst(_.primary(), code) shouldEqual | ||
"""HashConstructorPrimary | ||
| HashConstructor | ||
| { | ||
| HashConstructorElements | ||
| HashConstructorElement | ||
| ** | ||
| PrimaryExpression | ||
| VariableReferencePrimary | ||
| VariableIdentifierVariableReference | ||
| VariableIdentifier | ||
| x | ||
| , | ||
| WsOrNl | ||
| HashConstructorElement | ||
| ** | ||
| PrimaryExpression | ||
| VariableReferencePrimary | ||
| VariableIdentifierVariableReference | ||
| VariableIdentifier | ||
| y | ||
| }""".stripMargin | ||
|
||
} | ||
|
||
"it contains an association between two splatting identifiers" in { | ||
val code = "{**x, y => 1, **z}" | ||
printAst(_.primary(), code) shouldEqual | ||
"""HashConstructorPrimary | ||
| HashConstructor | ||
| { | ||
| HashConstructorElements | ||
| HashConstructorElement | ||
| ** | ||
| PrimaryExpression | ||
| VariableReferencePrimary | ||
| VariableIdentifierVariableReference | ||
| VariableIdentifier | ||
| x | ||
| , | ||
| WsOrNl | ||
| HashConstructorElement | ||
| Association | ||
| PrimaryExpression | ||
| VariableReferencePrimary | ||
| VariableIdentifierVariableReference | ||
| VariableIdentifier | ||
| y | ||
| => | ||
| WsOrNl | ||
| PrimaryExpression | ||
| LiteralPrimary | ||
| NumericLiteralLiteral | ||
| NumericLiteral | ||
| UnsignedNumericLiteral | ||
| 1 | ||
| , | ||
| WsOrNl | ||
| HashConstructorElement | ||
| ** | ||
| PrimaryExpression | ||
| VariableReferencePrimary | ||
| VariableIdentifierVariableReference | ||
| VariableIdentifier | ||
| z | ||
| }""".stripMargin | ||
} | ||
|
||
"it contains a single splatting method invocation" in { | ||
val code = "{**group_by_type(some)}" | ||
printAst(_.primary(), code) shouldEqual | ||
"""HashConstructorPrimary | ||
| HashConstructor | ||
| { | ||
| HashConstructorElements | ||
| HashConstructorElement | ||
| ** | ||
| PrimaryExpression | ||
| InvocationWithParenthesesPrimary | ||
| MethodIdentifier | ||
| group_by_type | ||
| ArgsOnlyArgumentsWithParentheses | ||
| ( | ||
| Arguments | ||
| ExpressionArgument | ||
| PrimaryExpression | ||
| VariableReferencePrimary | ||
| VariableIdentifierVariableReference | ||
| VariableIdentifier | ||
| some | ||
| ) | ||
| }""".stripMargin | ||
} | ||
} | ||
} | ||
|
||
} |