Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
tonisevener committed Dec 15, 2023
1 parent f5e5800 commit 9ea3aca
Showing 1 changed file with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -10,8 +10,9 @@ final class WKSourceEditorFormatterTests: XCTestCase {
var baseFormatter: WKSourceEditorFormatterBase!
var boldItalicsFormatter: WKSourceEditorFormatterBoldItalics!
var templateFormatter: WKSourceEditorFormatterTemplate!
var strikethroughFormatter: WKSourceEditorFormatterStrikethrough!
var formatters: [WKSourceEditorFormatter] {
return [baseFormatter, templateFormatter, boldItalicsFormatter]
return [baseFormatter, templateFormatter, boldItalicsFormatter, strikethroughFormatter]
}

override func setUpWithError() throws {
@@ -21,6 +22,7 @@ final class WKSourceEditorFormatterTests: XCTestCase {
self.colors.baseForegroundColor = WKTheme.light.text
self.colors.orangeForegroundColor = WKTheme.light.editorOrange
self.colors.purpleForegroundColor = WKTheme.light.editorPurple
self.colors.greenForegroundColor = WKTheme.light.editorGreen

self.fonts = WKSourceEditorFonts()
self.fonts.baseFont = WKFont.for(.body, compatibleWith: traitCollection)
@@ -31,6 +33,7 @@ final class WKSourceEditorFormatterTests: XCTestCase {
self.baseFormatter = WKSourceEditorFormatterBase(colors: colors, fonts: fonts, textAlignment: .left)
self.boldItalicsFormatter = WKSourceEditorFormatterBoldItalics(colors: colors, fonts: fonts)
self.templateFormatter = WKSourceEditorFormatterTemplate(colors: colors, fonts: fonts)
self.strikethroughFormatter = WKSourceEditorFormatterStrikethrough(colors: colors, fonts: fonts)
}

override func tearDownWithError() throws {
@@ -709,4 +712,58 @@ final class WKSourceEditorFormatterTests: XCTestCase {
XCTAssertEqual(refAttributes[.font] as! UIFont, fonts.baseFont, "Incorrect ref formatting")
XCTAssertEqual(refAttributes[.foregroundColor] as! UIColor, colors.baseForegroundColor, "Incorrect ref formatting")
}

func testStrikethrough() {
let string = "Testing. <s>Strikethrough.</s> 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 strikethroughOpenRange = NSRange(location: 0, length: 0)
let strikethroughOpenAttributes = mutAttributedString.attributes(at: 9, effectiveRange: &strikethroughOpenRange)

var strikethroughContentRange = NSRange(location: 0, length: 0)
let strikethroughContentAttributes = mutAttributedString.attributes(at: 12, effectiveRange: &strikethroughContentRange)

var strikethroughCloseRange = NSRange(location: 0, length: 0)
let strikethroughCloseAttributes = mutAttributedString.attributes(at: 26, effectiveRange: &strikethroughCloseRange)

var base2Range = NSRange(location: 0, length: 0)
let base2Attributes = mutAttributedString.attributes(at: 32, 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")

// "<s>"
XCTAssertEqual(strikethroughOpenRange.location, 9, "Incorrect strikethrough formatting")
XCTAssertEqual(strikethroughOpenRange.length, 3, "Incorrect strikethrough formatting")
XCTAssertEqual(strikethroughOpenAttributes[.font] as! UIFont, fonts.baseFont, "Incorrect strikethrough formatting")
XCTAssertEqual(strikethroughOpenAttributes[.foregroundColor] as! UIColor, colors.greenForegroundColor, "Incorrect strikethrough formatting")

// "Strikethrough."
XCTAssertEqual(strikethroughContentRange.location, 12, "Incorrect content formatting")
XCTAssertEqual(strikethroughContentRange.length, 14, "Incorrect content formatting")
XCTAssertEqual(strikethroughContentAttributes[.font] as! UIFont, fonts.baseFont, "Incorrect content formatting")
XCTAssertEqual(strikethroughContentAttributes[.foregroundColor] as! UIColor, colors.baseForegroundColor, "Incorrect content formatting")

// "</s>"
XCTAssertEqual(strikethroughCloseRange.location, 26, "Incorrect strikethrough formatting")
XCTAssertEqual(strikethroughCloseRange.length, 4, "Incorrect strikethrough formatting")
XCTAssertEqual(strikethroughCloseAttributes[.font] as! UIFont, fonts.baseFont, "Incorrect strikethrough formatting")
XCTAssertEqual(strikethroughCloseAttributes[.foregroundColor] as! UIColor, colors.greenForegroundColor, "Incorrect strikethrough formatting")

// " Testing"
XCTAssertEqual(base2Range.location, 30, "Incorrect base formatting")
XCTAssertEqual(base2Range.length, 8, "Incorrect base formatting")
XCTAssertEqual(base2Attributes[.font] as! UIFont, fonts.baseFont, "Incorrect base formatting")
XCTAssertEqual(base2Attributes[.foregroundColor] as! UIColor, colors.baseForegroundColor, "Incorrect base formatting")
}
}

0 comments on commit 9ea3aca

Please sign in to comment.