-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[c#] Classes, Methods, and Method Decorators (#3962)
* Generating type declarations with modifiers * Generating method nodes with modifiers, a block, parameters, and a method return Resolves #3854 Resolves #3855 Resolves #3951
- Loading branch information
1 parent
89b1df1
commit 9d79faa
Showing
6 changed files
with
267 additions
and
35 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
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
41 changes: 41 additions & 0 deletions
41
...ntends/csharpsrc2cpg/src/test/scala/io/joern/csharpsrc2cpg/querying/ast/MethodTests.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,41 @@ | ||
package io.joern.csharpsrc2cpg.querying.ast | ||
|
||
import io.joern.csharpsrc2cpg.testfixtures.CSharpCode2CpgFixture | ||
import io.shiftleft.codepropertygraph.generated.ModifierTypes | ||
import io.shiftleft.semanticcpg.language.* | ||
|
||
class MethodTests extends CSharpCode2CpgFixture { | ||
|
||
"a basic class declaration with method" should { | ||
val cpg = code(basicBoilerplate(), "Program.cs") | ||
|
||
"generate a method node with type decl parent" in { | ||
val x = cpg.method.nameExact("Main").head | ||
x.fullName should startWith("HelloWorld.Program.Main:void") | ||
// TODO: Extract types from parameters and these should work | ||
// x.fullName shouldBe "HelloWorld.Program.Main:void(string[])" | ||
// x.signature shouldBe "void(string[])" | ||
x.filename shouldBe "Program.cs" | ||
|
||
x.typeDecl match | ||
case Some(typeDecl) => typeDecl.name shouldBe "Program" | ||
case None => fail("No TYPE_DECL parent found!") | ||
} | ||
|
||
"generate a method node with the correct modifiers" in { | ||
val List(x, y) = cpg.method.nameExact("Main").modifier.l: @unchecked | ||
x.modifierType shouldBe ModifierTypes.INTERNAL | ||
y.modifierType shouldBe ModifierTypes.STATIC | ||
} | ||
|
||
"generate a method node with a parameter" in { | ||
val List(x) = cpg.method.nameExact("Main").parameter.l: @unchecked | ||
x.name shouldBe "args" | ||
} | ||
|
||
"generate a method node with a block" in { | ||
cpg.method.nameExact("Main").body.l should not be empty | ||
} | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
...ends/csharpsrc2cpg/src/test/scala/io/joern/csharpsrc2cpg/querying/ast/TypeDeclTests.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,50 @@ | ||
package io.joern.csharpsrc2cpg.querying.ast | ||
|
||
import io.joern.csharpsrc2cpg.testfixtures.CSharpCode2CpgFixture | ||
import io.shiftleft.semanticcpg.language.* | ||
import io.shiftleft.codepropertygraph.generated.ModifierTypes | ||
|
||
class TypeDeclTests extends CSharpCode2CpgFixture { | ||
|
||
"a basic class declaration" should { | ||
val cpg = code("public class Container { }", "Container.cs") | ||
|
||
"generate a type declaration with the correct properties" in { | ||
val x = cpg.typeDecl.nameExact("Container").head | ||
x.fullName shouldBe "Container" | ||
x.filename shouldBe "Container.cs" | ||
x.aliasTypeFullName shouldBe None | ||
x.inheritsFromTypeFullName shouldBe Seq.empty | ||
} | ||
|
||
"generate a type declaration with the correct modifiers" in { | ||
val x = cpg.typeDecl.nameExact("Container").head | ||
x.modifier.modifierType.head shouldBe ModifierTypes.PUBLIC | ||
} | ||
} | ||
|
||
"a basic class declaration within a namespace" should { | ||
val cpg = code( | ||
"""namespace SampleNamespace | ||
|{ | ||
| private class SampleClass { } | ||
|} | ||
|""".stripMargin, | ||
"SampleClass.cs" | ||
) | ||
|
||
"generate a type declaration with the correct properties" in { | ||
val x = cpg.typeDecl.nameExact("SampleClass").head | ||
x.fullName shouldBe "SampleNamespace.SampleClass" | ||
x.filename shouldBe "SampleClass.cs" | ||
x.aliasTypeFullName shouldBe None | ||
x.inheritsFromTypeFullName shouldBe Seq.empty | ||
} | ||
|
||
"generate a type declaration with the correct modifiers" in { | ||
val x = cpg.typeDecl.nameExact("SampleClass").head | ||
x.modifier.modifierType.head shouldBe ModifierTypes.PRIVATE | ||
} | ||
} | ||
|
||
} |