mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Keep calm: remember that the standard library has many more public exports than the average target, and that this contains ALL of them at once. I also deliberately tried to tag nearly every top-level decl, even if that was just to explicitly mark things @internal, to make sure I didn't miss something. This does export more than we might want to, mostly for protocol conformance reasons, along with our simple-but-limiting typealias rule. I tried to also mark things private where possible, but it's really going to be up to the standard library owners to get this right. This is also only validated against top-level access control; I haven't fully tested against member-level access control yet, and none of our semantic restrictions are in place. Along the way I also noticed bits of stdlib cruft; to keep this patch understandable, I didn't change any of them. Swift SVN r19145
281 lines
7.6 KiB
Swift
281 lines
7.6 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
|
|
|
|
@public 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()
|
|
}
|
|
}
|
|
|
|
@public 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()
|
|
}
|
|
}
|
|
|
|
@public 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
|
|
@public 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')]:
|
|
|
|
@public 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']:
|
|
|
|
@public 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()
|
|
}
|
|
}
|
|
|
|
@public 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
|
|
|
|
@public 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 = ""
|
|
}
|
|
|
|
@public func assertionSuccess() -> AssertionResult {
|
|
return AssertionResult(isPass: true)
|
|
}
|
|
|
|
@public func assertionFailure() -> AssertionResult {
|
|
return AssertionResult(isPass: false)
|
|
}
|
|
|
|
%for BoolType in ['Bool', 'AssertionResult']:
|
|
|
|
@public 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()
|
|
}
|
|
}
|
|
|
|
@public 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
|
|
|
|
@public 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()
|
|
}
|
|
}
|
|
|
|
@public 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()
|
|
}
|
|
}
|
|
|
|
@public struct TestCase {
|
|
@public init(_ name: String) {
|
|
self.name = name
|
|
}
|
|
|
|
@public mutating func test(name: String, testFunction: () -> ()) {
|
|
_tests.append(_Test(name: name, code: testFunction))
|
|
}
|
|
|
|
@public 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.
|
|
|
|
@public 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)
|
|
}
|
|
|
|
@public 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)
|
|
}
|
|
|