SPB Git

Repository Public

CatalogTests.swift 4.4 KB 81 lines swift

1import Foundation2import Testing3import SheetCore4import SheetUI5import Statistics6import AnalysisKit7@testable import StatsBasic89/// Every cell of every sheet as text (value, formula flag, style), to compare workbook states.10func snapshot(_ wb: Workbook) -> [String] {11    wb.sheets.map { s in12        var cells = [String]()13        s.cells.forEachAll { r, c, cell in14            if cell.hasContent || cell.style != 0 { cells.append("\(r),\(c)=\(cell.computed)|\(wb.styles[cell.style].hashValue)") }15        }16        return s.name + ":" + cells.sorted().joined(separator: ";") + "|objects=\(s.meta.objects.count)"17    }18}1920@MainActor @Suite struct CatalogTests {21    @Test func toolListIsWellFormed() {22        let tools = StatsBasicTools.all23        #expect(tools.count == 13)24        #expect(Set(tools.map(\.id)).count == tools.count)25        #expect(tools.map(\.group).reduce(into: [String]()) { if $0.last != $1 { $0.append($1) } } == ["Describe", "Compare", "Relationships"])26        for t in tools {27            #expect(!t.title.isEmpty && !t.summary.isEmpty && !t.info.isEmpty && t.demo != nil, "\(t.id)")28            #expect(Set(t.fields.map(\.id)).count == t.fields.count, "duplicate field id in \(t.id)")29        }30        // every test offers the significance level used by the interpretation31        for t in tools where t.group == "Compare" && t.id != "compare.ci" { #expect(t.field("alpha") != nil, "\(t.id)") }32    }3334    @Test func formsShowOnlyTheRelevantFields() {35        let t = TTestTool.descriptor36        var v = FormValues(defaults: t.fields)37        func visible() -> Set<String> { Set(t.fields.filter { $0.isVisible(v) }.map(\.id)) }38        #expect(visible().contains("mu") && !visible().contains("y") && !visible().contains("variances"))39        v.set("kind", choice: "two")40        #expect(visible().isSuperset(of: ["y", "variances", "difference"]) && !visible().contains("mu"))41        let ci = ConfidenceIntervalTool.descriptor42        var c = FormValues(defaults: ci.fields)43        c.set("kind", choice: "proportion")44        #expect(!ci.fields.first { $0.id == "data" }!.isVisible(c) && ci.fields.first { $0.id == "trials" }!.isVisible(c))45    }4647    @Test func frenchCatalogIsComplete() throws {48        let fr = try #require(AnalysisLocalization.localized(Bundle.module, "fr-CA"))49        #expect(fr.localizedString(forKey: "Standard deviation", value: nil, table: nil) == "Écart-type")50        #expect(fr.localizedString(forKey: "Degrees of freedom", value: nil, table: nil) == "Degrés de liberté")51        #expect(fr.localizedString(forKey: "Chi-square test of independence", value: nil, table: nil) == "Test d’indépendance du khi-deux")52        #expect(fr.localizedString(forKey: "Analysis of variance", value: nil, table: nil) == "Analyse de variance")53    }5455    @Test func testReportIsWrittenWithFormatsAndUndoRestoresTheWorkbook() throws {56        let session = columnSession([("a", sampleA.map { $0 }), ("b", sampleB.map { $0 })])57        let result = try run(TTestTool.descriptor, on: session, range: "A1:B11") { $0.set("kind", choice: "two"); $0.set("x", column: 0); $0.set("y", column: 1) }58        let before = snapshot(session.workbook)59        let outcome = ReportWriter.write(result, to: .newSheet, includeCharts: true, session: session)60        let wb = session.workbook61        #expect(wb.sheets.count == 2 && wb.sheets[1].name == "t-test")62        let sh = wb.sheets[1]63        #expect(sh.value(0, 0) == .text("Welch two-sample t-test") && wb.styles[sh.styleID(0, 0)].bold)64        // find the p-value row of the detail table: value and number format65        let ref = try Inference.tTest(sampleA, sampleB)66        var found = false67        for r in 0...outcome.range.r1 where sh.value(r, 0) == .text("p-value") && r > 8 {68            #expect(close(sh.value(r, 1).numberValue, ref.pValue) && wb.styles[sh.styleID(r, 1)].numberFormat == "0.0000")69            found = true70        }71        #expect(found)72        var texts = [String]()73        sh.cells.forEachAll { _, _, cell in if case .text(let s) = cell.computed { texts.append(s) } }74        #expect(texts.contains { $0.contains("reject the null hypothesis") })75        #expect(texts.contains("The two samples are independent"))76        #expect(outcome.chartCount == 1 && sh.meta.objects.count == 1)77        session.undo()78        #expect(snapshot(session.workbook) == before && session.workbook.sheets.count == 1 && !session.engine.canUndo)79    }80}81
Files main
Menu

Theme