NetNewsWire/Modules/AppKitExtras/Tests/AppKitExtrasTests/NSMenuExtensionsTests.swift
2024-07-06 21:07:05 -07:00

66 lines
1.5 KiB
Swift
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// NSMenuExtensionsTests.swift
//
//
// Created by Brent Simmons on 5/18/24.
//
#if os(macOS)
import AppKit
import XCTest
import AppKitExtras
final class NSMenuExtensionsTests: XCTestCase {
// MARK: - Test addSeparatorIfNeeded
func testAddSeparatorIfNeeded_NoSeparator() {
let menu = NSMenu(title: "Test")
menu.addItem(menuItemForTesting())
menu.addItem(menuItemForTesting())
menu.addSeparatorIfNeeded()
XCTAssertTrue(menu.items.last!.isSeparatorItem)
}
func testAddSeparatorIfNeeded_EmptyMenu() {
let menu = NSMenu(title: "Test")
menu.addSeparatorIfNeeded()
// A separator should not be added to a menu with 0 items,
// since a menu should never start with a separator item.
XCTAssertEqual(menu.items.count, 0)
}
func testAddSeparatorIfNeeded_HasSeparator() {
let menu = NSMenu(title: "Test")
menu.addItem(menuItemForTesting())
menu.addItem(menuItemForTesting())
menu.addItem(.separator())
let menuItemsCount = menu.items.count
XCTAssertTrue(menu.items.last!.isSeparatorItem)
// Should not get added  last item is already separator
menu.addSeparatorIfNeeded()
// Make sure last item is still separator
XCTAssertTrue(menu.items.last!.isSeparatorItem)
// Count should be same as before calling `addSeparatorIfNeeded()`
XCTAssertEqual(menu.items.count, menuItemsCount)
}
}
private extension NSMenuExtensionsTests {
private func menuItemForTesting(_ title: String = "Test NSMenuItem") -> NSMenuItem {
NSMenuItem(title: title, action: nil, keyEquivalent: "")
}
}
#endif