From c3670638236a4d58b835b993dff94beda52ab622 Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Sat, 17 Aug 2024 20:58:59 -0600 Subject: [PATCH] Tests: Add first test for LibWeb Swift bindings Just to sanity check that we can import the library, and that it at least interprets the generated enumeration values properly, let's do some simple testing of the swift integration. --- Tests/LibWeb/CMakeLists.txt | 7 ++++ Tests/LibWeb/TestLibWebSwiftBindings.swift | 47 ++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 Tests/LibWeb/TestLibWebSwiftBindings.swift diff --git a/Tests/LibWeb/CMakeLists.txt b/Tests/LibWeb/CMakeLists.txt index dad1c094b86..e6430c26581 100644 --- a/Tests/LibWeb/CMakeLists.txt +++ b/Tests/LibWeb/CMakeLists.txt @@ -14,3 +14,10 @@ foreach(source IN LISTS TEST_SOURCES) endforeach() target_link_libraries(TestFetchURL PRIVATE LibURL) + +if (ENABLE_SWIFT) + add_executable(TestLibWebSwiftBindings TestLibWebSwiftBindings.swift) + target_link_libraries(TestLibWebSwiftBindings PRIVATE AK LibWeb) + target_compile_options(TestLibWebSwiftBindings PRIVATE -parse-as-library) + add_test(NAME TestLibWebSwiftBindings COMMAND TestLibWebSwiftBindings) +endif() diff --git a/Tests/LibWeb/TestLibWebSwiftBindings.swift b/Tests/LibWeb/TestLibWebSwiftBindings.swift new file mode 100644 index 00000000000..802af299ecc --- /dev/null +++ b/Tests/LibWeb/TestLibWebSwiftBindings.swift @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2024, Andrew Kaster + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +import AK +import LibWeb +import Foundation + +class StandardError: TextOutputStream { + func write(_ string: Swift.String) { + try! FileHandle.standardError.write(contentsOf: Data(string.utf8)) + } +} + +@main +struct TestLibWebSwiftBindings { + + static func testEnumsAreBound() { + var standardError = StandardError() + print("Testing LibWeb enum types...", to: &standardError) + + print("Web.DOM.NodeType.ELEMENT_NODE == \(Web.DOM.NodeType.ELEMENT_NODE)", to: &standardError) + precondition(Web.DOM.NodeType.ELEMENT_NODE.rawValue == 1) + + print("Web.Bindings.NavigationType.Push == \(Web.Bindings.NavigationType.Push)", to: &standardError) + precondition(Web.Bindings.NavigationType.Push.rawValue == 0) + + let end = Web.Bindings.idl_enum_to_string(Web.Bindings.ScrollLogicalPosition.End) + let end_view = end.__bytes_as_string_viewUnsafe().bytes(); + let end_string = Swift.String(bytes: end_view, encoding: .utf8)! + + print("Web.Bindings.idl_enum_to_string(Web.Bindings.ScrollLogicalPosition.End) == \(end_string)", to: &standardError) + precondition(end_string == "end") + + print("LibWeb enum types pass", to: &standardError) + } + + static func main() { + var standardError = StandardError() + print("Starting test suite...", to: &standardError) + testEnumsAreBound() + + print("All tests pass", to: &standardError) + } +} \ No newline at end of file