PureXML/Tests/XPathContextTests.swift
Mihaela Mihaljevic 83e6e27d12 style(format): adopt --importgrouping testable-bottom from canonical rules-swift
Group @testable imports at the bottom, per public/rules-swift's swiftformat. A
behavior-preserving import-ordering reformat across 252 files (mostly tests).
--trailingclosures was deliberately not adopted (cosmetic, and swiftformat flags
it as caution-worthy).
2026-06-18 20:33:07 +02:00

40 lines
1.7 KiB
Swift

import Testing
@testable import PureXML
@Suite("XPath evaluation context")
struct XPathContextTests {
@Test("A relative query evaluates against an explicit context node")
func test_contextNode() throws {
let tree = try PureXML.parseTree("<r><a><b>deep</b></a><a><b>other</b></a></r>")
let firstA = tree.firstChild?.firstChild
let value = try #require(firstA)
let result = try PureXML.XPath.Query("b").value(at: value)
#expect(result.string == "deep")
}
@Test("position() and last() reflect the supplied context")
func test_positionAndSize() throws {
let tree = try PureXML.parseTree("<r/>")
let node = try #require(tree.firstChild)
#expect(try PureXML.XPath.Query("position()").value(at: node, position: 3, size: 7).number == 3)
#expect(try PureXML.XPath.Query("last()").value(at: node, position: 3, size: 7).number == 7)
}
@Test("Variable bindings are visible to the evaluated expression")
func test_variableBindings() throws {
let tree = try PureXML.parseTree("<r/>")
let node = try #require(tree.firstChild)
let value = try PureXML.XPath.Query("$x * 2").value(at: node, variables: ["x": .number(21)])
#expect(value.number == 42)
}
@Test("Upward axes resolve from the context node's place in the tree")
func test_upwardFromContext() throws {
let tree = try PureXML.parseTree("<r id=\"root\"><a><b/></a></r>")
let deepB = tree.firstChild?.firstChild?.firstChild
let node = try #require(deepB)
let result = try PureXML.XPath.Query("ancestor::r/@id").value(at: node)
#expect(result.string == "root")
}
}