Skip to content

Commit

Permalink
[ruby] Added parser tests from official parser testS (#4872)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiDreyer authored Aug 23, 2024
1 parent 5f89dfe commit fc7d004
Show file tree
Hide file tree
Showing 25 changed files with 525 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class AstPrinter extends RubyParserBaseVisitor[String] {
val condition = visit(ctx.expressionOrCommand())
val body = visit(ctx.doClause())

s"${ctx.UNTIL.getText} $condition $body$ls${ctx.END.getText}"
s"${ctx.UNTIL.getText} $condition$ls$body$ls${ctx.END.getText}"
}

override def visitBeginEndExpression(ctx: RubyParser.BeginEndExpressionContext): String = {
Expand Down Expand Up @@ -1104,6 +1104,7 @@ class AstPrinter extends RubyParserBaseVisitor[String] {
}

override def visitRescueClause(ctx: RubyParser.RescueClauseContext): String = {
val outputSb = new StringBuilder(ctx.RESCUE().getText)
val exceptionClassList = Option(ctx.exceptionClassList).map(visit).getOrElse("")
val variables = Option(ctx.exceptionVariableAssignment).map(visit).getOrElse("")
val thenClause = visit(ctx.thenClause)
Expand All @@ -1112,7 +1113,14 @@ class AstPrinter extends RubyParserBaseVisitor[String] {
if Option(ctx.thenClause().THEN()).isDefined then s" ${ctx.thenClause().THEN().getText}"
else ""

s"${ctx.RESCUE().getText} $exceptionClassList => $variables $thenKeyword $thenClause".strip()
if exceptionClassList != "" then outputSb.append(s" $exceptionClassList")
if variables != "" then outputSb.append(s" => $variables")

outputSb.append(thenKeyword)

if thenClause != "" then outputSb.append(s"\n${thenClause}")

outputSb.toString()
}

override def visitEnsureClause(ctx: RubyParser.EnsureClauseContext): String = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import io.joern.rubysrc2cpg.testfixtures.RubyParserFixture
import org.scalatest.matchers.should.Matchers

class ArrayParserTests extends RubyParserFixture with Matchers {
"fixme" ignore {
test("[1, 2 => 3]", "[1,2=> 3]") // syntax error
}

"array structures" in {
test("[]")
test("%w[]")
Expand Down Expand Up @@ -48,4 +52,8 @@ class ArrayParserTests extends RubyParserFixture with Matchers {
test("%I{}")
test("%I(x#{0} x1)")
}

"array params" in {
test("[1 => 2]", "[1=> 2]")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,47 @@ import io.joern.rubysrc2cpg.testfixtures.RubyParserFixture
import org.scalatest.matchers.should.Matchers

class AssignmentParserTests extends RubyParserFixture with Matchers {
"fixme" ignore {
test("A.B ||= c 1") // Possible string issue - result is missing A.B on LHS
test("a[:b] ||= c 1, 2") // Possible string issue - result is missing a[:b] on LHS
test("A::b += 1") // Possible string issue - result is missing + in += operator
test("A::B *= c d") // Possible string issue - result is missing A::B *= on LHS
test("A::b *= c d") // Possible string issue - result is missing A::B *= on LHS
test("a.b ||= c 1") // Possible string issue - result is missing a.b ||= on LHS
test("a = b, *c, d") // Syntax error
test("*, a = b") // Syntax error
test("*, x, y, z = f") // Syntax error
}

"Single assignment" in {
test("x=1", "x = 1")
test("hash[:sym] = s[:sym]")
test("a = 1, 2, 3, 4")
test("a = b.c 1")
test("a ||= b")
test("a &&= b")
test("a += 1")
test("a /= 1")
test("a[[1, 2]] = 3", "a[[1,2]] = 3")
test("a[] += b")
test("@a = 42")
test("a&.b = 1", "a&.b= 1")
test("c = a&.b")
}

"Multiple assignment" in {
test("p, q = [foo(), bar()]")
test("a, b::c = d")
test("a, b.C = d")
test("::A, ::B = 1, 2")
test("[1,2,3,4][from..to] = [\"a\",\"b\",\"c\"]")
test("a, = b.c 1")
test("(a, b) = c.d")
test("a ||= b.c 2")
test("a, b, c, * = f")
test("a, b, c, *s = f")
test("*s, x, y, z = f")
test("a = b 1 rescue 2")
}

"Destructured Assignment" in {
Expand All @@ -23,4 +56,26 @@ class AssignmentParserTests extends RubyParserFixture with Matchers {
test("a, b, c = 1, 2, *list")
test("a, b, c = 1, *list")
}

"Class Constant Assign" in {
test("A::b = 1")
test("a.B = 1")
}

"Assignment with block" in {
test(
"""h[k]=begin
|42
|end
|""".stripMargin,
"""h[k] = begin
|42
|end""".stripMargin
)
}

"Assignment with rescue" in {
test("a = 1 rescue 2")
test("a = b(1) rescue 2")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ import io.joern.rubysrc2cpg.testfixtures.RubyParserFixture
import org.scalatest.matchers.should.Matchers

class BeginStatementParserTests extends RubyParserFixture with Matchers {
"BEGIN statement" in {
// TODO: Fix - valid for Ruby 2, but not 3
// test("BEGIN { 1 }")
// test("BEGIN {}")
// TODO: Syntax Errors
"BEGIN statement" ignore {
test("BEGIN { 1 }")
test("BEGIN {}")
}

// TODO: Syntax errors
"END statement" ignore {
test("END { 1 }")
test("END {}")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.joern.rubysrc2cpg.parser

import io.joern.rubysrc2cpg.testfixtures.RubyParserFixture
import org.scalatest.matchers.should.Matchers

class BitwiseOperatorParserTests extends RubyParserFixture with Matchers {
"Bitwise operators" in {
test("1 & 3")
test("2 & 4 & 3")
test("1 | 9")
test("1 ^ 20")
test("1 >> 2")
test("1 << 2")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.joern.rubysrc2cpg.parser

import io.joern.rubysrc2cpg.testfixtures.RubyParserFixture
import org.scalatest.matchers.should.Matchers

class BlockParserTests extends RubyParserFixture with Matchers {
"Blocks" in {
test("""a = 42
|a""".stripMargin)

test(
"""a
|b # comment
|c
|""".stripMargin,
"""a
|b
|c""".stripMargin
)

test(
"""a
|b # comment
|# another comment
|c
|""".stripMargin,
"""a
|b
|c""".stripMargin
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,12 @@ class BooleanParserTests extends RubyParserFixture with Matchers {
test("1 && !2")
test("1 || 2 || 3")
test("1 && 2 && 3")
test("1 != 2")
test("1 == [:b, :c]", "1 == [:b,:c]")
test("! foo 1", "!foo 1")
}

"Spaceship" in {
test("a <=> b")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ import io.joern.rubysrc2cpg.testfixtures.RubyParserFixture
import org.scalatest.matchers.should.Matchers

class CaseConditionParserTests extends RubyParserFixture with Matchers {
"fixme" ignore {
// Splat arg missing from output
test("""case a
|when *b then
|end
|""".stripMargin)
}

"A case expression" in {
test(
"""case something
Expand Down Expand Up @@ -52,5 +60,6 @@ class CaseConditionParserTests extends RubyParserFixture with Matchers {
|3
|end""".stripMargin
)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,41 @@ class ClassDefinitionParserTests extends RubyParserFixture with Matchers {
|end
|end""".stripMargin
)
test(
"""class Foo
| def self.show
| end
|end
|""".stripMargin,
"""class Foo
|def <body>
|
|end
|def self.show
| end
|end""".stripMargin
)
}

"class definitions with comments" in {
test(
"""#blah 1
|#blah 2
|class X
|#blah 3
|def blah
|#blah4
|end
|end
|""".stripMargin,
"""class X
|def <body>
|
|end
|def blah
|#blah4
|end
|end""".stripMargin
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ class ControlStructureParserTests extends RubyParserFixture with Matchers {
)
}

"until" in {
test("""until not var.nil?
|'foo'
|end""".stripMargin)
}

"if" in {
test(
"""if __LINE__ > 1 then
Expand Down Expand Up @@ -55,6 +61,27 @@ class ControlStructureParserTests extends RubyParserFixture with Matchers {
|456
|end""".stripMargin
)

test(
"if a..b then end",
"""if a..b
|end""".stripMargin
)

test(
"if :x; end",
"""if :x
|end""".stripMargin
)

test(
"if not var.nil? then 'foo' else 'bar'\nend",
"""if not var.nil?
|'foo'
|else
|'bar'
|end""".stripMargin
)
}

"for loops" in {
Expand Down
Loading

0 comments on commit fc7d004

Please sign in to comment.