Add unit tests

This commit is contained in:
2025-10-25 18:56:55 +03:00
parent ddc47abdde
commit bbb7f14650
38 changed files with 1051 additions and 526 deletions

View File

@@ -0,0 +1,82 @@
import Foundation
import Testing
import DataLiteCore
struct FunctionRegexpTests {
@Test func metadata() {
#expect(Regexp.argc == 2)
#expect(Regexp.name == "REGEXP")
#expect(Regexp.options == [.deterministic, .innocuous])
}
@Test func invalidArguments() {
let arguments: Arguments = [.int(1), .text("value")]
#expect(
performing: {
try Regexp.invoke(args: arguments)
},
throws: {
switch $0 {
case Regexp.Error.invalidArguments:
return true
default:
return false
}
}
)
}
@Test func invalidPattern() {
let arguments: Arguments = [.text("("), .text("value")]
#expect(
performing: {
try Regexp.invoke(args: arguments)
},
throws: {
switch $0 {
case Regexp.Error.regexError:
return true
default:
return false
}
}
)
}
@Test func matchesPattern() throws {
let arguments: Arguments = [.text("foo.*"), .text("foobar")]
#expect(try Regexp.invoke(args: arguments) as? Bool == true)
}
@Test func doesNotMatchPattern() throws {
let arguments: Arguments = [.text("bar.*"), .text("foobar")]
#expect(try Regexp.invoke(args: arguments) as? Bool == false)
}
}
private extension FunctionRegexpTests {
typealias Regexp = Function.Regexp
struct Arguments: ArgumentsProtocol, ExpressibleByArrayLiteral {
private let values: [SQLiteValue]
var startIndex: Int { values.startIndex }
var endIndex: Int { values.endIndex }
init(_ values: [SQLiteValue]) {
self.values = values
}
init(arrayLiteral elements: SQLiteValue...) {
self.values = elements
}
subscript(index: Int) -> SQLiteValue {
values[index]
}
func index(after i: Int) -> Int {
values.index(after: i)
}
}
}