SPB Git

Repository Public MIT

AppTests.swift 3.0 KB 67 lines swift

1// ORIX Calc2// File: Tests/AppTests.swift3// Description: App-level tests: launch options, persistence across sessions, backup round trip.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import Testing9import ORIXUI10import ORIXExpression11import ORIXWorkspace12@testable import ORIXCalc1314@MainActor @Suite struct AppTests {15    func makeApp() -> AppModel {16        AppModel(settings: AppSettings(defaults: UserDefaults(suiteName: "orix.tests.\(UUID().uuidString)")!), inMemory: true)17    }1819    @Test func launchOptions() {20        let o = LaunchOptions(arguments: ["app", "-ORIXMode", "graph", "-ORIXDemo", "1", "-ORIXSection", "tvm"])21        #expect(o.mode == .graph && o.demo && o.section == "tvm" && !o.inMemory)22    }23    @Test func historyAndVariablesAreShared() {24        let app = makeApp()25        guard case .success = app.evaluate("rate = 0.05", mode: .standard) else { Issue.record("evaluation failed"); return }26        guard case .success(let r) = app.evaluate("1000 * (1 + rate)^10", mode: .scientific) else { Issue.record("evaluation failed"); return }27        #expect(r.value?.doubleValue.map { abs($0 - 1628.894626777442) < 1e-9 } == true)28        #expect(app.history.count == 2 && app.history[0].mode == .scientific)29        #expect(app.variables.contains { $0.name == "rate" })30        app.deleteHistory([app.history[0].id])31        #expect(app.history.count == 1)32    }33    @Test func backupRoundTrip() throws {34        let a = makeApp()35        _ = a.evaluate("x = 1/3", mode: .standard)36        _ = a.evaluate("f(t) = t^2 + x", mode: .scientific)37        a.saveWorkspace(WorkspaceDocument(title: "Sheet", cells: [.formula("a = 2"), .formula("b = a * 21")]))38        let data = try #require(a.exportBackup())3940        let b = makeApp()41        #expect(b.importBackup(data) != nil)42        #expect(b.engine.context.variables["x"] == Value(BigRational(1, 3)))43        guard case .success(let r) = b.evaluate("f(2)", mode: .standard) else { Issue.record("function missing"); return }44        #expect(r.value == Value(BigRational(13, 3)))45        #expect(b.workspaces().map(\.title) == ["Sheet"])46        #expect(b.importBackup(Data("{}".utf8)) == nil)47    }48    @Test func workspaceFileOpening() throws {49        let app = makeApp()50        let doc = WorkspaceDocument(title: "Opened", cells: [.formula("k = 7")])51        #expect(app.openWorkspaceFile(try WorkspaceFile.encode(doc)) == nil)52        #expect(app.mode == .workspace && app.pendingWorkspaceDocument?.title == "Opened")53        #expect(app.openWorkspaceFile(Data("garbage".utf8)) != nil)54    }55    @Test func settingsDriveFormatting() {56        let app = makeApp()57        app.settings.data.decimalPlaces = 258        app.settings.data.useGrouping = false59        app.applySettings()60        #expect(app.format(Value(BigRational(2, 3))).hasSuffix("67"))61        // the stored value is untouched by display rounding62        _ = app.evaluate("y = 2/3", mode: .standard)63        guard case .success(let r) = app.evaluate("y * 3", mode: .standard) else { return }64        #expect(r.value == Value(2))65    }66}67
Files main
Menu

Theme