From d99a53bbe031a1902921d70bfa687058098aecfb Mon Sep 17 00:00:00 2001 From: peefy Date: Tue, 13 Aug 2024 13:04:30 +0800 Subject: [PATCH] feat: add all suites and examples for kotlin API Signed-off-by: peefy --- README.md | 30 + java/README.md | 5 +- .../test/java/com/kcl/ListOptionsTest.java | 2 +- ...leTest.java => LoadSettingsFilesTest.java} | 2 +- java/src/test/java/com/kcl/TestingTest.java | 2 +- kotlin/README.md | 618 +++++++++++++++++- kotlin/src/test/kotlin/com/kcl/FormatTest.kt | 38 ++ .../test/kotlin/com/kcl/GetSchemaTypeTest.kt | 23 + .../src/test/kotlin/com/kcl/GetVersionTest.kt | 17 + kotlin/src/test/kotlin/com/kcl/LintTest.kt | 21 + .../test/kotlin/com/kcl/ListOptionsTest.kt | 22 + .../test/kotlin/com/kcl/ListVariablesTest.kt | 20 + .../test/kotlin/com/kcl/LoadPackageTest.kt | 22 + .../kotlin/com/kcl/LoadSettingsFilesTest.kt | 23 + .../test/kotlin/com/kcl/OverrideFileTest.kt | 41 ++ .../src/test/kotlin/com/kcl/ParseFileTest.kt | 20 + .../test/kotlin/com/kcl/ParseProgramTest.kt | 20 + kotlin/src/test/kotlin/com/kcl/RenameTest.kt | 41 ++ kotlin/src/test/kotlin/com/kcl/TestingTest.kt | 22 + .../kotlin/com/kcl/UpdateDependenciesTest.kt | 31 + kotlin/src/test_data/override_file/main.k | 5 - .../override_file/{main.bak => main.k.bk} | 0 22 files changed, 1012 insertions(+), 13 deletions(-) rename java/src/test/java/com/kcl/{LoadSettingsFileTest.java => LoadSettingsFilesTest.java} (95%) create mode 100644 kotlin/src/test/kotlin/com/kcl/FormatTest.kt create mode 100644 kotlin/src/test/kotlin/com/kcl/GetSchemaTypeTest.kt create mode 100644 kotlin/src/test/kotlin/com/kcl/GetVersionTest.kt create mode 100644 kotlin/src/test/kotlin/com/kcl/LintTest.kt create mode 100644 kotlin/src/test/kotlin/com/kcl/ListOptionsTest.kt create mode 100644 kotlin/src/test/kotlin/com/kcl/ListVariablesTest.kt create mode 100644 kotlin/src/test/kotlin/com/kcl/LoadPackageTest.kt create mode 100644 kotlin/src/test/kotlin/com/kcl/LoadSettingsFilesTest.kt create mode 100644 kotlin/src/test/kotlin/com/kcl/OverrideFileTest.kt create mode 100644 kotlin/src/test/kotlin/com/kcl/ParseFileTest.kt create mode 100644 kotlin/src/test/kotlin/com/kcl/ParseProgramTest.kt create mode 100644 kotlin/src/test/kotlin/com/kcl/RenameTest.kt create mode 100644 kotlin/src/test/kotlin/com/kcl/TestingTest.kt create mode 100644 kotlin/src/test/kotlin/com/kcl/UpdateDependenciesTest.kt delete mode 100644 kotlin/src/test_data/override_file/main.k rename kotlin/src/test_data/override_file/{main.bak => main.k.bk} (100%) diff --git a/README.md b/README.md index 557fd195..8dff2d39 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,36 @@ main(); ### Kotlin +Refer to [this](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-apache-maven-registry#authenticating-to-github-packages) to configure your Maven; set up your GitHub account and Token in the `settings.xml`. + +#### Maven + +In your project's pom.xml, configure our repository as follows: + +```xml + + + github + https://maven.pkg.github.com/kcl-lang/* + + true + + + +``` + +This way you'll be able to import the above dependency to use the SDK. + +```xml + + com.kcl + kcl-lib + 0.10.0-alpha.2-SNAPSHOT + +``` + +Write the code + ```kotlin import com.kcl.api.API import com.kcl.api.execProgramArgs diff --git a/java/README.md b/java/README.md index 1991cbef..4369ae60 100644 --- a/java/README.md +++ b/java/README.md @@ -50,12 +50,9 @@ public class ExecProgramTest { ## Developing - Install `Java 8+` +- Install `Maven` - Install `cargo` and `Python` (for Rust code building) -```shell -pnpm install -``` - ### Building ```shell diff --git a/java/src/test/java/com/kcl/ListOptionsTest.java b/java/src/test/java/com/kcl/ListOptionsTest.java index 0f326bbd..590ad659 100644 --- a/java/src/test/java/com/kcl/ListOptionsTest.java +++ b/java/src/test/java/com/kcl/ListOptionsTest.java @@ -9,7 +9,7 @@ public class ListOptionsTest { @Test - public void testParseProgram() throws Exception { + public void testListOptionsApi() throws Exception { ParseProgram_Args args = ParseProgram_Args.newBuilder().addPaths("./src/test_data/option/main.k").build(); API apiInstance = new API(); diff --git a/java/src/test/java/com/kcl/LoadSettingsFileTest.java b/java/src/test/java/com/kcl/LoadSettingsFilesTest.java similarity index 95% rename from java/src/test/java/com/kcl/LoadSettingsFileTest.java rename to java/src/test/java/com/kcl/LoadSettingsFilesTest.java index 7a8eefca..3b4c3536 100644 --- a/java/src/test/java/com/kcl/LoadSettingsFileTest.java +++ b/java/src/test/java/com/kcl/LoadSettingsFilesTest.java @@ -7,7 +7,7 @@ import com.kcl.api.Spec.LoadSettingsFiles_Args; import com.kcl.api.Spec.LoadSettingsFiles_Result; -public class LoadSettingsFileTest { +public class LoadSettingsFilesTest { @Test public void testLoadSettingsFile() throws Exception { API api = new API(); diff --git a/java/src/test/java/com/kcl/TestingTest.java b/java/src/test/java/com/kcl/TestingTest.java index 90861bbc..03e35224 100644 --- a/java/src/test/java/com/kcl/TestingTest.java +++ b/java/src/test/java/com/kcl/TestingTest.java @@ -9,7 +9,7 @@ public class TestingTest { @Test - public void testLintPathApi() throws Exception { + public void testTestingApi() throws Exception { API apiInstance = new API(); Test_Args args = Test_Args.newBuilder().addPkgList("./src/test_data/testing/...").build(); Test_Result result = apiInstance.test(args); diff --git a/kotlin/README.md b/kotlin/README.md index 28c4b907..ddfa45aa 100644 --- a/kotlin/README.md +++ b/kotlin/README.md @@ -1,3 +1,619 @@ # KCL Artifact Library for Kotlin -This repo is under development, PRs welcome! +## Installation + +Refer to [this](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-apache-maven-registry#authenticating-to-github-packages) to configure your Maven; set up your GitHub account and Token in the `settings.xml`. + +### Maven + +In your project's `pom.xml`, configure our repository as follows: + +```xml + + + github + https://maven.pkg.github.com/kcl-lang/* + + true + + + +``` + +This way you'll be able to import the above dependency to use the SDK. + +```xml + + com.kcl + kcl-lib-kotlin + 0.10.0-alpha.2-SNAPSHOT + +``` + +Write the code + +```kotlin +import com.kcl.api.API +import com.kcl.api.execProgramArgs + +val args = execProgramArgs { kFilenameList += "schema.k" } +val api = API() +val result = api.execProgram(args) +``` + +## Developing + +- Install `Java 8+` +- Install `Maven` +- Install `cargo` and `Python` (for Rust code building) + +### Building + +```shell +make build +``` + +### Testing + +```shell +make test +``` + +## API Reference + +### execProgram + +Execute KCL file with arguments and return the JSON/YAML result. + +
Example +

+ +The content of `schema.k` is + +```python +schema AppConfig: + replicas: int + +app: AppConfig { + replicas: 2 +} +``` + +Kotlin Code + +```kotlin +import com.kcl.api.API +import com.kcl.api.execProgramArgs + +val args = execProgramArgs { kFilenameList += "schema.k" } +val api = API() +val result = api.execProgram(args) +``` + +

+
+ +### parseFile + +Parse KCL single file to Module AST JSON string with import dependencies and parse errors. + +
Example +

+ +The content of `schema.k` is + +```python +schema AppConfig: + replicas: int + +app: AppConfig { + replicas: 2 +} +``` + +Kotlin Code + +```kotlin +import com.kcl.api.API +import com.kcl.api.parseFileArgs + +val args = parseFileArgs { path = "schema.k" } +val api = API() +val result = api.parseFile(args) +``` + +

+
+ +### loadPackage + +loadPackage provides users with the ability to parse KCL program and semantic model information including symbols, types, definitions, etc. + +
Example +

+ +The content of `schema.k` is + +```python +schema AppConfig: + replicas: int + +app: AppConfig { + replicas: 2 +} +``` + +Kotlin Code + +```kotlin +import com.kcl.api.API +import com.kcl.api.loadPackageArgs +import com.kcl.api.parseProgramArgs + +val args = loadPackageArgs { parseArgs = parseProgramArgs { paths += "schema.k" }; resolveAst = true } +val api = API() +val result = api.loadPackage(args) +``` + +

+
+ +### listVariables + +listVariables provides users with the ability to parse KCL program and get all variables by specs. + +
Example +

+ +The content of `schema.k` is + +```python +schema AppConfig: + replicas: int + +app: AppConfig { + replicas: 2 +} +``` + +Kotlin Code + +```kotlin +import com.kcl.api.API +import com.kcl.api.listVariablesArgs + +val args = listVariablesArgs { files += "./src/test_data/schema.k" } +val api = API() +val result = api.listVariables(args) +``` + +

+
+ +### listOptions + +listOptions provides users with the ability to parse KCL program and get all option information. + +
Example +

+ +The content of `options.k` is + +```python +a = option("key1") +b = option("key2", required=True) +c = { + metadata.key = option("metadata-key") +} +``` + +Kotlin Code + +```kotlin +import com.kcl.api.API +import com.kcl.api.parseProgramArgs + +val args = parseProgramArgs { paths += "options.k" } +val api = API() +val result = api.listOptions(args) +``` + +

+
+ +### getSchemaTypeMapping + +Get schema type mapping defined in the program. + +
Example +

+ +The content of `schema.k` is + +```python +schema AppConfig: + replicas: int + +app: AppConfig { + replicas: 2 +} +``` + +Kotlin Code + +```kotlin +import com.kcl.api.API +import com.kcl.api.execProgramArgs +import com.kcl.api.getSchemaTypeMappingArgs + +val args = getSchemaTypeMappingArgs { execArgs = execProgramArgs { kFilenameList += "schema.k" } } +val api = API() +val result = api.getSchemaTypeMapping(args) +val appSchemaType = result.schemaTypeMappingMap["app"] ?: throw AssertionError("App schema type not found") +val replicasAttr = appSchemaType.properties["replicas"] ?: throw AssertionError("App schema type of `replicas` not found") +``` + +

+
+ +### overrideFile + +Override KCL file with arguments. See [https://www.kcl-lang.io/docs/user_docs/guides/automation](https://www.kcl-lang.io/docs/user_docs/guides/automation) for more override spec guide. + +
Example +

+ +The content of `main.k` is + +```python +a = 1 + +b = { + "a": 1 + "b": 2 +} +``` + +Kotlin Code + +```kotlin +import com.kcl.api.API +import com.kcl.api.overrideFileArgs + +val api = API() +val result = api.overrideFile( + overrideFileArgs { + file = "main.k"; + specs += spec + } +) +``` + +

+
+ +### formatCode + +Format the code source. + +
Example +

+ +Kotlin Code + +```kotlin +import com.kcl.api.API +import com.kcl.api.formatCodeArgs + +val sourceCode = "schema Person:\n" + + " name: str\n" + + " age: int\n" + + " check:\n" + + " 0 < age < 120\n" +val args = formatCodeArgs { source = sourceCode } +val api = API() +val result = api.formatCode(args) +``` + +

+
+ +### formatPath + +Format KCL file or directory path contains KCL files and returns the changed file paths. + +
Example +

+ +The content of `format_path.k` is + +```python +schema Person: + name: str + age: int + + check: + 0 < age < 120 +``` + +Kotlin Code + +```kotlin +import com.kcl.api.API +import com.kcl.api.formatPathArgs + +val args = formatPathArgs { path = "format_path.k" } +val api = API() +val result = api.formatPath(args) +``` + +

+
+ +### lintPath + +Lint files and return error messages including errors and warnings. + +
Example +

+ +The content of `lint_path.k` is + +```python +import math + +a = 1 +``` + +Kotlin Code + +```kotlin +import com.kcl.api.API +import com.kcl.api.lintPathArgs + +val args = lintPathArgs { paths += "lint_path.k" } +val api = API() +val result = api.lintPath(args) +``` + +

+
+ +### validateCode + +Validate code using schema and JSON/YAML data strings. + +
Example +

+ +Kotlin Code + +```kotlin +import com.kcl.api.API +import com.kcl.api.validateCodeArgs + +val args = validateCodeArgs { + code = "schema Person:\n" + " name: str\n" + " age: int\n" + " check:\n" + " 0 < age < 120\n" + data = "{\"name\": \"Alice\", \"age\": 10}" +} +val api = API(); +val result = api.validateCode(args); +``` + +

+
+ +### rename + +Rename all the occurrences of the target symbol in the files. This API will rewrite files if they contain symbols to be renamed. Return the file paths that got changed. + +
Example +

+ +The content of `main.k` is + +```python +a = 1 +b = a +``` + +Kotlin Code + +```kotlin +import com.kcl.api.API +import com.kcl.api.renameArgs + +val args = renameArgs { + packageRoot = "." + filePaths += "./main.k" + symbolPath = "a" + newName = "a2" +} +val api = API() +val result = api.rename(args) +``` + +

+
+ +### renameCode + +Rename all the occurrences of the target symbol and return the modified code if any code has been changed. This API won't rewrite files but return the changed code. + +
Example +

+ +Kotlin Code + +```kotlin +import com.kcl.api.API +import com.kcl.api.renameCodeArgs + +val api = API() +val args = renameCodeArgs { + packageRoot = "/mock/path" + sourceCodes.put("/mock/path/main.k", "a = 1\nb = a") + symbolPath = "a" + newName = "a2" +} +val result = api.renameCode(args) +``` + +

+
+ +### test + +Test KCL packages with test arguments. + +
Example +

+ +Kotlin Code + +```kotlin +import com.kcl.api.API +import com.kcl.api.testArgs + +val args = testArgs { + pkgList += "/path/to/test/package" +} +val api = API() +val result = api.test(args) +``` + +

+
+ +### loadSettingsFiles + +Load the setting file config defined in `kcl.yaml` + +
Example +

+ +The content of `kcl.yaml` is + +```yaml +kcl_cli_configs: + strict_range_check: true +kcl_options: + - key: key + value: value +``` + +Kotlin Code + +```kotlin +import com.kcl.api.API +import com.kcl.api.loadSettingsFilesArgs + +val args = loadSettingsFilesArgs { files += "kcl.yaml" } +val api = API() +val result = api.loadSettingsFiles(args) +``` + +

+
+ +### updateDependencies + +Download and update dependencies defined in the `kcl.mod` file and return the external package name and location list. + +
Example +

+ +The content of `module/kcl.mod` is + +```yaml +[package] +name = "mod_update" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] +helloworld = { oci = "oci://ghcr.io/kcl-lang/helloworld", tag = "0.1.0" } +flask = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests", commit = "ade147b" } +``` + +Kotlin Code + +```kotlin +import com.kcl.api.API +import com.kcl.api.updateDependenciesArgs + +val api = API() +val args = updateDependenciesArgs { manifestPath = "module" } +val result = api.updateDependencies(args) +``` + +

+
+ +Call `execProgram` with external dependencies + +
Example +

+ +The content of `module/kcl.mod` is + +```yaml +[package] +name = "mod_update" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] +helloworld = { oci = "oci://ghcr.io/kcl-lang/helloworld", tag = "0.1.0" } +flask = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests", commit = "ade147b" } +``` + +The content of `module/main.k` is + +```python +import helloworld +import flask + +a = helloworld.The_first_kcl_program +``` + +Kotlin Code + +```kotlin +import com.kcl.api.API +import com.kcl.api.execProgramArgs +import com.kcl.api.updateDependenciesArgs + +val api = API() +val args = updateDependenciesArgs { manifestPath = "module" } +val result = api.updateDependencies(args) +val execArgs = execProgramArgs { + kFilenameList += "module/main.k" + externalPkgs.addAll(result.externalPkgsList) +} +val execResult = api.execProgram(execArgs) +``` + +

+
+ +### getVersion + +Return the KCL service version information. + +
Example +

+ +Kotlin Code + +```kotlin +import com.kcl.api.API +import com.kcl.api.getVersionArgs + +val api = API() +val args = getVersionArgs {} +val result = api.getVersion(args) +``` + +

+
diff --git a/kotlin/src/test/kotlin/com/kcl/FormatTest.kt b/kotlin/src/test/kotlin/com/kcl/FormatTest.kt new file mode 100644 index 00000000..26608474 --- /dev/null +++ b/kotlin/src/test/kotlin/com/kcl/FormatTest.kt @@ -0,0 +1,38 @@ +package com.kcl + +import com.kcl.api.API +import com.kcl.api.formatCodeArgs +import com.kcl.api.formatPathArgs +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.Assertions.assertEquals + +class FormatTest { + companion object { + private const val TEST_PATH = "./src/test_data/format_path/test.k" + } + + @Test + fun testFormatPathApi() { + val args = formatPathArgs { path = TEST_PATH } + val api = API() + val result = api.formatPath(args) + } + + @Test + fun testFormatCodeApi() { + val sourceCode = "schema Person:\n" + + " name: str\n" + + " age: int\n" + + " check:\n" + + " 0 < age < 120\n" + val args = formatCodeArgs { source = sourceCode } + val api = API() + val result = api.formatCode(args) + val expectedFormattedCode = "schema Person:\n" + + " name: str\n" + + " age: int\n\n" + + " check:\n" + + " 0 < age < 120\n\n" + assertEquals(result.formatted.toStringUtf8(), expectedFormattedCode) + } +} diff --git a/kotlin/src/test/kotlin/com/kcl/GetSchemaTypeTest.kt b/kotlin/src/test/kotlin/com/kcl/GetSchemaTypeTest.kt new file mode 100644 index 00000000..a4300835 --- /dev/null +++ b/kotlin/src/test/kotlin/com/kcl/GetSchemaTypeTest.kt @@ -0,0 +1,23 @@ +package com.kcl + +import com.kcl.api.API +import com.kcl.api.execProgramArgs +import com.kcl.api.getSchemaTypeMappingArgs +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.Assertions.assertEquals + +class GetSchemaTypeTest { + companion object { + private const val TEST_FILE = "./src/test_data/schema.k" + } + + @Test + fun testGetSchemaTypeApi() { + val args = getSchemaTypeMappingArgs { execArgs = execProgramArgs { kFilenameList += TEST_FILE } } + val api = API() + val result = api.getSchemaTypeMapping(args) + val appSchemaType = result.schemaTypeMappingMap["app"] ?: throw AssertionError("App schema type not found") + val replicasAttr = appSchemaType.properties["replicas"] ?: throw AssertionError("App schema type of `replicas` not found") + assertEquals(replicasAttr.type, "int") + } +} diff --git a/kotlin/src/test/kotlin/com/kcl/GetVersionTest.kt b/kotlin/src/test/kotlin/com/kcl/GetVersionTest.kt new file mode 100644 index 00000000..bb2f129c --- /dev/null +++ b/kotlin/src/test/kotlin/com/kcl/GetVersionTest.kt @@ -0,0 +1,17 @@ +package com.kcl + +import com.kcl.api.API +import com.kcl.api.getVersionArgs +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.Assertions.assertEquals + +class GetVersionTest { + @Test + fun testGetVersion() { + val args = getVersionArgs {} + val api = API() + val result = api.getVersion(args) + assertEquals(result.versionInfo.contains("Version"), true) + assertEquals(result.versionInfo.contains("GitCommit"), true) + } +} diff --git a/kotlin/src/test/kotlin/com/kcl/LintTest.kt b/kotlin/src/test/kotlin/com/kcl/LintTest.kt new file mode 100644 index 00000000..03aa1f3d --- /dev/null +++ b/kotlin/src/test/kotlin/com/kcl/LintTest.kt @@ -0,0 +1,21 @@ +package com.kcl + +import com.kcl.api.API +import com.kcl.api.lintPathArgs +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.Assertions.assertEquals + +class LintTest { + companion object { + private const val TEST_PATH = "./src/test_data/lint_path/test-lint.k" + } + + @Test + fun testLintPathApi() { + val args = lintPathArgs { paths += TEST_PATH } + val api = API() + val result = api.lintPath(args) + val foundWarning = result.resultsList.any { warning -> warning.contains("Module 'math' imported but unused") } + assertEquals(foundWarning, true) + } +} diff --git a/kotlin/src/test/kotlin/com/kcl/ListOptionsTest.kt b/kotlin/src/test/kotlin/com/kcl/ListOptionsTest.kt new file mode 100644 index 00000000..17fd1482 --- /dev/null +++ b/kotlin/src/test/kotlin/com/kcl/ListOptionsTest.kt @@ -0,0 +1,22 @@ +package com.kcl + +import com.kcl.api.API +import com.kcl.api.parseProgramArgs +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.Assertions.assertEquals + +class ListOptionsTest { + companion object { + private const val TEST_FILE = "./src/test_data/option/main.k" + } + + @Test + fun testListOptionsApi() { + val args = parseProgramArgs { paths += TEST_FILE } + val api = API() + val result = api.listOptions(args) + assertEquals("key1", result.optionsList[0].name) + assertEquals("key2", result.optionsList[1].name) + assertEquals("metadata-key", result.optionsList[2].name) + } +} diff --git a/kotlin/src/test/kotlin/com/kcl/ListVariablesTest.kt b/kotlin/src/test/kotlin/com/kcl/ListVariablesTest.kt new file mode 100644 index 00000000..96b4641f --- /dev/null +++ b/kotlin/src/test/kotlin/com/kcl/ListVariablesTest.kt @@ -0,0 +1,20 @@ +package com.kcl + +import com.kcl.api.API +import com.kcl.api.listVariablesArgs +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.Assertions.assertEquals + +class ListVariablesTest { + companion object { + private const val TEST_FILE = "./src/test_data/schema.k" + } + + @Test + fun testListVariablesApi() { + val args = listVariablesArgs { files += TEST_FILE } + val api = API() + val result = api.listVariables(args) + assertEquals(result.variablesMap["app"]?.variablesList?.get(0)?.value, "AppConfig {replicas: 2}") + } +} diff --git a/kotlin/src/test/kotlin/com/kcl/LoadPackageTest.kt b/kotlin/src/test/kotlin/com/kcl/LoadPackageTest.kt new file mode 100644 index 00000000..ceb2b01e --- /dev/null +++ b/kotlin/src/test/kotlin/com/kcl/LoadPackageTest.kt @@ -0,0 +1,22 @@ +package com.kcl + +import com.kcl.api.API +import com.kcl.api.loadPackageArgs +import com.kcl.api.parseProgramArgs +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.Assertions.assertEquals + +class LoadPackageTest { + companion object { + private const val TEST_FILE = "./src/test_data/schema.k" + } + + @Test + fun testLoadPackageApi() { + val args = loadPackageArgs { parseArgs = parseProgramArgs { paths += TEST_FILE }; resolveAst = true } + val api = API() + val result = api.loadPackage(args) + assertEquals(result.parseErrorsList.size, 0) + assertEquals(result.typeErrorsList.size, 0) + } +} diff --git a/kotlin/src/test/kotlin/com/kcl/LoadSettingsFilesTest.kt b/kotlin/src/test/kotlin/com/kcl/LoadSettingsFilesTest.kt new file mode 100644 index 00000000..d5505a2f --- /dev/null +++ b/kotlin/src/test/kotlin/com/kcl/LoadSettingsFilesTest.kt @@ -0,0 +1,23 @@ +package com.kcl + +import com.kcl.api.API +import com.kcl.api.loadSettingsFilesArgs +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.Assertions.assertEquals + +class LoadSettingsFileTest { + companion object { + private const val TEST_FILE = "./src/test_data/settings/kcl.yaml" + } + + @Test + fun testLoadSettingsFilesApi() { + val args = loadSettingsFilesArgs { files += TEST_FILE } + val api = API() + val result = api.loadSettingsFiles(args) + assertEquals(result.kclCliConfigs.filesCount, 0) + assertEquals(result.kclCliConfigs.strictRangeCheck, true) + assertEquals(result.kclOptionsList[0].key, "key") + assertEquals(result.kclOptionsList[0].value, "\"value\"") + } +} diff --git a/kotlin/src/test/kotlin/com/kcl/OverrideFileTest.kt b/kotlin/src/test/kotlin/com/kcl/OverrideFileTest.kt new file mode 100644 index 00000000..6f80f964 --- /dev/null +++ b/kotlin/src/test/kotlin/com/kcl/OverrideFileTest.kt @@ -0,0 +1,41 @@ +package com.kcl + +import com.kcl.api.API +import com.kcl.api.overrideFileArgs +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.Assertions.assertEquals + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +class OverrideFileTest { + companion object { + private const val TEST_FILE = "./src/test_data/schema.k" + } + + @Test + fun testOverrideFileApi() { + val api = API() + val testCases = arrayOf("a=2", "b.a=[1, 2, 3]") + val source = Paths.get("./src/test_data/override_file/main.k.bk") + val target = Paths.get("./src/test_data/override_file/main.k") + if (Files.exists(target)) { + Files.delete(target) + } + Files.copy(source, target) + for (spec in testCases) { + val result = api.overrideFile( + overrideFileArgs { + file = "./src/test_data/override_file/main.k"; + specs += spec + } + ) + assertEquals(result.result, true) + } + + if (Files.exists(target)) { + Files.delete(target) + } + } +} diff --git a/kotlin/src/test/kotlin/com/kcl/ParseFileTest.kt b/kotlin/src/test/kotlin/com/kcl/ParseFileTest.kt new file mode 100644 index 00000000..60bc0265 --- /dev/null +++ b/kotlin/src/test/kotlin/com/kcl/ParseFileTest.kt @@ -0,0 +1,20 @@ +package com.kcl + +import com.kcl.api.API +import com.kcl.api.parseFileArgs +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.Assertions.assertEquals + +class ParseFileTest { + companion object { + private const val TEST_FILE = "./src/test_data/parse/main.k" + } + + @Test + fun testParseFileApi() { + val args = parseFileArgs { path = TEST_FILE } + val api = API() + val result = api.parseFile(args) + assertEquals(result.depsCount, 2) + } +} diff --git a/kotlin/src/test/kotlin/com/kcl/ParseProgramTest.kt b/kotlin/src/test/kotlin/com/kcl/ParseProgramTest.kt new file mode 100644 index 00000000..4bbef54a --- /dev/null +++ b/kotlin/src/test/kotlin/com/kcl/ParseProgramTest.kt @@ -0,0 +1,20 @@ +package com.kcl + +import com.kcl.api.API +import com.kcl.api.parseProgramArgs +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.Assertions.assertEquals + +class ParseProgramTest { + companion object { + private const val TEST_FILE = "./src/test_data/parse/main.k" + } + + @Test + fun testParseProgramApi() { + val args = parseProgramArgs { paths += TEST_FILE } + val api = API() + val result = api.parseProgram(args) + assertEquals(result.pathsCount, 3) + } +} diff --git a/kotlin/src/test/kotlin/com/kcl/RenameTest.kt b/kotlin/src/test/kotlin/com/kcl/RenameTest.kt new file mode 100644 index 00000000..e516c166 --- /dev/null +++ b/kotlin/src/test/kotlin/com/kcl/RenameTest.kt @@ -0,0 +1,41 @@ +package com.kcl + +import com.kcl.api.API +import com.kcl.api.renameCodeArgs +import com.kcl.api.renameArgs +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.Assertions.assertEquals + +import java.nio.charset.StandardCharsets +import java.nio.file.Files +import java.nio.file.Paths + +class RenameTest { + @Test + fun testRenameCodeApi() { + val args = renameCodeArgs { + packageRoot = "/mock/path" + sourceCodes.put("/mock/path/main.k", "a = 1\nb = a") + symbolPath = "a" + newName = "a2" + } + val api = API() + val result = api.renameCode(args) + assertEquals(result.changedCodes["/mock/path/main.k"], "a2 = 1\nb = a2") + } + + @Test + fun testRenameApi() { + val backupContent = Files.readAllBytes(Paths.get("./src/test_data/rename/main.bak")).toString(StandardCharsets.UTF_8) + Files.write(Paths.get("./src/test_data/rename/main.k"), backupContent.toByteArray(StandardCharsets.UTF_8)) + val args = renameArgs { + packageRoot = "./src/test_data/rename" + filePaths += "./src/test_data/rename/main.k" + symbolPath = "a" + newName = "a2" + } + val api = API() + val result = api.rename(args) + assertEquals(result.changedFilesList[0].contains("main.k"), true) + } +} diff --git a/kotlin/src/test/kotlin/com/kcl/TestingTest.kt b/kotlin/src/test/kotlin/com/kcl/TestingTest.kt new file mode 100644 index 00000000..dd61ca7d --- /dev/null +++ b/kotlin/src/test/kotlin/com/kcl/TestingTest.kt @@ -0,0 +1,22 @@ +package com.kcl + +import com.kcl.api.API +import com.kcl.api.testArgs +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.Assertions.assertEquals + +class TestingTest { + companion object { + private const val TEST_PKG = "./src/test_data/testing/..." + } + + @Test + fun testTestingApi() { + val args = testArgs { + pkgList += TEST_PKG + } + val api = API() + val result = api.test(args) + assertEquals(result.infoCount, 2) + } +} diff --git a/kotlin/src/test/kotlin/com/kcl/UpdateDependenciesTest.kt b/kotlin/src/test/kotlin/com/kcl/UpdateDependenciesTest.kt new file mode 100644 index 00000000..65b09af5 --- /dev/null +++ b/kotlin/src/test/kotlin/com/kcl/UpdateDependenciesTest.kt @@ -0,0 +1,31 @@ +package com.kcl + +import com.kcl.api.API +import com.kcl.api.execProgramArgs +import com.kcl.api.updateDependenciesArgs +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.Assertions.assertEquals + +class UpdateDependenciesTest { + @Test + fun testUpdateDependencies() { + val args = updateDependenciesArgs { manifestPath = "./src/test_data/update_dependencies" } + val api = API() + val result = api.updateDependencies(args) + assertEquals(result.externalPkgsList.size, 2) + } + + @Test + fun testExecProgramWithExternalDependencies() { + val args = updateDependenciesArgs { manifestPath = "./src/test_data/update_dependencies" } + val api = API() + val result = api.updateDependencies(args) + assertEquals(result.externalPkgsList.size, 2) + val execArgs = execProgramArgs { + kFilenameList += "./src/test_data/update_dependencies/main.k" + externalPkgs.addAll(result.externalPkgsList) + } + val execResult = api.execProgram(execArgs) + assertEquals(execResult.yamlResult, "a: Hello World!") + } +} diff --git a/kotlin/src/test_data/override_file/main.k b/kotlin/src/test_data/override_file/main.k deleted file mode 100644 index 460fcac5..00000000 --- a/kotlin/src/test_data/override_file/main.k +++ /dev/null @@ -1,5 +0,0 @@ -a = 1 -b = { - "a": 2 - "b": 2 -} diff --git a/kotlin/src/test_data/override_file/main.bak b/kotlin/src/test_data/override_file/main.k.bk similarity index 100% rename from kotlin/src/test_data/override_file/main.bak rename to kotlin/src/test_data/override_file/main.k.bk