Skip to content

Commit

Permalink
Cleanup tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
twstokes committed Mar 31, 2022
1 parent 23f609a commit 8a6463f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import UIKit
import XCTest

@testable import WordPress

class MenuItemsViewControllerTests: XCTestCase {

private var context: NSManagedObjectContext!
Expand All @@ -20,53 +17,96 @@ class MenuItemsViewControllerTests: XCTestCase {

/// Tests that no string is provided when there is nothing to announce.
func testOrderingChangeVOStringNoChanges() {
let noChanges = MenuItemsViewController.generateOrderingChangeVOString(nil, parentChanged: false, before: nil, after: nil)
let noChanges = MenuItemsViewController.generateOrderingChangeVOString(
nil,
parentChanged: false,
before: nil,
after: nil
)
XCTAssertNil(noChanges)

let newParent = MenuItem(context: context)
newParent.name = "New Parent"
let newParent = newMenuItem(named: "New Parent")

/// This is a programming error. parentChanged should always be set to true if the parent indeed changed.
let parentChangedFalse = MenuItemsViewController.generateOrderingChangeVOString(newParent, parentChanged: false, before: nil, after: nil)
let parentChangedFalse = MenuItemsViewController.generateOrderingChangeVOString(
newParent,
parentChanged: false,
before: nil,
after: nil
)
XCTAssertNil(parentChangedFalse)
}

/// Tests handling of changes to the menu item's parent.
func testOrderingChangeVOStringParentChanged() {
let newParent = MenuItem(context: context)
newParent.name = "New Parent"

/// test when the parent changed but it was the top level (no parent)
let topLevelString = MenuItemsViewController.generateOrderingChangeVOString(nil, parentChanged: true, before: nil, after: nil)
let newParent = newMenuItem(named: "New Parent")

let topLevelString = MenuItemsViewController.generateOrderingChangeVOString(
nil,
parentChanged: true,
before: nil,
after: nil
)
XCTAssertEqual(topLevelString, "Top level")

/// test when the parent changed and it was a menu item
let newParentString = MenuItemsViewController.generateOrderingChangeVOString(newParent, parentChanged: true, before: nil, after: nil)
let newParentString = MenuItemsViewController.generateOrderingChangeVOString(
newParent,
parentChanged: true,
before: nil,
after: nil
)
XCTAssertEqual(newParentString, "Child of New Parent")
}

/// Tests handling of changes to the menu item's parent and order.
func testOrderingChangeVOString() {
let newParent = MenuItem(context: context)
newParent.name = "New Parent"

let afterItem = MenuItem(context: context)
afterItem.name = "Item A"

let beforeItem = MenuItem(context: context)
beforeItem.name = "Item B"

let beforeString = MenuItemsViewController.generateOrderingChangeVOString(nil, parentChanged: false, before: beforeItem, after: nil)
let newParent = newMenuItem(named: "New Parent")
let afterItem = newMenuItem(named: "Item A")
let beforeItem = newMenuItem(named: "Item B")

let beforeString = MenuItemsViewController.generateOrderingChangeVOString(
nil,
parentChanged: false,
before: beforeItem,
after: nil
)
XCTAssertEqual(beforeString, "Before Item B")

let afterString = MenuItemsViewController.generateOrderingChangeVOString(nil, parentChanged: false, before: nil, after: afterItem)
let afterString = MenuItemsViewController.generateOrderingChangeVOString(
nil,
parentChanged: false,
before: nil,
after: afterItem
)
XCTAssertEqual(afterString, "After Item A")

let parentAndBeforeString = MenuItemsViewController.generateOrderingChangeVOString(newParent, parentChanged: true, before: beforeItem, after: nil)
let parentAndBeforeString = MenuItemsViewController.generateOrderingChangeVOString(
newParent,
parentChanged: true,
before: beforeItem,
after: nil
)
XCTAssertEqual(parentAndBeforeString, "Child of New Parent. Before Item B")

let parentAndBeforeAndAfterString = MenuItemsViewController.generateOrderingChangeVOString(newParent, parentChanged: true, before: beforeItem, after: afterItem)
let parentAndBeforeAndAfterString = MenuItemsViewController.generateOrderingChangeVOString(
newParent,
parentChanged: true,
before: beforeItem,
after: afterItem
)
XCTAssertEqual(parentAndBeforeAndAfterString, "Child of New Parent. After Item A. Before Item B")
}

// MARK: - Private Helpers

fileprivate func newMenuItem(named name: String) -> MenuItem {
let entityName = MenuItem.classNameWithoutNamespaces()
let entity = NSEntityDescription.insertNewObject(forEntityName: entityName, into: context)

let menuItem = entity as! MenuItem
// set a name to make debugging easier
menuItem.name = name

return menuItem
}
}
6 changes: 3 additions & 3 deletions WordPress/WordPressTest/Menus/MenuItemTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class MenuItemTests: XCTestCase {
context = nil
}

/// Tests detection of descendants.
func testIsDescendantOfItem() {
let itemA = newMenuItem(named: "Item A")
let itemB = newMenuItem(named: "Item B")
Expand Down Expand Up @@ -50,6 +51,7 @@ class MenuItemTests: XCTestCase {
XCTAssertFalse(fIsNotDescendantOfA)
}

/// Tests that the last descendant of an item is found.
func testLastDescendantInOrderedItems() {
let itemA = newMenuItem(named: "Item A")
let itemB = newMenuItem(named: "Item B")
Expand Down Expand Up @@ -81,19 +83,17 @@ class MenuItemTests: XCTestCase {
itemE.parent = itemD
itemF.parent = itemD

/// Item B has a child, but is the last descendant of Item A
let lastDescendant = itemA.lastDescendant(inOrderedItems: orderedItems)
XCTAssertEqual(lastDescendant, itemB)

/// Item C has no descendants
let descendantOfItemC = itemC.lastDescendant(inOrderedItems: orderedItems)
XCTAssertEqual(descendantOfItemC, nil)

/// Item F should be the last descendant of Item D
let descendantOfItemD = itemD.lastDescendant(inOrderedItems: orderedItems)
XCTAssertEqual(descendantOfItemD, itemF)
}

/// Tests that preceding siblings are found.
func testPrecedingSiblingInOrderedItems() {
let itemA = newMenuItem(named: "Item A")
let itemB = newMenuItem(named: "Item B")
Expand Down

0 comments on commit 8a6463f

Please sign in to comment.