Add tests

This commit is contained in:
Shadowfacts 2022-12-10 15:36:48 -05:00
parent 9aa089fd09
commit 798644bf8b
3 changed files with 38 additions and 1 deletions

View File

@ -1,4 +1,4 @@
// swift-tools-version: 5.6 // swift-tools-version: 5.7
// The swift-tools-version declares the minimum version of Swift required to build this package. // The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription import PackageDescription
@ -7,6 +7,7 @@ let package = Package(
name: "highlight-swift", name: "highlight-swift",
platforms: [ platforms: [
.macOS(.v11), .macOS(.v11),
.iOS(.v16),
], ],
products: [ products: [
// Products define the executables and libraries a package produces, and make them visible to other packages. // Products define the executables and libraries a package produces, and make them visible to other packages.
@ -25,5 +26,6 @@ let package = Package(
.target( .target(
name: "highlight-swift", name: "highlight-swift",
dependencies: ["Splash"]), dependencies: ["Splash"]),
.testTarget(name: "highlight-swift-tests", dependencies: ["highlight-swift"])
] ]
) )

View File

@ -0,0 +1,5 @@
import XCTest
XCTMain([
testCase(HighlightTests.allTests),
])

View File

@ -0,0 +1,30 @@
//
// HighlightTests.swift
//
//
// Created by Shadowfacts on 12/10/22.
//
import XCTest
@testable import highlight_swift
class HighlightTests: XCTestCase {
static var allTests = [
("testHighlight", testHighlight),
]
func testHighlight() {
var code = "1+1"
var len: UInt64 = 0
let result = code.withUTF8 { codePtr in
withUnsafeMutablePointer(to: &len) { lenPtr in
highlight(codePtr: UnsafeRawPointer(codePtr.baseAddress!), codeLen: UInt64(codePtr.count), htmlLenPtr: lenPtr)
}
}
let start = UnsafePointer(result.assumingMemoryBound(to: UInt8.self))
let htmlPtr = UnsafeBufferPointer(start: start, count: Int(len))
let data = Data(buffer: htmlPtr)
let expected = "<span class=\"hl-num\">1</span>+<span class=\"hl-num\">1</span>"
XCTAssertEqual(String(data: data, encoding: .utf8), expected)
}
}