Skip to content

Commit

Permalink
Handle properly the conversion between String <-> NSString
Browse files Browse the repository at this point in the history
  • Loading branch information
rlaguilar committed Nov 11, 2018
1 parent 10d6b09 commit 0d1c557
Show file tree
Hide file tree
Showing 17 changed files with 86 additions and 88 deletions.
12 changes: 6 additions & 6 deletions Example/Tests/BoldElementTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,20 @@ class BoldElementTests: XCTestCase {

// MARK: - Styles tests
func testStyles_ReturnsBoldTrait() {
let markdown = "**Hello**"
let markdown: NSString = "**Hello**"

let fontTraits = element.styles(forMatch: markdown).first { $0.attributeKey == .fontTraits }

XCTAssertEqual(fontTraits?.value as? UIFontDescriptorSymbolicTraits, .traitBold)
XCTAssertEqual(fontTraits?.range, markdown.range, "The traits should be applied the whole string")
XCTAssertEqual(fontTraits?.range, NSRange(location: 0, length: markdown.length), "The traits should be applied the whole string")
}

func testStyles_ReturnsIndicatorsColor() {
let markdown = "**Hello**"
let markdown: NSString = "**Hello**"
let expectedColors = [element.symbolsColor, element.symbolsColor]
let expectedRanges = [
NSRange(location: 0, length: 2),
NSRange(location: markdown.count - 2, length: 2)
NSRange(location: markdown.length - 2, length: 2)
]

let styles = element.styles(forMatch: markdown)
Expand All @@ -135,14 +135,14 @@ class BoldElementTests: XCTestCase {

// MARK: - Replacement ranges
func testReplacementRanges_ReturnValidRanges() {
let markdown = "**Hello**"
let markdown: NSString = "**Hello**"
let expectedRanges = [
ReplacementRange(
range: NSRange(location: 0, length: 2),
replacementValue: NSAttributedString()
),
ReplacementRange(
range: NSRange(location: markdown.count - 2, length: 2),
range: NSRange(location: markdown.length - 2, length: 2),
replacementValue: NSAttributedString()
)
]
Expand Down
4 changes: 2 additions & 2 deletions Example/Tests/BulletElementTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class BulletElementTests: XCTestCase {

// MARK: - Styles tests
func testStyles_WhenCalled_ReturnsForegroundColorForIndicator() {
let samples = [
let samples: [NSString] = [
"- Hello",
"* Hello"
]
Expand All @@ -89,7 +89,7 @@ class BulletElementTests: XCTestCase {

// MARK: - Replacement ranges
func testReplacementRanges_ReturnValidRanges() {
let markdown = "- Hello"
let markdown: NSString = "- Hello"

let expectedRanges = [
ReplacementRange(
Expand Down
6 changes: 3 additions & 3 deletions Example/Tests/HeaderElementTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class HeaderElementTests: XCTestCase {

// MARK: - Styles tests
func testStyles_ReturnsCorrectFontSize() {
let samples = [
let samples: [NSString] = [
"# Hello",
"## Hello",
"### Hello",
Expand All @@ -117,7 +117,7 @@ class HeaderElementTests: XCTestCase {
}

func testStyles_ReturnsCorrectForegroundColor() {
let samples = [
let samples: [NSString] = [
"# Hello",
"## Hello",
"### Hello",
Expand All @@ -141,7 +141,7 @@ class HeaderElementTests: XCTestCase {

// MARK: - Replacement ranges
func testReplacementRanges_ReturnValidRanges() {
let markdown = "## Hello"
let markdown: NSString = "## Hello"
let expectedRanges = [
ReplacementRange(
range: NSRange(location: 0, length: 3),
Expand Down
12 changes: 6 additions & 6 deletions Example/Tests/InlineCodeElementTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,20 @@ class InlineCodeElementTests: XCTestCase {

// MARK: - Styles tests
func testStyles_ReturnsCodeFont() {
let markdown = "`Hello`"
let markdown: NSString = "`Hello`"

let fontStyle = element.styles(forMatch: markdown).first { $0.attributeKey == .font }

XCTAssertEqual(fontStyle?.value as? UIFont, codeFont.dynamic())
XCTAssertEqual(fontStyle?.range, markdown.range, "The font should be applied the whole string")
XCTAssertEqual(fontStyle?.range, NSRange(location: 0, length: markdown.length), "The font should be applied the whole string")
}

func testStyles_ReturnsIndicatorsColor() {
let markdown = "`Hello`"
let markdown: NSString = "`Hello`"
let expectedColors = [element.symbolsColor, element.symbolsColor]
let expectedRanges = [
NSRange(location: 0, length: 1),
NSRange(location: markdown.count - 1, length: 1)
NSRange(location: markdown.length - 1, length: 1)
]

let styles = element.styles(forMatch: markdown)
Expand All @@ -137,14 +137,14 @@ class InlineCodeElementTests: XCTestCase {

// MARK: - Replacement ranges
func testReplacementRanges_ReturnValidRanges() {
let markdown = "`Hello`"
let markdown: NSString = "`Hello`"
let expectedRanges = [
ReplacementRange(
range: NSRange(location: 0, length: 1),
replacementValue: NSAttributedString()
),
ReplacementRange(
range: NSRange(location: markdown.count - 1, length: 1),
range: NSRange(location: markdown.length - 1, length: 1),
replacementValue: NSAttributedString()
)
]
Expand Down
14 changes: 7 additions & 7 deletions Example/Tests/ItalicElementTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,20 @@ class ItalicElementTests: XCTestCase {

// MARK: - Styles tests
func testStyles_ReturnsItalicFontTrait() {
let markdown = "*Hello*"
let markdown: NSString = "*Hello*"

let fontTraits = element.styles(forMatch: markdown).first { $0.attributeKey == .fontTraits }

XCTAssertEqual(fontTraits?.value as? UIFontDescriptorSymbolicTraits, .traitItalic)
XCTAssertEqual(fontTraits?.range, markdown.range, "The traits should be applied the whole string")
XCTAssertEqual(fontTraits?.range, NSRange(location: 0, length: markdown.length), "The traits should be applied the whole string")
}

func testStyles_ReturnsIndicatorsColor() {
let markdown = "_Hello_"
let markdown: NSString = "_Hello_"
let expectedColors = [element.symbolsColor, element.symbolsColor]
let expectedRanges = [
NSRange(location: 0, length: 1),
NSRange(location: markdown.count - 1, length: 1)
NSRange(location: markdown.length - 1, length: 1)
]

let styles = element.styles(forMatch: markdown)
Expand All @@ -143,14 +143,14 @@ class ItalicElementTests: XCTestCase {

// MARK: - Replacement ranges
func testReplacementRanges_ReturnValidRanges() {
let markdown = "_Hello_"
let markdown: NSString = "_Hello_"
let expectedRanges = [
ReplacementRange(
range: NSRange(location: 0, length: 1),
replacementValue: NSAttributedString()
),
ReplacementRange(
range: NSRange(location: markdown.count - 1, length: 1),
range: NSRange(location: markdown.length - 1, length: 1),
replacementValue: NSAttributedString()
)
]
Expand Down
10 changes: 5 additions & 5 deletions Example/Tests/LinkElementTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class LinkElementTests: XCTestCase {

// MARK: - Styles tests
func testStyles_ReturnsValidStylesCount() {
let markdown = "[a](sample.com)"
let markdown: NSString = "[a](sample.com)"

let styles = element.styles(forMatch: markdown)

Expand All @@ -129,7 +129,7 @@ class LinkElementTests: XCTestCase {
}

func testStyles_ReturnsLinkAction() {
let markdown = "[A](sample.com)"
let markdown: NSString = "[A](sample.com)"

let linkAction = element.styles(forMatch: markdown).first { $0.attributeKey == .link }

Expand All @@ -138,7 +138,7 @@ class LinkElementTests: XCTestCase {
}

func testStyles_ReturnsLinkColor() {
let markdown = "[A](www.sample.com)"
let markdown: NSString = "[A](www.sample.com)"

let linkColor = element.styles(forMatch: markdown)
.first { $0.attributeKey == .foregroundColor && $0.startIndex == 1 }
Expand All @@ -148,7 +148,7 @@ class LinkElementTests: XCTestCase {
}

func testStyles_ReturnsIndicatorsColor() {
let markdown = "[a](www.s.com)"
let markdown: NSString = "[a](www.s.com)"

let expectedRanges = [
NSRange(location: 0, length: 1),
Expand All @@ -169,7 +169,7 @@ class LinkElementTests: XCTestCase {

// MARK: - Replacement ranges
func testReplacementRanges_ReturnValidRanges() {
let markdown = "[a](sample.com)"
let markdown: NSString = "[a](sample.com)"
let expectedRanges = [
ReplacementRange(
range: NSRange(location: 0, length: 1),
Expand Down
12 changes: 6 additions & 6 deletions Example/Tests/StrikethroughElementTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ class StrikethroughElementTests: XCTestCase {

// MARK: - Styles tests
func testStyles_ReturnsStrikethroughSyle() {
let markdown = "~~Hello~~"
let expectedRange = NSRange(location: 2, length: markdown.count - 4)
let markdown: NSString = "~~Hello~~"
let expectedRange = NSRange(location: 2, length: markdown.length - 4)

let strikethrough = element.styles(forMatch: markdown).first { $0.attributeKey == .strikethroughStyle }

Expand All @@ -119,11 +119,11 @@ class StrikethroughElementTests: XCTestCase {
}

func testStyles_ReturnsIndicatorsColor() {
let markdown = "~~Hello~~"
let markdown: NSString = "~~Hello~~"
let expectedColors = [element.symbolsColor, element.symbolsColor]
let expectedRanges = [
NSRange(location: 0, length: 2),
NSRange(location: markdown.count - 2, length: 2)
NSRange(location: markdown.length - 2, length: 2)
]

let styles = element.styles(forMatch: markdown)
Expand All @@ -136,14 +136,14 @@ class StrikethroughElementTests: XCTestCase {

// MARK: - Replacement ranges
func testReplacementRanges_ReturnValidRanges() {
let markdown = "~~Hello~~"
let markdown: NSString = "~~Hello~~"
let expectedRanges = [
ReplacementRange(
range: NSRange(location: 0, length: 2),
replacementValue: NSAttributedString()
),
ReplacementRange(
range: NSRange(location: markdown.count - 2, length: 2),
range: NSRange(location: markdown.length - 2, length: 2),
replacementValue: NSAttributedString()
)
]
Expand Down
10 changes: 5 additions & 5 deletions Markdowner/Classes/Default Elements/BoldElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ open class BoldElement: MarkdownElement {
super.init(regex: regex)
}

open override func styles(forMatch match: String) -> [MarkdownElement.Style] {
open override func styles(forMatch match: NSString) -> [MarkdownElement.Style] {
let fontStyle = Style(
attributeKey: .fontTraits,
value: UIFontDescriptorSymbolicTraits.traitBold,
startIndex: 0,
length: match.count
length: match.length
)

let indicatorRanges = [
NSRange(location: 0, length: 2),
NSRange(location: match.count - 2, length: 2)
NSRange(location: match.length - 2, length: 2)
]

let foregroundStyles = indicatorRanges.map {
Expand All @@ -51,8 +51,8 @@ open class BoldElement: MarkdownElement {
return BoldElement(symbolsColor: stylesConfiguration.symbolsColor)
}

open override func replacementRanges(forMatch match: String) -> [ReplacementRange] {
let ranges = [NSRange(location: 0, length: 2), NSRange(location: match.count - 2, length: 2)]
open override func replacementRanges(forMatch match: NSString) -> [ReplacementRange] {
let ranges = [NSRange(location: 0, length: 2), NSRange(location: match.length - 2, length: 2)]
return ranges.map { ReplacementRange(range: $0, replacementValue: NSAttributedString()) }
}
}
4 changes: 2 additions & 2 deletions Markdowner/Classes/Default Elements/BulletElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ open class BulletElement: MarkdownElement {
super.init(regex: regex)
}

open override func styles(forMatch match: String) -> [MarkdownElement.Style] {
open override func styles(forMatch match: NSString) -> [MarkdownElement.Style] {
let indicatorColorStyle = Style.init(
attributeKey: .foregroundColor,
value: symbolsColor,
Expand All @@ -41,7 +41,7 @@ open class BulletElement: MarkdownElement {
return [indicatorColorStyle]
}

open override func replacementRanges(forMatch match: String) -> [ReplacementRange] {
open override func replacementRanges(forMatch match: NSString) -> [ReplacementRange] {
let range = NSRange(location: 0, length: 2)
let font = useDynamicType ? self.font.dynamic() : self.font

Expand Down
12 changes: 6 additions & 6 deletions Markdowner/Classes/Default Elements/HeaderElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ open class HeaderElement: MarkdownElement {
super.init(regex: regex)
}

open override func styles(forMatch match: String) -> [MarkdownElement.Style] {
open override func styles(forMatch match: NSString) -> [MarkdownElement.Style] {
let level = self.level(forMatch: match)

let fontStyle = Style(
attributeKey: .font,
value: fontProvider.font(forLevel: level),
startIndex: 0,
length: match.count
length: match.length
)

let indicatorForegroundStyle = Style(
Expand All @@ -58,17 +58,17 @@ open class HeaderElement: MarkdownElement {
)
}

open override func replacementRanges(forMatch match: String) -> [ReplacementRange] {
open override func replacementRanges(forMatch match: NSString) -> [ReplacementRange] {
let matchLevel = self.level(forMatch: match)
let range = NSRange(location: 0, length: matchLevel.rawValue + 1)
let replacementRange = ReplacementRange(range: range, replacementValue: NSAttributedString())
return [replacementRange]
}

private func level(forMatch match: String) -> Level {
let fullRange = NSRange(location: 0, length: match.count)
private func level(forMatch match: NSString) -> Level {
let fullRange = NSRange(location: 0, length: match.length)

guard let regexMatch = regex.matches(in: match, options: [], range: fullRange).first else {
guard let regexMatch = regex.matches(in: match as String, options: [], range: fullRange).first else {
fatalError("ERROR: Unable to find match for the given input")
}

Expand Down
10 changes: 5 additions & 5 deletions Markdowner/Classes/Default Elements/InlineCodeElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ open class InlineCodeElement: MarkdownElement {
super.init(regex: regex)
}

open override func styles(forMatch match: String) -> [MarkdownElement.Style] {
open override func styles(forMatch match: NSString) -> [MarkdownElement.Style] {
let fontStyle = Style(
attributeKey: .font,
value: useDynamicType ? font.dynamic() : font,
startIndex: 0,
length: match.count
length: match.length
)

let indicatorRanges = [
NSRange(location: 0, length: 1),
NSRange(location: match.count - 1, length: 1)
NSRange(location: match.length - 1, length: 1)
]

let foregroundStyles = indicatorRanges.map {
Expand All @@ -60,8 +60,8 @@ open class InlineCodeElement: MarkdownElement {
)
}

open override func replacementRanges(forMatch match: String) -> [ReplacementRange] {
let ranges = [NSRange(location: 0, length: 1), NSRange(location: match.count - 1, length: 1)]
open override func replacementRanges(forMatch match: NSString) -> [ReplacementRange] {
let ranges = [NSRange(location: 0, length: 1), NSRange(location: match.length - 1, length: 1)]
return ranges.map { ReplacementRange(range: $0, replacementValue: NSAttributedString()) }
}
}
Loading

0 comments on commit 0d1c557

Please sign in to comment.