SPB Git

Repository Public MIT

GlassTabBarTests.swift 8.1 KB 201 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Tests/SPGlassNavigationTests/GlassTabBarTests.swift8// Module           : Tests9// Repository       : https://git.spboucher.ai/sp-glasskit10// Project Website  : https://glass.spboucher.ai11// Created          : 2026-12-0212// Last Modified    : 2026-12-0213//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : MIT33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation42import Testing43@testable import SPGlassNavigation4445private enum DemoTab: String, Hashable, Sendable, CaseIterable {46    case home, library, settings47}4849private enum DemoRoute: Hashable, Sendable {50    case album(String)51    case track(Int)52}5354@Suite("GlassTabBar")55struct GlassTabBarTests {56    private typealias State = GlassTabNavigationState<DemoTab, DemoRoute>5758    private let router = GlassTabRouter<DemoTab, DemoRoute>(59        schemes: ["demo", "https"],60        tabs: ["home": .home, "Library": .library, "settings": .settings]61    ) { tab, components, query in62        switch (tab, components.count) {63        case (_, 0):64            return []65        case (.library, 1):66            return [.album(components[0])]67        case (.library, 3) where components[1] == "tracks":68            guard let number = Int(components[2]) else { return nil }69            return [.album(components[0]), .track(number)]70        case (.settings, 1) where components[0] == "track":71            guard let value = query.first(where: { $0.name == "n" })?.value, let number = Int(value) else { return nil }72            return [.track(number)]73        default:74            return nil75        }76    }7778    @Test func eachTabKeepsItsOwnPath() {79        var state = State(selection: .home)80        state.push(.album("a1"))81        state.selection = .library82        state.push(.album("b1"))83        state.push(.track(3))84        state.selection = .home85        #expect(state.path(for: .home) == [.album("a1")])86        #expect(state.path(for: .library) == [.album("b1"), .track(3)])87        #expect(state.path(for: .settings).isEmpty)88    }8990    @Test func pushingInAnotherTabDoesNotChangeTheSelection() {91        var state = State(selection: .home)92        state.push(.track(1), in: .settings)93        #expect(state.selection == .home)94        #expect(state.path(for: .settings) == [.track(1)])95    }9697    @Test func popToRootClearsOnlyOneTab() {98        var state = State(selection: .home, paths: [.home: [.track(1)], .library: [.track(2)]])99        state.popToRoot()100        #expect(state.path(for: .home).isEmpty)101        #expect(state.path(for: .library) == [.track(2)])102        state.popToRoot(.library)103        #expect(state == State(selection: .home))104    }105106    @Test func emptyPathsCompareEqualToAbsentPaths() {107        var state = State(selection: .home, paths: [.library: []])108        #expect(state == State(selection: .home))109        state.setPath([], for: .settings)110        #expect(state == State(selection: .home))111    }112113    @Test func openingADestinationSelectsItsTabAndReplacesItsPath() {114        var state = State(selection: .home, paths: [.home: [.track(9)], .library: [.album("old")]])115        state.open(GlassTabDestination(tab: .library, path: [.album("new"), .track(2)]))116        #expect(state.selection == .library)117        #expect(state.path(for: .library) == [.album("new"), .track(2)])118        #expect(state.path(for: .home) == [.track(9)])119    }120121    @Test func reconcileDropsRemovedTabs() {122        var state = State(selection: .settings, paths: [.settings: [.track(1)], .library: [.track(2)]])123        state.reconcile(with: [.home, .library], fallback: .home)124        #expect(state.selection == .home)125        #expect(state.path(for: .settings).isEmpty)126        #expect(state.path(for: .library) == [.track(2)])127128        var unchanged = State(selection: .library)129        unchanged.reconcile(with: [.home, .library], fallback: .home)130        #expect(unchanged.selection == .library)131    }132133    @Test func customSchemeUsesTheHostAsTheTab() throws {134        let url = try #require(URL(string: "demo://library/a42/tracks/7"))135        #expect(router.destination(for: url) == GlassTabDestination(tab: .library, path: [.album("a42"), .track(7)]))136    }137138    @Test func webLinksUseTheFirstPathComponent() throws {139        let url = try #require(URL(string: "https://example.com/library/a42"))140        #expect(router.destination(for: url) == GlassTabDestination(tab: .library, path: [.album("a42")]))141        let root = try #require(URL(string: "https://example.com/settings/"))142        #expect(router.destination(for: root) == GlassTabDestination(tab: .settings))143    }144145    @Test func tabsAndSchemesIgnoreCase() throws {146        let url = try #require(URL(string: "DEMO://LIBRARY"))147        #expect(router.destination(for: url)?.tab == .library)148    }149150    @Test func queryItemsReachThePathBuilder() throws {151        let url = try #require(URL(string: "demo://settings/track?n=12"))152        #expect(router.destination(for: url)?.path == [.track(12)])153    }154155    @Test("Unknown schemes, tabs and paths are rejected", arguments: [156        "other://library",157        "demo://unknown",158        "demo://library/a/b",159        "demo://library/a/tracks/not-a-number",160        "demo://settings/track",161        "https://example.com/",162        "demo:",163    ])164    func rejects(_ string: String) throws {165        let url = try #require(URL(string: string))166        #expect(router.destination(for: url) == nil)167    }168169    @Test func routerWithoutSchemesAcceptsAnyAndDefaultsToRootOnly() throws {170        let simple = GlassTabRouter<DemoTab, DemoRoute>(tabs: ["home": .home])171        #expect(simple.destination(for: try #require(URL(string: "anything://home"))) == GlassTabDestination(tab: .home))172        #expect(simple.destination(for: try #require(URL(string: "anything://home/extra"))) == nil)173    }174175    @Test func typedDestinationsNeedAKnownTab() {176        let simple = GlassTabRouter<DemoTab, DemoRoute>(tabs: ["home": .home])177        #expect(simple.destination(tab: .home, path: [.track(1)]) == GlassTabDestination(tab: .home, path: [.track(1)]))178        #expect(simple.destination(tab: .settings) == nil)179    }180181    @Test func badgesFormatCountsAndHideEmptyOnes() {182        let locale = Locale(identifier: "en_US")183        #expect(GlassTabBadgeFormatter.string(nil) == nil)184        #expect(GlassTabBadgeFormatter.string(.count(0)) == nil)185        #expect(GlassTabBadgeFormatter.string(.count(-4)) == nil)186        #expect(GlassTabBadgeFormatter.string(.count(3), locale: locale) == "3")187        #expect(GlassTabBadgeFormatter.string(.count(1200), locale: locale) == "1,200")188        #expect(GlassTabBadgeFormatter.string(.text("  New ")) == "New")189        #expect(GlassTabBadgeFormatter.string(.text("   ")) == nil)190    }191192    @Test func tabItemsAreValues() {193        let a = GlassTabItem(id: DemoTab.home, title: "Home", systemImage: "house", badge: .count(2))194        var b = a195        #expect(a == b && a.id == .home && !a.isSearch)196        b.badge = nil197        #expect(a != b)198        #expect(GlassTabBarMinimizeBehavior.allCases.count == 4)199    }200}201
Files main
Menu

Theme