From 0c5dc3706416b068cdc7493dfc1df344e8a8a58a Mon Sep 17 00:00:00 2001 From: Marc Daehne Date: Fri, 2 Sep 2022 12:37:45 +0200 Subject: [PATCH] add cpp parsing test file --- parsing/cppParser_test.go | 127 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 parsing/cppParser_test.go diff --git a/parsing/cppParser_test.go b/parsing/cppParser_test.go new file mode 100644 index 0000000..b49fa64 --- /dev/null +++ b/parsing/cppParser_test.go @@ -0,0 +1,127 @@ +package parsing + +import ( + "bytes" + "testing" +) + +func TestParseCppSourceUnit(t *testing.T) { + testCases := []struct { + name string + fileContent string + expected []string + }{ + { + name: "simple class without imports", + fileContent: `package de.sandstorm.test; + + public class Main { + + };`, + expected: []string{"de", "sandstorm", "test", "Main"}, + }, + { + name: "simple class without imports with default visibility", + fileContent: `package de.sandstorm.test; + + class Main { + + };`, + expected: []string{"de", "sandstorm", "test", "Main"}, + }, + { + name: "simple class with internal dependencies", + fileContent: `package de.sandstorm.test; + + #include "path/dependency.h" // some comment + + public class Main { + + };`, + expected: [][]string{ + []string{"de", "sandstorm", "test", "Main"}, + []string{"path", "dependency.h"} + }, + }, + { + name: "simple class with external dependencies", + fileContent: `package de.sandstorm.test; + + #include // some comment + + public class Main { + + };`, + expected: [][]string{ + []string{"de", "sandstorm", "test", "Main"}, + []string{"dependency.h"} + }, + }, + { + name: "simple class with external dependencies", + fileContent: `package de.sandstorm.test; + + #include // links to a file which contains the actual dependencies + + public class Main { + + };`, + expected: [][]string{ + []string{"de", "sandstorm", "test", "Main"}, + []string{"dependency"} + }, + } + // TODO: test private static final class + } + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + file := bytes.NewBufferString(testCase.fileContent) + AssertEquals(t, + testCase.expected, + ParseJavaSourceUnit(file), + ) + }) + } +} + +func TestParseJavaImports(t *testing.T) { + testCases := []struct { + name string + fileContent string + expected [][]string + }{ + { + name: "simple class with mixed imports", + fileContent: ` + package de.sandstorm.test; + + #include ; + #include ; + #include "myQList.h"; + #include "path/myQList.h"; + + public class Main { + + } + `, + expected: [][]string{ + []string{"de", "sandstorm", "test", "Main"}, + []string{"QList"}, + []string{"path", "QList2.h"}, + []string{"myList.h"}, + []string{"path", "myList.h"} + }, + }, + // TODO: test with different parameters + } + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + file := bytes.NewBufferString(testCase.fileContent) + actual, _ := ParseJavaImports( + file) + AssertEquals(t, + testCase.expected, + actual) + }) + } +}