-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[c2cpg] Enable file content and content for method and type decl (#4195)
- Loading branch information
1 parent
339a0c7
commit b4ff8b3
Showing
5 changed files
with
132 additions
and
44 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
74 changes: 74 additions & 0 deletions
74
joern-cli/frontends/c2cpg/src/test/scala/io/joern/c2cpg/io/CodeDumperFromContentTest.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,74 @@ | ||
package io.joern.c2cpg.io | ||
|
||
import io.joern.c2cpg.testfixtures.CCodeToCpgSuite | ||
import io.joern.c2cpg.Config | ||
import io.shiftleft.semanticcpg.language.* | ||
|
||
class CodeDumperFromContentTest extends CCodeToCpgSuite { | ||
|
||
private implicit val finder: NodeExtensionFinder = DefaultNodeExtensionFinder | ||
|
||
"dumping code from content" should { | ||
val cpg = code(""" | ||
|// A comment | ||
|int my_func(int param1) | ||
|{ | ||
| int x = foo(param1); | ||
|} | ||
|""".stripMargin).withConfig(Config().withDisableFileContent(false)) | ||
|
||
"allow one to dump a method node's source code from `File.contents`" in { | ||
inside(cpg.method.nameExact("my_func").dumpRaw.l) { | ||
case content :: Nil => | ||
content.linesIterator.map(_.strip).l shouldBe List( | ||
"int my_func(int param1) /* <=== my_func */", | ||
"{", | ||
"int x = foo(param1);", | ||
"}" | ||
) | ||
case content => fail(s"Expected exactly 1 content dump, but got: $content") | ||
} | ||
} | ||
} | ||
|
||
"code from method content" should { | ||
val myFuncContent = | ||
"""int my_func(int param1) | ||
|{ | ||
| int x = foo(param1); | ||
|}""".stripMargin | ||
|
||
val cpg = code(s""" | ||
|// A comment | ||
|$myFuncContent; | ||
|""".stripMargin).withConfig(Config().withDisableFileContent(false)) | ||
|
||
"allow one to dump a method node's source code from `Method.content`" in { | ||
val List(content) = cpg.method.nameExact("my_func").content.l | ||
content shouldBe myFuncContent | ||
} | ||
} | ||
|
||
"code from typedecl content" should { | ||
val myClassContent = | ||
"""class Foo { | ||
| public: | ||
| int a; | ||
| string b; | ||
|}""".stripMargin | ||
|
||
val cpg = code( | ||
s""" | ||
|// A comment | ||
|$myClassContent; | ||
|""".stripMargin, | ||
"Foo.cpp" | ||
).withConfig(Config().withDisableFileContent(false)) | ||
|
||
"allow one to dump a method node's source code from `TypeDecl.content`" in { | ||
val List(content) = cpg.typeDecl.nameExact("Foo").content.l | ||
content shouldBe myClassContent | ||
} | ||
} | ||
|
||
} |
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