mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-05-17 10:30:35 +00:00
Enable noForceUnwrapInTests rule by default
This commit is contained in:
committed by
Cal Stephens
parent
079939f88e
commit
7aaff1cab5
@@ -38,6 +38,5 @@
|
||||
# rules
|
||||
|
||||
--enable isEmpty
|
||||
# --enable noForceUnwrapInTests
|
||||
# --enable noGuardInTests
|
||||
--enable preferFinalClasses
|
||||
|
||||
@@ -40,6 +40,7 @@
|
||||
* [modifierOrder](#modifierOrder)
|
||||
* [modifiersOnSameLine](#modifiersOnSameLine)
|
||||
* [noForceTryInTests](#noForceTryInTests)
|
||||
* [noForceUnwrapInTests](#noForceUnwrapInTests)
|
||||
* [numberFormatting](#numberFormatting)
|
||||
* [opaqueGenericParameters](#opaqueGenericParameters)
|
||||
* [preferCountWhere](#preferCountWhere)
|
||||
@@ -117,7 +118,6 @@
|
||||
* [isEmpty](#isEmpty)
|
||||
* [markTypes](#markTypes)
|
||||
* [noExplicitOwnership](#noExplicitOwnership)
|
||||
* [noForceUnwrapInTests](#noForceUnwrapInTests)
|
||||
* [noGuardInTests](#noGuardInTests)
|
||||
* [organizeDeclarations](#organizeDeclarations)
|
||||
* [preferFinalClasses](#preferFinalClasses)
|
||||
|
||||
@@ -391,11 +391,11 @@ final class ConsumerTests: XCTestCase {
|
||||
XCTAssertEqual(Consumer<String>.string("Thanks 👍").description, "'Thanks 👍'")
|
||||
}
|
||||
|
||||
func testCharacterDescription() {
|
||||
func testCharacterDescription() throws {
|
||||
XCTAssertEqual(Consumer<String>.character("!").description, "'!'")
|
||||
XCTAssertEqual(Consumer<String>.character(in: "A" ... "F").description, "'A' – 'F'")
|
||||
XCTAssertEqual(Consumer<String>
|
||||
.character(in: UnicodeScalar(11)! ... UnicodeScalar(17)!).description, "U+000B – U+0011")
|
||||
XCTAssertEqual(try Consumer<String>
|
||||
.character(in: XCTUnwrap(UnicodeScalar(11)) ... UnicodeScalar(17)!).description, "U+000B – U+0011")
|
||||
XCTAssertEqual(Consumer<String>.character(in: "👍" ... "👍").description, "'👍'")
|
||||
XCTAssertEqual(Consumer<String>.character(in: "12").description, "'1' or '2'")
|
||||
XCTAssertEqual(Consumer<String>.character(in: "1356").description, "'1', '3', '5' or '6'")
|
||||
@@ -808,6 +808,6 @@ final class ConsumerTests: XCTestCase {
|
||||
|
||||
func testLabelledListTransform() {
|
||||
let parser: Consumer<String> = .oneOrMore("foo")
|
||||
XCTAssertEqual(try parser.match("foofoo").transform { $1 } as! [String], ["foo", "foo"])
|
||||
XCTAssertEqual(try parser.match("foofoo").transform { $1 } as? [String], ["foo", "foo"])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,8 +95,8 @@ final class PerformanceTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testJSONSerialization() {
|
||||
let data = json.data(using: .utf8)!
|
||||
func testJSONSerialization() throws {
|
||||
let data = try XCTUnwrap(json.data(using: .utf8))
|
||||
measure {
|
||||
_ = try! JSONSerialization.jsonObject(with: data, options: [])
|
||||
}
|
||||
|
||||
@@ -145,11 +145,11 @@ final class TransformTests: XCTestCase {
|
||||
XCTAssert(plane.translated(by: offset).isEqual(to: expected))
|
||||
}
|
||||
|
||||
func testRotatePlane() {
|
||||
func testRotatePlane() throws {
|
||||
let normal = Vector(0.5, 1, 0.5).normalized()
|
||||
let position = Vector(10, 5, -3)
|
||||
let plane = Plane(unchecked: normal, pointOnPlane: position)
|
||||
let rotation = Rotation(axis: Vector(12, 3, 4).normalized(), radians: 0.2)!
|
||||
let rotation = try XCTUnwrap(Rotation(axis: Vector(12, 3, 4).normalized(), radians: 0.2))
|
||||
let rotatedNormal = normal.rotated(by: rotation)
|
||||
let rotatedPosition = position.rotated(by: rotation)
|
||||
let expected = Plane(unchecked: rotatedNormal, pointOnPlane: rotatedPosition)
|
||||
@@ -175,19 +175,19 @@ final class TransformTests: XCTestCase {
|
||||
XCTAssert(plane.scaled(by: scale).isEqual(to: expected))
|
||||
}
|
||||
|
||||
func testTransformPlane() {
|
||||
func testTransformPlane() throws {
|
||||
let path = Path(unchecked: [
|
||||
.point(1, 2, 3),
|
||||
.point(7, -2, 12),
|
||||
.point(-2, 7, 14),
|
||||
])
|
||||
let plane = path.plane!
|
||||
let transform = Transform(
|
||||
let plane = try XCTUnwrap(path.plane)
|
||||
let transform = try Transform(
|
||||
offset: Vector(-7, 3, 4.5),
|
||||
rotation: Rotation(axis: Vector(11, 3, -1).normalized(), radians: 1.3)!,
|
||||
rotation: XCTUnwrap(Rotation(axis: Vector(11, 3, -1).normalized(), radians: 1.3)),
|
||||
scale: Vector(7, 2.0, 0.3)
|
||||
)
|
||||
let expected = path.transformed(by: transform).plane!
|
||||
let expected = try XCTUnwrap(path.transformed(by: transform).plane)
|
||||
XCTAssert(plane.transformed(by: transform).isEqual(to: expected))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -854,9 +854,9 @@ final class AnyExpressionTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testSubscriptStringWithHalfOpenIndexRange() {
|
||||
let expression = AnyExpression("'foo'[range]", constants: [
|
||||
"range": "foo".range(of: "fo")!,
|
||||
func testSubscriptStringWithHalfOpenIndexRange() throws {
|
||||
let expression = try AnyExpression("'foo'[range]", constants: [
|
||||
"range": XCTUnwrap("foo".range(of: "fo")),
|
||||
])
|
||||
XCTAssertEqual(try expression.evaluate(), "fo")
|
||||
}
|
||||
@@ -868,84 +868,84 @@ final class AnyExpressionTests: XCTestCase {
|
||||
XCTAssertEqual(try expression.evaluate(), "fo")
|
||||
}
|
||||
|
||||
func testSubscriptSubstringWithHalfOpenIndexRange() {
|
||||
let expression = AnyExpression("foo[range]", constants: [
|
||||
func testSubscriptSubstringWithHalfOpenIndexRange() throws {
|
||||
let expression = try AnyExpression("foo[range]", constants: [
|
||||
"foo": Substring("foo"),
|
||||
"range": "foo".range(of: "fo")!,
|
||||
"range": XCTUnwrap("foo".range(of: "fo")),
|
||||
])
|
||||
XCTAssertEqual(try expression.evaluate(), "fo")
|
||||
}
|
||||
|
||||
func testSubscriptNSStringWithHalfOpenIndexRange() {
|
||||
let expression = AnyExpression("foo[range]", constants: [
|
||||
func testSubscriptNSStringWithHalfOpenIndexRange() throws {
|
||||
let expression = try AnyExpression("foo[range]", constants: [
|
||||
"foo": "foo" as NSString,
|
||||
"range": "foo".range(of: "fo")!,
|
||||
"range": XCTUnwrap("foo".range(of: "fo")),
|
||||
])
|
||||
XCTAssertEqual(try expression.evaluate(), "fo")
|
||||
}
|
||||
|
||||
func testSubscriptStringWithInvalidHalfOpenIndexRange() {
|
||||
let expression = AnyExpression("'foo'[range]", constants: [
|
||||
"range": "foobar".range(of: "bar")!,
|
||||
func testSubscriptStringWithInvalidHalfOpenIndexRange() throws {
|
||||
let expression = try AnyExpression("'foo'[range]", constants: [
|
||||
"range": XCTUnwrap("foobar".range(of: "bar")),
|
||||
])
|
||||
XCTAssertThrowsError(try expression.evaluate() as Any) { error in
|
||||
XCTAssertEqual(error as? Expression.Error, .stringBounds("foo", 3))
|
||||
}
|
||||
}
|
||||
|
||||
func testSubscriptStringWithInvalidClosedIndexRange() {
|
||||
let expression = AnyExpression("'foo'[range]", constants: [
|
||||
"range": "foobar".range(of: "bar")!.lowerBound ... "foobar".endIndex,
|
||||
func testSubscriptStringWithInvalidClosedIndexRange() throws {
|
||||
let expression = try AnyExpression("'foo'[range]", constants: [
|
||||
"range": XCTUnwrap("foobar".range(of: "bar")?.lowerBound) ... "foobar".endIndex,
|
||||
])
|
||||
XCTAssertThrowsError(try expression.evaluate() as Any) { error in
|
||||
XCTAssertEqual(error as? Expression.Error, .stringBounds("foo", 3))
|
||||
}
|
||||
}
|
||||
|
||||
func testSubscriptSubstringWithInvalidHalfOpenIndexRange() {
|
||||
let expression = AnyExpression("foo[range]", constants: [
|
||||
func testSubscriptSubstringWithInvalidHalfOpenIndexRange() throws {
|
||||
let expression = try AnyExpression("foo[range]", constants: [
|
||||
"foo": "barfoo".suffix(3),
|
||||
"range": "barfoo".range(of: "bar")!,
|
||||
"range": XCTUnwrap("barfoo".range(of: "bar")),
|
||||
])
|
||||
XCTAssertThrowsError(try expression.evaluate() as Any) { error in
|
||||
XCTAssertEqual(error as? Expression.Error, .stringBounds("foo", -3))
|
||||
}
|
||||
}
|
||||
|
||||
func testSubscriptNSStringWithInvalidHalfOpenIndexRangeLowerBound() {
|
||||
let expression = AnyExpression("foo[range]", constants: [
|
||||
func testSubscriptNSStringWithInvalidHalfOpenIndexRangeLowerBound() throws {
|
||||
let expression = try AnyExpression("foo[range]", constants: [
|
||||
"foo": "foo" as NSString,
|
||||
"range": "foobarbaz".range(of: "baz")!,
|
||||
"range": XCTUnwrap("foobarbaz".range(of: "baz")),
|
||||
])
|
||||
XCTAssertThrowsError(try expression.evaluate() as Any) { error in
|
||||
XCTAssertEqual(error as? Expression.Error, .stringBounds("foo", 6))
|
||||
}
|
||||
}
|
||||
|
||||
func testSubscriptNSStringWithInvalidHalfOpenIndexRangeUpperBound() {
|
||||
let expression = AnyExpression("foo[range]", constants: [
|
||||
func testSubscriptNSStringWithInvalidHalfOpenIndexRangeUpperBound() throws {
|
||||
let expression = try AnyExpression("foo[range]", constants: [
|
||||
"foo": "foo" as NSString,
|
||||
"range": "foobar".range(of: "obar")!,
|
||||
"range": XCTUnwrap("foobar".range(of: "obar")),
|
||||
])
|
||||
XCTAssertThrowsError(try expression.evaluate() as Any) { error in
|
||||
XCTAssertEqual(error as? Expression.Error, .stringBounds("foo", 6))
|
||||
}
|
||||
}
|
||||
|
||||
func testSubscriptNSStringWithInvalidClosedIndexRangeLowerBound() {
|
||||
let expression = AnyExpression("foo[range]", constants: [
|
||||
func testSubscriptNSStringWithInvalidClosedIndexRangeLowerBound() throws {
|
||||
let expression = try AnyExpression("foo[range]", constants: [
|
||||
"foo": "foo" as NSString,
|
||||
"range": "foobarbaz".range(of: "baz")!.lowerBound ... "foobarbaz".endIndex,
|
||||
"range": XCTUnwrap("foobarbaz".range(of: "baz")?.lowerBound) ... "foobarbaz".endIndex,
|
||||
])
|
||||
XCTAssertThrowsError(try expression.evaluate() as Any) { error in
|
||||
XCTAssertEqual(error as? Expression.Error, .stringBounds("foo", 6))
|
||||
}
|
||||
}
|
||||
|
||||
func testSubscriptNSStringWithInvalidClosedIndexRangeUpperBound() {
|
||||
let expression = AnyExpression("foo[range]", constants: [
|
||||
func testSubscriptNSStringWithInvalidClosedIndexRangeUpperBound() throws {
|
||||
let expression = try AnyExpression("foo[range]", constants: [
|
||||
"foo": "foo" as NSString,
|
||||
"range": "foobar".range(of: "obar")!.lowerBound ... "foobar".endIndex,
|
||||
"range": XCTUnwrap("foobar".range(of: "obar")?.lowerBound) ... "foobar".endIndex,
|
||||
])
|
||||
XCTAssertThrowsError(try expression.evaluate() as Any) { error in
|
||||
XCTAssertEqual(error as? Expression.Error, .stringBounds("foo", 6))
|
||||
@@ -1070,26 +1070,26 @@ final class AnyExpressionTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testSubscriptStringFromIndexRange() {
|
||||
let expression = AnyExpression("'foo'[index...]", constants: [
|
||||
"index": "foo".index(of: "o")!,
|
||||
func testSubscriptStringFromIndexRange() throws {
|
||||
let expression = try AnyExpression("'foo'[index...]", constants: [
|
||||
"index": XCTUnwrap("foo".index(of: "o")),
|
||||
])
|
||||
XCTAssertEqual(try expression.evaluate(), "oo")
|
||||
}
|
||||
|
||||
func testSubscriptStringFromInvalidIndexRange() {
|
||||
let expression = AnyExpression("'foo'[index...]", constants: [
|
||||
"index": "food".index(of: "d")!,
|
||||
func testSubscriptStringFromInvalidIndexRange() throws {
|
||||
let expression = try AnyExpression("'foo'[index...]", constants: [
|
||||
"index": XCTUnwrap("food".index(of: "d")),
|
||||
])
|
||||
XCTAssertThrowsError(try expression.evaluate() as Any) { error in
|
||||
XCTAssertEqual(error as? Expression.Error, .stringBounds("foo", 3))
|
||||
}
|
||||
}
|
||||
|
||||
func testSubscriptStringFromInvalidIndexRange2() {
|
||||
func testSubscriptStringFromInvalidIndexRange2() throws {
|
||||
#if swift(>=4)
|
||||
let expression = AnyExpression("foo[index...]", constants: [
|
||||
"foo": "afoo"["afoo".range(of: "foo")!],
|
||||
let expression = try AnyExpression("foo[index...]", constants: [
|
||||
"foo": "afoo"[XCTUnwrap("afoo".range(of: "foo"))],
|
||||
"index": "afoo".startIndex,
|
||||
])
|
||||
XCTAssertThrowsError(try expression.evaluate() as Any) { error in
|
||||
@@ -1146,10 +1146,10 @@ final class AnyExpressionTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testSubscriptStringThroughInvalidIndexRange2() {
|
||||
func testSubscriptStringThroughInvalidIndexRange2() throws {
|
||||
#if swift(>=4)
|
||||
let expression = AnyExpression("foo[...index]", constants: [
|
||||
"foo": "afoo"["afoo".range(of: "foo")!],
|
||||
let expression = try AnyExpression("foo[...index]", constants: [
|
||||
"foo": "afoo"[XCTUnwrap("afoo".range(of: "foo"))],
|
||||
"index": "afoo".startIndex,
|
||||
])
|
||||
XCTAssertThrowsError(try expression.evaluate() as Any) { error in
|
||||
|
||||
@@ -7,7 +7,7 @@ final class AttributedStringExpressionTests: XCTestCase {
|
||||
func testAttributedStringExpressionTextAndFont() throws {
|
||||
let node = LayoutNode()
|
||||
let expression = LayoutExpression(attributedStringExpression: "foo", for: node)
|
||||
let result = try expression?.evaluate() as! NSAttributedString
|
||||
let result = try XCTUnwrap(try expression?.evaluate() as? NSAttributedString)
|
||||
XCTAssertEqual(result.string, "foo")
|
||||
XCTAssertEqual(result.attribute(NSAttributedString.Key.font, at: 0, effectiveRange: nil) as? UIFont, .systemFont(ofSize: 17))
|
||||
}
|
||||
@@ -15,7 +15,7 @@ final class AttributedStringExpressionTests: XCTestCase {
|
||||
func testAttributedStringHTMLExpression() throws {
|
||||
let node = LayoutNode()
|
||||
let expression = LayoutExpression(attributedStringExpression: "<b>foo</b>", for: node)
|
||||
let result = try expression?.evaluate() as! NSAttributedString
|
||||
let result = try XCTUnwrap(try expression?.evaluate() as? NSAttributedString)
|
||||
XCTAssertEqual(result.string, "foo")
|
||||
XCTAssertEqual(result.attribute(NSAttributedString.Key.font, at: 0, effectiveRange: nil) as? UIFont, .boldSystemFont(ofSize: 17))
|
||||
}
|
||||
@@ -24,7 +24,7 @@ final class AttributedStringExpressionTests: XCTestCase {
|
||||
let node = LayoutNode()
|
||||
let text = "🤔😂"
|
||||
let expression = LayoutExpression(attributedStringExpression: "<i>\(text)</i>", for: node)
|
||||
let result = try expression?.evaluate() as! NSAttributedString
|
||||
let result = try XCTUnwrap(try expression?.evaluate() as? NSAttributedString)
|
||||
XCTAssertEqual(result.string, text)
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ final class AttributedStringExpressionTests: XCTestCase {
|
||||
label.font = UIFont(name: "Courier", size: 57)
|
||||
let node = LayoutNode(view: label)
|
||||
let expression = LayoutExpression(attributedStringExpression: "foo", for: node)
|
||||
let result = try expression?.evaluate() as! NSAttributedString
|
||||
let result = try XCTUnwrap(try expression?.evaluate() as? NSAttributedString)
|
||||
XCTAssertEqual(result.attribute(NSAttributedString.Key.font, at: 0, effectiveRange: nil) as? UIFont, label.font)
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ final class AttributedStringExpressionTests: XCTestCase {
|
||||
label.textColor = .red
|
||||
let node = LayoutNode(view: label)
|
||||
let expression = LayoutExpression(attributedStringExpression: "foo", for: node)
|
||||
let result = try expression?.evaluate() as! NSAttributedString
|
||||
let result = try XCTUnwrap(try expression?.evaluate() as? NSAttributedString)
|
||||
XCTAssertEqual(result.attribute(NSAttributedString.Key.foregroundColor, at: 0, effectiveRange: nil) as? UIColor, .red)
|
||||
}
|
||||
|
||||
@@ -51,8 +51,8 @@ final class AttributedStringExpressionTests: XCTestCase {
|
||||
label.textAlignment = .right
|
||||
let node = LayoutNode(view: label)
|
||||
let expression = LayoutExpression(attributedStringExpression: "foo", for: node)
|
||||
let result = try expression?.evaluate() as! NSAttributedString
|
||||
let paragraphStyle = result.attribute(NSAttributedString.Key.paragraphStyle, at: 0, effectiveRange: nil) as! NSParagraphStyle
|
||||
let result = try XCTUnwrap(try expression?.evaluate() as? NSAttributedString)
|
||||
let paragraphStyle = try XCTUnwrap(result.attribute(NSAttributedString.Key.paragraphStyle, at: 0, effectiveRange: nil) as? NSParagraphStyle)
|
||||
XCTAssertEqual(paragraphStyle.alignment, .right)
|
||||
}
|
||||
|
||||
@@ -61,15 +61,15 @@ final class AttributedStringExpressionTests: XCTestCase {
|
||||
label.lineBreakMode = .byTruncatingHead
|
||||
let node = LayoutNode(view: label)
|
||||
let expression = LayoutExpression(attributedStringExpression: "foo", for: node)
|
||||
let result = try expression?.evaluate() as! NSAttributedString
|
||||
let paragraphStyle = result.attribute(NSAttributedString.Key.paragraphStyle, at: 0, effectiveRange: nil) as! NSParagraphStyle
|
||||
let result = try XCTUnwrap(try expression?.evaluate() as? NSAttributedString)
|
||||
let paragraphStyle = try XCTUnwrap(result.attribute(NSAttributedString.Key.paragraphStyle, at: 0, effectiveRange: nil) as? NSParagraphStyle)
|
||||
XCTAssertEqual(paragraphStyle.lineBreakMode, .byTruncatingHead)
|
||||
}
|
||||
|
||||
func testAttributedStringContainingStringConstant() throws {
|
||||
let node = LayoutNode(constants: ["bar": "bar"])
|
||||
let expression = LayoutExpression(attributedStringExpression: "hello world {bar}", for: node)
|
||||
let result = try expression?.evaluate() as! NSAttributedString
|
||||
let result = try XCTUnwrap(try expression?.evaluate() as? NSAttributedString)
|
||||
XCTAssertEqual(result.string, "hello world bar")
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ final class AttributedStringExpressionTests: XCTestCase {
|
||||
NSAttributedString.Key.foregroundColor: UIColor.red,
|
||||
])])
|
||||
let expression = LayoutExpression(attributedStringExpression: "hello world {bar}", for: node)
|
||||
let result = try expression?.evaluate() as! NSAttributedString
|
||||
let result = try XCTUnwrap(try expression?.evaluate() as? NSAttributedString)
|
||||
XCTAssertEqual(result.string, "hello world bar")
|
||||
XCTAssertEqual(result.attribute(NSAttributedString.Key.foregroundColor, at: 12, effectiveRange: nil) as? UIColor, .red)
|
||||
}
|
||||
@@ -86,7 +86,7 @@ final class AttributedStringExpressionTests: XCTestCase {
|
||||
func testAttributedStringContainingHTMLConstant() throws {
|
||||
let node = LayoutNode(constants: ["bar": "<i>bar</i>"])
|
||||
let expression = LayoutExpression(attributedStringExpression: "<b>foo {bar}</b>", for: node)
|
||||
let result = try expression?.evaluate() as! NSAttributedString
|
||||
let result = try XCTUnwrap(try expression?.evaluate() as? NSAttributedString)
|
||||
XCTAssertEqual(result.string, "foo bar")
|
||||
XCTAssertEqual(result.attribute(NSAttributedString.Key.font, at: 0, effectiveRange: nil) as? UIFont, .boldSystemFont(ofSize: 17))
|
||||
let traits = (result.attribute(NSAttributedString.Key.font, at: 4, effectiveRange: nil) as? UIFont)?.fontDescriptor.symbolicTraits
|
||||
@@ -97,7 +97,7 @@ final class AttributedStringExpressionTests: XCTestCase {
|
||||
func testAttributedStringContainingAmbiguousTokens() throws {
|
||||
let node = LayoutNode(constants: ["foo": "$(2)", "bar": "$(3)"])
|
||||
let expression = LayoutExpression(attributedStringExpression: "<b>$(1)</b>{foo}{bar}", for: node)
|
||||
let result = try expression?.evaluate() as! NSAttributedString
|
||||
let result = try XCTUnwrap(try expression?.evaluate() as? NSAttributedString)
|
||||
XCTAssertEqual(result.string, "$(1)$(2)$(3)")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,11 +28,11 @@ final class FontExpressionTests: XCTestCase {
|
||||
|
||||
#endif
|
||||
|
||||
func testBoldTrait() {
|
||||
func testBoldTrait() throws {
|
||||
let node = LayoutNode()
|
||||
let expression = LayoutExpression(fontExpression: "{UIFontDescriptorSymbolicTraits.traitBold}", for: node)
|
||||
let descriptor = UIFont.systemFont(ofSize: UIFont.defaultSize).fontDescriptor
|
||||
let expected = UIFont(descriptor: descriptor.withSymbolicTraits([.traitBold])!, size: UIFont.defaultSize)
|
||||
let expected = try UIFont(descriptor: XCTUnwrap(descriptor.withSymbolicTraits([.traitBold])), size: UIFont.defaultSize)
|
||||
XCTAssertEqual(try expression?.evaluate() as? UIFont, expected)
|
||||
}
|
||||
|
||||
@@ -57,39 +57,39 @@ final class FontExpressionTests: XCTestCase {
|
||||
XCTAssertEqual(try expression?.evaluate() as? UIFont, expected)
|
||||
}
|
||||
|
||||
func testBoldItalic() {
|
||||
func testBoldItalic() throws {
|
||||
let node = LayoutNode()
|
||||
let expression = LayoutExpression(fontExpression: "bold italic", for: node)
|
||||
let descriptor = UIFont.systemFont(ofSize: UIFont.defaultSize, weight: .bold).fontDescriptor
|
||||
let traits = descriptor.symbolicTraits.union([.traitBold, .traitItalic])
|
||||
let expected = UIFont(descriptor: descriptor.withSymbolicTraits(traits)!, size: UIFont.defaultSize)
|
||||
let expected = try UIFont(descriptor: XCTUnwrap(descriptor.withSymbolicTraits(traits)), size: UIFont.defaultSize)
|
||||
XCTAssertEqual(try expression?.evaluate() as? UIFont, expected)
|
||||
}
|
||||
|
||||
func testCondensed() {
|
||||
func testCondensed() throws {
|
||||
let node = LayoutNode()
|
||||
let expression = LayoutExpression(fontExpression: "condensed", for: node)
|
||||
let descriptor = UIFont.systemFont(ofSize: UIFont.defaultSize).fontDescriptor
|
||||
let traits = descriptor.symbolicTraits.union([.traitCondensed])
|
||||
let expected = UIFont(descriptor: descriptor.withSymbolicTraits(traits)!, size: UIFont.defaultSize)
|
||||
let expected = try UIFont(descriptor: XCTUnwrap(descriptor.withSymbolicTraits(traits)), size: UIFont.defaultSize)
|
||||
XCTAssertEqual(try expression?.evaluate() as? UIFont, expected)
|
||||
}
|
||||
|
||||
func testBlackCondensed() {
|
||||
func testBlackCondensed() throws {
|
||||
let node = LayoutNode()
|
||||
let expression = LayoutExpression(fontExpression: "black condensed", for: node)
|
||||
let descriptor = UIFont.systemFont(ofSize: UIFont.defaultSize, weight: .black).fontDescriptor
|
||||
let traits = descriptor.symbolicTraits.union([.traitCondensed])
|
||||
let expected = UIFont(descriptor: descriptor.withSymbolicTraits(traits)!, size: UIFont.defaultSize)
|
||||
let expected = try UIFont(descriptor: XCTUnwrap(descriptor.withSymbolicTraits(traits)), size: UIFont.defaultSize)
|
||||
XCTAssertEqual(try expression?.evaluate() as? UIFont, expected)
|
||||
}
|
||||
|
||||
func testMonospace() {
|
||||
func testMonospace() throws {
|
||||
let node = LayoutNode()
|
||||
let expression = LayoutExpression(fontExpression: "monospace", for: node)
|
||||
let descriptor = UIFont(name: "Courier", size: UIFont.defaultSize)!.fontDescriptor
|
||||
let descriptor = try XCTUnwrap(UIFont(name: "Courier", size: UIFont.defaultSize)?.fontDescriptor)
|
||||
let traits = descriptor.symbolicTraits.union([.traitMonoSpace])
|
||||
let expected = UIFont(descriptor: descriptor.withSymbolicTraits(traits)!, size: UIFont.defaultSize)
|
||||
let expected = try UIFont(descriptor: XCTUnwrap(descriptor.withSymbolicTraits(traits)), size: UIFont.defaultSize)
|
||||
XCTAssertEqual(try expression?.evaluate() as? UIFont, expected)
|
||||
}
|
||||
|
||||
@@ -109,68 +109,68 @@ final class FontExpressionTests: XCTestCase {
|
||||
XCTAssertEqual(try expression?.evaluate() as? UIFont, expected)
|
||||
}
|
||||
|
||||
func testBoldEscapedFontNameWithSpaces() {
|
||||
func testBoldEscapedFontNameWithSpaces() throws {
|
||||
let node = LayoutNode()
|
||||
let name = "helvetica neue"
|
||||
let expression = LayoutExpression(fontExpression: "'\(name)' bold", for: node)
|
||||
let descriptor = UIFont(name: name, size: UIFont.defaultSize)!.fontDescriptor
|
||||
let descriptor = try XCTUnwrap(UIFont(name: name, size: UIFont.defaultSize)?.fontDescriptor)
|
||||
let traits = descriptor.symbolicTraits.union([.traitBold])
|
||||
let expected = UIFont(descriptor: descriptor.withSymbolicTraits(traits)!, size: UIFont.defaultSize)
|
||||
let expected = try UIFont(descriptor: XCTUnwrap(descriptor.withSymbolicTraits(traits)), size: UIFont.defaultSize)
|
||||
XCTAssertEqual(try expression?.evaluate() as? UIFont, expected)
|
||||
}
|
||||
|
||||
func testBlackEscapedFontNameWithSpaces() {
|
||||
func testBlackEscapedFontNameWithSpaces() throws {
|
||||
let node = LayoutNode()
|
||||
let expression = LayoutExpression(fontExpression: "'helvetica neue' black", for: node)
|
||||
let expected = UIFont(name: "HelveticaNeue-CondensedBlack", size: UIFont.defaultSize)!
|
||||
let expected = try XCTUnwrap(UIFont(name: "HelveticaNeue-CondensedBlack", size: UIFont.defaultSize))
|
||||
XCTAssertEqual(try expression?.evaluate() as? UIFont, expected)
|
||||
}
|
||||
|
||||
func testUltralightEscapedFontNameWithSpaces() {
|
||||
func testUltralightEscapedFontNameWithSpaces() throws {
|
||||
let node = LayoutNode()
|
||||
let expression = LayoutExpression(fontExpression: "'helvetica neue' ultralight", for: node)
|
||||
let expected = UIFont(name: "HelveticaNeue-UltraLight", size: UIFont.defaultSize)!
|
||||
let expected = try XCTUnwrap(UIFont(name: "HelveticaNeue-UltraLight", size: UIFont.defaultSize))
|
||||
XCTAssertEqual(try expression?.evaluate() as? UIFont, expected)
|
||||
}
|
||||
|
||||
func testBoldUnescapedFontNameWithSpaces() {
|
||||
func testBoldUnescapedFontNameWithSpaces() throws {
|
||||
let node = LayoutNode()
|
||||
let name = "helvetica neue"
|
||||
let expression = LayoutExpression(fontExpression: "\(name) bold", for: node)
|
||||
let descriptor = UIFont(name: name, size: UIFont.defaultSize)!.fontDescriptor
|
||||
let descriptor = try XCTUnwrap(UIFont(name: name, size: UIFont.defaultSize)?.fontDescriptor)
|
||||
let traits = descriptor.symbolicTraits.union([.traitBold])
|
||||
let expected = UIFont(descriptor: descriptor.withSymbolicTraits(traits)!, size: UIFont.defaultSize)
|
||||
let expected = try UIFont(descriptor: XCTUnwrap(descriptor.withSymbolicTraits(traits)), size: UIFont.defaultSize)
|
||||
XCTAssertEqual(try expression?.evaluate() as? UIFont, expected)
|
||||
}
|
||||
|
||||
func testBlackUnescapedFontNameWithSpaces() {
|
||||
func testBlackUnescapedFontNameWithSpaces() throws {
|
||||
let node = LayoutNode()
|
||||
let expression = LayoutExpression(fontExpression: "helvetica neue black", for: node)
|
||||
let expected = UIFont(name: "HelveticaNeue-CondensedBlack", size: UIFont.defaultSize)!
|
||||
let expected = try XCTUnwrap(UIFont(name: "HelveticaNeue-CondensedBlack", size: UIFont.defaultSize))
|
||||
XCTAssertEqual(try expression?.evaluate() as? UIFont, expected)
|
||||
}
|
||||
|
||||
func testBlackUnescapedFontNameWithSpaces2() {
|
||||
func testBlackUnescapedFontNameWithSpaces2() throws {
|
||||
let node = LayoutNode()
|
||||
let expression = LayoutExpression(fontExpression: "Apple SD Gothic Neo light", for: node)
|
||||
let expected = UIFont(name: "AppleSDGothicNeo-Light", size: UIFont.defaultSize)!
|
||||
let expected = try XCTUnwrap(UIFont(name: "AppleSDGothicNeo-Light", size: UIFont.defaultSize))
|
||||
XCTAssertEqual(try expression?.evaluate() as? UIFont, expected)
|
||||
}
|
||||
|
||||
func testUltralightUnescapedFontNameWithSpaces() {
|
||||
func testUltralightUnescapedFontNameWithSpaces() throws {
|
||||
let node = LayoutNode()
|
||||
let expression = LayoutExpression(fontExpression: "helvetica neue ultralight", for: node)
|
||||
let expected = UIFont(name: "HelveticaNeue-UltraLight", size: UIFont.defaultSize)!
|
||||
let expected = try XCTUnwrap(UIFont(name: "HelveticaNeue-UltraLight", size: UIFont.defaultSize))
|
||||
XCTAssertEqual(try expression?.evaluate() as? UIFont, expected)
|
||||
}
|
||||
|
||||
func testExplicitFontWithBoldAttributes() {
|
||||
let font = UIFont(name: "courier", size: 15)!
|
||||
func testExplicitFontWithBoldAttributes() throws {
|
||||
let font = try XCTUnwrap(UIFont(name: "courier", size: 15))
|
||||
let node = LayoutNode(constants: ["font": font])
|
||||
let expression = LayoutExpression(fontExpression: "{font} bold", for: node)
|
||||
let descriptor = font.fontDescriptor
|
||||
let traits = descriptor.symbolicTraits.union([.traitBold])
|
||||
let expected = UIFont(descriptor: descriptor.withSymbolicTraits(traits)!, size: font.pointSize)
|
||||
let expected = try UIFont(descriptor: XCTUnwrap(descriptor.withSymbolicTraits(traits)), size: font.pointSize)
|
||||
XCTAssertEqual(try expression?.evaluate() as? UIFont, expected)
|
||||
}
|
||||
|
||||
@@ -301,7 +301,7 @@ final class FontExpressionTests: XCTestCase {
|
||||
$0.lowercased().contains("-\(weightKey.lowercased())")
|
||||
}
|
||||
if !expected.isEmpty {
|
||||
let name = try (expression!.evaluate() as! UIFont).fontName
|
||||
let name = try try XCTUnwrap((expression?.evaluate() as? UIFont)?.fontName)
|
||||
XCTAssertTrue(expected.contains(name), "\(expected) does not contain \(name)")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -444,9 +444,9 @@ final class LayoutExpressionTests: XCTestCase {
|
||||
XCTAssertEqual(expression?.symbols.isEmpty, true)
|
||||
}
|
||||
|
||||
func testSetLayerContentsWithCGImageConstant() {
|
||||
func testSetLayerContentsWithCGImageConstant() throws {
|
||||
UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
|
||||
let image: AnyObject = UIGraphicsGetImageFromCurrentImageContext()!.cgImage!
|
||||
let image: AnyObject = try XCTUnwrap(UIGraphicsGetImageFromCurrentImageContext()?.cgImage)
|
||||
UIGraphicsEndImageContext()
|
||||
let node = LayoutNode(
|
||||
constants: ["image": image],
|
||||
@@ -456,9 +456,9 @@ final class LayoutExpressionTests: XCTestCase {
|
||||
XCTAssertTrue(node.view.layer.contents as AnyObject === image)
|
||||
}
|
||||
|
||||
func testSetLayerContentsWithUIImageConstant() {
|
||||
func testSetLayerContentsWithUIImageConstant() throws {
|
||||
UIGraphicsBeginImageContext(CGSize(width: 1, height: 1))
|
||||
let image = UIGraphicsGetImageFromCurrentImageContext()!
|
||||
let image = try XCTUnwrap(UIGraphicsGetImageFromCurrentImageContext())
|
||||
UIGraphicsEndImageContext()
|
||||
let node = LayoutNode(
|
||||
constants: ["image": image],
|
||||
@@ -494,21 +494,21 @@ final class LayoutExpressionTests: XCTestCase {
|
||||
let node = LayoutNode()
|
||||
let expression = LayoutExpression(doubleExpression: "5 + 6", for: node)
|
||||
XCTAssertEqual(expression?.isConstant, true)
|
||||
XCTAssertEqual(try expression?.evaluate() as! Double, 11)
|
||||
XCTAssertEqual(try expression?.evaluate() as? Double, 11)
|
||||
}
|
||||
|
||||
func testConstant() {
|
||||
let node = LayoutNode(constants: ["foo": 5])
|
||||
let expression = LayoutExpression(doubleExpression: "foo + 6", for: node)
|
||||
XCTAssertEqual(expression?.isConstant, true)
|
||||
XCTAssertEqual(try expression?.evaluate() as! Double, 11)
|
||||
XCTAssertEqual(try expression?.evaluate() as? Double, 11)
|
||||
}
|
||||
|
||||
func testState() {
|
||||
let node = LayoutNode(state: ["foo": 5])
|
||||
let expression = LayoutExpression(doubleExpression: "foo + 6", for: node)
|
||||
XCTAssertEqual(expression?.isConstant, false)
|
||||
XCTAssertEqual(try expression?.evaluate() as! Double, 11)
|
||||
XCTAssertEqual(try expression?.evaluate() as? Double, 11)
|
||||
}
|
||||
|
||||
func testInheritedConstant() {
|
||||
@@ -517,7 +517,7 @@ final class LayoutExpressionTests: XCTestCase {
|
||||
parent.update()
|
||||
let expression = LayoutExpression(doubleExpression: "foo + 6", for: child)
|
||||
XCTAssertEqual(expression?.isConstant, true)
|
||||
XCTAssertEqual(try expression?.evaluate() as! Double, 11)
|
||||
XCTAssertEqual(try expression?.evaluate() as? Double, 11)
|
||||
}
|
||||
|
||||
func testParentLiteralExpression() {
|
||||
@@ -526,7 +526,7 @@ final class LayoutExpressionTests: XCTestCase {
|
||||
parent.update()
|
||||
let expression = LayoutExpression(doubleExpression: "parent.height + 6", for: child)
|
||||
XCTAssertEqual(expression?.isConstant, true)
|
||||
XCTAssertEqual(try expression?.evaluate() as! Double, 11)
|
||||
XCTAssertEqual(try expression?.evaluate() as? Double, 11)
|
||||
}
|
||||
|
||||
func testParentConstantExpression() {
|
||||
@@ -535,7 +535,7 @@ final class LayoutExpressionTests: XCTestCase {
|
||||
parent.update()
|
||||
let expression = LayoutExpression(doubleExpression: "parent.height + 6", for: child)
|
||||
XCTAssertEqual(expression?.isConstant, true)
|
||||
XCTAssertEqual(try expression?.evaluate() as! Double, 17)
|
||||
XCTAssertEqual(try expression?.evaluate() as? Double, 17)
|
||||
}
|
||||
|
||||
func testLiteralParameter() throws {
|
||||
@@ -551,7 +551,7 @@ final class LayoutExpressionTests: XCTestCase {
|
||||
parent.update()
|
||||
let expression = LayoutExpression(doubleExpression: "foo + 6", for: child)
|
||||
XCTAssertEqual(expression?.isConstant, true)
|
||||
XCTAssertEqual(try expression?.evaluate() as! Double, 11)
|
||||
XCTAssertEqual(try expression?.evaluate() as? Double, 11)
|
||||
}
|
||||
|
||||
func testConstantParameter() throws {
|
||||
@@ -567,7 +567,7 @@ final class LayoutExpressionTests: XCTestCase {
|
||||
parent.update()
|
||||
let expression = LayoutExpression(doubleExpression: "foo + 6", for: child)
|
||||
XCTAssertEqual(expression?.isConstant, true)
|
||||
XCTAssertEqual(try expression?.evaluate() as! Double, 14)
|
||||
XCTAssertEqual(try expression?.evaluate() as? Double, 14)
|
||||
}
|
||||
|
||||
func testLiteralMacro() throws {
|
||||
@@ -582,7 +582,7 @@ final class LayoutExpressionTests: XCTestCase {
|
||||
parent.update()
|
||||
let expression = LayoutExpression(doubleExpression: "BAR + 6", for: child)
|
||||
XCTAssertEqual(expression?.isConstant, true)
|
||||
XCTAssertEqual(try expression?.evaluate() as! Double, 11)
|
||||
XCTAssertEqual(try expression?.evaluate() as? Double, 11)
|
||||
}
|
||||
|
||||
func testArrayMacro() throws {
|
||||
@@ -591,7 +591,7 @@ final class LayoutExpressionTests: XCTestCase {
|
||||
node.update()
|
||||
let expression = LayoutExpression(doubleExpression: "ITEMS[1]", for: node)
|
||||
XCTAssertEqual(expression?.isConstant, true)
|
||||
XCTAssertEqual(try expression?.evaluate() as! Double, 2)
|
||||
XCTAssertEqual(try expression?.evaluate() as? Double, 2)
|
||||
}
|
||||
|
||||
func testArrayMacro2() throws {
|
||||
@@ -600,7 +600,7 @@ final class LayoutExpressionTests: XCTestCase {
|
||||
node.update()
|
||||
let expression = LayoutExpression(doubleExpression: "ITEMS[1]", for: node)
|
||||
XCTAssertEqual(expression?.isConstant, true)
|
||||
XCTAssertEqual(try expression?.evaluate() as! Double, 2)
|
||||
XCTAssertEqual(try expression?.evaluate() as? Double, 2)
|
||||
}
|
||||
|
||||
func testArrayConstant() {
|
||||
@@ -608,7 +608,7 @@ final class LayoutExpressionTests: XCTestCase {
|
||||
node.update()
|
||||
let expression = LayoutExpression(stringExpression: "{items[0]}", for: node)
|
||||
XCTAssertEqual(expression?.isConstant, true)
|
||||
XCTAssertEqual(try expression?.evaluate() as! String, "foo")
|
||||
XCTAssertEqual(try expression?.evaluate() as? String, "foo")
|
||||
}
|
||||
|
||||
// MARK: Edge cases
|
||||
|
||||
@@ -113,14 +113,14 @@ final class LayoutNodeTests: XCTestCase {
|
||||
// MARK: Invalid node errors
|
||||
|
||||
func testUnknownClass() throws {
|
||||
let layout = try Layout(xmlData: "<Foo/>".data(using: .utf8)!)
|
||||
let layout = try Layout(xmlData: XCTUnwrap("<Foo/>".data(using: .utf8)))
|
||||
XCTAssertThrowsError(try LayoutNode(layout: layout)) { error in
|
||||
XCTAssert("\(error)".contains("Unknown class Foo"))
|
||||
}
|
||||
}
|
||||
|
||||
func testInvalidClass() throws {
|
||||
let layout = try Layout(xmlData: "<NSObject/>".data(using: .utf8)!)
|
||||
let layout = try Layout(xmlData: XCTUnwrap("<NSObject/>".data(using: .utf8)))
|
||||
XCTAssertThrowsError(try LayoutNode(layout: layout)) { error in
|
||||
XCTAssert("\(error)".contains("NSObject is not a subclass of UIView"))
|
||||
}
|
||||
@@ -243,7 +243,7 @@ final class LayoutNodeTests: XCTestCase {
|
||||
let node = try LayoutNode(xmlData: xmlData)
|
||||
node.setState(["name": "Foo"])
|
||||
node.update()
|
||||
XCTAssertEqual((node.view as! UILabel).text, "Foo")
|
||||
XCTAssertEqual((node.view as? UILabel)?.text, "Foo")
|
||||
}
|
||||
|
||||
func testMacroNameShadowsState() throws {
|
||||
@@ -251,7 +251,7 @@ final class LayoutNodeTests: XCTestCase {
|
||||
let node = try LayoutNode(xmlData: xmlData)
|
||||
node.setState(["name": "Foo"])
|
||||
node.update()
|
||||
XCTAssertEqual((node.view.subviews[0] as! UILabel).text, "Foo")
|
||||
XCTAssertEqual((node.view.subviews[0] as? UILabel)?.text, "Foo")
|
||||
}
|
||||
|
||||
func testMacroNameShadowsConstant() throws {
|
||||
|
||||
@@ -27,8 +27,8 @@ final class LayoutLoaderTests: XCTestCase {
|
||||
let projectURL = directory.deletingLastPathComponent().appendingPathComponent("Project.xcodeproj")
|
||||
let fileURL = directory.appendingPathComponent("baz.swift")
|
||||
do {
|
||||
try "project".data(using: .utf8)!.write(to: projectURL)
|
||||
try "file".data(using: .utf8)!.write(to: fileURL)
|
||||
try try XCTUnwrap("project".data(using: .utf8)?.write(to: projectURL))
|
||||
try try XCTUnwrap("file".data(using: .utf8)?.write(to: fileURL))
|
||||
let loader = LayoutLoader()
|
||||
let path = loader.findProjectDirectory(at: fileURL.path)
|
||||
XCTAssertEqual(path, projectURL.deletingLastPathComponent())
|
||||
|
||||
@@ -35,8 +35,8 @@ final class PropertiesTests: XCTestCase {
|
||||
XCTAssertNil(result) // Not supoported yet
|
||||
}
|
||||
|
||||
func testCGPointPropertyType() {
|
||||
let result = TestObject.allPropertyTypes()["testPoint"]!
|
||||
func testCGPointPropertyType() throws {
|
||||
let result = try XCTUnwrap(TestObject.allPropertyTypes()["testPoint"])
|
||||
let expected = RuntimeType(CGPoint.self)
|
||||
let control = RuntimeType(NSValue.self)
|
||||
XCTAssertEqual(result, expected)
|
||||
|
||||
@@ -230,15 +230,15 @@ final class RuntimeTypeTests: XCTestCase {
|
||||
XCTAssertNotNil(runtimeType.cast([5.0]))
|
||||
XCTAssertNotNil(runtimeType.cast(NSArray()))
|
||||
XCTAssertNotNil(runtimeType.cast([String]()))
|
||||
XCTAssertEqual(runtimeType.cast(5) as! [Int], [5]) // Stringified and array-ified
|
||||
XCTAssertEqual(runtimeType.cast(5) as? [Int], [5]) // Stringified and array-ified
|
||||
}
|
||||
|
||||
func testCastStringArray() {
|
||||
let runtimeType = RuntimeType([String].self)
|
||||
XCTAssertNotNil(runtimeType.cast(["foo"]))
|
||||
XCTAssertEqual(runtimeType.cast([5]) as! [String], ["5"]) // Anything can be stringified
|
||||
XCTAssertEqual(runtimeType.cast("foo") as! [String], ["foo"]) // Is array-ified
|
||||
XCTAssertEqual(runtimeType.cast(5) as! [String], ["5"]) // Stringified and array-ified
|
||||
XCTAssertEqual(runtimeType.cast([5]) as? [String], ["5"]) // Anything can be stringified
|
||||
XCTAssertEqual(runtimeType.cast("foo") as? [String], ["foo"]) // Is array-ified
|
||||
XCTAssertEqual(runtimeType.cast(5) as? [String], ["5"]) // Stringified and array-ified
|
||||
}
|
||||
|
||||
func testCastArrayArray() {
|
||||
|
||||
@@ -14,11 +14,11 @@ private final class TestViewController: UIViewController {
|
||||
}
|
||||
|
||||
final class SelectorExpressionTests: XCTestCase {
|
||||
func testSetControlAction() {
|
||||
func testSetControlAction() throws {
|
||||
let node = LayoutNode(view: UIControl(), expressions: ["touchUpInside": "foo:"])
|
||||
let viewController = TestViewController()
|
||||
XCTAssertNoThrow(try node.mount(in: viewController))
|
||||
let control = node.view as! UIControl
|
||||
let control = try XCTUnwrap(node.view as? UIControl)
|
||||
XCTAssertEqual(control.actions(forTarget: viewController, forControlEvent: .touchUpInside)?.first, "foo:")
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ final class StateTests: XCTestCase {
|
||||
|
||||
func testNestedStateDictionary() {
|
||||
let node = LayoutNode(state: ["foo": ["bar": "baz"]])
|
||||
XCTAssertEqual(try node.value(forSymbol: "foo") as! [String: String], ["bar": "baz"])
|
||||
XCTAssertEqual(try node.value(forSymbol: "foo") as? [String: String], ["bar": "baz"])
|
||||
XCTAssertEqual(try node.value(forSymbol: "foo.bar") as? String, "baz")
|
||||
}
|
||||
|
||||
@@ -42,13 +42,13 @@ final class StateTests: XCTestCase {
|
||||
XCTAssertEqual(try node.value(forSymbol: "bar") as? String, "baz")
|
||||
}
|
||||
|
||||
func testOptionalStruct() {
|
||||
func testOptionalStruct() throws {
|
||||
var state: TestState? = TestState()
|
||||
let node = LayoutNode(state: state as Any)
|
||||
XCTAssertEqual(try node.value(forSymbol: "foo") as? Int, 5)
|
||||
XCTAssertEqual(try node.value(forSymbol: "bar") as? String, "baz")
|
||||
state?.foo = 10
|
||||
node.setState(state!) // Force unwrap
|
||||
try node.setState(XCTUnwrap(state)) // Force unwrap
|
||||
XCTAssertEqual(try node.value(forSymbol: "foo") as? Int, 10)
|
||||
}
|
||||
|
||||
|
||||
@@ -6,30 +6,30 @@ import XCTest
|
||||
final class XMLTests: XCTestCase {
|
||||
// MARK: Malformed XML
|
||||
|
||||
func testEmptyXML() {
|
||||
func testEmptyXML() throws {
|
||||
let input = ""
|
||||
XCTAssertThrowsError(try Layout(xmlData: input.data(using: .utf8)!)) { error in
|
||||
XCTAssertThrowsError(try Layout(xmlData: XCTUnwrap(input.data(using: .utf8)))) { error in
|
||||
XCTAssert("\(error)".contains("Empty"))
|
||||
}
|
||||
}
|
||||
|
||||
func testHTMLAtRoot() {
|
||||
func testHTMLAtRoot() throws {
|
||||
let input = "<html></html>"
|
||||
XCTAssertThrowsError(try Layout(xmlData: input.data(using: .utf8)!)) { error in
|
||||
XCTAssertThrowsError(try Layout(xmlData: XCTUnwrap(input.data(using: .utf8)))) { error in
|
||||
XCTAssert("\(error)".contains("Invalid root"))
|
||||
}
|
||||
}
|
||||
|
||||
func testViewInsideHTML() {
|
||||
func testViewInsideHTML() throws {
|
||||
let input = "<UIView><p><UIView/></p></UIView>"
|
||||
XCTAssertThrowsError(try Layout(xmlData: input.data(using: .utf8)!)) { error in
|
||||
XCTAssertThrowsError(try Layout(xmlData: XCTUnwrap(input.data(using: .utf8)))) { error in
|
||||
XCTAssert("\(error)".contains("Unsupported HTML"))
|
||||
}
|
||||
}
|
||||
|
||||
func testViewInsideHTMLInsideLabel() {
|
||||
func testViewInsideHTMLInsideLabel() throws {
|
||||
let input = "<UILabel><p>hello <UIView/> world</p></UILabel>"
|
||||
XCTAssertThrowsError(try Layout(xmlData: input.data(using: .utf8)!)) { error in
|
||||
XCTAssertThrowsError(try Layout(xmlData: XCTUnwrap(input.data(using: .utf8)))) { error in
|
||||
guard let layoutError = error as? LayoutError else {
|
||||
XCTFail("\(error)")
|
||||
return
|
||||
@@ -39,99 +39,99 @@ final class XMLTests: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testMismatchedHTML() {
|
||||
func testMismatchedHTML() throws {
|
||||
let input = "<UILabel>Some <b>bold</bold> text</UILabel>"
|
||||
let xmlData = input.data(using: .utf8)!
|
||||
let xmlData = try XCTUnwrap(input.data(using: .utf8))
|
||||
XCTAssertThrowsError(try Layout(xmlData: xmlData)) { error in
|
||||
XCTAssert("\(error)".contains("bold"))
|
||||
}
|
||||
}
|
||||
|
||||
func testMissingParameterAttribute() {
|
||||
func testMissingParameterAttribute() throws {
|
||||
let input = "<UILabel><param name=\"text\" value=\"foo\"/></UILabel>"
|
||||
let xmlData = input.data(using: .utf8)!
|
||||
let xmlData = try XCTUnwrap(input.data(using: .utf8))
|
||||
XCTAssertThrowsError(try Layout(xmlData: xmlData)) { error in
|
||||
XCTAssert("\(error)".contains("type is a required attribute"))
|
||||
}
|
||||
}
|
||||
|
||||
func testExtraParameterAttribute() {
|
||||
func testExtraParameterAttribute() throws {
|
||||
let input = "<UILabel><param name=\"text\" type=\"String\" value=\"foo\"/></UILabel>"
|
||||
let xmlData = input.data(using: .utf8)!
|
||||
let xmlData = try XCTUnwrap(input.data(using: .utf8))
|
||||
XCTAssertThrowsError(try Layout(xmlData: xmlData)) { error in
|
||||
XCTAssert("\(error)".contains("Unexpected attribute value"))
|
||||
}
|
||||
}
|
||||
|
||||
func testUnknownParameterType() {
|
||||
func testUnknownParameterType() throws {
|
||||
let input = "<UILabel><param name=\"text\" type=\"Foo\"/></UILabel>"
|
||||
let xmlData = input.data(using: .utf8)!
|
||||
let xmlData = try XCTUnwrap(input.data(using: .utf8))
|
||||
XCTAssertThrowsError(try Layout(xmlData: xmlData)) { error in
|
||||
XCTAssert("\(error)".contains("Unknown or unsupported type"))
|
||||
}
|
||||
}
|
||||
|
||||
func testChildNodeInParameter() {
|
||||
func testChildNodeInParameter() throws {
|
||||
let input = "<UILabel><param name=\"text\">foo</param></UILabel>"
|
||||
let xmlData = input.data(using: .utf8)!
|
||||
let xmlData = try XCTUnwrap(input.data(using: .utf8))
|
||||
XCTAssertThrowsError(try Layout(xmlData: xmlData)) { error in
|
||||
XCTAssert("\(error)".contains("should not contain sub-nodes"))
|
||||
}
|
||||
}
|
||||
|
||||
func testMissingMacroAttribute() {
|
||||
func testMissingMacroAttribute() throws {
|
||||
let input = "<UILabel><macro key=\"text\" value=\"foo\"/></UILabel>"
|
||||
let xmlData = input.data(using: .utf8)!
|
||||
let xmlData = try XCTUnwrap(input.data(using: .utf8))
|
||||
XCTAssertThrowsError(try Layout(xmlData: xmlData)) { error in
|
||||
XCTAssert("\(error)".contains("name is a required attribute"))
|
||||
}
|
||||
}
|
||||
|
||||
func testExtraMacroAttribute() {
|
||||
func testExtraMacroAttribute() throws {
|
||||
let input = "<UILabel><macro name=\"text\" type=\"String\" value=\"foo\"/></UILabel>"
|
||||
let xmlData = input.data(using: .utf8)!
|
||||
let xmlData = try XCTUnwrap(input.data(using: .utf8))
|
||||
XCTAssertThrowsError(try Layout(xmlData: xmlData)) { error in
|
||||
XCTAssert("\(error)".contains("Unexpected attribute type"))
|
||||
}
|
||||
}
|
||||
|
||||
func testChildNodeInMacro() {
|
||||
func testChildNodeInMacro() throws {
|
||||
let input = "<UILabel><macro name=\"text\">foo</macro></UILabel>"
|
||||
let xmlData = input.data(using: .utf8)!
|
||||
let xmlData = try XCTUnwrap(input.data(using: .utf8))
|
||||
XCTAssertThrowsError(try Layout(xmlData: xmlData)) { error in
|
||||
XCTAssert("\(error)".contains("should not contain sub-nodes"))
|
||||
}
|
||||
}
|
||||
|
||||
func testStringInOutletAttribute() {
|
||||
func testStringInOutletAttribute() throws {
|
||||
let input = "<UILabel outlet=\"foo\"/>"
|
||||
let xmlData = input.data(using: .utf8)!
|
||||
let xmlData = try XCTUnwrap(input.data(using: .utf8))
|
||||
XCTAssertNoThrow(try Layout(xmlData: xmlData))
|
||||
}
|
||||
|
||||
func testCommentedOutOutletAttribute() {
|
||||
func testCommentedOutOutletAttribute() throws {
|
||||
let input = "<UILabel outlet=\"//foo\"/>"
|
||||
let xmlData = input.data(using: .utf8)!
|
||||
let xmlData = try XCTUnwrap(input.data(using: .utf8))
|
||||
XCTAssertNoThrow(try Layout(xmlData: xmlData))
|
||||
}
|
||||
|
||||
func testEmptyOutletAttribute() {
|
||||
func testEmptyOutletAttribute() throws {
|
||||
let input = "<UILabel outlet=\"\"/>"
|
||||
let xmlData = input.data(using: .utf8)!
|
||||
let xmlData = try XCTUnwrap(input.data(using: .utf8))
|
||||
XCTAssertNoThrow(try Layout(xmlData: xmlData))
|
||||
}
|
||||
|
||||
func testExpressionInXMLAttribute() {
|
||||
func testExpressionInXMLAttribute() throws {
|
||||
let input = "<UILabel xml=\"{foo}\"/>"
|
||||
let xmlData = input.data(using: .utf8)!
|
||||
let xmlData = try XCTUnwrap(input.data(using: .utf8))
|
||||
XCTAssertThrowsError(try Layout(xmlData: xmlData)) { error in
|
||||
XCTAssert("\(error)".contains("xml must be a literal value"))
|
||||
}
|
||||
}
|
||||
|
||||
func testExpressionInTemplateAttribute() {
|
||||
func testExpressionInTemplateAttribute() throws {
|
||||
let input = "<UILabel template=\"{foo}.xml\"/>"
|
||||
let xmlData = input.data(using: .utf8)!
|
||||
let xmlData = try XCTUnwrap(input.data(using: .utf8))
|
||||
XCTAssertThrowsError(try Layout(xmlData: xmlData)) { error in
|
||||
XCTAssert("\(error)".contains("template must be a literal value"))
|
||||
}
|
||||
@@ -141,7 +141,7 @@ final class XMLTests: XCTestCase {
|
||||
|
||||
func testDiscardLeadingWhitespace() throws {
|
||||
let input = " <UIView/>"
|
||||
let xmlData = input.data(using: .utf8)!
|
||||
let xmlData = try XCTUnwrap(input.data(using: .utf8))
|
||||
let xml = try XMLParser.parse(data: xmlData)
|
||||
guard xml.count == 1, case let .node(name, _, _) = xml[0] else {
|
||||
XCTFail()
|
||||
@@ -152,14 +152,14 @@ final class XMLTests: XCTestCase {
|
||||
|
||||
func testDiscardWhitespaceInsideLabel() throws {
|
||||
let input = "<UILabel>\n Foo\n</UILabel>"
|
||||
let xmlData = input.data(using: .utf8)!
|
||||
let xmlData = try XCTUnwrap(input.data(using: .utf8))
|
||||
let layout = try Layout(xmlData: xmlData)
|
||||
XCTAssertEqual(layout.body, "Foo")
|
||||
}
|
||||
|
||||
func testInterleavedTextAndViewsInsideLabel() throws {
|
||||
let input = "<UILabel>Foo<UIView/>Bar</UILabel>"
|
||||
let xmlData = input.data(using: .utf8)!
|
||||
let xmlData = try XCTUnwrap(input.data(using: .utf8))
|
||||
let layout = try Layout(xmlData: xmlData)
|
||||
XCTAssertEqual(layout.body, "FooBar")
|
||||
}
|
||||
@@ -167,7 +167,7 @@ final class XMLTests: XCTestCase {
|
||||
func testPreserveWhitespaceInsideHTML() throws {
|
||||
let html = "Some <b>bold </b>and<i> italic</i> text"
|
||||
let input = "<UILabel>\n \(html)\n</UILabel>"
|
||||
let xmlData = input.data(using: .utf8)!
|
||||
let xmlData = try XCTUnwrap(input.data(using: .utf8))
|
||||
let layout = try Layout(xmlData: xmlData)
|
||||
XCTAssertEqual(layout.body, html)
|
||||
}
|
||||
@@ -175,7 +175,7 @@ final class XMLTests: XCTestCase {
|
||||
func testPreserveHTMLAttributes() throws {
|
||||
let html = "An <img src=\"foo.jpg\"/> tag"
|
||||
let input = "<UILabel>\n \(html)\n</UILabel>"
|
||||
let xmlData = input.data(using: .utf8)!
|
||||
let xmlData = try XCTUnwrap(input.data(using: .utf8))
|
||||
let layout = try Layout(xmlData: xmlData)
|
||||
XCTAssertEqual(layout.body, html)
|
||||
}
|
||||
@@ -191,7 +191,7 @@ final class XMLTests: XCTestCase {
|
||||
func testNoEncodeHTMLEntitiesInText() throws {
|
||||
let text = "2 legs are < 4 legs"
|
||||
let input = "<UILabel>\(text.xmlEncoded())</UILabel>"
|
||||
let xmlData = input.data(using: .utf8)!
|
||||
let xmlData = try XCTUnwrap(input.data(using: .utf8))
|
||||
let layout = try Layout(xmlData: xmlData)
|
||||
XCTAssertEqual(layout.body, text)
|
||||
}
|
||||
@@ -199,7 +199,7 @@ final class XMLTests: XCTestCase {
|
||||
func testEncodeHTMLEntitiesInHTML() throws {
|
||||
let html = "2 legs are < 4 legs<br/>"
|
||||
let input = "<UILabel>\(html)</UILabel>"
|
||||
let xmlData = input.data(using: .utf8)!
|
||||
let xmlData = try XCTUnwrap(input.data(using: .utf8))
|
||||
let layout = try Layout(xmlData: xmlData)
|
||||
XCTAssertEqual(layout.body, html)
|
||||
}
|
||||
@@ -207,7 +207,7 @@ final class XMLTests: XCTestCase {
|
||||
func testEncodeHTMLEntitiesInHTML2() throws {
|
||||
let html = "<p>2 legs are < 4 legs</p>"
|
||||
let input = "<UILabel>\(html)</UILabel>"
|
||||
let xmlData = input.data(using: .utf8)!
|
||||
let xmlData = try XCTUnwrap(input.data(using: .utf8))
|
||||
let layout = try Layout(xmlData: xmlData)
|
||||
XCTAssertEqual(layout.body, html)
|
||||
}
|
||||
@@ -215,7 +215,7 @@ final class XMLTests: XCTestCase {
|
||||
func testEncodeHTMLEntitiesInHTML3() throws {
|
||||
let html = "<b>trial</b> & error"
|
||||
let input = "<UILabel>\(html)</UILabel>"
|
||||
let xmlData = input.data(using: .utf8)!
|
||||
let xmlData = try XCTUnwrap(input.data(using: .utf8))
|
||||
let layout = try Layout(xmlData: xmlData)
|
||||
XCTAssertEqual(layout.body, html)
|
||||
}
|
||||
|
||||
@@ -202,7 +202,7 @@ final class UIKitSymbols: XCTestCase {
|
||||
return result
|
||||
}
|
||||
|
||||
func testBuildLayoutToolSymbols() {
|
||||
func testBuildLayoutToolSymbols() throws {
|
||||
if #available(iOS 11.0, *) {} else {
|
||||
XCTFail("Must be run with latest iOS SDK to ensure all symbols are supported")
|
||||
return
|
||||
@@ -212,7 +212,7 @@ final class UIKitSymbols: XCTestCase {
|
||||
var output = ""
|
||||
let properties = getProperties()
|
||||
for name in properties.keys.sorted() {
|
||||
let props = properties[name]!
|
||||
let props = try XCTUnwrap(properties[name])
|
||||
var superclassName: String?
|
||||
if let cls = NSClassFromString(name), let superclass = class_getSuperclass(cls),
|
||||
superclass is UIView.Type || superclass is UIViewController.Type
|
||||
@@ -228,7 +228,7 @@ final class UIKitSymbols: XCTestCase {
|
||||
} else {
|
||||
output += "\n"
|
||||
for prop in props.keys.sorted() {
|
||||
let type = props[prop]!
|
||||
let type = try XCTUnwrap(props[prop])
|
||||
output += " \"\(prop)\": \"\(type)\",\n"
|
||||
}
|
||||
output += " ]\n"
|
||||
@@ -257,7 +257,7 @@ final class UIKitSymbols: XCTestCase {
|
||||
XCTAssertNoThrow(try output.write(to: url, atomically: true, encoding: .utf8))
|
||||
}
|
||||
|
||||
func testBuildSublimeCompletions() {
|
||||
func testBuildSublimeCompletions() throws {
|
||||
if #available(iOS 11.0, *) {} else {
|
||||
XCTFail("Must be run with latest iOS SDK to ensure all symbols are supported")
|
||||
return
|
||||
@@ -275,10 +275,10 @@ final class UIKitSymbols: XCTestCase {
|
||||
}
|
||||
let properties = getProperties()
|
||||
for name in properties.keys.sorted() {
|
||||
let props = properties[name]!
|
||||
let props = try XCTUnwrap(properties[name])
|
||||
rows.append("{ \"trigger\": \"\(name)\", \"contents\": \"\(name) $0/>\" }")
|
||||
for prop in props.keys.sorted() {
|
||||
let type = props[prop]!
|
||||
let type = try XCTUnwrap(props[prop])
|
||||
let row = "{ \"trigger\": \"\(prop)\t\(type)\", \"contents\": \"\(prop)\" }"
|
||||
if !rows.contains(row) {
|
||||
rows.append(row)
|
||||
|
||||
@@ -656,7 +656,7 @@ final class SprinterTests: XCTestCase {
|
||||
let string = "%c"
|
||||
let formatString = try FormatString(string)
|
||||
XCTAssertEqual(try formatString.print("a"), "a")
|
||||
XCTAssertEqual(try formatString.print("a"), String(format: string, UnicodeScalar("a")!.value))
|
||||
XCTAssertEqual(try formatString.print("a"), try String(format: string, XCTUnwrap(UnicodeScalar("a")?.value)))
|
||||
}
|
||||
|
||||
func testPrintUnicodeChar() throws {
|
||||
@@ -669,7 +669,7 @@ final class SprinterTests: XCTestCase {
|
||||
let string = "%C"
|
||||
let formatString = try FormatString(string)
|
||||
XCTAssertEqual(try formatString.print("ü"), "ü")
|
||||
XCTAssertEqual(try formatString.print("ü"), String(format: string, UnicodeScalar("ü")!.value))
|
||||
XCTAssertEqual(try formatString.print("ü"), try String(format: string, XCTUnwrap(UnicodeScalar("ü")?.value)))
|
||||
}
|
||||
|
||||
func testPrintUnicodeWideChar() throws {
|
||||
|
||||
@@ -6,7 +6,6 @@ import Foundation
|
||||
public extension FormatRule {
|
||||
static let noForceUnwrapInTests = FormatRule(
|
||||
help: "Use XCTUnwrap or #require in test cases, rather than force unwrapping.",
|
||||
disabledByDefault: true,
|
||||
orderAfter: [.urlMacro, .noForceTryInTests, .throwingTests]
|
||||
) { formatter in
|
||||
guard let testFramework = formatter.detectTestingFramework() else {
|
||||
|
||||
Reference in New Issue
Block a user