SPB Git

Repository Public MIT

GlassSelectionTests.swift 8.0 KB 168 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Tests/SPGlassFormsTests/GlassSelectionTests.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 Testing43import SPGlassCore44@testable import SPGlassForms4546@Suite("Selection controls")47struct GlassSelectionTests {48    private typealias Math = GlassStepperMath4950    @Test func stepperMovesByWholeSteps() {51        #expect(Math.stepped(3, by: 1, .increment, in: 0...10) == 4)52        #expect(Math.stepped(3, by: 1, .decrement, in: 0...10) == 2)53        #expect(Math.stepped(4, by: 2, .increment, in: 0...10) == 6)54        #expect(Math.stepped(-5, by: 5, .decrement, in: -20...20) == -10)55    }5657    @Test func stepperStopsAtItsBounds() {58        #expect(Math.stepped(10, by: 1, .increment, in: 0...10) == 10)59        #expect(Math.stepped(0, by: 1, .decrement, in: 0...10) == 0)60        #expect(Math.stepped(9.5, by: 1, .increment, in: 0...10) == 10)61        #expect(Math.stepped(99, by: 1, .decrement, in: 0...10) == 9)62        #expect(Math.stepped(-99, by: 1, .increment, in: 0...10) == 1)63    }6465    @Test func floatingPointStepsDoNotAccumulateNoise() {66        var value = 0.067        var seen: [Double] = []68        for _ in 0..<10 {69            value = Math.stepped(value, by: 0.1, .increment, in: 0...1)70            seen.append(value)71        }72        #expect(seen == [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0])73        for _ in 0..<10 { value = Math.stepped(value, by: 0.1, .decrement, in: 0...1) }74        #expect(value == 0)75        #expect(Math.stepped(0.1 + 0.2, by: 0.1, .increment, in: 0...1) == 0.4)76        #expect(Math.stepped(1.15, by: 0.05, .increment, in: 1...2) == 1.2)77        #expect(Math.stepped(0.75, by: 0.25, .decrement, in: 0...2.5) == 0.5)78    }7980    @Test func stepsAreCountedFromTheLowerBound() {81        #expect(Math.stepped(1.5, by: 2, .increment, in: 1.5...10) == 3.5)82        #expect(Math.stepped(0.05, by: 0.1, .increment, in: 0.05...1) == 0.15)83        #expect(Math.stepped(-0.3, by: 0.1, .increment, in: -0.5...0.5) == -0.2)84    }8586    @Test func aValueBetweenStepsMovesToTheNeighbouringStep() {87        #expect(Math.stepped(3.4, by: 1, .increment, in: 0...10) == 4)88        #expect(Math.stepped(3.4, by: 1, .decrement, in: 0...10) == 3)89        #expect(Math.stepped(0.26, by: 0.25, .decrement, in: 0...1) == 0.25)90    }9192    @Test func boundsNotAlignedOnTheStep() {93        // Steps from 0 by 3 are 0, 3, 6, 9; the upper bound 10 is not one of them.94        #expect(Math.stepped(9, by: 3, .increment, in: 0...10) == 10)95        #expect(Math.stepped(10, by: 3, .decrement, in: 0...10) == 9)96        #expect(Math.stepped(10, by: 3, .increment, in: 0...10) == 10)97        #expect(Math.stepped(0.9, by: 0.4, .increment, in: 0.1...1) == 1)98        #expect(Math.stepped(1, by: 0.4, .decrement, in: 0.1...1) == 0.9)99    }100101    @Test("A step of zero or less only clamps", arguments: [0.0, -1.0])102    func degenerateStep(step: Double) {103        #expect(Math.stepped(5, by: step, .increment, in: 0...10) == 5)104        #expect(Math.stepped(50, by: step, .decrement, in: 0...10) == 10)105    }106107    @Test func stepperKnowsWhenAButtonIsUseless() {108        #expect(Math.canIncrement(9, in: 0...10) && !Math.canIncrement(10, in: 0...10))109        #expect(Math.canDecrement(1, in: 0...10) && !Math.canDecrement(0, in: 0...10))110    }111112    @Test func valueAtStepIsExactForDecimalSteps() {113        #expect(Math.value(atStep: 3, from: 0, step: 0.1) == 0.3)114        #expect(Math.value(atStep: 7, from: 0.05, step: 0.01) == 0.12)115        #expect(Math.value(atStep: 4, from: -1, step: 0.5) == 1)116        #expect(Math.decimals(of: 0.25) == 2 && Math.decimals(of: 3) == 0 && Math.decimals(of: 0.001) == 3)117    }118119    @Test func sliderSnapsToStepsInsideItsBounds() {120        #expect(GlassSliderMath.snapped(0.34, in: 0...1, step: 0.1) == 0.3)121        #expect(GlassSliderMath.snapped(0.36, in: 0...1, step: 0.1) == 0.4)122        #expect(GlassSliderMath.snapped(7, in: 0...5, step: 1) == 5)123        #expect(GlassSliderMath.snapped(-2, in: 0...5, step: 1) == 0)124        #expect(GlassSliderMath.snapped(9.9, in: 0...10, step: 3) == 9)125        #expect(GlassSliderMath.snapped(0.123, in: 0...1, step: nil) == 0.123)126        #expect(GlassSliderMath.snapped(0.123, in: 0...1, step: 0) == 0.123)127        #expect(GlassSliderMath.snapped(18.3, in: 16...28, step: 0.5) == 18.5)128    }129130    @Test func sliderFraction() {131        #expect(GlassSliderMath.fraction(5, in: 0...10) == 0.5)132        #expect(GlassSliderMath.fraction(-5, in: 0...10) == 0)133        #expect(GlassSliderMath.fraction(50, in: 0...10) == 1)134        #expect(GlassSliderMath.fraction(3, in: 3...3) == 0)135        #expect(GlassSliderMath.fraction(22, in: 16...28) == 0.5)136    }137138    @Test func pickerPresentationAdaptsToThePlatform() {139        #expect(GlassPickerPresentation.menu.resolved(for: .iOS) == .menu)140        #expect(GlassPickerPresentation.menu.resolved(for: .macOS) == .menu)141        #expect(GlassPickerPresentation.wheel.resolved(for: .iOS) == .wheel)142        #expect(GlassPickerPresentation.wheel.resolved(for: .iPadOS) == .wheel)143        #expect(GlassPickerPresentation.wheel.resolved(for: .macOS) == .menu)144        #expect(GlassPickerPresentation.inline.resolved(for: .macOS) == .radioGroup)145        #expect(GlassPickerPresentation.inline.resolved(for: .iOS) == .inline)146    }147148    @Test func togglesUseACheckboxOnlyOnMacOS() {149        #expect(GlassToggleAppearance.checkbox.resolved(for: .macOS) == .checkbox)150        #expect(GlassToggleAppearance.checkbox.resolved(for: .iOS) == .switch)151        #expect(GlassToggleAppearance.checkbox.resolved(for: .iPadOS) == .switch)152        #expect(GlassToggleAppearance.switch.resolved(for: .macOS) == .switch)153        #expect(GlassToggleAppearance.automatic.resolved(for: .iOS) == .automatic)154    }155156    @Test func segmentsAreTriedOnlyWhenTheyCanBeRead() {157        typealias Layout = GlassSegmentedLayout158        #expect(Layout.presentation(optionCount: 3, isAccessibilitySize: false) == .segmentedIfFits)159        #expect(Layout.presentation(optionCount: 5, isAccessibilitySize: false) == .segmentedIfFits)160        #expect(Layout.presentation(optionCount: 6, isAccessibilitySize: false) == .menu)161        #expect(Layout.presentation(optionCount: 3, isAccessibilitySize: true) == .menu)162        #expect(Layout.presentation(optionCount: 1, isAccessibilitySize: false) == .menu)163        #expect(Layout.presentation(optionCount: 0, isAccessibilitySize: false) == .menu)164        #expect(Layout.presentation(optionCount: 7, isAccessibilitySize: false, maximumSegments: 8) == .segmentedIfFits)165        #expect(Layout.presentation(optionCount: 3, isAccessibilitySize: false, maximumSegments: 2) == .menu)166    }167}168
Files main
Menu

Theme