92 lines
2.4 KiB
Swift
92 lines
2.4 KiB
Swift
//
|
|
// Test.swift
|
|
// HRW
|
|
//
|
|
// Created by Isaac Paul on 9/16/25.
|
|
// Non-commercial license, see LICENSE.MD in the project root for details
|
|
//
|
|
|
|
import XCTest
|
|
@testable import HRW
|
|
|
|
|
|
let test = """
|
|
<!DOCTYPE html>
|
|
<html><body>
|
|
<h1>My First Heading</h1>
|
|
|
|
<p>My first paragraph.</p>
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
let test2 = """
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>Unload Event Example</title>
|
|
</head>
|
|
<body onunload="myFunction()">
|
|
|
|
<h1>Welcome to my page!</h1>
|
|
<p>This is some content within the body.</p>
|
|
<textarea id='output' style='width: 100%;' rows='10' disabled></textarea>
|
|
|
|
<script>
|
|
window.addEventListener('unload', function() {
|
|
console.log('Page is unloading. HTML Body Content:');
|
|
console.log(document.body.outerHTML);
|
|
});
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
public extension HTMLNode {
|
|
func expectChild<T>(_ type:T.Type, _ index:Int) throws -> T {
|
|
if (children.count == 0) {
|
|
throw AppError("No children")
|
|
}
|
|
if (index >= children.count) {
|
|
throw AppError("No child at index \(index)")
|
|
}
|
|
let child = children[index]
|
|
guard let result = child as? T else {
|
|
throw AppError("Child is not correct expected type of \(String(describing: type))")
|
|
}
|
|
return result
|
|
}
|
|
}
|
|
|
|
final class HRWTests: XCTestCase {
|
|
|
|
func testExample() throws {
|
|
|
|
guard let xmlReader = XMLParser(str: test) else { throw EmptyStringError() }
|
|
let rootNodes = try xmlReader.readObjects()
|
|
let htmlRoot = rootNodes[2]
|
|
let cType = htmlRoot as! Html
|
|
let body = try cType.expectChild(Body.self, 0)
|
|
let h1 = try body.expectChild(H1.self, 1)
|
|
let text = try h1.expectChild(HTMLText.self, 0)
|
|
|
|
XCTAssertEqual(text.content, "My First Heading")
|
|
print(htmlRoot.toString())
|
|
}
|
|
|
|
func testSerialization() throws {
|
|
guard let xmlReader = XMLParser(str: test2) else { throw EmptyStringError() }
|
|
let rootNodes = try xmlReader.readObjects()
|
|
let htmlRoot = rootNodes[2]
|
|
print(htmlRoot.toString())
|
|
}
|
|
|
|
func testBindingGenerator() throws {
|
|
IHtmlNodeContainerUtility.sharedInstance.defaultBaseDir = "/Users/isaacpaul/Projects/swift-projects/HRW/Tests/HRWTests"
|
|
let idk = try Example()
|
|
print(idk.error_container.renderAttributes())
|
|
idk.rootNode.accept_charset = nil
|
|
}
|
|
}
|