Skip to content

Commit

Permalink
Separate idl reader code from parser and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xpenatan committed Jul 2, 2023
1 parent 03d6db1 commit 347daae
Show file tree
Hide file tree
Showing 18 changed files with 350 additions and 40 deletions.
80 changes: 80 additions & 0 deletions IDLReader/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
apply plugin: "java"
apply plugin: 'signing'

def module_name = "IDLReader"

dependencies {
testImplementation "junit:junit:$project.jUnitVersion"
}

//task fromClasses(type: Jar) {
// from(sourceSets.main.output) {
// }
//}
//
//task javadocJar(type: Jar) {
// archiveClassifier.set("javadoc")
// from javadoc
//}
//
//task sourcesJar(type: Jar) {
// archiveClassifier.set("sources")
// from sourceSets.main.allSource
//}
//
//javadoc {
// options.encoding = 'UTF-8'
// options.addStringOption('Xdoclint:none', '-quiet')
//}
//
//publishing {
// publications {
// maven(MavenPublication) {
// version project.jParserVersion + project.jParserType
// project.version = project.jParserVersion + project.jParserType
// artifactId module_name
// artifact fromClasses
// artifact javadocJar
// artifact sourcesJar
//
// pom {
// name = "jParser"
// description = "Java code parser"
// url = "http://github.com/xpenatan/jParser"
// developers {
// developer {
// id = "Xpe"
// name = "Natan"
// }
// }
// scm {
// connection = "scm:git:git://github.com/xpenatan/jParser.git"
// developerConnection = "scm:git:ssh://github.com/xpenatan/jParser.git"
// url = "http://github.com/xpenatan/jParser/tree/master"
// }
// licenses {
// license {
// name = "The Apache License, Version 2.0"
// url = "https://www.apache.org/licenses/LICENSE-2.0.txt"
// }
// }
// withXml {
// def dependenciesNode = asNode().appendNode('dependencies')
// configurations.implementation.allDependencies.each {
// def dependencyNode = dependenciesNode.appendNode('dependency')
// dependencyNode.appendNode('groupId', it.group)
// dependencyNode.appendNode('artifactId', it.name)
// dependencyNode.appendNode('version', it.version)
// }
// }
// }
// }
// }
//}
//
//signing {
// def signingKey = System.getenv("signingKey")
// def signingPassword = System.getenv("signingPassword")
// useInMemoryPgpKeys(signingKey, signingPassword)
// sign publishing.publications.maven
//}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.xpenatan.jparser.core.idl;
package com.github.xpenatan.jparser.idl;

/**
* @author xpenatan
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.xpenatan.jparser.core.idl;
package com.github.xpenatan.jparser.idl;

import java.util.ArrayList;

Expand All @@ -9,8 +9,8 @@ public class IDLClass {
public final IDLFile idlFile;

public String name;
public String prefixName = "";
public String jsImplementation;

public IDLClassHeader classHeader;

public final ArrayList<String> classLines = new ArrayList<>();
public final ArrayList<IDLConstructor> constructors = new ArrayList<>();
Expand All @@ -23,9 +23,8 @@ public IDLClass(IDLFile idlFile) {

public void initClass(ArrayList<String> lines) {
classLines.addAll(lines);
setupHeader();
setInterfaceName();
setPrefixName();
setJsImplementation();
setAttributesAndMethods();
}

Expand Down Expand Up @@ -70,20 +69,15 @@ private void setInterfaceName() {
}
}

private void setJsImplementation() {
String prefix = "[JSImplementation=";
String line = searchLine(prefix, true, false);
if(line != null) {
jsImplementation = line.replace(prefix, "").replace("]", "").replace("\"", "");
}
}

private void setPrefixName() {
String prefix = "[Prefix =";
String line = searchLine(prefix, true, false);
if(line != null) {
prefixName = line.replace(prefix, "").replace("]", "").replace("\"", "");
private void setupHeader() {
String line = "";
if(classLines.size() > 0) {
String headerLine = classLines.get(0);
if(IDLClassHeader.isLineHeader(headerLine)) {
line = headerLine;
}
}
classHeader = new IDLClassHeader(line, this);
}

private String searchLine(String text, boolean startsWith, boolean endsWith) {
Expand All @@ -110,6 +104,6 @@ else if(endsWith) {
}

public String getName() {
return prefixName + name;
return classHeader.prefixName + name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.github.xpenatan.jparser.idl;

import java.util.ArrayList;

/**
* @author xpenatan
*/
public class IDLClassHeader {
public final IDLClass idlClass;

public String headerLine;

public String prefixName = "";
public String jsImplementation;
public boolean isNoDelete;

public ArrayList<String> options = new ArrayList<>();

public IDLClassHeader(String headerLine, IDLClass idlClass) {
this.headerLine = headerLine;
this.idlClass = idlClass;

if(!headerLine.isEmpty()) {
if(headerLine.startsWith("[") && headerLine.endsWith("]")) {
headerLine = headerLine.replace("[", "").replace("]", "");
if(headerLine.contains(",")) {
String[] commaSplit = headerLine.split(",");
for(String option : commaSplit) {
options.add(option.trim());
}
}
else {
options.add(headerLine);
}

setupPrefixName();
setupJSImplementation();
setupNoDelete();
}
else {
throw new RuntimeException("Wrong header: " + headerLine);
}
}
}

public void setupPrefixName() {
for(String option : options) {
if(option.startsWith("Prefix")) {
String[] split = option.split("=");
prefixName = split[1].replace("\"", "").trim();
}
}
}

private void setupJSImplementation() {
for(String option : options) {
if(option.startsWith("JSImplementation")) {
String[] split = option.split("=");
jsImplementation = split[1].replace("\"", "").trim();
}
}
}

private void setupNoDelete() {
for(String option : options) {
if(option.equals("NoDelete")) {
isNoDelete = true;
}
}
}

public static boolean isLineHeader(String line) {
return line.startsWith("[Prefix") || line.startsWith("[JSImplementation") || line.startsWith("[NoDelete");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.xpenatan.jparser.core.idl;
package com.github.xpenatan.jparser.idl;

import java.util.ArrayList;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.xpenatan.jparser.core.idl;
package com.github.xpenatan.jparser.idl;

import java.util.ArrayList;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.xpenatan.jparser.core.idl;
package com.github.xpenatan.jparser.idl;

import java.util.ArrayList;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.xpenatan.jparser.core.idl;
package com.github.xpenatan.jparser.idl;

import java.util.ArrayList;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.xpenatan.jparser.core.idl;
package com.github.xpenatan.jparser.idl;

import java.io.BufferedReader;
import java.io.File;
Expand All @@ -8,8 +8,29 @@
/**
* @author xpenatan
*/
public class IDLParser {
public class IDLReader {

public final ArrayList<IDLFile> fileArray = new ArrayList<>();

public IDLClass getClass(String name) {
for(int i = 0; i < fileArray.size(); i++) {
IDLFile idlFile = fileArray.get(i);
IDLClass idlClass = idlFile.getClass(name);
if(idlClass != null) {
return idlClass;
}
}
return null;
}

public static IDLReader readIDL(String path) {
IDLReader reader = new IDLReader();
IDLFile idlFile = parseFile(path);
reader.fileArray.add(idlFile);
return reader;
}

@Deprecated
public static IDLFile parseFile(String path) {
path = path.replace("\\", File.separator);
IDLFile idlFile = new IDLFile();
Expand Down Expand Up @@ -50,7 +71,7 @@ private static void parseFile(IDLFile idlFile, ArrayList<String> lines, ArrayLis
boolean justAdded = false;

if(!foundStartClass) {
if(line.startsWith("interface ") || line.startsWith("[Prefix") || line.startsWith("[JSImplementation") || line.startsWith("[NoDelete")) {
if(line.startsWith("interface ") || IDLClassHeader.isLineHeader(line)) {
foundStartClass = true;
classLines.clear();
justAdded = true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.github.xpenatan.jparser.idl;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

public class IDLReaderTest {


@BeforeClass
public static void setUp() throws Exception {
}

@Test
public void test_NoDeleteClassTest_not_null() {
IDLReader idlReader = IDLReader.readIDL("src\\test\\resources\\idl\\Test.idl");
IDLClass idlClass = idlReader.getClass("NoDeleteClassTest");
Assert.assertNotNull(idlClass);
}

@Test
public void test_PrefixClassTest_not_null() {
IDLReader idlReader = IDLReader.readIDL("src\\test\\resources\\idl\\Test.idl");
IDLClass idlClass = idlReader.getClass("PrefixClassTest");
Assert.assertNotNull(idlClass);
}

@Test
public void test_PrefixClassTest_should_contains_prefix() {
IDLReader idlReader = IDLReader.readIDL("src\\test\\resources\\idl\\Test.idl");
IDLClass idlClass = idlReader.getClass("PrefixClassTest");
Assert.assertEquals("MyPrefix::", idlClass.classHeader.prefixName);
}

@Test
public void test_PrefixNoDeleteClassTest_not_null() {
IDLReader idlReader = IDLReader.readIDL("src\\test\\resources\\idl\\Test.idl");
IDLClass idlClass = idlReader.getClass("PrefixNoDeleteClassTest");
Assert.assertNotNull(idlClass);
}

@Test
public void test_PrefixNoDeleteClassTest_contains_prefix_and_no_delete() {
IDLReader idlReader = IDLReader.readIDL("src\\test\\resources\\idl\\Test.idl");
IDLClass idlClass = idlReader.getClass("PrefixNoDeleteClassTest");
Assert.assertEquals("MyPrefix::", idlClass.classHeader.prefixName);
Assert.assertEquals(true, idlClass.classHeader.isNoDelete);
}

@Test
public void test_NoDeletePrefixClassTest_not_null() {
IDLReader idlReader = IDLReader.readIDL("src\\test\\resources\\idl\\Test.idl");
IDLClass idlClass = idlReader.getClass("NoDeletePrefixClassTest");
Assert.assertNotNull(idlClass);
}

@Test
public void test_NoDeletePrefixClassTest_contains_prefix_and_no_delete() {
IDLReader idlReader = IDLReader.readIDL("src\\test\\resources\\idl\\Test.idl");
IDLClass idlClass = idlReader.getClass("NoDeletePrefixClassTest");
Assert.assertEquals("MyPrefix::", idlClass.classHeader.prefixName);
Assert.assertEquals(true, idlClass.classHeader.isNoDelete);
}

@Test
public void test_ConcreteJSImplementationClassTest_not_null() {
IDLReader idlReader = IDLReader.readIDL("src\\test\\resources\\idl\\Test.idl");
IDLClass idlClass = idlReader.getClass("ConcreteJSImplementationClassTest");
Assert.assertNotNull(idlClass);
}

@Test
public void test_ConcreteJSImplementationClassTest_contains_implementation() {
IDLReader idlReader = IDLReader.readIDL("src\\test\\resources\\idl\\Test.idl");
IDLClass idlClass = idlReader.getClass("ConcreteJSImplementationClassTest");
Assert.assertEquals("NoDeletePrefixClassTest", idlClass.classHeader.jsImplementation);
}

@Test
public void test_NormalClassTest_not_null() {
IDLReader idlReader = IDLReader.readIDL("src\\test\\resources\\idl\\Test.idl");
IDLClass idlClass = idlReader.getClass("NormalClassTest");
Assert.assertNotNull(idlClass);
}

@Test
public void test_ExtendClassTest_not_null() {
IDLReader idlReader = IDLReader.readIDL("src\\test\\resources\\idl\\Test.idl");
IDLClass idlClass = idlReader.getClass("ExtendClassTest");
Assert.assertNotNull(idlClass);
}


}

Loading

0 comments on commit 347daae

Please sign in to comment.