微型框架集合
t (FLet.testing): 🧪 快速测试预期结果
t
是一个使用闭包和错误的简单测试框架。 您可以创建一个包含多个步骤、预期和断言的 suite
。 预期可用于 expect
一个或多个断言。 t
可用于在函数内部快速测试,以确保某些内容按预期工作。 如果需要,t
也可以用于单元测试。c (FLet.composition): 📦 使用转换和缓存的微型组合
c
是一个简单的组合框架。 您可以创建单向或双向的转换。 还有一个可以设置和解析值的缓存。 c
可用于任何地方来创建转换或与缓存交互。o (FLet.transput): 文件、URL 和控制台的输出和输入
o
是一个简单的框架,用于输出到文件、URL、控制台,甚至使用 UserNotifications 注册通知。 o
还可以从文件、URL 或控制台获取输入。 目前,o
可以在 macOS、iOS 和 watchOS 上使用。t.suite {
// Add an expectation that asserting true is true and that 2 is equal to 2
try t.expect {
try t.assert(true)
try t.assert(2, isEqualTo: 2)
}
// Add an assertion that asserting false is not true
try t.assert(notTrue: false)
// Add an assertion that "Hello" is not equal to "World"
try t.assert("Hello", isNotEqualTo: "World")
// Log a message
t.log("📣 Test Log Message")
// Log a t.error
t.log(error: t.error(description: "Mock Error"))
// Log any error
struct SomeError: Error { }
t.log(error: SomeError())
// Add an assertion to check if a value is nil
let someValue: String? = nil
try t.assert(isNil: someValue)
// Add an assertion to check if a value is not nil
let someOtherValue: String? = "💠"
try t.assert(isNotNil: someOtherValue)
}
try t.expect {
let someValue: String? = "Hello"
try t.assert(isNil: someValue)
}
try t.assert("Hello", isEqualTo: "World")
t.log("📣 Test Log Message")
// Assert suite is true
XCTAssert(
t.suite {
try t.assert(true)
}
)
// Assert expectation is true
XCTAssertNoThrow(
try t.expect("true is true and that 2 is equal to 2") {
try t.assert(true)
try t.assert(2, isEqualTo: 2)
}
)
// Assert is false
XCTAssertThrowsError(
try t.assert(false)
)
let cache = c.cache()
cache.set(value: Double.pi, forKey: "🥧")
let pi: Double = cache.get("🥧") ?? 0
try t.assert(pi, isEqualTo: .pi)
let resolvedValue: Double = cache.resolve("🥧")
try t.assert(resolvedValue, isEqualTo: .pi)
let someCache: Cache = ...
// Set the value of a Cache with any hashable key
c.set(value: someCache, forKey: "someCache")
// Get an optional Cache using any hashable key
let anotherCache: Cache? = c.get(0)
// Require that a Cache exist using a `.get` with a force unwrap
let requiredCache: Cache = c.resolve(0)
let transformer = c.transformer(
from: { string in Int(string) },
to: { int in "\(String(describing: int))" }
)
let string = transformer.to(3)
let int = transformer.from("3")
try t.assert(transformer.to(int), isEqualTo: string)
o.console.out("Value to print: ", terminator: "") // (oTests/oTests.swift@7) [testExample()]: Value to print:
o.console.out(o.console.in()) // Type in "???"; (oTests/oTests.swift@8) [testExample()]: Optional("???")
let filename: String = ...
// Write the value 4, an Int, to the file named `filename`. Files using o.file are base64Encoded.
try o.file.out(4, filename: filename)
// Asserts
XCTAssertNoThrow(try o.file.in(filename: filename) as Int)
XCTAssertEqual(try? o.file.in(filename: filename), 4)
// Delete the File
try o.file.delete(filename: filename)
// Assert deletion
XCTAssertThrowsError(try o.file.in(filename: filename) as Int)
struct Post: Codable {
let userId: Int
let id: Int
let title: String
let body: String
}
// GET Request
o.url.in(
url: URL(string: "api/posts")!,
successHandler: { (posts: [Post], response) in
print(posts)
}
)
// POST Request
let post = Post(userId: 1, id: 1, title: "First!", body: "")
try o.url.out(
url: URL(string: "api/posts/\(post.id)")!,
value: post,
successHandler: { data, response in
print(response)
}
)
// Request Notification Authorization
o.notification.requestAuthorization()
// Set UNUserNotificationCenter.current's delegate to `o.notification.delegate`
o.notification.registerDelegate()
// Schedule a Notification
o.notification.post(
title: "Hello!",
subtitle: "o.notification",
body: "Woo Hoo!",
trigger: UNTimeIntervalNotificationTrigger(
timeInterval: 3,
repeats: false
)
)
public typealias __ = FLet