Skip to content

Commit

Permalink
feat: <C#> add basic parameters support
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Feb 14, 2020
1 parent f8ea7c2 commit c97f5b3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,14 @@ class CSharpFullIdentListener(val fileName: String) : CSharpAstListener() {
"TerminalNodeImpl" -> {
val methodDeclaration = memberDeclaration.method_declaration()
val methodName = methodDeclaration.method_member_name()
val parameters =
this.buildFunctionParameters(methodDeclaration.formal_parameter_list())

val codeFunction = CodeFunction(
Package = codeDataStruct.Package,
Name = methodName.text,
Modifiers = buildModifiers(memberCtx)
Modifiers = buildFunctionModifiers(memberCtx),
Parameters = parameters
)

codeDataStruct.Functions += codeFunction
Expand All @@ -112,7 +115,26 @@ class CSharpFullIdentListener(val fileName: String) : CSharpAstListener() {
}
}

private fun buildModifiers(memberCtx: CSharpParser.Class_member_declarationContext): Array<String> {
private fun buildFunctionParameters(formalParameterList: CSharpParser.Formal_parameter_listContext?): Array<CodeProperty> {
var parameters : Array<CodeProperty> = arrayOf()
if (formalParameterList!!.fixed_parameters() != null) {
for (fixedParamCtx in formalParameterList.fixed_parameters().fixed_parameter()) {
val argDeclCtx = fixedParamCtx.arg_declaration()
if (argDeclCtx != null) {
val parameter = CodeProperty(
TypeType = argDeclCtx.type_().text,
TypeValue = argDeclCtx.identifier().text
)

parameters += parameter
}
}
}

return parameters
}

private fun buildFunctionModifiers(memberCtx: CSharpParser.Class_member_declarationContext): Array<String> {
var modifiers: Array<String> = arrayOf()
for (allMemberModifiercontext in memberCtx.all_member_modifiers().all_member_modifier()) {
modifiers += allMemberModifiercontext.text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.Assertions.*

internal class CSharpFullIdentListenerTest {
private val helloworld = """
using System;
namespace HelloWorldApp {
class Geeks {
static void Main(string[] args) {
Console.WriteLine("Hello World!");
Console.ReadKey();
}
}
}
"""

@Test
fun shouldIdentUsingSystem() {
val code = """
using System;
namespace HelloWorldApp {
class Geeks {
static void Main(string[] args) {
Console.WriteLine("Hello World!");
Console.ReadKey();
}
}
}
"""
val codeFile = CSharpAnalyser().analysis(code, "hello.cs")
val codeFile = CSharpAnalyser().analysis(helloworld, "hello.cs")
assertEquals(codeFile.Imports.size, 1)
assertEquals(codeFile.Imports[0].Source, "System")
}
Expand Down Expand Up @@ -119,23 +119,20 @@ namespace HelloWorldApp {

@Test
fun shouldIdentNameSpaceClassMethodSupport() {
val code = """
using System;
namespace HelloWorldApp {
class Geeks {
static void Main(string[] args) {
Console.WriteLine("Hello World!");
Console.ReadKey();
}
}
}
"""
val codeContainer = CSharpAnalyser().analysis(code, "hello.cs")
val codeContainer = CSharpAnalyser().analysis(helloworld, "hello.cs")
val codeDataStruct = codeContainer.Containers[0].DataStructures[0]
assertEquals(codeDataStruct.Functions.size, 1)
assertEquals(codeDataStruct.Functions[0].Name, "Main")
assertEquals(codeDataStruct.Functions[0].Modifiers.size, 1)
assertEquals(codeDataStruct.Functions[0].Modifiers[0], "static")
}

@Test
fun shouldIdentNameSpaceClassParametersSupport() {
val codeContainer = CSharpAnalyser().analysis(helloworld, "hello.cs")
val codeDataStruct = codeContainer.Containers[0].DataStructures[0]
assertEquals(codeDataStruct.Functions[0].Parameters.size, 1)
assertEquals(codeDataStruct.Functions[0].Parameters[0].TypeType, "string[]")
assertEquals(codeDataStruct.Functions[0].Parameters[0].TypeValue, "args")
}
}

0 comments on commit c97f5b3

Please sign in to comment.