
UtilsTest.pudu
Pudu47 lines3.5 KB
1/** @Test.Utils.UtilsTest.Suite — JSON access, text bounds, and URIs */2module PuduLangMcp.Utils.UtilsTest34import Std.Io as Io5import Std.Json as Json6import Std.Test as Test7import PuduLangMcp.Utils.JsonAccess as Access8import PuduLangMcp.Utils.TextBounds as TextBounds9import PuduLangMcp.Utils.Uri as Uri101112fn main() -> Int {13 let value = Json.object(&[("name", Json.Text("x")), ("count", Json.Number(2)), ("flag", Json.Boolean(true)), ("nested", Json.object(&[("deep", Json.Number(9))]))])14 let checks = Test.suite("Utils", &[15 Test.equals("a string member is read", &Access.text(&value, "name"), &Some("x")),16 Test.equals("a member of another kind is not text", &Access.text(&value, "count"), &None),17 Test.equals("an integer member is read", &Access.integer(&value, "count"), &Some(2)),18 Test.equals("a boolean member is read", &Access.boolean(&value, "flag"), &Some(true)),19 Test.equals("a member of a non-object is none", &Access.text(&Json.Text("x"), "name"), &None),20 Test.equals("a path is followed", &Access.pathInt(&value, &["nested", "deep"]), &Some(9)),21 Test.equals("a broken path is none", &Access.pathText(&value, &["nested", "missing"]), &None),22 Test.equals("members of a non-object are none", &Access.members(&Json.Null).length(), &0),23 Test.equals("kinds follow JSON Schema", &[Json.Null, Json.Boolean(true), Json.Number(1), Json.Fractional("1.5"), Json.Text(""), Json.list(&[]), Json.object(&[])].map(|held: Json.Json| Access.kindOf(&held)), &["null", "boolean", "integer", "number", "string", "array", "object"]),24 Test.equals("text within its bound is unchanged", &TextBounds.bounded("abc", 3), &"abc"),25 Test.equals("longer text is cut and says so", &TextBounds.bounded("abcdef", 4), &"abcd\n[truncated: 2 more characters]"),26 Test.equals("a bound of zero keeps only the marker", &TextBounds.bounded("ab", 0), &"\n[truncated: 2 more characters]"),27 Test.equals("a snippet centres on its match", &TextBounds.snippet("one two three four five", "three", 9), &"…two three…"),28 Test.equals("a snippet without a match starts at the beginning", &TextBounds.snippet("one two three", "zzz", 3), &"one…"),29 Test.equals("empty text fits a zero bound", &TextBounds.bounded("", 0), &""),30 Test.equals("text exactly as wide as the window has no ellipsis", &TextBounds.snippet("abc", "zz", 3), &"abc"),31 Test.equals("a match at the start has no leading ellipsis", &TextBounds.snippet("three one two", "three", 5), &"three…"),32 Test.equals("a window ending at the text's end has no trailing ellipsis", &TextBounds.snippet("abcdefgh", "g", 4), &"…efgh"),33 Test.equals("an empty needle is no match", &TextBounds.snippet("abcdef", "", 3), &"abc…"),34 Test.equals("a snippet is one line", &TextBounds.singleLine("a\n\tb c\r\n"), &"a b c"),35 Test.equals("a file URI escapes spaces and percent signs", &Uri.fileUri("/tmp/a b/%c.pudu"), &"file:///tmp/a%20b/%25c.pudu"),36 Test.equals("a file URI round-trips", &Uri.pathOfFileUri(Uri.fileUri("/tmp/x #1?.pudu")), &Some("/tmp/x #1?.pudu")),37 Test.equals("another scheme is no file", &Uri.pathOfFileUri("https://x"), &None),38 Test.equals("a resource path drops its scheme", &Uri.resourcePath("pudu://docs/errors", "pudu://"), &Some("docs/errors")),39 Test.equals("a foreign scheme has no resource path", &Uri.resourcePath("file:///x", "pudu://"), &None)40 ])41 let ran = Test.run(&checks)42 for failure in Test.failuresOf(&ran) {43 let _reported = Io.writeErrorLine(failure)44 }45 Test.report(&ran)46}47