Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added factory functions for creating TextStyles based off existing values #18

Closed
wants to merge 11 commits into from
12 changes: 12 additions & 0 deletions Example/Marker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
objects = {

/* Begin PBXBuildFile section */
2FC3E7E41F0FC57E0024E2D1 /* TextStyleFactoryFunctionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2FC3E7E21F0FC5500024E2D1 /* TextStyleFactoryFunctionTests.swift */; };
2FC3E7E71F0FFB2B0024E2D1 /* TextStyleEquatableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2FC3E7E51F0FFB220024E2D1 /* TextStyleEquatableTests.swift */; };
2FC3E7EA1F0FFEF90024E2D1 /* TextTransformEquatableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2FC3E7E81F0FFEEC0024E2D1 /* TextTransformEquatableTests.swift */; };
4811C04D1D0B6FB5007279CB /* AppTheme.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4811C04C1D0B6FB5007279CB /* AppTheme.swift */; };
485ED1131D09E89F00119D5D /* AvenirNextFont.swift in Sources */ = {isa = PBXBuildFile; fileRef = 485ED1121D09E89F00119D5D /* AvenirNextFont.swift */; };
485ED1181D0A091800119D5D /* ThemeSettingsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 485ED1171D0A091800119D5D /* ThemeSettingsViewController.swift */; };
Expand Down Expand Up @@ -38,6 +41,9 @@
08F1825B7EA29E36898092FB /* Marker.podspec */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = Marker.podspec; path = ../Marker.podspec; sourceTree = "<group>"; };
1E6E60D0D2F5667C8156B123 /* Pods_Marker_Tests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Marker_Tests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
2408A1D80FB40943D0B15C58 /* Pods-Marker_Tests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Marker_Tests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Marker_Tests/Pods-Marker_Tests.debug.xcconfig"; sourceTree = "<group>"; };
2FC3E7E21F0FC5500024E2D1 /* TextStyleFactoryFunctionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TextStyleFactoryFunctionTests.swift; sourceTree = "<group>"; };
2FC3E7E51F0FFB220024E2D1 /* TextStyleEquatableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TextStyleEquatableTests.swift; sourceTree = "<group>"; };
2FC3E7E81F0FFEEC0024E2D1 /* TextTransformEquatableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TextTransformEquatableTests.swift; sourceTree = "<group>"; };
31D2DC9BD8B466270B5299FF /* README.md */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
4811C04C1D0B6FB5007279CB /* AppTheme.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppTheme.swift; sourceTree = "<group>"; };
485ED1121D09E89F00119D5D /* AvenirNextFont.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AvenirNextFont.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -173,6 +179,9 @@
isa = PBXGroup;
children = (
486EC7691DB440D3009BCA7A /* ParserTests.swift */,
2FC3E7E51F0FFB220024E2D1 /* TextStyleEquatableTests.swift */,
2FC3E7E21F0FC5500024E2D1 /* TextStyleFactoryFunctionTests.swift */,
2FC3E7E81F0FFEEC0024E2D1 /* TextTransformEquatableTests.swift */,
607FACE91AFB9204008FA782 /* Supporting Files */,
);
path = Tests;
Expand Down Expand Up @@ -414,7 +423,10 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
2FC3E7EA1F0FFEF90024E2D1 /* TextTransformEquatableTests.swift in Sources */,
486EC76B1DB440E8009BCA7A /* ParserTests.swift in Sources */,
2FC3E7E71F0FFB2B0024E2D1 /* TextStyleEquatableTests.swift in Sources */,
2FC3E7E41F0FC57E0024E2D1 /* TextStyleFactoryFunctionTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
56 changes: 51 additions & 5 deletions Example/Tests/ParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,42 @@ class ParserTests: XCTestCase {
}
}

func testParseUnderlinedElements() {
do {
let (parsedString, parsedElements) = try MarkdownParser.parse("==abc== def ==ghi==")

XCTAssert(parsedString == "abc def ghi")
XCTAssert(parsedElements.count == 2)

parsedElements.forEach { XCTAssert($0.isUnderlinedElement()) }

XCTAssert(parsedElements[0].range == parsedString.range(of: "abc"))
XCTAssert(parsedElements[1].range == parsedString.range(of: "ghi"))
} catch {
XCTFail("Parsing failed.")
}
}

func testParseMixedElements() {
do {
let (parsedString, parsedElements) = try MarkdownParser.parse("*abc* __def__ _ghi_ **jkl** ~~mno~~")
let (parsedString, parsedElements) = try MarkdownParser.parse("*abc* __def__ _ghi_ **jkl** ~~mno~~ ==pqr==")

XCTAssert(parsedString == "abc def ghi jkl mno")
XCTAssert(parsedElements.count == 5)
XCTAssert(parsedString == "abc def ghi jkl mno pqr")
XCTAssert(parsedElements.count == 6)

XCTAssert(parsedElements[0].isEmElement())
XCTAssert(parsedElements[1].isStrongElement())
XCTAssert(parsedElements[2].isEmElement())
XCTAssert(parsedElements[3].isStrongElement())
XCTAssert(parsedElements[4].isStrikethroughElement())
XCTAssert(parsedElements[5].isUnderlinedElement())

XCTAssert(parsedElements[0].range == parsedString.range(of: "abc"))
XCTAssert(parsedElements[1].range == parsedString.range(of: "def"))
XCTAssert(parsedElements[2].range == parsedString.range(of: "ghi"))
XCTAssert(parsedElements[3].range == parsedString.range(of: "jkl"))
XCTAssert(parsedElements[4].range == parsedString.range(of: "mno"))
XCTAssert(parsedElements[5].range == parsedString.range(of: "pqr"))
} catch {
XCTFail("Parsing failed.")
}
Expand Down Expand Up @@ -116,22 +134,35 @@ class ParserTests: XCTestCase {
}
}

func testLiteralEqual() {
do {
let (parsedString, parsedElements) = try MarkdownParser.parse("two + two = four")

XCTAssert(parsedString == "two + two = four")
XCTAssert(parsedElements.count == 0)
} catch {
XCTFail("Parsing failed.")
}
}

func testElementsInMiddleOfWords() {
do {
let (parsedString, parsedElements) = try MarkdownParser.parse("the_quick_brown*fox*jumps__over__the**lazy**dog.")
let (parsedString, parsedElements) = try MarkdownParser.parse("the_quick_brown*fox*jumps__over__the**lazy**==dog==.")

XCTAssert(parsedString == "thequickbrownfoxjumpsoverthelazydog.")
XCTAssert(parsedElements.count == 4)
XCTAssert(parsedElements.count == 5)

XCTAssert(parsedElements[0].isEmElement())
XCTAssert(parsedElements[1].isEmElement())
XCTAssert(parsedElements[2].isStrongElement())
XCTAssert(parsedElements[3].isStrongElement())
XCTAssert(parsedElements[4].isUnderlinedElement())

XCTAssert(parsedElements[0].range == parsedString.range(of: "quick"))
XCTAssert(parsedElements[1].range == parsedString.range(of: "fox"))
XCTAssert(parsedElements[2].range == parsedString.range(of: "over"))
XCTAssert(parsedElements[3].range == parsedString.range(of: "lazy"))
XCTAssert(parsedElements[4].range == parsedString.range(of: "dog"))
} catch {
XCTFail("Parsing failed.")
}
Expand Down Expand Up @@ -163,6 +194,12 @@ class ParserTests: XCTestCase {
} catch {
XCTAssert(error as! ElementParser.ParserError == ElementParser.ParserError.unclosedTags)
}

do {
let _ = try MarkdownParser.parse("Not ==correct.")
} catch {
XCTAssert(error as! ElementParser.ParserError == ElementParser.ParserError.unclosedTags)
}
}

}
Expand Down Expand Up @@ -196,4 +233,13 @@ private extension MarkdownElement {
}
}

func isUnderlinedElement() -> Bool {
switch self {
case .underline(_):
return true
default:
return false
}
}

}
110 changes: 110 additions & 0 deletions Example/Tests/TextStyleEquatableTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//
// TextStyleEquatableTests.swift
// Marker
//
// Created by Harlan Kellaway on 7/7/17.
//
//

import UIKit
import XCTest
@testable import Marker

class TextStyleEquatableTests: XCTestCase {

var textStyle: TextStyle!

override func setUp() {
super.setUp()

textStyle = TextStyle(font: UIFont(name: "Helvetica", size: 10)!,
emFont: UIFont(name: "Helvetica-Oblique", size: 10)!,
strongFont: UIFont(name: "Helvetica-Bold", size: 10)!,
textColor: UIColor.red,
characterSpacing: 1,
lineSpacing: 2,
lineHeightMultiple: 3,
minimumLineHeight: 4,
maximumLineHeight: 5,
firstLineHeadIndent: 6,
headIndent: 7,
paragraphSpacing: 8,
paragraphSpacingBefore: 9,
textAlignment: .left,
lineBreakMode: .byWordWrapping,
strikethroughStyle: .styleSingle,
strikethroughColor: UIColor.red,
textTransform: .lowercased)
}

override func tearDown() {
textStyle = nil

super.tearDown()
}

func testTextStyle_isEqual_whenAllPropertiesTheSame() {
let sameTextStyle = TextStyle(font: textStyle.font,
emFont: textStyle.emFont,
strongFont: textStyle.strongFont,
textColor: textStyle.textColor,
characterSpacing: textStyle.characterSpacing,
lineSpacing: textStyle.lineSpacing,
lineHeightMultiple: textStyle.lineHeightMultiple,
minimumLineHeight: textStyle.minimumLineHeight,
maximumLineHeight: textStyle.maximumLineHeight,
firstLineHeadIndent: textStyle.firstLineHeadIndent,
headIndent: textStyle.headIndent,
paragraphSpacing: textStyle.paragraphSpacing,
paragraphSpacingBefore: textStyle.paragraphSpacingBefore,
textAlignment: textStyle.textAlignment,
lineBreakMode: textStyle.lineBreakMode,
strikethroughStyle: textStyle.strikethroughStyle,
strikethroughColor: textStyle.strikethroughColor,
textTransform: textStyle.textTransform)

XCTAssertEqual(textStyle, sameTextStyle)
}

func testTextStyle_isNotEqual_whenPropertiesAreDiffernt() {
let differentFont = textStyle.with(newFont: textStyle.font.withSize(textStyle.font.pointSize + 10))
let differentEmFont = textStyle.with(newEmFont: textStyle.emFont.withSize(textStyle.emFont.pointSize + 10))
let differentStrongFont = textStyle.with(newStrongFont: textStyle.strongFont.withSize(textStyle.strongFont.pointSize + 10))
let differentTextColor = textStyle.with(newTextColor: UIColor.blue)
let differentCharacterSpacing = textStyle.with(newCharacterSpacing: textStyle.characterSpacing! + 10)
let differentLineSpacing = textStyle.with(newLineSpacing: textStyle.lineSpacing! + 10)
let differentLineHeightMultiple = textStyle.with(newLineHeightMultiple: textStyle.lineHeightMultiple! + 10)
let differentMinimumLineHeight = textStyle.with(newMinimumLineHeight: textStyle.minimumLineHeight! + 10)
let differentMaximumLineHeight = textStyle.with(newMaximumLineHeight: textStyle.maximumLineHeight! + 10)
let differentFirstLineHeadIndent = textStyle.with(newFirstLineHeadIndent: textStyle.firstLineHeadIndent! + 10)
let differentHeadIndent = textStyle.with(newHeadIndent: textStyle.headIndent! + 10)
let differentParagraphSpacing = textStyle.with(newParagraphSpacing: textStyle.paragraphSpacing! + 10)
let differentParagraphSpacingBefore = textStyle.with(newParagraphSpacingBefore: textStyle.paragraphSpacingBefore! + 10)
let differentTextAlignment = textStyle.with(newTextAlignment: .right)
let differentLineBreakMode = textStyle.with(newLineBreakMode: .byCharWrapping)
let differentStrikethroughStyle = textStyle.with(newStrikethroughStyle: .styleDouble)
let differentStrikethroughColor = textStyle.with(newStrikethroughColor: UIColor.blue)
let differentTextTransform = textStyle.with(newTextTransform: .uppercased)


XCTAssertNotEqual(textStyle, differentFont)
XCTAssertNotEqual(textStyle, differentEmFont)
XCTAssertNotEqual(textStyle, differentStrongFont)
XCTAssertNotEqual(textStyle, differentTextColor)
XCTAssertNotEqual(textStyle, differentCharacterSpacing)
XCTAssertNotEqual(textStyle, differentLineSpacing)
XCTAssertNotEqual(textStyle, differentLineHeightMultiple)
XCTAssertNotEqual(textStyle, differentMinimumLineHeight)
XCTAssertNotEqual(textStyle, differentMaximumLineHeight)
XCTAssertNotEqual(textStyle, differentFirstLineHeadIndent)
XCTAssertNotEqual(textStyle, differentHeadIndent)
XCTAssertNotEqual(textStyle, differentParagraphSpacing)
XCTAssertNotEqual(textStyle, differentParagraphSpacingBefore)
XCTAssertNotEqual(textStyle, differentTextAlignment)
XCTAssertNotEqual(textStyle, differentLineBreakMode)
XCTAssertNotEqual(textStyle, differentStrikethroughStyle)
XCTAssertNotEqual(textStyle, differentStrikethroughColor)
XCTAssertNotEqual(textStyle, differentTextTransform)
}

}
Loading