Files
pkgs.lux/packages/xml/test_integration.lux
Brandon Lucas cbb66fbb73 Add frontmatter, markdown, path, xml, rss, and web packages
Sync local packages into the registry repo and update index.json
and README.md to include all 9 packages.
2026-02-24 21:04:20 -05:00

106 lines
5.4 KiB
Plaintext

import lib
// Integration tests: building real-world XML documents
// Build an HTML page structure
fn test_build_html_page(): Unit with {Test} = {
let head = lib.el("head", [
lib.textEl("title", "My Page"),
lib.selfClosing("meta", [lib.attr("charset", "utf-8")]),
lib.selfClosing("link", [lib.attr("rel", "stylesheet"), lib.attr("href", "style.css")])
])
let body = lib.el("body", [
lib.el("header", [lib.textEl("h1", "Welcome")]),
lib.el("main", [lib.textEl("p", "Hello world")]),
lib.el("footer", [lib.textEl("p", "Copyright 2025")])
])
let html = lib.el("html", [head, body])
let result = lib.render(html)
Test.assert(String.contains(result, "<title>My Page</title>"), "page has title")
Test.assert(String.contains(result, "<meta charset=\"utf-8\"/>"), "page has meta charset")
Test.assert(String.contains(result, "<h1>Welcome</h1>"), "page has h1")
Test.assert(String.contains(result, "<p>Hello world</p>"), "page has paragraph")
Test.assert(String.startsWith(result, "<html>"), "starts with html tag")
Test.assert(String.endsWith(result, "</html>"), "ends with html tag")
}
// Build a sitemap.xml
fn test_build_sitemap(): Unit with {Test} = {
let url1 = lib.el("url", [
lib.textEl("loc", "https://example.com/"),
lib.textEl("lastmod", "2025-01-29"),
lib.textEl("priority", "1.0")
])
let url2 = lib.el("url", [
lib.textEl("loc", "https://example.com/about"),
lib.textEl("lastmod", "2025-01-15"),
lib.textEl("priority", "0.8")
])
let urlset = lib.element("urlset", [lib.attr("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")], [url1, url2])
let sitemap = lib.document(urlset)
Test.assert(String.startsWith(sitemap, "<?xml version=\"1.0\""), "sitemap has XML declaration")
Test.assert(String.contains(sitemap, "xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\""), "sitemap has namespace")
Test.assert(String.contains(sitemap, "<loc>https://example.com/</loc>"), "sitemap has first URL")
Test.assert(String.contains(sitemap, "<loc>https://example.com/about</loc>"), "sitemap has second URL")
}
// Build a form with various input types
fn test_build_form(): Unit with {Test} = {
let form = lib.element("form", [lib.attr("action", "/submit"), lib.attr("method", "post")], [
lib.selfClosing("input", [lib.attr("type", "text"), lib.attr("name", "username")]),
lib.selfClosing("input", [lib.attr("type", "password"), lib.attr("name", "pass")]),
lib.textElAttr("button", [lib.attr("type", "submit")], "Login")
])
let result = lib.render(form)
Test.assert(String.contains(result, "action=\"/submit\""), "form has action")
Test.assert(String.contains(result, "type=\"text\""), "has text input")
Test.assert(String.contains(result, "type=\"password\""), "has password input")
Test.assert(String.contains(result, "<button type=\"submit\">Login</button>"), "has submit button")
}
// Build nested table structure
fn test_build_table(): Unit with {Test} = {
let headerRow = lib.el("tr", [lib.textEl("th", "Name"), lib.textEl("th", "Age")])
let row1 = lib.el("tr", [lib.textEl("td", "Alice"), lib.textEl("td", "30")])
let row2 = lib.el("tr", [lib.textEl("td", "Bob"), lib.textEl("td", "25")])
let table = lib.el("table", [lib.el("thead", [headerRow]), lib.el("tbody", [row1, row2])])
let result = lib.render(table)
Test.assert(String.contains(result, "<th>Name</th>"), "has header Name")
Test.assert(String.contains(result, "<td>Alice</td>"), "has cell Alice")
Test.assert(String.contains(result, "<td>25</td>"), "has cell 25")
}
// documentWithDecl for custom processing instructions
fn test_custom_declaration(): Unit with {Test} = {
let decl = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<?xml-stylesheet type=\"text/xsl\" href=\"transform.xsl\"?>"
let root = lib.textEl("data", "value")
let result = lib.documentWithDecl(decl, root)
Test.assert(String.contains(result, "xml-stylesheet"), "has stylesheet PI")
Test.assert(String.contains(result, "<data>value</data>"), "has root element")
}
// Build SVG-like structure
fn test_build_svg(): Unit with {Test} = {
let svg = lib.element("svg", [lib.attr("xmlns", "http://www.w3.org/2000/svg"), lib.attr("width", "100"), lib.attr("height", "100")], [
lib.selfClosing("circle", [lib.attr("cx", "50"), lib.attr("cy", "50"), lib.attr("r", "40")]),
lib.selfClosing("rect", [lib.attr("x", "10"), lib.attr("y", "10"), lib.attr("width", "80"), lib.attr("height", "80")])
])
let result = lib.render(svg)
Test.assert(String.contains(result, "<svg xmlns=\"http://www.w3.org/2000/svg\""), "svg has namespace")
Test.assert(String.contains(result, "<circle"), "has circle")
Test.assert(String.contains(result, "<rect"), "has rect")
}
// Content with special characters throughout
fn test_special_chars_in_content(): Unit with {Test} = {
let node = lib.el("message", [
lib.textEl("from", "Alice & Bob"),
lib.textEl("subject", "Re: <Important>"),
lib.el("body", [lib.cdata("Use <html> & \"xml\" freely")])
])
let result = lib.render(node)
Test.assert(String.contains(result, "Alice &amp; Bob"), "from is escaped")
Test.assert(String.contains(result, "&lt;Important&gt;"), "subject is escaped")
Test.assert(String.contains(result, "<![CDATA[Use <html> & \"xml\" freely]]>"), "cdata preserves content")
}