Repository Public MIT
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP GlassKit7// File : Tests/SPGlassDataTests/GlassDataGridTests.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 SwiftUI43import Testing44@testable import SPGlassData4546@Suite("GlassDataGrid")47struct GlassDataGridTests {48 struct Item: Identifiable {49 let id: Int50 }5152 /// A collection that counts how many elements were read, to check that nothing walks it eagerly.53 final class Counter: @unchecked Sendable {54 var reads = 055 }5657 struct CountingCollection: RandomAccessCollection {58 let count: Int59 let counter: Counter60 var startIndex: Int { 0 }61 var endIndex: Int { count }62 subscript(position: Int) -> Item {63 counter.reads += 164 return Item(id: position)65 }66 }6768 @Test func fixedLayoutsHaveOneItemPerTrack() {69 #expect(GlassGridLayout.fixed(3).gridItems(spacing: 8).count == 3)70 #expect(GlassGridLayout.fixed(0).gridItems(spacing: 8).count == 1)71 #expect(GlassGridLayout.fixed(-4).gridItems(spacing: 8).count == 1)72 #expect(GlassGridLayout.fixed(3).gridItems(spacing: 8).allSatisfy { $0.spacing == 8 })73 }7475 @Test func adaptiveLayoutsAreOneAdaptiveItem() {76 #expect(GlassGridLayout.adaptive(minimum: 120).gridItems(spacing: 12).count == 1)77 #expect(GlassGridLayout.adaptive(minimum: 120, maximum: 40).gridItems(spacing: 12).count == 1)78 }7980 @Test func trackCountFollowsTheAvailableLength() {81 let layout = GlassGridLayout.adaptive(minimum: 100)82 #expect(layout.trackCount(in: 320, spacing: 10) == 3)83 #expect(layout.trackCount(in: 100, spacing: 10) == 1)84 #expect(layout.trackCount(in: 210, spacing: 10) == 2)85 #expect(layout.trackCount(in: 209, spacing: 10) == 1)86 #expect(layout.trackCount(in: 0, spacing: 10) == 1)87 #expect(layout.trackCount(in: -50, spacing: 10) == 1)88 #expect(layout.trackCount(in: .infinity, spacing: 10) == 1)89 #expect(layout.trackCount(in: .nan, spacing: 10) == 1)90 #expect(GlassGridLayout.adaptive(minimum: 0).trackCount(in: 10, spacing: 0) == 10)91 #expect(GlassGridLayout.fixed(4).trackCount(in: 10, spacing: 10) == 4)92 #expect(GlassGridLayout.fixed(0).trackCount(in: 500, spacing: 10) == 1)93 }9495 @MainActor96 @Test("Creating a grid reads no element of a 50 000 element collection")97 func nothingIsEagerlyEvaluated() {98 let counter = Counter()99 let data = CountingCollection(count: 50_000, counter: counter)100 let grid = GlassDataGrid(data) { item in Text(verbatim: "\(item.id)") }101 _ = grid102 #expect(counter.reads == 0)103 #expect(data.count == 50_000)104 #expect(counter.reads == 0)105 }106107 @Test func selectionTogglesIdentifiers() {108 var selection: Set<Int> = []109 selection = GlassTableLogic.toggling(7, in: selection)110 selection = GlassTableLogic.toggling(9, in: selection)111 #expect(selection == [7, 9])112 selection = GlassTableLogic.toggling(7, in: selection)113 #expect(selection == [9])114 }115}116