Tests/OrgAppTests/PlotChartTests.swift

06941b2cf25c6ccc1629723cd7951910ce15e67a
orgstar/Tests/OrgAppTests/PlotChartTests.swift history · blame · raw

38 lines · 2080 bytes

 1import Foundation
 2import OrgCore
 3@testable import OrgApp
 4import Testing
 5
 6@MainActor
 7struct PlotChartTests {
 8    @Test func writesImagesAndPDF() throws {
 9        let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-plotchart-\(UUID().uuidString)")
10        defer { try? FileManager.default.removeItem(at: folder) }
11        let texts = [
12            "#+PLOT: title:\"Lines\" ind:1 file:\"a.png\"\n| x | a | b |\n|---+---+---|\n| 1 | 2 | 3 |\n| 2 | 4 | 1 |\n| 3 | 3 | 5 |\n",
13            "#+PLOT: ind:1 with:histograms file:\"b.jpg\"\n| name | n |\n|------+---|\n| a | 3 |\n| b | 5 |\n",
14            "#+PLOT: type:grid ind:1 file:\"c.pdf\"\n| r | a | b |\n|---+---+---|\n| x | 1 | 2 |\n| y | 3 | 4 |\n",
15            "#+PLOT: type:3d file:\"d.png\"\n| 1 | 2 | 3 |\n| 2 | 4 | 6 |\n| 3 | 6 | 9 |\n",
16        ]
17        for text in texts {
18            let plot = try TablePlot.plot(text, at: (text as NSString).range(of: "|").location)
19            let url = try #require(PlotOutput.file(plot, directory: folder))
20            try PlotOutput.write(plot, to: url)
21            let size = (try FileManager.default.attributesOfItem(atPath: url.path)[.size] as? Int) ?? 0
22            #expect(size > 1000, "\(url.lastPathComponent) is \(size) bytes")
23            if let copy = ProcessInfo.processInfo.environment["ORGSTAR_PLOT_DUMP"] {
24                try? FileManager.default.removeItem(atPath: copy + "/" + url.lastPathComponent)
25                try FileManager.default.copyItem(atPath: url.path, toPath: copy + "/" + url.lastPathComponent)
26            }
27        }
28        let svg = try TablePlot.plot("#+PLOT: file:\"e.svg\"\n| 1 | 2 |\n", at: 25)
29        #expect(throws: PlotOutput.Failure.self) { try PlotOutput.write(svg, to: PlotOutput.file(svg, directory: folder)!) }
30    }
31
32    @Test func interpolatesTheMatrix() {
33        let m: [[Double]] = [[1, 2], [3, 4]]
34        #expect(PlotChart.Surface.interpolate(m, 1, 1) == 1)
35        #expect(PlotChart.Surface.interpolate(m, 2, 2) == 4)
36        #expect(PlotChart.Surface.interpolate(m, 1.5, 1.5) == 2.5)
37    }
38}