From 45c2e6b37aaf02850b74df9f479b415ce550630c Mon Sep 17 00:00:00 2001 From: Federico Tomassetti Date: Wed, 14 Feb 2024 11:09:35 +0100 Subject: [PATCH] Add tests for EGL serialization and deserialization --- gradle.properties | 1 + starlasu-demo/build.gradle.kts | 10 ++ .../src/test/kotlin/EGLLionWebConversion.kt | 34 +++++ .../src/test/resources/egl/SQLBatch.egl | 123 ++++++++++++++++++ .../egl/rosetta-code-count-examples-1.egl | 76 +++++++++++ .../egl/rosetta-code-count-examples-2.egl | 37 ++++++ 6 files changed, 281 insertions(+) create mode 100644 starlasu-demo/src/test/kotlin/EGLLionWebConversion.kt create mode 100644 starlasu-demo/src/test/resources/egl/SQLBatch.egl create mode 100644 starlasu-demo/src/test/resources/egl/rosetta-code-count-examples-1.egl create mode 100644 starlasu-demo/src/test/resources/egl/rosetta-code-count-examples-2.egl diff --git a/gradle.properties b/gradle.properties index 64b486b..f15982b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,3 +6,4 @@ okhttpVersion=4.12.0 jvmVersion=1.8 rpgParserVersion=2.1.42 javaModuleVersion=0.9.15 +eglParserVersion=0.0.1 diff --git a/starlasu-demo/build.gradle.kts b/starlasu-demo/build.gradle.kts index bca2984..e2251e1 100644 --- a/starlasu-demo/build.gradle.kts +++ b/starlasu-demo/build.gradle.kts @@ -11,6 +11,7 @@ val lionwebVersion = extra["lionwebVersion"] val kolasuVersion = extra["kolasuVersion"] val rpgParserVersion = extra["rpgParserVersion"] val javaModuleVersion = extra["javaModuleVersion"] +val eglParserVersion = extra["eglParserVersion"] val githubUser = ( project.findProperty("starlasu.github.user") @@ -36,6 +37,14 @@ repositories { password = githubToken } } + maven { + name = project.name + url = uri("https://maven.pkg.github.com/Strumenta/kolasu-EGL-langmodule") + credentials { + username = githubUser + password = githubToken + } + } } dependencies { @@ -46,6 +55,7 @@ dependencies { implementation("com.strumenta.langmodules.kolasu-java-langmodule:ast:$javaModuleVersion") testImplementation(kotlin("test-junit5")) testImplementation("com.strumenta:rpg-parser:$rpgParserVersion") + testImplementation("com.strumenta.langmodules.kolasu-EGL-langmodule:ast:$eglParserVersion") testImplementation("com.strumenta:rpg-parser-symbol-resolution:$rpgParserVersion") testImplementation("commons-io:commons-io:2.7") testImplementation("org.slf4j:slf4j-simple:1.7.30") diff --git a/starlasu-demo/src/test/kotlin/EGLLionWebConversion.kt b/starlasu-demo/src/test/kotlin/EGLLionWebConversion.kt new file mode 100644 index 0000000..438f53e --- /dev/null +++ b/starlasu-demo/src/test/kotlin/EGLLionWebConversion.kt @@ -0,0 +1,34 @@ +import com.strumenta.egl.ast.EglCompilationUnit +import com.strumenta.egl.ast.kLanguage +import com.strumenta.egl.parser.EGLKolasuParser +import com.strumenta.kolasu.parsing.ParsingResult +import org.apache.commons.io.input.BOMInputStream +import java.io.InputStream +import kotlin.test.Ignore +import kotlin.test.Test + +class EGLLionWebConversion : AbstractLionWebConversion(kLanguage) { + + override fun parse(inputStream: InputStream): ParsingResult { + val parser = EGLKolasuParser() + return parser.parse(inputStream) + } + + @Test + fun sqlBatch() { + val inputStream = this.javaClass.getResourceAsStream("/egl/SQLBatch.egl") ?: throw IllegalStateException() + checkSerializationAndDeserialization(inputStream) + } + + @Test + fun `rosetta-code-count-examples-1`() { + val inputStream = this.javaClass.getResourceAsStream("/egl/rosetta-code-count-examples-1.egl") ?: throw IllegalStateException() + checkSerializationAndDeserialization(inputStream) + } + + @Test + fun `rosetta-code-count-examples-2`() { + val inputStream = this.javaClass.getResourceAsStream("/egl/rosetta-code-count-examples-2.egl") ?: throw IllegalStateException() + checkSerializationAndDeserialization(inputStream) + } +} diff --git a/starlasu-demo/src/test/resources/egl/SQLBatch.egl b/starlasu-demo/src/test/resources/egl/SQLBatch.egl new file mode 100644 index 0000000..6923e26 --- /dev/null +++ b/starlasu-demo/src/test/resources/egl/SQLBatch.egl @@ -0,0 +1,123 @@ +package com.CompanyB.CustomerPackage; +program SQLBatch type basicProgram + + dept Department; + + function main() + try + clearTable(); + loadTable(); + getRow(); + updateRow(); + getAllRows(); + forceRecordNotFound(); + forceSQLException(); + onException (ex SQLException) + sqlFailure(); + onException (ex AnyException) + anyFailure( ex ); + end + end + + function getRow( ) + setVariation ( "Get row from department table" ); + getDept("D100"); + end + + function updateRow() + set dept empty; + setVariation ( "Update department table" ); + dept.id = "T100"; + get dept forUpdate; + if ( dept is noRecordFound ) + sqlFailure(); + else + dept.description = "Test Engineers"; + replace dept; + commit(); + end + getDept( "T100" ); + end + + function getAllRows() + setVariation ( "Get all departments" ); + dept.id = " "; + open deptSet for dept; + foreach ( dept ) + showDept(); + end + end + + function clearTable() + setVariation ( "Clear all rows from department table" ); + dept.id = " "; + execute delete + #sql{ + delete from DEPARTMENT_TABLE + where + id >= :dept.id + } for dept; + commit(); + end + + function forceRecordNotFound() + setVariation( "Try to get a department that is not in the table"); + getDept("B100"); + end + + function forceSQLException() + setVariation("Force an sql exception by dropping a non-existent table"); + prepare dropStmt from "DROP TABLE NOT_A_TABLE"; + execute dropStmt; + end + + function loadTable() + setVariation ( "Load department table" ); + addDept("A100","Architects","AM0001"); + addDept("D100","Developers","DM0001"); + addDept("T100","Testers","TM0001"); + addDept("M100","Managers","MM0001"); + commit(); + end + + function addDept( deptNo string, desc String, manager string ) + dept.id = deptNo; + dept.description = desc; + dept.manager = manager; + add dept; + end + + function getDept( id string ) + set dept empty; + dept.id = id; + get dept; + showDept(); + end + + function showDept() + if ( dept is noRecordFound ) + writeStdOut ( "Department " + dept.id + " not found." ); + else + writeStdOut( "Department " + dept.id + + " description: " + dept.description ); + end + end + + function setVariation ( desc string ) + writeStdOut ( "Variation = " + desc ); + end + + function sqlFailure() + writeStdOut ( "SQL Exception, SQL code = + " + sysVar.sqlData.sqlCode + + ", SQL state: " + sysVar.sqlData.sqlstate ); + rollback(); + end + + function anyFailure( ex AnyException ) + writeStdOut ( "Unexpected Exception, Error code = " + ex.messageID ); + writeStdOut ( ex.message ); + rollback(); + end + +end \ No newline at end of file diff --git a/starlasu-demo/src/test/resources/egl/rosetta-code-count-examples-1.egl b/starlasu-demo/src/test/resources/egl/rosetta-code-count-examples-1.egl new file mode 100644 index 0000000..3547076 --- /dev/null +++ b/starlasu-demo/src/test/resources/egl/rosetta-code-count-examples-1.egl @@ -0,0 +1,76 @@ +package com.eglexamples.client; + +import org.eclipse.edt.rui.widgets.*; + +handler RosettaCodeHandler type RUIhandler{initialUI =[ui], title = "Rosetta Code Tasks and Counts"} + + ui GridLayout{columns = 3, rows = 4, cellPadding = 4, children = [ b1, dg1, l1, l2, l3, l4 ]}; + + b1 Button{ layoutData = new GridLayoutData{ row = 1, column = 1 }, text = "Go!", onClick = b1_onClick }; + l1 TextLabel{ layoutData = new GridLayoutData{ row = 1, column = 2 }, text = "Total Tasks:" }; + l2 TextLabel{ layoutData = new GridLayoutData{ row = 1, column = 3 }, text = "0" }; + + l3 TextLabel{ layoutData = new GridLayoutData{ row = 2, column = 2 }, text = "Total Implementations:" }; + l4 TextLabel{ layoutData = new GridLayoutData{ row = 2, column = 3 }, text = "0" }; + + dg1 DataGrid{ layoutData = new GridLayoutData{ row = 3, column = 1, horizontalSpan = 3 }, + pageSize = 10, showScrollbar = true, + columns = [ new DataGridColumn{name = "title", displayName = "Task", width=220}, + new DataGridColumn{name = "count", displayName = "Count", width=100} ] }; + + cmcontinue string?; + title string?; + allTasks Task[]; + + restBindingTasks IHttp? = new HttpRest{ + restType = eglx.rest.ServiceType.TrueRest, + request.uri = "http://rosettacode.org/mw/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=1&format=json"}; + + restBindingPageDetail IHttp? = new HttpRest{ + restType = eglx.rest.ServiceType.TrueRest, + request.uri = "http://rosettacode.org/mw/index.php"}; + + function b1_onClick(event Event in) + call ProxyFunctions.listTasks("") using restBindingTasks + returning to listTasksCallBack onException exceptionHandler; + end + + function listTasksCallBack(retResult RosettaCodeJSON in) + title = retResult.query.categorymembers[1].title; + cmcontinue = retResult.queryContinue.categorymembers.cmcontinue; + + call ProxyFunctions.fetchPageDetail(title) using restBindingPageDetail + returning to pageDetailCallBack onException exceptionHandler; + end + + function pageDetailCallBack(pageResults string in) + count int = countSubstring("=={{header", pageResults); + allTasks.appendElement(new Task { title = title, count = count }); + l2.text = l2.text as int + 1; + l4.text = l4.text as int + count; + + if(cmcontinue != null) + call ProxyFunctions.listTasks(cmcontinue) using restBindingTasks + returning to listTasksCallBack onException exceptionHandler; + else + dg1.data = allTasks as any[]; + end + end + + function countSubstring(substr string in, str string in) returns(int) + if(str.length() > 0 and substr.length() > 0) + return (str.length() - str.replaceStr(subStr, "").length()) / subStr.length(); + else + return 0; + end + end + + function exceptionHandler(exp AnyException in) + end + +end + +record Task + title string; + count int; +end diff --git a/starlasu-demo/src/test/resources/egl/rosetta-code-count-examples-2.egl b/starlasu-demo/src/test/resources/egl/rosetta-code-count-examples-2.egl new file mode 100644 index 0000000..c672f8c --- /dev/null +++ b/starlasu-demo/src/test/resources/egl/rosetta-code-count-examples-2.egl @@ -0,0 +1,37 @@ +package com.eglexamples.client; + +library ProxyFunctions + + function listTasks(continueLocation String in) returns (RosettaCodeJSON) { + @Rest{method = _GET, uriTemplate = "&cmcontinue={continueLocation}", + requestFormat = None, responseFormat = JSON} + } + end + + function fetchPageDetail(title String in) returns (String) { + @Rest{method = _GET, uriTemplate = "?title={title}&action=raw", + requestFormat = None, responseFormat = None} + } + end + +end + +record RosettaCodeJSON + query Query; + queryContinue QueryContinue{JSONName = "query-continue"}; +end + +record Query + categorymembers Categorymembers[]; +end + +record Categorymembers + cmcontinue string?; + pageid int?; + ns int?; + title string?; +end + +record QueryContinue + categorymembers Categorymembers; +end