mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
281 lines
7.4 KiB
Swift
281 lines
7.4 KiB
Swift
%# -*- mode: swift -*-
|
|
|
|
%# Ignore the following admonition; it applies to the resulting .swift file only
|
|
//// Automatically Generated From StdlibUnittest.swift.gyb. Do Not Edit Directly
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See http://swift.org/LICENSE.txt for license information
|
|
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import Darwin
|
|
|
|
var _anyExpectFailed = false
|
|
|
|
func expectEqual<T : Equatable>(
|
|
expected: T, actual: T,
|
|
file: String = __FILE__, line: UWord = __LINE__
|
|
) {
|
|
if expected != actual {
|
|
_anyExpectFailed = true
|
|
println("check failed at \(file), line \(line)")
|
|
println("expected: \"\(expected)\" (of type \(_stdlib_getTypeName(expected)))")
|
|
println("actual: \"\(actual)\" (of type \(_stdlib_getTypeName(expected)))")
|
|
println()
|
|
}
|
|
}
|
|
|
|
func expectEqual<T : Equatable>(
|
|
expected: T, actual: T,
|
|
collectMoreInfo: () -> String,
|
|
file: String = __FILE__, line: UWord = __LINE__
|
|
) {
|
|
if expected != actual {
|
|
_anyExpectFailed = true
|
|
println("check failed at \(file), line \(line)")
|
|
println("expected: \"\(expected)\" (of type \(_stdlib_getTypeName(expected)))")
|
|
println("actual: \"\(actual)\" (of type \(_stdlib_getTypeName(expected)))")
|
|
println(collectMoreInfo())
|
|
println()
|
|
}
|
|
}
|
|
|
|
func expectNotEqual<T : Equatable>(
|
|
expected: T, actual: T,
|
|
file: String = __FILE__, line: UWord = __LINE__
|
|
) {
|
|
if expected == actual {
|
|
_anyExpectFailed = true
|
|
println("check failed at \(file), line \(line)")
|
|
println("unexpected value: \"\(actual)\" (of type \(_stdlib_getTypeName(actual)))")
|
|
println()
|
|
}
|
|
}
|
|
|
|
// Can not write a sane set of overloads using generics because of:
|
|
// <rdar://problem/17015923> Array->NSArray implicit conversion insanity
|
|
func expectOptionalEqual<T : Equatable>(
|
|
expected: T, actual: T?,
|
|
file: String = __FILE__, line: UWord = __LINE__
|
|
) {
|
|
if !actual || expected != actual! {
|
|
_anyExpectFailed = true
|
|
println("check failed at \(file), line \(line)")
|
|
println("expected: \"\(expected)\" (of type \(_stdlib_getTypeName(expected)))")
|
|
println("actual: \"\(actual)\" (of type \(_stdlib_getTypeName(actual)))")
|
|
println()
|
|
}
|
|
}
|
|
|
|
// Array<T> is not Equatable if T is. Provide additional overloads.
|
|
// Same for Dictionary.
|
|
%for (Generic, EquatableType) in [
|
|
% ('<T : Equatable>', 'ContiguousArray<T>'),
|
|
% ('<T : Equatable>', 'Slice<T>'),
|
|
% ('<T : Equatable>', 'Array<T>'),
|
|
% ('<T, U : Equatable>', 'Dictionary<T, U>'),
|
|
% ('<T : ForwardIndex>', 'T')]:
|
|
|
|
func expectEqual${Generic}(
|
|
expected: ${EquatableType}, actual: ${EquatableType},
|
|
file: String = __FILE__, line: UWord = __LINE__
|
|
) {
|
|
if expected != actual {
|
|
_anyExpectFailed = true
|
|
println("check failed at \(file), line \(line)")
|
|
println("expected: \"\(expected)\" (of type \(_stdlib_getTypeName(expected)))")
|
|
println("actual: \"\(actual)\" (of type \(_stdlib_getTypeName(actual)))")
|
|
println()
|
|
}
|
|
}
|
|
|
|
func _expectNotEqual${Generic}(
|
|
expected: ${EquatableType}, actual: ${EquatableType},
|
|
file: String = __FILE__, line: UWord = __LINE__
|
|
) {
|
|
if expected == actual {
|
|
_anyExpectFailed = true
|
|
println("check failed at \(file), line \(line)")
|
|
println("unexpected value: \"\(actual)\" (of type \(_stdlib_getTypeName(actual)))")
|
|
println()
|
|
}
|
|
}
|
|
|
|
%end
|
|
|
|
%for ComparableType in ['Int']:
|
|
|
|
func expectLE(
|
|
expected: ${ComparableType}, actual: ${ComparableType},
|
|
file: String = __FILE__, line: UWord = __LINE__
|
|
) {
|
|
if !(expected <= actual) {
|
|
_anyExpectFailed = true
|
|
println("check failed at \(file), line \(line)")
|
|
println("expected: \"\(expected)\"")
|
|
println("actual: \"\(actual)\"")
|
|
println()
|
|
}
|
|
}
|
|
|
|
func expectGE(
|
|
expected: ${ComparableType}, actual: ${ComparableType},
|
|
file: String = __FILE__, line: UWord = __LINE__
|
|
) {
|
|
if !(expected >= actual) {
|
|
_anyExpectFailed = true
|
|
println("check failed at \(file), line \(line)")
|
|
println("expected: \"\(expected)\"")
|
|
println("actual: \"\(actual)\"")
|
|
println()
|
|
}
|
|
}
|
|
|
|
%end
|
|
|
|
struct AssertionResult : Printable, LogicValue {
|
|
init(isPass: Bool) {
|
|
self._isPass = isPass
|
|
}
|
|
|
|
func getLogicValue() -> Bool {
|
|
return _isPass
|
|
}
|
|
|
|
func withDescription(description: String) -> AssertionResult {
|
|
var result = self
|
|
result.description += description
|
|
return result
|
|
}
|
|
|
|
let _isPass: Bool
|
|
var description: String = ""
|
|
}
|
|
|
|
func assertionSuccess() -> AssertionResult {
|
|
return AssertionResult(isPass: true)
|
|
}
|
|
|
|
func assertionFailure() -> AssertionResult {
|
|
return AssertionResult(isPass: false)
|
|
}
|
|
|
|
%for BoolType in ['Bool', 'AssertionResult']:
|
|
|
|
func expectTrue(
|
|
actual: ${BoolType},
|
|
file: String = __FILE__, line: UWord = __LINE__
|
|
) {
|
|
if !actual {
|
|
_anyExpectFailed = true
|
|
println("check failed at \(file), line \(line)")
|
|
println("expected: true")
|
|
println("actual: \(actual)")
|
|
println()
|
|
}
|
|
}
|
|
|
|
func expectFalse(
|
|
actual: ${BoolType},
|
|
file: String = __FILE__, line: UWord = __LINE__
|
|
) {
|
|
if actual {
|
|
_anyExpectFailed = true
|
|
println("check failed at \(file), line \(line)")
|
|
println("expected: false")
|
|
println("actual: \(actual)")
|
|
println()
|
|
}
|
|
}
|
|
|
|
%end
|
|
|
|
func expectEmpty<T>(
|
|
value: Optional<T>,
|
|
file: String = __FILE__, line: UWord = __LINE__
|
|
) {
|
|
if value {
|
|
_anyExpectFailed = true
|
|
println("check failed at \(file), line \(line)")
|
|
println("expected optional to be empty")
|
|
println("actual: \"\(value)\"")
|
|
println()
|
|
}
|
|
}
|
|
|
|
func expectNotEmpty<T>(
|
|
value: Optional<T>,
|
|
file: String = __FILE__, line: UWord = __LINE__
|
|
) {
|
|
if !value {
|
|
_anyExpectFailed = true
|
|
println("check failed at \(file), line \(line)")
|
|
println("expected optional to be non-empty")
|
|
println()
|
|
}
|
|
}
|
|
|
|
struct TestCase {
|
|
init(_ name: String) {
|
|
self.name = name
|
|
}
|
|
|
|
mutating func test(name: String, testFunction: () -> ()) {
|
|
_tests.append(_Test(name: name, code: testFunction))
|
|
}
|
|
|
|
mutating func run() {
|
|
var anyTestFailed = false
|
|
for t in _tests {
|
|
var fullTestName = "\(name).\(t.name)"
|
|
println("[ RUN ] \(fullTestName)")
|
|
_anyExpectFailed = false
|
|
t.code()
|
|
if _anyExpectFailed {
|
|
anyTestFailed = true
|
|
println("[ FAIL ] \(fullTestName)")
|
|
} else {
|
|
println("[ OK ] \(fullTestName)")
|
|
}
|
|
}
|
|
if anyTestFailed {
|
|
println("Some tests failed, aborting")
|
|
abort()
|
|
} else {
|
|
println("\(name): All tests passed")
|
|
}
|
|
}
|
|
|
|
struct _Test {
|
|
var name: String
|
|
var code: () -> ()
|
|
}
|
|
|
|
var name: String
|
|
var _tests: _Test[] = []
|
|
}
|
|
|
|
// These APIs don't really belong in a unittesting library, but are useful
|
|
// in tests, and stdlib does not have such facilities yet.
|
|
|
|
func asHex(a: UInt8[]) -> String {
|
|
// FIXME: commented out because of:
|
|
// <rdar://problem/17399536> SIL devirt crash: Assertion failed: (!empty())
|
|
// return "[ " + ", ".join(a.map { "0x" + String($0, radix: 16) }) + " ]"
|
|
return toString(a)
|
|
}
|
|
|
|
func asHex(a: UInt32[]) -> String {
|
|
// FIXME: commented out because of:
|
|
// <rdar://problem/17399536> SIL devirt crash: Assertion failed: (!empty())
|
|
// return "[ " + ", ".join(a.map { "0x" + String($0, radix: 16) }) + " ]"
|
|
return toString(a)
|
|
}
|
|
|