Skip to content

Commit

Permalink
Merge pull request #114 from Peefy/feat-more-dotnet-api-and-docs
Browse files Browse the repository at this point in the history
feat: add all dotnet API tests and reference docs
  • Loading branch information
Peefy authored Jul 15, 2024
2 parents e13b25a + 33d3a15 commit b1fc8c0
Show file tree
Hide file tree
Showing 33 changed files with 1,091 additions and 29 deletions.
259 changes: 259 additions & 0 deletions dotnet/KclLib.Tests/APITest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,108 @@ public void TestExecProgramAPIFileNotFound()
}
}

[TestMethod]
public void TestParseProgramAPI()
{
var path = Path.Combine(parentDirectory, "test_data", "schema.k");
// Prepare arguments for parsing the KCL program
var args = new ParseProgram_Args();
args.Paths.Add(path);
// Instantiate API and call parse_program method

var result = new API().ParseProgram(args);

// Assert the parsing results
Assert.AreEqual(1, result.Paths.Count);
Assert.AreEqual(0, result.Errors.Count);
}

[TestMethod]
public void TestParseFileApi()
{
var path = Path.Combine(parentDirectory, "test_data", "schema.k");
// Prepare arguments for parsing a single KCL file
var args = new ParseFile_Args { Path = path };

// Instantiate API and call parse_file method
var result = new API().ParseFile(args);

// Assert the parsing results
Assert.AreEqual(0, result.Deps.Count);
Assert.AreEqual(0, result.Errors.Count);
}

[TestMethod]
public void TestOverrideFileAPI()
{
// Backup and restore test file for each test run
string bakFile = Path.Combine(parentDirectory, "test_data", "override_file", "main.bak");
string testFile = Path.Combine(parentDirectory, "test_data", "override_file", "main.k");
File.WriteAllText(testFile, File.ReadAllText(bakFile));

// Prepare arguments for overriding the KCL file
var args = new OverrideFile_Args
{
File = testFile,
};
args.Specs.Add("b.a=2");
// Instantiate API and call override_file method

var result = new API().OverrideFile(args);

// Assert the outcomes of the override operation
Assert.AreEqual(0, result.ParseErrors.Count);
Assert.AreEqual(true, result.Result);
}

[TestMethod]
public void TestFormatPathAPI()
{
var api = new API();
var args = new FormatPath_Args();
var path = Path.Combine(parentDirectory, "test_data", "format_path", "test.k");
args.Path = path;
var result = api.FormatPath(args);
}

[TestMethod]
public void TestFormatCodeAPI()
{
string sourceCode = "schema Person:\n" + " name: str\n" + " age: int\n" + " check:\n"
+ " 0 < age < 120\n";
string expectedFormattedCode = "schema Person:\n" + " name: str\n" + " age: int\n\n" + " check:\n"
+ " 0 < age < 120\n\n";
var api = new API();
var args = new FormatCode_Args();
args.Source = sourceCode;
var result = api.FormatCode(args);
Assert.AreEqual(expectedFormattedCode, result.Formatted.ToStringUtf8(), result.ToString());
}

[TestMethod]
public void TestGetSchemaTypeAPI()
{
var path = Path.Combine(parentDirectory, "test_data", "schema.k");
var execArgs = new ExecProgram_Args();
execArgs.KFilenameList.Add(path);
var args = new GetSchemaTypeMapping_Args();
args.ExecArgs = execArgs;
var result = new API().GetSchemaTypeMapping(args);
Assert.AreEqual("int", result.SchemaTypeMapping["app"].Properties["replicas"].Type, result.ToString());
}

[TestMethod]
public void TestListOptionsAPI()
{
var path = Path.Combine(parentDirectory, "test_data", "option", "main.k");
var args = new ParseProgram_Args();
args.Paths.Add(path);
var result = new API().ListOptions(args);
Assert.AreEqual("key1", result.Options[0].Name);
Assert.AreEqual("key2", result.Options[1].Name);
Assert.AreEqual("metadata-key", result.Options[2].Name);
}

[TestMethod]
public void TestListVariablesAPI()
{
Expand All @@ -47,6 +149,163 @@ public void TestListVariablesAPI()
Assert.AreEqual("AppConfig {replicas = 2}", result.Variables["app"].Variables[0].Value, result.ToString());
}

[TestMethod]
public void TestLoadPackagesAPI()
{
var path = Path.Combine(parentDirectory, "test_data", "schema.k");
var args = new LoadPackage_Args();
args.ResolveAst = true;
args.ParseArgs = new ParseProgram_Args();
args.ParseArgs.Paths.Add(path);
var result = new API().LoadPackage(args);
var firstSymbol = result.Symbols.Values.FirstOrDefault();
Assert.AreEqual(true, firstSymbol != null);
}

[TestMethod]
public void TestLintPathAPI()
{
var path = Path.Combine(parentDirectory, "test_data", "lint_path", "test-lint.k");
var args = new LintPath_Args();
args.Paths.Add(path);
var result = new API().LintPath(args);
bool foundWarning = result.Results.Any(warning => warning.Contains("Module 'math' imported but unused"));
Assert.AreEqual(true, foundWarning, result.ToString());
}

[TestMethod]
public void TestValidateCodeAPI()
{
// Define the code schema and data
string code = @"
schema Person:
name: str
age: int
check:
0 < age < 120
";
string data = "{\"name\": \"Alice\", \"age\": 10}";

// Prepare arguments for validating the code
var args = new ValidateCode_Args
{
Code = code,
Data = data,
Format = "json"
};

// Instantiate API and call validate_code method

var result = new API().ValidateCode(args);

// Assert the validation results
Assert.AreEqual(true, result.Success);
Assert.AreEqual(string.Empty, result.ErrMessage);
}

[TestMethod]
public void TestRenameAPI()
{
var root = Path.Combine(parentDirectory, "test_data", "rename");
var renameFilePath = Path.Combine(parentDirectory, "test_data", "rename", "main.k");
var renameBakFilePath = Path.Combine(parentDirectory, "test_data", "rename", "main.bak");
File.WriteAllText(renameFilePath, File.ReadAllText(renameBakFilePath));
var args = new Rename_Args
{
PackageRoot = root,
SymbolPath = "a",
NewName = "a2"
};
args.FilePaths.Add(renameFilePath);

var result = new API().Rename(args);
Assert.AreEqual(true, result.ChangedFiles.First().Contains("main.k"));
}

[TestMethod]
public void TestRenameCodeAPI()
{
var args = new RenameCode_Args
{
PackageRoot = "/mock/path",
SymbolPath = "a",
SourceCodes = { { "/mock/path/main.k", "a = 1\nb = a" } },
NewName = "a2"
};

var result = new API().RenameCode(args);
Assert.AreEqual("a2 = 1\nb = a2", result.ChangedCodes["/mock/path/main.k"]);
}

[TestMethod]
public void TestTestingAPI()
{
var pkg = Path.Combine(parentDirectory, "test_data", "testing");
var args = new Test_Args();
args.PkgList.Add(pkg + "/...");

var result = new API().Test(args);
Assert.AreEqual(2, result.Info.Count);
}

[TestMethod]
public void TestLoadSettingsFilesAPI()
{
var workDir = Path.Combine(parentDirectory, "test_data");
var settingsFile = Path.Combine(workDir, "settings", "kcl.yaml");
var args = new LoadSettingsFiles_Args
{
WorkDir = workDir,
};
args.Files.Add(settingsFile);

var result = new API().LoadSettingsFiles(args);
Assert.AreEqual(0, result.KclCliConfigs.Files.Count);
Assert.AreEqual(true, result.KclCliConfigs.StrictRangeCheck);
Assert.AreEqual(true, result.KclOptions.Any(o => o.Key == "key" && o.Value == "\"value\""));
}

[TestMethod]
public void TestUpdateDependenciesAPI()
{
var manifestPath = Path.Combine(parentDirectory, "test_data", "update_dependencies");
// Prepare arguments for updating dependencies.
var args = new UpdateDependencies_Args { ManifestPath = manifestPath };
// Instantiate API and call update_dependencies method.

var result = new API().UpdateDependencies(args);
// Collect package names.
var pkgNames = result.ExternalPkgs.Select(pkg => pkg.PkgName).ToList();
// Assertions.
Assert.AreEqual(2, pkgNames.Count);
}

[TestMethod]
public void TestExecAPIWithExternalDependencies()
{
var manifestPath = Path.Combine(parentDirectory, "test_data", "update_dependencies");
var testFile = Path.Combine(manifestPath, "main.k");
// First, update dependencies.
var updateArgs = new UpdateDependencies_Args { ManifestPath = manifestPath };

var depResult = new API().UpdateDependencies(updateArgs);
// Prepare arguments for executing the program with external dependencies.
var execArgs = new ExecProgram_Args();
execArgs.KFilenameList.Add(testFile);
execArgs.ExternalPkgs.AddRange(depResult.ExternalPkgs);
// Execute the program and assert the result.
var execResult = new API().ExecProgram(execArgs);
Assert.AreEqual("a: Hello World!", execResult.YamlResult);
}

[TestMethod]
public void TestGetVersion()
{
var result = new API().GetVersion(new GetVersion_Args());
Assert.AreEqual(true, result.VersionInfo.Contains("Version"), result.ToString());
Assert.AreEqual(true, result.VersionInfo.Contains("GitCommit"), result.ToString());
}

static string FindCsprojInParentDirectory(string directory)
{
string parentDirectory = Directory.GetParent(directory).FullName;
Expand Down
1 change: 1 addition & 0 deletions dotnet/KclLib.Tests/test_data/format_path/test.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a = 1
5 changes: 5 additions & 0 deletions dotnet/KclLib.Tests/test_data/get_schema_ty/aaa/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "aaa"
edition = "0.0.1"
version = "0.0.1"

10 changes: 10 additions & 0 deletions dotnet/KclLib.Tests/test_data/get_schema_ty/aaa/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import bbb as b
import ccc as c

a = b.B {
name: "b instance in a"
}

a_c = c.C {
name: "c instance in a"
}
5 changes: 5 additions & 0 deletions dotnet/KclLib.Tests/test_data/get_schema_ty/bbb/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "bbb"
edition = "0.0.1"
version = "0.0.1"

2 changes: 2 additions & 0 deletions dotnet/KclLib.Tests/test_data/get_schema_ty/bbb/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema B:
name: str
5 changes: 5 additions & 0 deletions dotnet/KclLib.Tests/test_data/get_schema_ty/ccc/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "ccc"
edition = "0.0.1"
version = "0.0.1"

2 changes: 2 additions & 0 deletions dotnet/KclLib.Tests/test_data/get_schema_ty/ccc/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema C:
name: str
3 changes: 3 additions & 0 deletions dotnet/KclLib.Tests/test_data/lint_path/test-lint.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import math

a = 1
5 changes: 5 additions & 0 deletions dotnet/KclLib.Tests/test_data/option/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
a = option("key1")
b = option("key2", required=True)
c = {
metadata.key = option("metadata-key")
}
6 changes: 6 additions & 0 deletions dotnet/KclLib.Tests/test_data/override_file/main.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
a = 1

b = {
"a": 1
"b": 2
}
5 changes: 5 additions & 0 deletions dotnet/KclLib.Tests/test_data/override_file/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
a = 1
b = {
"a": 2
"b": 2
}
Empty file.
5 changes: 5 additions & 0 deletions dotnet/KclLib.Tests/test_data/parse/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import pkg1
import pkg2

a1 = pkg1.a
a2 = pkg2.a
1 change: 1 addition & 0 deletions dotnet/KclLib.Tests/test_data/parse/pkg1/pkg.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a = 1
1 change: 1 addition & 0 deletions dotnet/KclLib.Tests/test_data/parse/pkg2/pkg.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a = 1
2 changes: 2 additions & 0 deletions dotnet/KclLib.Tests/test_data/rename/main.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a = 1
b = a
2 changes: 2 additions & 0 deletions dotnet/KclLib.Tests/test_data/rename/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a2 = 1
b = a2
5 changes: 5 additions & 0 deletions dotnet/KclLib.Tests/test_data/settings/kcl.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kcl_cli_configs:
strict_range_check: true
kcl_options:
- key: key
value: value
3 changes: 3 additions & 0 deletions dotnet/KclLib.Tests/test_data/testing/module/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[package]
name = "test_data"

3 changes: 3 additions & 0 deletions dotnet/KclLib.Tests/test_data/testing/module/pkg/func.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
func = lambda x {
x
}
7 changes: 7 additions & 0 deletions dotnet/KclLib.Tests/test_data/testing/module/pkg/func_test.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
test_func_0 = lambda {
assert func("a") == "a"
}

test_func_1 = lambda {
assert func("b") == "b"
}
8 changes: 8 additions & 0 deletions dotnet/KclLib.Tests/test_data/update_dependencies/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[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" }
4 changes: 4 additions & 0 deletions dotnet/KclLib.Tests/test_data/update_dependencies/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import helloworld
import flask

a = helloworld.The_first_kcl_program
3 changes: 3 additions & 0 deletions dotnet/KclLib.Tests/test_data/variables/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
a = 1
b = [1, 2, 3]
c = {"a": "b"}
Loading

0 comments on commit b1fc8c0

Please sign in to comment.