Skip to content

Commit

Permalink
Merge pull request #96 from msrutek-paylocity/feature/test-improvements
Browse files Browse the repository at this point in the history
Test improvements
  • Loading branch information
danielsaidi authored Dec 5, 2023
2 parents 6ee5107 + 7c6ebb3 commit b340212
Show file tree
Hide file tree
Showing 21 changed files with 225 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,4 @@ final class RichTextAttributeReader_AlignmentTests: XCTestCase {
}
}

private class TestReader: RichTextAttributeReader {

var attributedString = NSAttributedString(string: "foo bar")
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import XCTest

final class RichTextAttributeReaderWriterTests: XCTestCase {

let string = NSMutableAttributedString(string: "foo bar baz")
private let string = NSMutableAttributedString(string: "foo bar baz")

func testSettingAttributeWithinRangeOnlySetsAttributeWithinTheProvidedRange() {
let color = ColorRepresentable.yellow
Expand Down
58 changes: 29 additions & 29 deletions Tests/RichTextKitTests/Data/RichTextDataFormatTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,19 @@ import XCTest

final class RichTextDataFormatTests: XCTestCase {

let vendorType = UTType(exportedAs: "fooType")
private let vendorType = UTType(exportedAs: "fooType")

lazy var vendorFormat = RichTextDataFormat.vendorArchivedData(
private lazy var vendorFormat = RichTextDataFormat.vendorArchivedData(
id: "foo",
fileExtension: "fooFile",
fileFormatText: "Foo File (*.foo)",
uniformType: vendorType
)

func idResult(for format: RichTextDataFormat) -> String {
format.id
}

func isArchivedDataResult(for format: RichTextDataFormat) -> Bool {
format.isArchivedDataFormat
}

func formatResult(for format: RichTextDataFormat) -> [RichTextDataFormat] {
format.convertibleFormats
}

func extensionResult(for format: RichTextDataFormat) -> String {
format.standardFileExtension
}

func imageResult(for format: RichTextDataFormat) -> Bool {
format.supportsImages
}

func uniformTypeResult(for format: RichTextDataFormat) -> UTType {
format.uniformType
}


func testLibraryFormatsReturnAllNonVendorFormats() {
XCTAssertEqual(RichTextDataFormat.libraryFormats, [.archivedData, .plainText, .rtf])
}


func testIdIsValidForAllFormats() {
XCTAssertEqual(idResult(for: .archivedData), "archivedData")
XCTAssertEqual(idResult(for: .plainText), "plainText")
Expand All @@ -65,7 +39,7 @@ final class RichTextDataFormatTests: XCTestCase {
XCTAssertEqual(isArchivedDataResult(for: vendorFormat), true)
}

func testConvertableFormatsAreValidForAllFormats() {
func testConvertibleFormatsAreValidForAllFormats() {
XCTAssertEqual(formatResult(for: .archivedData), [.plainText, .rtf])
XCTAssertEqual(formatResult(for: .plainText), [.archivedData, .rtf])
XCTAssertEqual(formatResult(for: .rtf), [.archivedData, .plainText])
Expand Down Expand Up @@ -93,3 +67,29 @@ final class RichTextDataFormatTests: XCTestCase {
XCTAssertEqual(uniformTypeResult(for: vendorFormat), vendorType)
}
}

private extension RichTextDataFormatTests {
func idResult(for format: RichTextDataFormat) -> String {
format.id
}

func isArchivedDataResult(for format: RichTextDataFormat) -> Bool {
format.isArchivedDataFormat
}

func formatResult(for format: RichTextDataFormat) -> [RichTextDataFormat] {
format.convertibleFormats
}

func extensionResult(for format: RichTextDataFormat) -> String {
format.standardFileExtension
}

func imageResult(for format: RichTextDataFormat) -> Bool {
format.supportsImages
}

func uniformTypeResult(for format: RichTextDataFormat) -> UTType {
format.uniformType
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import RichTextKit
import XCTest

final class NSAttributedString_EmptyTests: XCTestCase {

func testEmptyAttributedString() {
let string = NSAttributedString.empty
XCTAssertEqual(string.string, "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import RichTextKit
import XCTest

final class String_CharactersTest: XCTestCase {

func testStringCharactersAreValid() {
XCTAssertEqual(String.carriageReturn, "\r")
XCTAssertEqual(String.newLine, "\n")
Expand Down
47 changes: 24 additions & 23 deletions Tests/RichTextKitTests/Extensions/String+ParagraphTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,10 @@ import RichTextKit
import XCTest

final class String_ParagraphTests: XCTestCase {

let none = "foo bar baz"
let single = "foo\nbar baz"
let multi = "foo\nbar\rbaz"

func currentResult(for string: String, from location: UInt) -> UInt {
string.findIndexOfCurrentParagraph(from: location)
}

func nextTesult(for string: String, from location: UInt) -> UInt {
string.findIndexOfNextParagraph(from: location)
}

private let none = "foo bar baz"
private let single = "foo\nbar baz"
private let multi = "foo\nbar\rbaz"

func testIndexOfCurrentParagraphIsCorrectForEmptyString() {
XCTAssertEqual(currentResult(for: "", from: 0), 0)
Expand All @@ -49,25 +40,35 @@ final class String_ParagraphTests: XCTestCase {


func testIndexOfNextParagraphIsCorrectForEmptyString() {
XCTAssertEqual(nextTesult(for: "", from: 0), 0)
XCTAssertEqual(nextTesult(for: "", from: 20), 0)
XCTAssertEqual(nextResult(for: "", from: 0), 0)
XCTAssertEqual(nextResult(for: "", from: 20), 0)
}

func testIndexOfNextParagraphIsCorrectForStringWithNoNextParagraph() {
XCTAssertEqual(nextTesult(for: none, from: 0), 0)
XCTAssertEqual(nextTesult(for: none, from: 10), 0)
XCTAssertEqual(nextTesult(for: none, from: 20), 0)
XCTAssertEqual(nextResult(for: none, from: 0), 0)
XCTAssertEqual(nextResult(for: none, from: 10), 0)
XCTAssertEqual(nextResult(for: none, from: 20), 0)
}

func testIndexOfNextParagraphIsCorrectForStringWithSingleNextParagraph() {
XCTAssertEqual(nextTesult(for: single, from: 0), 4)
XCTAssertEqual(nextTesult(for: single, from: 5), 4)
XCTAssertEqual(nextTesult(for: single, from: 10), 4)
XCTAssertEqual(nextResult(for: single, from: 0), 4)
XCTAssertEqual(nextResult(for: single, from: 5), 4)
XCTAssertEqual(nextResult(for: single, from: 10), 4)
}

func testIndexOfNextParagraphIsCorrectForStringWithMultipleNextParagraphs() {
XCTAssertEqual(nextTesult(for: multi, from: 0), 4)
XCTAssertEqual(nextTesult(for: multi, from: 5), 8)
XCTAssertEqual(nextTesult(for: multi, from: 10), 8)
XCTAssertEqual(nextResult(for: multi, from: 0), 4)
XCTAssertEqual(nextResult(for: multi, from: 5), 8)
XCTAssertEqual(nextResult(for: multi, from: 10), 8)
}
}

private extension String_ParagraphTests {
func currentResult(for string: String, from location: UInt) -> UInt {
string.findIndexOfCurrentParagraph(from: location)
}

func nextResult(for string: String, from location: UInt) -> UInt {
string.findIndexOfNextParagraph(from: location)
}
}
8 changes: 4 additions & 4 deletions Tests/RichTextKitTests/Extensions/String+SubscriptTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import RichTextKit
import XCTest

final class String_SubscriptTest: XCTestCase {

let string = "foo bar baz"

func testSharacterAtIndexIsValidWithinBounds() {
private let string = "foo bar baz"

func testCharacterAtIndexIsValidWithinBounds() {
XCTAssertEqual(string.character(at: 0), "f")
XCTAssertEqual(string.character(at: 10), "z")
}

func testSharacterAtIndexIsNilOutsideBounds() {
func testCharacterAtIndexIsNilOutsideBounds() {
XCTAssertNil(string.character(at: -1))
XCTAssertNil(string.character(at: 11))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import RichTextKit
import XCTest

final class StandardFontSizeProviderTests: XCTestCase {

func testStandardRichTextFontSize() {
XCTAssertEqual(CGFloat.standardRichTextFontSize, 16)
CGFloat.standardRichTextFontSize = .standardRichTextFontSize
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import XCTest

final class RichTextCoordinator_SubscriptionsTests: XCTestCase {

var text: NSAttributedString!
var textBinding: Binding<NSAttributedString>!
var textView: RichTextView!
var textContext: RichTextContext!
var coordinator: RichTextCoordinator!
private var text: NSAttributedString!
private var textBinding: Binding<NSAttributedString>!
private var textView: RichTextView!
private var textContext: RichTextContext!
private var coordinator: RichTextCoordinator!

override func setUp() {
super.setUp()

text = NSAttributedString(string: "foo bar baz")
textBinding = Binding(get: { self.text }, set: { self.text = $0 })
textView = RichTextView()
Expand All @@ -32,6 +34,15 @@ final class RichTextCoordinator_SubscriptionsTests: XCTestCase {
textView.setCurrentTextAlignment(.justified)
}

override func tearDown() {
text = nil
textBinding = nil
textView = nil
textContext = nil
coordinator = nil

super.tearDown()
}

func testTextCoordinatorIsNeededForUpdatesToTakePlace() {
XCTAssertNotNil(coordinator)
Expand Down
25 changes: 16 additions & 9 deletions Tests/RichTextKitTests/RichTextCoordinatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import XCTest
@testable import RichTextKit

final class RichTextCoordinatorTests: XCTestCase {

var text: NSAttributedString!
var textBinding: Binding<NSAttributedString>!
var view: RichTextView!
var context: RichTextContext!
var coordinator: RichTextCoordinator!
private var text: NSAttributedString!
private var textBinding: Binding<NSAttributedString>!
private var view: RichTextView!
private var context: RichTextContext!
private var coordinator: RichTextCoordinator!

override func setUp() {
super.setUp()

text = NSAttributedString(string: "foo bar baz")
textBinding = Binding(get: { self.text }, set: { self.text = $0 })
view = RichTextView()
Expand All @@ -34,6 +35,15 @@ final class RichTextCoordinatorTests: XCTestCase {
view.setCurrentTextAlignment(.justified)
}

override func tearDown() {
text = nil
textBinding = nil
view = nil
context = nil
coordinator = nil

super.tearDown()
}

func testInitializerSetsUpTextViewText() {
XCTAssertEqual(view.richText.string, "foo bar baz")
Expand All @@ -43,16 +53,13 @@ final class RichTextCoordinatorTests: XCTestCase {
XCTAssertTrue(view.delegate === coordinator)
}


func testRichTextPresenterUsesNestedTextView() {
let range = NSRange(location: 4, length: 3)
view.selectedRange = range
XCTAssertEqual(coordinator.text.wrappedValue.string, "foo bar baz")
XCTAssertEqual(coordinator.richTextContext.selectedRange, range)
}



func assertIsSyncedWithContext(macOSAlignment: RichTextAlignment = .left) {
XCTAssertEqual(context.fontName, view.currentFontName)
XCTAssertEqual(context.fontSize, view.currentFontSize)
Expand Down
22 changes: 17 additions & 5 deletions Tests/RichTextKitTests/RichTextEditorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ import XCTest

final class RichTextEditorTests: XCTestCase {

var text: NSAttributedString!
var textBinding: Binding<NSAttributedString>!
var editor: RichTextEditor!
var context: RichTextContext!
var coordinator: RichTextCoordinator!
private var text: NSAttributedString!
private var textBinding: Binding<NSAttributedString>!
private var editor: RichTextEditor!
private var context: RichTextContext!
private var coordinator: RichTextCoordinator!

override func setUp() {
super.setUp()

text = NSAttributedString(string: "foo bar baz")
textBinding = Binding(get: { self.text }, set: { self.text = $0 })
context = RichTextContext()
Expand All @@ -29,6 +31,16 @@ final class RichTextEditorTests: XCTestCase {
coordinator = editor.makeCoordinator()
}

override func tearDown() {
text = nil
textBinding = nil
editor = nil
context = nil
coordinator = nil

super.tearDown()
}

func testRichTextPresenterUsesContextSelectedRange() {
let range = NSRange(location: 4, length: 3)
context.selectRange(range)
Expand Down
13 changes: 10 additions & 3 deletions Tests/RichTextKitTests/RichTextPresenterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,23 @@ import RichTextKit
import XCTest

final class RichTextPresenterTests: XCTestCase {

var text: NSAttributedString!


private var text: NSAttributedString!
private var presenter: MockRichTextPresenter!

override func setUp() {
super.setUp()

text = NSAttributedString(string: "foo bar baz")
presenter = MockRichTextPresenter(text: text)
}

override func tearDown() {
text = nil
presenter = nil

super.tearDown()
}

func testTextRangeBeforeCursorReturnsRangeBeforeSelectionStart() {
presenter.selectedRange = NSRange(location: 4, length: 3)
Expand Down
3 changes: 1 addition & 2 deletions Tests/RichTextKitTests/RichTextReaderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import XCTest

final class RichTextReaderTests: XCTestCase {

let string = NSAttributedString(string: "foo bar baz")

private let string = NSAttributedString(string: "foo bar baz")

func testRichTextAtRangeIsValidForEmptyRangeInEmptyString() {
let range = NSRange(location: 0, length: 0)
Expand Down
Loading

0 comments on commit b340212

Please sign in to comment.