Skip to content

Commit

Permalink
Update to swift testing (#897)
Browse files Browse the repository at this point in the history
  • Loading branch information
bolsinga authored Sep 18, 2024
1 parent f841350 commit 3a98ac2
Show file tree
Hide file tree
Showing 12 changed files with 738 additions and 708 deletions.
52 changes: 26 additions & 26 deletions Tests/SiteTests/AnnumTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@
// Created by Greg Bolsinga on 6/10/23.
//

import XCTest
import Testing

@testable import Site

final class AnnumTests: XCTestCase {
func testFormat() throws {
XCTAssertEqual(Annum.year(1989).formatted(), "1989")
XCTAssertEqual(Annum.year(1989).formatted(.year), "1989")
XCTAssertEqual(Annum.year(1989).formatted(.json), "1989")
XCTAssertEqual(Annum.year(1989).formatted(.urlPath), "1989")

XCTAssertEqual(Annum.unknown.formatted(), "Year Unknown")
XCTAssertEqual(Annum.unknown.formatted(.year), "Year Unknown")
XCTAssertEqual(Annum.unknown.formatted(.json), "unknown")
XCTAssertEqual(Annum.unknown.formatted(.urlPath), "other")
struct AnnumTests {
@Test func format() {
#expect(Annum.year(1989).formatted() == "1989")
#expect(Annum.year(1989).formatted(.year) == "1989")
#expect(Annum.year(1989).formatted(.json) == "1989")
#expect(Annum.year(1989).formatted(.urlPath) == "1989")

#expect(Annum.unknown.formatted() == "Year Unknown")
#expect(Annum.unknown.formatted(.year) == "Year Unknown")
#expect(Annum.unknown.formatted(.json) == "unknown")
#expect(Annum.unknown.formatted(.urlPath) == "other")
}

func testParse() throws {
XCTAssertEqual(try Annum("1989"), Annum.year(1989))
@Test func parse() throws {
#expect(try Annum("1989") == Annum.year(1989))

XCTAssertEqual(try Annum(" 1989"), Annum.year(1989))
XCTAssertEqual(try Annum("1989 "), Annum.year(1989))
#expect(try Annum(" 1989") == Annum.year(1989))
#expect(try Annum("1989 ") == Annum.year(1989))

let newlineBefore = """
Expand All @@ -40,18 +40,18 @@ final class AnnumTests: XCTestCase {
19
89
"""
XCTAssertThrowsError(try Annum(newlineBefore))
XCTAssertThrowsError(try Annum(newlineAfter))
XCTAssertThrowsError(try Annum(newlineMiddle))
#expect(throws: (any Error).self) { try Annum(newlineBefore) }
#expect(throws: (any Error).self) { try Annum(newlineAfter) }
#expect(throws: (any Error).self) { try Annum(newlineMiddle) }

XCTAssertThrowsError(try Annum("z1989"))
XCTAssertThrowsError(try Annum("1989z"))
#expect(throws: (any Error).self) { try Annum("z1989") }
#expect(throws: (any Error).self) { try Annum("1989z") }

XCTAssertThrowsError(try Annum("zzz"))
XCTAssertThrowsError(try Annum("Year Unknown"))
XCTAssertEqual(try Annum("unknown"), Annum.unknown)
#expect(throws: (any Error).self) { try Annum("zzz") }
#expect(throws: (any Error).self) { try Annum("Year Unknown") }
#expect(try Annum("unknown") == Annum.unknown)

XCTAssertThrowsError(try Annum(""))
XCTAssertThrowsError(try Annum(" "))
#expect(throws: (any Error).self) { try Annum("") }
#expect(throws: (any Error).self) { try Annum(" ") }
}
}
74 changes: 43 additions & 31 deletions Tests/SiteTests/ArchiveCategoryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,54 @@
// Created by Greg Bolsinga on 8/8/23.
//

import XCTest
import Foundation
import Testing

@testable import Site

final class ArchiveCategoryTests: XCTestCase {
func testFormat() throws {
XCTAssertEqual(ArchiveCategory.today.formatted(.urlPath), "/dates/today.html")
XCTAssertEqual(ArchiveCategory.stats.formatted(.urlPath), "/stats.html")
XCTAssertEqual(ArchiveCategory.shows.formatted(.urlPath), "/dates/stats.html")
XCTAssertEqual(ArchiveCategory.venues.formatted(.urlPath), "/venues/stats.html")
XCTAssertEqual(ArchiveCategory.artists.formatted(.urlPath), "/bands/stats.html")
struct ArchiveCategoryTests {
@Test func format() {
#expect(ArchiveCategory.today.formatted(.urlPath) == "/dates/today.html")
#expect(ArchiveCategory.stats.formatted(.urlPath) == "/stats.html")
#expect(ArchiveCategory.shows.formatted(.urlPath) == "/dates/stats.html")
#expect(ArchiveCategory.venues.formatted(.urlPath) == "/venues/stats.html")
#expect(ArchiveCategory.artists.formatted(.urlPath) == "/bands/stats.html")
}

func testParseURLFormat() throws {
XCTAssertEqual(
try ArchiveCategory(URL(string: "https://www.example.com/dates/today.html")!),
ArchiveCategory.today)
XCTAssertEqual(
try ArchiveCategory(URL(string: "https://www.example.com/stats.html")!), ArchiveCategory.stats
)
XCTAssertEqual(
try ArchiveCategory(URL(string: "https://www.example.com/dates/stats.html")!),
ArchiveCategory.shows)
XCTAssertEqual(
try ArchiveCategory(URL(string: "https://www.example.com/venues/stats.html")!),
ArchiveCategory.venues)
XCTAssertEqual(
try ArchiveCategory(URL(string: "https://www.example.com/bands/stats.html")!),
ArchiveCategory.artists)
@Test func parseURLFormat() throws {
#expect(
try ArchiveCategory(URL(string: "https://www.example.com/dates/today.html")!)
== ArchiveCategory.today)
#expect(
try ArchiveCategory(URL(string: "https://www.example.com/stats.html")!)
== ArchiveCategory.stats)
#expect(
try ArchiveCategory(URL(string: "https://www.example.com/dates/stats.html")!)
== ArchiveCategory.shows)
#expect(
try ArchiveCategory(URL(string: "https://www.example.com/venues/stats.html")!)
== ArchiveCategory.venues)
#expect(
try ArchiveCategory(URL(string: "https://www.example.com/bands/stats.html")!)
== ArchiveCategory.artists)

XCTAssertThrowsError(try ArchiveCategory(URL(string: "https://www.example.com/today.html")!))
XCTAssertThrowsError(
try ArchiveCategory(URL(string: "https://www.example.com/today.html#blah")!))
XCTAssertThrowsError(try ArchiveCategory(URL(string: "https://www.example.com/today")!))
XCTAssertThrowsError(try ArchiveCategory(URL(string: "https://www.example.com/bands/")!))
XCTAssertThrowsError(try ArchiveCategory(URL(string: "https://www.example.com/")!))
XCTAssertThrowsError(try ArchiveCategory(URL(string: "http://www.example.com/")!))
#expect(throws: (any Error).self) {
try ArchiveCategory(URL(string: "https://www.example.com/today.html")!)
}
#expect(throws: (any Error).self) {
try ArchiveCategory(URL(string: "https://www.example.com/today.html#blah")!)
}
#expect(throws: (any Error).self) {
try ArchiveCategory(URL(string: "https://www.example.com/today")!)
}
#expect(throws: (any Error).self) {
try ArchiveCategory(URL(string: "https://www.example.com/bands/")!)
}
#expect(throws: (any Error).self) {
try ArchiveCategory(URL(string: "https://www.example.com/")!)
}
#expect(throws: (any Error).self) {
try ArchiveCategory(URL(string: "http://www.example.com/")!)
}
}
}
130 changes: 65 additions & 65 deletions Tests/SiteTests/ArchiveNavigationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,132 +5,132 @@
// Created by Greg Bolsinga on 6/24/23.
//

import XCTest
import Testing

@testable import Site

final class ArchiveNavigationTests: XCTestCase {
func testNavigateToCategory() throws {
struct ArchiveNavigationTests {
@Test func navigateToCategory() {
let ar = ArchiveNavigation()
#if os(iOS) || os(tvOS)
XCTAssertNotNil(ar.state.category)
#expect(ar.state.category != nil)
#endif
XCTAssertEqual(ar.state.category, ArchiveNavigation.State.defaultCategory)
XCTAssertTrue(ar.state.path.isEmpty)
#expect(ar.state.category == ArchiveNavigation.State.defaultCategory)
#expect(ar.state.path.isEmpty)

ar.navigate(to: .today)
XCTAssertEqual(ar.state.category, .today)
XCTAssertTrue(ar.state.path.isEmpty)
#expect(ar.state.category == .today)
#expect(ar.state.path.isEmpty)

ar.navigate(to: .stats)
XCTAssertEqual(ar.state.category, .stats)
XCTAssertTrue(ar.state.path.isEmpty)
#expect(ar.state.category == .stats)
#expect(ar.state.path.isEmpty)

ar.navigate(to: .artists)
XCTAssertEqual(ar.state.category, .artists)
XCTAssertTrue(ar.state.path.isEmpty)
#expect(ar.state.category == .artists)
#expect(ar.state.path.isEmpty)

ar.navigate(to: .venues)
XCTAssertEqual(ar.state.category, .venues)
XCTAssertTrue(ar.state.path.isEmpty)
#expect(ar.state.category == .venues)
#expect(ar.state.path.isEmpty)

ar.navigate(to: .shows)
XCTAssertEqual(ar.state.category, .shows)
XCTAssertTrue(ar.state.path.isEmpty)
#expect(ar.state.category == .shows)
#expect(ar.state.path.isEmpty)

#if os(iOS) || os(tvOS)
ar.navigate(to: nil)
XCTAssertNil(ar.state.category)
XCTAssertTrue(ar.state.path.isEmpty)
#expect(ar.state.category == nil)
#expect(ar.state.path.isEmpty)
#endif
}

func testNavigateToCategory_existingPath() {
@Test func navigateToCategory_existingPath() {
let ar = ArchiveNavigation(
ArchiveNavigation.State(
category: .artists, path: [Artist(id: "id", name: "name").archivePath]))
#if os(iOS) || os(tvOS)
XCTAssertNotNil(ar.state.category)
#expect(ar.state.category != nil)
#endif
XCTAssertEqual(ar.state.category, .artists)
XCTAssertFalse(ar.state.path.isEmpty)
XCTAssertEqual(ar.state.path.first!, Artist(id: "id", name: "name").archivePath)
#expect(ar.state.category == .artists)
#expect(!ar.state.path.isEmpty)
#expect(ar.state.path.first! == Artist(id: "id", name: "name").archivePath)

ar.navigate(to: .venues)
#if os(iOS) || os(tvOS)
XCTAssertNotNil(ar.state.category)
#expect(ar.state.category != nil)
#endif
XCTAssertEqual(ar.state.category, .venues)
XCTAssertTrue(ar.state.path.isEmpty)
#expect(ar.state.category == .venues)
#expect(ar.state.path.isEmpty)
}

func testNavigateToArchivePath() throws {
@Test func navigateToArchivePath() {
let ar = ArchiveNavigation()
#if os(iOS) || os(tvOS)
XCTAssertNotNil(ar.state.category)
#expect(ar.state.category != nil)
#endif
XCTAssertEqual(ar.state.category, ArchiveNavigation.State.defaultCategory)
XCTAssertTrue(ar.state.path.isEmpty)
#expect(ar.state.category == ArchiveNavigation.State.defaultCategory)
#expect(ar.state.path.isEmpty)

ar.navigate(to: ArchivePath.artist("id"))
#if os(iOS) || os(tvOS)
XCTAssertNotNil(ar.state.category)
#expect(ar.state.category != nil)
#endif
XCTAssertEqual(ar.state.category, ArchiveNavigation.State.defaultCategory)
XCTAssertEqual(ar.state.path.count, 1)
XCTAssertNotNil(ar.state.path.last)
XCTAssertEqual(ar.state.path.last!, ArchivePath.artist("id"))
#expect(ar.state.category == ArchiveNavigation.State.defaultCategory)
#expect(ar.state.path.count == 1)
#expect(ar.state.path.last != nil)
#expect(ar.state.path.last! == ArchivePath.artist("id"))

ar.navigate(to: ArchivePath.venue("id"))
#if os(iOS) || os(tvOS)
XCTAssertNotNil(ar.state.category)
#expect(ar.state.category != nil)
#endif
XCTAssertEqual(ar.state.category, ArchiveNavigation.State.defaultCategory)
XCTAssertEqual(ar.state.path.count, 2)
XCTAssertNotNil(ar.state.path.last)
XCTAssertEqual(ar.state.path.last!, ArchivePath.venue("id"))
#expect(ar.state.category == ArchiveNavigation.State.defaultCategory)
#expect(ar.state.path.count == 2)
#expect(ar.state.path.last != nil)
#expect(ar.state.path.last! == ArchivePath.venue("id"))

ar.navigate(to: ArchivePath.show("id"))
#if os(iOS) || os(tvOS)
XCTAssertNotNil(ar.state.category)
#expect(ar.state.category != nil)
#endif
XCTAssertEqual(ar.state.category, ArchiveNavigation.State.defaultCategory)
XCTAssertEqual(ar.state.path.count, 3)
XCTAssertNotNil(ar.state.path.last)
XCTAssertEqual(ar.state.path.last!, ArchivePath.show("id"))
#expect(ar.state.category == ArchiveNavigation.State.defaultCategory)
#expect(ar.state.path.count == 3)
#expect(ar.state.path.last != nil)
#expect(ar.state.path.last! == ArchivePath.show("id"))

ar.navigate(to: ArchivePath.year(Annum.year(1989)))
#if os(iOS) || os(tvOS)
XCTAssertNotNil(ar.state.category)
#expect(ar.state.category != nil)
#endif
XCTAssertEqual(ar.state.category, ArchiveNavigation.State.defaultCategory)
XCTAssertEqual(ar.state.path.count, 4)
XCTAssertNotNil(ar.state.path.last)
XCTAssertEqual(ar.state.path.last!, ArchivePath.year(Annum.year(1989)))
#expect(ar.state.category == ArchiveNavigation.State.defaultCategory)
#expect(ar.state.path.count == 4)
#expect(ar.state.path.last != nil)
#expect(ar.state.path.last! == ArchivePath.year(Annum.year(1989)))
}

func testNavigateToArchivePath_noDoubles() throws {
@Test func navigateToArchivePath_noDoubles() {
let ar = ArchiveNavigation()
ar.navigate(to: ArchivePath.artist("id"))
XCTAssertEqual(ar.state.path.count, 1)
XCTAssertEqual(ar.state.path.first!, ArchivePath.artist("id"))
XCTAssertEqual(ar.state.category, ArchiveNavigation.State.defaultCategory)
#expect(ar.state.path.count == 1)
#expect(ar.state.path.first! == ArchivePath.artist("id"))
#expect(ar.state.category == ArchiveNavigation.State.defaultCategory)

ar.navigate(to: ArchivePath.artist("id"))
XCTAssertEqual(ar.state.path.count, 1)
XCTAssertEqual(ar.state.path.first!, ArchivePath.artist("id"))
XCTAssertEqual(ar.state.category, ArchiveNavigation.State.defaultCategory)
#expect(ar.state.path.count == 1)
#expect(ar.state.path.first! == ArchivePath.artist("id"))
#expect(ar.state.category == ArchiveNavigation.State.defaultCategory)

ar.navigate(to: ArchivePath.artist("id-1"))
XCTAssertEqual(ar.state.path.count, 2)
XCTAssertEqual(ar.state.path.first!, ArchivePath.artist("id"))
XCTAssertEqual(ar.state.path.last!, ArchivePath.artist("id-1"))
XCTAssertEqual(ar.state.category, ArchiveNavigation.State.defaultCategory)
#expect(ar.state.path.count == 2)
#expect(ar.state.path.first! == ArchivePath.artist("id"))
#expect(ar.state.path.last! == ArchivePath.artist("id-1"))
#expect(ar.state.category == ArchiveNavigation.State.defaultCategory)

ar.navigate(to: ArchivePath.venue("vid-1"))
XCTAssertEqual(ar.state.path.count, 3)
XCTAssertEqual(ar.state.path.first!, ArchivePath.artist("id"))
XCTAssertEqual(ar.state.path.last!, ArchivePath.venue("vid-1"))
XCTAssertEqual(ar.state.category, ArchiveNavigation.State.defaultCategory)
#expect(ar.state.path.count == 3)
#expect(ar.state.path.first! == ArchivePath.artist("id"))
#expect(ar.state.path.last! == ArchivePath.venue("vid-1"))
#expect(ar.state.category == ArchiveNavigation.State.defaultCategory)
}
}
Loading

0 comments on commit 3a98ac2

Please sign in to comment.