Skip to content

Commit b1fc8c0

Browse files
authored
Merge pull request #114 from Peefy/feat-more-dotnet-api-and-docs
feat: add all dotnet API tests and reference docs
2 parents e13b25a + 33d3a15 commit b1fc8c0

File tree

33 files changed

+1091
-29
lines changed

33 files changed

+1091
-29
lines changed

dotnet/KclLib.Tests/APITest.cs

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,108 @@ public void TestExecProgramAPIFileNotFound()
3636
}
3737
}
3838

39+
[TestMethod]
40+
public void TestParseProgramAPI()
41+
{
42+
var path = Path.Combine(parentDirectory, "test_data", "schema.k");
43+
// Prepare arguments for parsing the KCL program
44+
var args = new ParseProgram_Args();
45+
args.Paths.Add(path);
46+
// Instantiate API and call parse_program method
47+
48+
var result = new API().ParseProgram(args);
49+
50+
// Assert the parsing results
51+
Assert.AreEqual(1, result.Paths.Count);
52+
Assert.AreEqual(0, result.Errors.Count);
53+
}
54+
55+
[TestMethod]
56+
public void TestParseFileApi()
57+
{
58+
var path = Path.Combine(parentDirectory, "test_data", "schema.k");
59+
// Prepare arguments for parsing a single KCL file
60+
var args = new ParseFile_Args { Path = path };
61+
62+
// Instantiate API and call parse_file method
63+
var result = new API().ParseFile(args);
64+
65+
// Assert the parsing results
66+
Assert.AreEqual(0, result.Deps.Count);
67+
Assert.AreEqual(0, result.Errors.Count);
68+
}
69+
70+
[TestMethod]
71+
public void TestOverrideFileAPI()
72+
{
73+
// Backup and restore test file for each test run
74+
string bakFile = Path.Combine(parentDirectory, "test_data", "override_file", "main.bak");
75+
string testFile = Path.Combine(parentDirectory, "test_data", "override_file", "main.k");
76+
File.WriteAllText(testFile, File.ReadAllText(bakFile));
77+
78+
// Prepare arguments for overriding the KCL file
79+
var args = new OverrideFile_Args
80+
{
81+
File = testFile,
82+
};
83+
args.Specs.Add("b.a=2");
84+
// Instantiate API and call override_file method
85+
86+
var result = new API().OverrideFile(args);
87+
88+
// Assert the outcomes of the override operation
89+
Assert.AreEqual(0, result.ParseErrors.Count);
90+
Assert.AreEqual(true, result.Result);
91+
}
92+
93+
[TestMethod]
94+
public void TestFormatPathAPI()
95+
{
96+
var api = new API();
97+
var args = new FormatPath_Args();
98+
var path = Path.Combine(parentDirectory, "test_data", "format_path", "test.k");
99+
args.Path = path;
100+
var result = api.FormatPath(args);
101+
}
102+
103+
[TestMethod]
104+
public void TestFormatCodeAPI()
105+
{
106+
string sourceCode = "schema Person:\n" + " name: str\n" + " age: int\n" + " check:\n"
107+
+ " 0 < age < 120\n";
108+
string expectedFormattedCode = "schema Person:\n" + " name: str\n" + " age: int\n\n" + " check:\n"
109+
+ " 0 < age < 120\n\n";
110+
var api = new API();
111+
var args = new FormatCode_Args();
112+
args.Source = sourceCode;
113+
var result = api.FormatCode(args);
114+
Assert.AreEqual(expectedFormattedCode, result.Formatted.ToStringUtf8(), result.ToString());
115+
}
116+
117+
[TestMethod]
118+
public void TestGetSchemaTypeAPI()
119+
{
120+
var path = Path.Combine(parentDirectory, "test_data", "schema.k");
121+
var execArgs = new ExecProgram_Args();
122+
execArgs.KFilenameList.Add(path);
123+
var args = new GetSchemaTypeMapping_Args();
124+
args.ExecArgs = execArgs;
125+
var result = new API().GetSchemaTypeMapping(args);
126+
Assert.AreEqual("int", result.SchemaTypeMapping["app"].Properties["replicas"].Type, result.ToString());
127+
}
128+
129+
[TestMethod]
130+
public void TestListOptionsAPI()
131+
{
132+
var path = Path.Combine(parentDirectory, "test_data", "option", "main.k");
133+
var args = new ParseProgram_Args();
134+
args.Paths.Add(path);
135+
var result = new API().ListOptions(args);
136+
Assert.AreEqual("key1", result.Options[0].Name);
137+
Assert.AreEqual("key2", result.Options[1].Name);
138+
Assert.AreEqual("metadata-key", result.Options[2].Name);
139+
}
140+
39141
[TestMethod]
40142
public void TestListVariablesAPI()
41143
{
@@ -47,6 +149,163 @@ public void TestListVariablesAPI()
47149
Assert.AreEqual("AppConfig {replicas = 2}", result.Variables["app"].Variables[0].Value, result.ToString());
48150
}
49151

152+
[TestMethod]
153+
public void TestLoadPackagesAPI()
154+
{
155+
var path = Path.Combine(parentDirectory, "test_data", "schema.k");
156+
var args = new LoadPackage_Args();
157+
args.ResolveAst = true;
158+
args.ParseArgs = new ParseProgram_Args();
159+
args.ParseArgs.Paths.Add(path);
160+
var result = new API().LoadPackage(args);
161+
var firstSymbol = result.Symbols.Values.FirstOrDefault();
162+
Assert.AreEqual(true, firstSymbol != null);
163+
}
164+
165+
[TestMethod]
166+
public void TestLintPathAPI()
167+
{
168+
var path = Path.Combine(parentDirectory, "test_data", "lint_path", "test-lint.k");
169+
var args = new LintPath_Args();
170+
args.Paths.Add(path);
171+
var result = new API().LintPath(args);
172+
bool foundWarning = result.Results.Any(warning => warning.Contains("Module 'math' imported but unused"));
173+
Assert.AreEqual(true, foundWarning, result.ToString());
174+
}
175+
176+
[TestMethod]
177+
public void TestValidateCodeAPI()
178+
{
179+
// Define the code schema and data
180+
string code = @"
181+
schema Person:
182+
name: str
183+
age: int
184+
check:
185+
0 < age < 120
186+
";
187+
string data = "{\"name\": \"Alice\", \"age\": 10}";
188+
189+
// Prepare arguments for validating the code
190+
var args = new ValidateCode_Args
191+
{
192+
Code = code,
193+
Data = data,
194+
Format = "json"
195+
};
196+
197+
// Instantiate API and call validate_code method
198+
199+
var result = new API().ValidateCode(args);
200+
201+
// Assert the validation results
202+
Assert.AreEqual(true, result.Success);
203+
Assert.AreEqual(string.Empty, result.ErrMessage);
204+
}
205+
206+
[TestMethod]
207+
public void TestRenameAPI()
208+
{
209+
var root = Path.Combine(parentDirectory, "test_data", "rename");
210+
var renameFilePath = Path.Combine(parentDirectory, "test_data", "rename", "main.k");
211+
var renameBakFilePath = Path.Combine(parentDirectory, "test_data", "rename", "main.bak");
212+
File.WriteAllText(renameFilePath, File.ReadAllText(renameBakFilePath));
213+
var args = new Rename_Args
214+
{
215+
PackageRoot = root,
216+
SymbolPath = "a",
217+
NewName = "a2"
218+
};
219+
args.FilePaths.Add(renameFilePath);
220+
221+
var result = new API().Rename(args);
222+
Assert.AreEqual(true, result.ChangedFiles.First().Contains("main.k"));
223+
}
224+
225+
[TestMethod]
226+
public void TestRenameCodeAPI()
227+
{
228+
var args = new RenameCode_Args
229+
{
230+
PackageRoot = "/mock/path",
231+
SymbolPath = "a",
232+
SourceCodes = { { "/mock/path/main.k", "a = 1\nb = a" } },
233+
NewName = "a2"
234+
};
235+
236+
var result = new API().RenameCode(args);
237+
Assert.AreEqual("a2 = 1\nb = a2", result.ChangedCodes["/mock/path/main.k"]);
238+
}
239+
240+
[TestMethod]
241+
public void TestTestingAPI()
242+
{
243+
var pkg = Path.Combine(parentDirectory, "test_data", "testing");
244+
var args = new Test_Args();
245+
args.PkgList.Add(pkg + "/...");
246+
247+
var result = new API().Test(args);
248+
Assert.AreEqual(2, result.Info.Count);
249+
}
250+
251+
[TestMethod]
252+
public void TestLoadSettingsFilesAPI()
253+
{
254+
var workDir = Path.Combine(parentDirectory, "test_data");
255+
var settingsFile = Path.Combine(workDir, "settings", "kcl.yaml");
256+
var args = new LoadSettingsFiles_Args
257+
{
258+
WorkDir = workDir,
259+
};
260+
args.Files.Add(settingsFile);
261+
262+
var result = new API().LoadSettingsFiles(args);
263+
Assert.AreEqual(0, result.KclCliConfigs.Files.Count);
264+
Assert.AreEqual(true, result.KclCliConfigs.StrictRangeCheck);
265+
Assert.AreEqual(true, result.KclOptions.Any(o => o.Key == "key" && o.Value == "\"value\""));
266+
}
267+
268+
[TestMethod]
269+
public void TestUpdateDependenciesAPI()
270+
{
271+
var manifestPath = Path.Combine(parentDirectory, "test_data", "update_dependencies");
272+
// Prepare arguments for updating dependencies.
273+
var args = new UpdateDependencies_Args { ManifestPath = manifestPath };
274+
// Instantiate API and call update_dependencies method.
275+
276+
var result = new API().UpdateDependencies(args);
277+
// Collect package names.
278+
var pkgNames = result.ExternalPkgs.Select(pkg => pkg.PkgName).ToList();
279+
// Assertions.
280+
Assert.AreEqual(2, pkgNames.Count);
281+
}
282+
283+
[TestMethod]
284+
public void TestExecAPIWithExternalDependencies()
285+
{
286+
var manifestPath = Path.Combine(parentDirectory, "test_data", "update_dependencies");
287+
var testFile = Path.Combine(manifestPath, "main.k");
288+
// First, update dependencies.
289+
var updateArgs = new UpdateDependencies_Args { ManifestPath = manifestPath };
290+
291+
var depResult = new API().UpdateDependencies(updateArgs);
292+
// Prepare arguments for executing the program with external dependencies.
293+
var execArgs = new ExecProgram_Args();
294+
execArgs.KFilenameList.Add(testFile);
295+
execArgs.ExternalPkgs.AddRange(depResult.ExternalPkgs);
296+
// Execute the program and assert the result.
297+
var execResult = new API().ExecProgram(execArgs);
298+
Assert.AreEqual("a: Hello World!", execResult.YamlResult);
299+
}
300+
301+
[TestMethod]
302+
public void TestGetVersion()
303+
{
304+
var result = new API().GetVersion(new GetVersion_Args());
305+
Assert.AreEqual(true, result.VersionInfo.Contains("Version"), result.ToString());
306+
Assert.AreEqual(true, result.VersionInfo.Contains("GitCommit"), result.ToString());
307+
}
308+
50309
static string FindCsprojInParentDirectory(string directory)
51310
{
52311
string parentDirectory = Directory.GetParent(directory).FullName;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a = 1
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[package]
2+
name = "aaa"
3+
edition = "0.0.1"
4+
version = "0.0.1"
5+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import bbb as b
2+
import ccc as c
3+
4+
a = b.B {
5+
name: "b instance in a"
6+
}
7+
8+
a_c = c.C {
9+
name: "c instance in a"
10+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[package]
2+
name = "bbb"
3+
edition = "0.0.1"
4+
version = "0.0.1"
5+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
schema B:
2+
name: str
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[package]
2+
name = "ccc"
3+
edition = "0.0.1"
4+
version = "0.0.1"
5+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
schema C:
2+
name: str
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import math
2+
3+
a = 1
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
a = option("key1")
2+
b = option("key2", required=True)
3+
c = {
4+
metadata.key = option("metadata-key")
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
a = 1
2+
3+
b = {
4+
"a": 1
5+
"b": 2
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
a = 1
2+
b = {
3+
"a": 2
4+
"b": 2
5+
}

dotnet/KclLib.Tests/test_data/parse/kcl.mod

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import pkg1
2+
import pkg2
3+
4+
a1 = pkg1.a
5+
a2 = pkg2.a
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a = 1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a = 1
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
a = 1
2+
b = a
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
a2 = 1
2+
b = a2
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kcl_cli_configs:
2+
strict_range_check: true
3+
kcl_options:
4+
- key: key
5+
value: value
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[package]
2+
name = "test_data"
3+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
func = lambda x {
2+
x
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
test_func_0 = lambda {
2+
assert func("a") == "a"
3+
}
4+
5+
test_func_1 = lambda {
6+
assert func("b") == "b"
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "mod_update"
3+
edition = "0.0.1"
4+
version = "0.0.1"
5+
6+
[dependencies]
7+
helloworld = { oci = "oci://ghcr.io/kcl-lang/helloworld", tag = "0.1.0" }
8+
flask = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests", commit = "ade147b" }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import helloworld
2+
import flask
3+
4+
a = helloworld.The_first_kcl_program
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
a = 1
2+
b = [1, 2, 3]
3+
c = {"a": "b"}

0 commit comments

Comments
 (0)