2

是否有适用于 Swift 代码的流畅匹配 API?领先的 Objective-C 匹配器候选者似乎是 OCHamcrest 和 Expecta,它们都依赖于复杂的宏(根据文档)不适用于 Swift 代码,例如

#define HC_assertThat(actual, matcher)  \
    HC_assertThatWithLocation(self, actual, matcher, __FILE__, __LINE__)

(OCHamcrest)

#define EXP_expect(actual) _EXP_expect(self, __LINE__, __FILE__, ^id{ return EXPObjectify((actual)); })

(预期)

是否有另一种适用于 Swift 的替代方案,或者以某种方式包装其中一个或另一个以便它们可以与 Swift 一起使用?


ETA:对于未来的读者——我查看了SwiftHamcrest(根据Jon Reid 的回答),但目前我已经选择了Quick/Nimble

4

2 回答 2

2

复杂的预处理器宏不会被翻译成它们的 Swift 替代品。Apple 有一个讨论 Swift 的博客,其中描述了他们是如何制作 assert 函数的

第一个宏很容易变成一个 Swift 函数

#define HC_assertThat(actual, matcher)  \
    HC_assertThatWithLocation(self, actual, matcher, __FILE__, __LINE__)

在斯威夫特:

func HC_assertThat(caller: AnyObject, actual: String, matcher: String, file: String = __FILE__, line: UWord = __LINE__) {
    HC_assertThatWithLocation(caller, actual, matcher, file.fileSystemRepresentation, Int32(line))
}

#define EXP_expect(actual) _EXP_expect(self, __LINE__, __FILE__, ^id{ return EXPObjectify((actual)); })

在斯威夫特:

func EXP_expect(caller: AnyObject, actual: String, line: UWord = __LINE__, file: String = __FILE__) {
    _EXP_expect(caller, Int32(line), file.fileSystemRepresentation, ({
        return EXPObjectify(caller)
    })
}

请注意,我猜测您想要传递给预处理器函数的内容。

于 2014-10-09T20:37:25.903 回答
1

https://github.com/nschum/SwiftHamcrest是 Hamcrest 的 Swift 原生实现

于 2014-10-19T05:42:11.640 回答