diff --git a/Components/Tests/ComponentsTests/WKSourceEditorFormatterTests.swift b/Components/Tests/ComponentsTests/WKSourceEditorFormatterTests.swift index a3764296794..f54b394874f 100644 --- a/Components/Tests/ComponentsTests/WKSourceEditorFormatterTests.swift +++ b/Components/Tests/ComponentsTests/WKSourceEditorFormatterTests.swift @@ -11,8 +11,9 @@ final class WKSourceEditorFormatterTests: XCTestCase { var boldItalicsFormatter: WKSourceEditorFormatterBoldItalics! var templateFormatter: WKSourceEditorFormatterTemplate! var strikethroughFormatter: WKSourceEditorFormatterStrikethrough! + var linkFormatter: WKSourceEditorFormatterLink! var formatters: [WKSourceEditorFormatter] { - return [baseFormatter, templateFormatter, boldItalicsFormatter, strikethroughFormatter] + return [baseFormatter, templateFormatter, boldItalicsFormatter, strikethroughFormatter, linkFormatter] } override func setUpWithError() throws { @@ -23,6 +24,7 @@ final class WKSourceEditorFormatterTests: XCTestCase { self.colors.orangeForegroundColor = WKTheme.light.editorOrange self.colors.purpleForegroundColor = WKTheme.light.editorPurple self.colors.greenForegroundColor = WKTheme.light.editorGreen + self.colors.blueForegroundColor = WKTheme.light.editorBlue self.fonts = WKSourceEditorFonts() self.fonts.baseFont = WKFont.for(.body, compatibleWith: traitCollection) @@ -34,6 +36,7 @@ final class WKSourceEditorFormatterTests: XCTestCase { self.boldItalicsFormatter = WKSourceEditorFormatterBoldItalics(colors: colors, fonts: fonts) self.templateFormatter = WKSourceEditorFormatterTemplate(colors: colors, fonts: fonts) self.strikethroughFormatter = WKSourceEditorFormatterStrikethrough(colors: colors, fonts: fonts) + self.linkFormatter = WKSourceEditorFormatterLink(colors: colors, fonts: fonts) } override func tearDownWithError() throws { @@ -766,4 +769,166 @@ final class WKSourceEditorFormatterTests: XCTestCase { XCTAssertEqual(base2Attributes[.font] as! UIFont, fonts.baseFont, "Incorrect base formatting") XCTAssertEqual(base2Attributes[.foregroundColor] as! UIColor, colors.baseForegroundColor, "Incorrect base formatting") } + + func testPlainLink() { + let string = "Testing. [[Link with spaces]]. Testing" + let mutAttributedString = NSMutableAttributedString(string: string) + + for formatter in formatters { + formatter.addSyntaxHighlighting(to: mutAttributedString, in: NSRange(location: 0, length: string.count)) + } + + var base1Range = NSRange(location: 0, length: 0) + let base1Attributes = mutAttributedString.attributes(at: 0, effectiveRange: &base1Range) + + var linkOpenRange = NSRange(location: 0, length: 0) + let linkOpenAttributes = mutAttributedString.attributes(at: 9, effectiveRange: &linkOpenRange) + + var linkContentRange = NSRange(location: 0, length: 0) + let linkContentAttributes = mutAttributedString.attributes(at: 11, effectiveRange: &linkContentRange) + + var linkCloseRange = NSRange(location: 0, length: 0) + let linkCloseAttributes = mutAttributedString.attributes(at: 27, effectiveRange: &linkCloseRange) + + var base2Range = NSRange(location: 0, length: 0) + let base2Attributes = mutAttributedString.attributes(at: 29, effectiveRange: &base2Range) + + // "Testing. " + XCTAssertEqual(base1Range.location, 0, "Incorrect base formatting") + XCTAssertEqual(base1Range.length, 9, "Incorrect base formatting") + XCTAssertEqual(base1Attributes[.font] as! UIFont, fonts.baseFont, "Incorrect base formatting") + XCTAssertEqual(base1Attributes[.foregroundColor] as! UIColor, colors.baseForegroundColor, "Incorrect base formatting") + + // "[[" + XCTAssertEqual(linkOpenRange.location, 9, "Incorrect link formatting") + XCTAssertEqual(linkOpenRange.length, 2, "Incorrect link formatting") + XCTAssertEqual(linkOpenAttributes[.font] as! UIFont, fonts.baseFont, "Incorrect link formatting") + XCTAssertEqual(linkOpenAttributes[.foregroundColor] as! UIColor, colors.blueForegroundColor, "Incorrect link formatting") + + // "Link with spaces" + XCTAssertEqual(linkContentRange.location, 11, "Incorrect link content formatting") + XCTAssertEqual(linkContentRange.length, 16, "Incorrect link content formatting") + XCTAssertEqual(linkContentAttributes[.font] as! UIFont, fonts.baseFont, "Incorrect link content formatting") + XCTAssertEqual(linkContentAttributes[.foregroundColor] as! UIColor, colors.blueForegroundColor, "Incorrect content formatting") + + // "]]" + XCTAssertEqual(linkCloseRange.location, 27, "Incorrect link formatting") + XCTAssertEqual(linkCloseRange.length, 2, "Incorrect link formatting") + XCTAssertEqual(linkCloseAttributes[.font] as! UIFont, fonts.baseFont, "Incorrect link formatting") + XCTAssertEqual(linkCloseAttributes[.foregroundColor] as! UIColor, colors.blueForegroundColor, "Incorrect link formatting") + + // ". Testing" + XCTAssertEqual(base2Range.location, 29, "Incorrect base formatting") + XCTAssertEqual(base2Range.length, 9, "Incorrect base formatting") + XCTAssertEqual(base2Attributes[.font] as! UIFont, fonts.baseFont, "Incorrect base formatting") + XCTAssertEqual(base2Attributes[.foregroundColor] as! UIColor, colors.baseForegroundColor, "Incorrect base formatting") + } + + func testPipedLink() { + let string = "Testing. [[Link|Link with spaces]]. Testing" + let mutAttributedString = NSMutableAttributedString(string: string) + + for formatter in formatters { + formatter.addSyntaxHighlighting(to: mutAttributedString, in: NSRange(location: 0, length: string.count)) + } + + var base1Range = NSRange(location: 0, length: 0) + let base1Attributes = mutAttributedString.attributes(at: 0, effectiveRange: &base1Range) + + var linkOpenRange = NSRange(location: 0, length: 0) + let linkOpenAttributes = mutAttributedString.attributes(at: 9, effectiveRange: &linkOpenRange) + + var linkContentRange = NSRange(location: 0, length: 0) + let linkContentAttributes = mutAttributedString.attributes(at: 11, effectiveRange: &linkContentRange) + + var linkCloseRange = NSRange(location: 0, length: 0) + let linkCloseAttributes = mutAttributedString.attributes(at: 32, effectiveRange: &linkCloseRange) + + var base2Range = NSRange(location: 0, length: 0) + let base2Attributes = mutAttributedString.attributes(at: 34, effectiveRange: &base2Range) + + // "Testing. " + XCTAssertEqual(base1Range.location, 0, "Incorrect base formatting") + XCTAssertEqual(base1Range.length, 9, "Incorrect base formatting") + XCTAssertEqual(base1Attributes[.font] as! UIFont, fonts.baseFont, "Incorrect base formatting") + XCTAssertEqual(base1Attributes[.foregroundColor] as! UIColor, colors.baseForegroundColor, "Incorrect base formatting") + + // "[[" + XCTAssertEqual(linkOpenRange.location, 9, "Incorrect link formatting") + XCTAssertEqual(linkOpenRange.length, 2, "Incorrect link formatting") + XCTAssertEqual(linkOpenAttributes[.font] as! UIFont, fonts.baseFont, "Incorrect link formatting") + XCTAssertEqual(linkOpenAttributes[.foregroundColor] as! UIColor, colors.blueForegroundColor, "Incorrect link formatting") + + // "Link|Link with spaces" + XCTAssertEqual(linkContentRange.location, 11, "Incorrect link content formatting") + XCTAssertEqual(linkContentRange.length, 21, "Incorrect link content formatting") + XCTAssertEqual(linkContentAttributes[.font] as! UIFont, fonts.baseFont, "Incorrect link content formatting") + XCTAssertEqual(linkContentAttributes[.foregroundColor] as! UIColor, colors.blueForegroundColor, "Incorrect content formatting") + + // "]]" + XCTAssertEqual(linkCloseRange.location, 32, "Incorrect link formatting") + XCTAssertEqual(linkCloseRange.length, 2, "Incorrect link formatting") + XCTAssertEqual(linkCloseAttributes[.font] as! UIFont, fonts.baseFont, "Incorrect link formatting") + XCTAssertEqual(linkCloseAttributes[.foregroundColor] as! UIColor, colors.blueForegroundColor, "Incorrect link formatting") + + // ". Testing" + XCTAssertEqual(base2Range.location, 34, "Incorrect base formatting") + XCTAssertEqual(base2Range.length, 9, "Incorrect base formatting") + XCTAssertEqual(base2Attributes[.font] as! UIFont, fonts.baseFont, "Incorrect base formatting") + XCTAssertEqual(base2Attributes[.foregroundColor] as! UIColor, colors.baseForegroundColor, "Incorrect base formatting") + } + + func testLinkFileWithNestedLinks() { + let string = "[[File:Cat with fish.jpg|thumb|left|Cat with [[fish]]|alt=Photo of cat looking at fish]]" + let mutAttributedString = NSMutableAttributedString(string: string) + + for formatter in formatters { + formatter.addSyntaxHighlighting(to: mutAttributedString, in: NSRange(location: 0, length: string.count)) + } + + var linkRange1 = NSRange(location: 0, length: 0) + let linkAttributes1 = mutAttributedString.attributes(at: 0, effectiveRange: &linkRange1) + + var linkRange2 = NSRange(location: 0, length: 0) + let linkAttributes2 = mutAttributedString.attributes(at: 45, effectiveRange: &linkRange2) + + var linkRange3 = NSRange(location: 0, length: 0) + let linkAttributes3 = mutAttributedString.attributes(at: 47, effectiveRange: &linkRange3) + + var linkRange4 = NSRange(location: 0, length: 0) + let linkAttributes4 = mutAttributedString.attributes(at: 51, effectiveRange: &linkRange4) + + var linkRange5 = NSRange(location: 0, length: 0) + let linkAttributes5 = mutAttributedString.attributes(at: 53, effectiveRange: &linkRange5) + + //"[[File:Cat with fish.jpg|thumb|left|Cat with " + XCTAssertEqual(linkRange1.location, 0, "Incorrect link formatting") + XCTAssertEqual(linkRange1.length, 45, "Incorrect link formatting") + XCTAssertEqual(linkAttributes1[.font] as! UIFont, fonts.baseFont, "Incorrect base formatting") + XCTAssertEqual(linkAttributes1[.foregroundColor] as! UIColor, colors.blueForegroundColor, "Incorrect link formatting") + + //"[[" + XCTAssertEqual(linkRange2.location, 45, "Incorrect link formatting") + XCTAssertEqual(linkRange2.length, 2, "Incorrect link formatting") + XCTAssertEqual(linkAttributes2[.font] as! UIFont, fonts.baseFont, "Incorrect base formatting") + XCTAssertEqual(linkAttributes2[.foregroundColor] as! UIColor, colors.blueForegroundColor, "Incorrect link formatting") + + //"fish" + XCTAssertEqual(linkRange3.location, 47, "Incorrect link formatting") + XCTAssertEqual(linkRange3.length, 4, "Incorrect link formatting") + XCTAssertEqual(linkAttributes3[.font] as! UIFont, fonts.baseFont, "Incorrect base formatting") + XCTAssertEqual(linkAttributes3[.foregroundColor] as! UIColor, colors.blueForegroundColor, "Incorrect link formatting") + + //"]]" + XCTAssertEqual(linkRange4.location, 51, "Incorrect link formatting") + XCTAssertEqual(linkRange4.length, 2, "Incorrect link formatting") + XCTAssertEqual(linkAttributes4[.font] as! UIFont, fonts.baseFont, "Incorrect base formatting") + XCTAssertEqual(linkAttributes4[.foregroundColor] as! UIColor, colors.blueForegroundColor, "Incorrect link formatting") + + //"|alt=Photo of cat looking at fish]]" + XCTAssertEqual(linkRange5.location, 53, "Incorrect link formatting") + XCTAssertEqual(linkRange5.length, 35, "Incorrect link formatting") + XCTAssertEqual(linkAttributes5[.font] as! UIFont, fonts.baseFont, "Incorrect base formatting") + XCTAssertEqual(linkAttributes5[.foregroundColor] as! UIColor, colors.blueForegroundColor, "Incorrect link formatting") + } }