SPB Git

Repository Public MIT

GlassTableTests.swift 5.9 KB 134 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Tests/SPGlassDataTests/GlassTableTests.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 SPGlassData4445@Suite("GlassTable")46struct GlassTableTests {47    struct City: Identifiable, Equatable {48        let id: Int49        let name: String50        let population: Int51    }5253    let cities = [54        City(id: 1, name: "Gatineau", population: 291_000),55        City(id: 2, name: "Québec", population: 549_000),56        City(id: 3, name: "amos", population: 12_800),57        City(id: 4, name: "Laval", population: 438_000),58        City(id: 5, name: "Lévis", population: 149_000),59        City(id: 6, name: "Twin", population: 149_000),60    ]6162    var columns: [GlassTableColumn<City>] {63        [64            GlassTableColumn("Name", id: "name") { $0.name },65            GlassTableColumn("Population", id: "population", sortingBy: { $0.population }) { String($0.population) },66        ]67    }6869    @Test func noSortKeepsTheHostOrder() {70        #expect(GlassTableLogic.sorted(cities, by: nil, columns: columns) == cities)71    }7273    @Test func unknownColumnKeepsTheHostOrder() {74        #expect(GlassTableLogic.sorted(cities, by: GlassTableSort(columnID: "area"), columns: columns) == cities)75    }7677    @Test func sortsByTypedValueAscendingAndDescending() {78        let ascending = GlassTableLogic.sorted(cities, by: GlassTableSort(columnID: "population"), columns: columns)79        #expect(ascending.map(\.id) == [3, 5, 6, 1, 4, 2])80        let descending = GlassTableLogic.sorted(cities, by: GlassTableSort(columnID: "population", order: .reverse), columns: columns)81        #expect(descending.map(\.id) == [2, 4, 1, 5, 6, 3])82    }8384    @Test("Rows that compare equal keep their original order in both directions")85    func sortIsStable() {86        let rows = (0..<200).map { City(id: $0, name: "n\($0)", population: $0 % 3) }87        for order in [SortOrder.forward, .reverse] {88            let sorted = GlassTableLogic.sorted(rows, by: GlassTableSort(columnID: "population", order: order), columns: columns)89            #expect(sorted.count == rows.count)90            for (previous, next) in zip(sorted, sorted.dropFirst()) where previous.population == next.population {91                #expect(previous.id < next.id)92            }93        }94    }9596    @Test func textColumnsIgnoreCaseAndCompareNumbersByValue() {97        let byName = GlassTableLogic.sorted(cities, by: GlassTableSort(columnID: "name"), columns: columns)98        #expect(byName.first?.name == "amos")99        let files = [City(id: 1, name: "File 10", population: 0), City(id: 2, name: "File 9", population: 0)]100        #expect(GlassTableLogic.sorted(files, by: GlassTableSort(columnID: "name"), columns: columns).map(\.id) == [2, 1])101    }102103    @Test func sortingAnEmptyCollectionIsEmpty() {104        #expect(GlassTableLogic.sorted([City](), by: GlassTableSort(columnID: "name"), columns: columns).isEmpty)105    }106107    @Test func selectingAColumnStartsAscendingThenFlips() {108        let first = GlassTableLogic.sort(afterSelecting: "name", current: nil)109        #expect(first == GlassTableSort(columnID: "name", order: .forward))110        let second = GlassTableLogic.sort(afterSelecting: "name", current: first)111        #expect(second.order == .reverse)112        #expect(GlassTableLogic.sort(afterSelecting: "name", current: second).order == .forward)113        #expect(GlassTableLogic.sort(afterSelecting: "population", current: second) == GlassTableSort(columnID: "population"))114    }115116    @Test func selectionToggles() {117        #expect(GlassTableLogic.toggling(2, in: Set([1])) == [1, 2])118        #expect(GlassTableLogic.toggling(1, in: Set([1])).isEmpty)119        #expect(GlassTableLogic.toggling(1, in: Int?.none) == 1)120        #expect(GlassTableLogic.toggling(1, in: Int?.some(1)) == nil)121        #expect(GlassTableLogic.toggling(2, in: Int?.some(1)) == 2)122    }123124    @Test func systemComparatorFollowsTheColumnAndItsOrder() {125        let column = columns[1]126        var comparator = GlassTableColumnComparator(columnID: column.id, order: .forward, comparator: column.comparator)127        #expect(comparator.compare(cities[2], cities[0]) == .orderedAscending)128        comparator.order = .reverse129        #expect(comparator.compare(cities[2], cities[0]) == .orderedDescending)130        #expect(comparator.compare(cities[4], cities[5]) == .orderedSame)131        #expect(comparator != GlassTableColumnComparator(columnID: column.id, order: .forward, comparator: column.comparator))132    }133}134
Files main
Menu

Theme