// Copyright © 2017 Schibsted. All rights reserved.
import XCTest
@testable import Layout
final class AttributedStringExpressionTests: XCTestCase {
func testAttributedStringExpressionTextAndFont() throws {
let node = LayoutNode()
let expression = LayoutExpression(attributedStringExpression: "foo", for: node)
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))
}
func testAttributedStringHTMLExpression() throws {
let node = LayoutNode()
let expression = LayoutExpression(attributedStringExpression: "foo", for: node)
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))
}
func testAttributedStringContainingUnicode() throws {
let node = LayoutNode()
let text = "🤔😂"
let expression = LayoutExpression(attributedStringExpression: "\(text)", for: node)
let result = try XCTUnwrap(try expression?.evaluate() as? NSAttributedString)
XCTAssertEqual(result.string, text)
}
func testAttributedStringInheritsFont() throws {
let label = UILabel()
label.font = UIFont(name: "Courier", size: 57)
let node = LayoutNode(view: label)
let expression = LayoutExpression(attributedStringExpression: "foo", for: node)
let result = try XCTUnwrap(try expression?.evaluate() as? NSAttributedString)
XCTAssertEqual(result.attribute(NSAttributedString.Key.font, at: 0, effectiveRange: nil) as? UIFont, label.font)
}
func testAttributedStringInheritsTextColor() throws {
let label = UILabel()
label.textColor = .red
let node = LayoutNode(view: label)
let expression = LayoutExpression(attributedStringExpression: "foo", for: node)
let result = try XCTUnwrap(try expression?.evaluate() as? NSAttributedString)
XCTAssertEqual(result.attribute(NSAttributedString.Key.foregroundColor, at: 0, effectiveRange: nil) as? UIColor, .red)
}
func testAttributedStringInheritsTextAlignment() throws {
let label = UILabel()
label.textAlignment = .right
let node = LayoutNode(view: label)
let expression = LayoutExpression(attributedStringExpression: "foo", for: node)
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)
}
func testAttributedStringInheritsLinebreakMode() throws {
let label = UILabel()
label.lineBreakMode = .byTruncatingHead
let node = LayoutNode(view: label)
let expression = LayoutExpression(attributedStringExpression: "foo", for: node)
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 XCTUnwrap(try expression?.evaluate() as? NSAttributedString)
XCTAssertEqual(result.string, "hello world bar")
}
func testAttributedStringContainingAttributedStringConstant() throws {
let node = LayoutNode(constants: ["bar": NSAttributedString(string: "bar", attributes: [
NSAttributedString.Key.foregroundColor: UIColor.red,
])])
let expression = LayoutExpression(attributedStringExpression: "hello world {bar}", for: node)
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)
}
func testAttributedStringContainingHTMLConstant() throws {
let node = LayoutNode(constants: ["bar": "bar"])
let expression = LayoutExpression(attributedStringExpression: "foo {bar}", for: node)
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
XCTAssert(traits?.contains(.traitItalic) == true)
XCTAssert(traits?.contains(.traitBold) == true)
}
func testAttributedStringContainingAmbiguousTokens() throws {
let node = LayoutNode(constants: ["foo": "$(2)", "bar": "$(3)"])
let expression = LayoutExpression(attributedStringExpression: "$(1){foo}{bar}", for: node)
let result = try XCTUnwrap(try expression?.evaluate() as? NSAttributedString)
XCTAssertEqual(result.string, "$(1)$(2)$(3)")
}
}