feat: add platform shells, CLI, formatting, plugins, tests, and benchmarks
Phase 4 — Platform shells: - calcpad-macos/: SwiftUI two-column editor with Rust FFI bridge (16 files) - calcpad-windows/: iced GUI with Windows 11 Fluent theme (7 files, 13 tests) - calcpad-web/: React 18 + CodeMirror 6 + WASM Worker + PWA (20 files) - calcpad-cli/: clap-based CLI with expression eval, pipe/stdin, JSON/CSV output, and interactive REPL with rustyline history Phase 5 — Engine modules: - formatting/: answer formatting (decimal/scientific/SI notation, thousands separators, currency), line type classification, clipboard values (93 tests) - plugins/: CalcPadPlugin trait, PluginRegistry, Rhai scripting stub (43 tests) - benches/: criterion benchmarks (single-line, 100/500-line sheets, DAG, incremental) - tests/sheet_scenarios.rs: 20 real-world integration tests - tests/proptest_fuzz.rs: 12 property-based fuzz tests 771 tests passing across workspace, 0 failures.
This commit is contained in:
37
calcpad-macos/Package.swift
Normal file
37
calcpad-macos/Package.swift
Normal file
@@ -0,0 +1,37 @@
|
||||
// swift-tools-version: 5.9
|
||||
|
||||
import PackageDescription
|
||||
|
||||
// Path to the Rust static library built by `cargo build --release`
|
||||
let rustLibPath = "../calcpad-engine/target/release"
|
||||
|
||||
let package = Package(
|
||||
name: "CalcPad",
|
||||
platforms: [
|
||||
.macOS(.v14)
|
||||
],
|
||||
targets: [
|
||||
.systemLibrary(
|
||||
name: "CCalcPadEngine",
|
||||
path: "Sources/CCalcPadEngine"
|
||||
),
|
||||
.executableTarget(
|
||||
name: "CalcPad",
|
||||
dependencies: ["CCalcPadEngine"],
|
||||
path: "Sources/CalcPad",
|
||||
linkerSettings: [
|
||||
.unsafeFlags(["-L\(rustLibPath)"]),
|
||||
.linkedLibrary("calcpad_engine"),
|
||||
]
|
||||
),
|
||||
.testTarget(
|
||||
name: "CalcPadTests",
|
||||
dependencies: ["CalcPad"],
|
||||
path: "Tests/CalcPadTests",
|
||||
linkerSettings: [
|
||||
.unsafeFlags(["-L\(rustLibPath)"]),
|
||||
.linkedLibrary("calcpad_engine"),
|
||||
]
|
||||
),
|
||||
]
|
||||
)
|
||||
90
calcpad-macos/Sources/CCalcPadEngine/calcpad.h
Normal file
90
calcpad-macos/Sources/CCalcPadEngine/calcpad.h
Normal file
@@ -0,0 +1,90 @@
|
||||
/**
|
||||
* CalcPad Engine — C FFI Header
|
||||
*
|
||||
* This header declares the C-compatible interface for the CalcPad calculation
|
||||
* engine, built in Rust. It is designed for consumption by Swift via a
|
||||
* bridging header or a module map.
|
||||
*
|
||||
* All functions are safe to call from any thread. Panics in Rust are caught
|
||||
* and converted to error results — they never unwind into the caller.
|
||||
*
|
||||
* Memory ownership:
|
||||
* - Strings returned by calcpad_eval_line / calcpad_eval_sheet are
|
||||
* heap-allocated by Rust and MUST be freed by calling calcpad_free_result.
|
||||
* - Passing NULL to calcpad_free_result is a safe no-op.
|
||||
*
|
||||
* JSON schema (version "1.0"):
|
||||
*
|
||||
* Single-line result (calcpad_eval_line):
|
||||
* {
|
||||
* "schema_version": "1.0",
|
||||
* "result": {
|
||||
* "value": { "kind": "Number", "value": 42.0 },
|
||||
* "metadata": {
|
||||
* "span": { "start": 0, "end": 4 },
|
||||
* "result_type": "Number",
|
||||
* "display": "42",
|
||||
* "raw_value": 42.0
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* Multi-line result (calcpad_eval_sheet):
|
||||
* {
|
||||
* "schema_version": "1.0",
|
||||
* "results": [ ... ] // array of result objects as above
|
||||
* }
|
||||
*/
|
||||
|
||||
#ifndef CALCPAD_H
|
||||
#define CALCPAD_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Evaluate a single line of CalcPad input.
|
||||
*
|
||||
* @param input A null-terminated UTF-8 string containing the expression.
|
||||
* Passing NULL returns a JSON error result.
|
||||
*
|
||||
* @return A heap-allocated, null-terminated JSON string containing the
|
||||
* versioned result. The caller MUST free this with
|
||||
* calcpad_free_result(). Returns NULL only on catastrophic
|
||||
* allocation failure.
|
||||
*/
|
||||
char *calcpad_eval_line(const char *input);
|
||||
|
||||
/**
|
||||
* Evaluate multiple lines of CalcPad input as a sheet.
|
||||
*
|
||||
* Variable assignments on earlier lines are visible to later lines
|
||||
* (e.g., "x = 5" on line 1 makes x available on line 2).
|
||||
*
|
||||
* @param lines An array of null-terminated UTF-8 strings.
|
||||
* NULL entries are treated as empty lines.
|
||||
* @param count The number of elements in the lines array.
|
||||
* Must be > 0.
|
||||
*
|
||||
* @return A heap-allocated, null-terminated JSON string containing the
|
||||
* versioned results array. The caller MUST free this with
|
||||
* calcpad_free_result(). Returns NULL only on catastrophic
|
||||
* allocation failure.
|
||||
*/
|
||||
char *calcpad_eval_sheet(const char *const *lines, int count);
|
||||
|
||||
/**
|
||||
* Free a result string previously returned by calcpad_eval_line or
|
||||
* calcpad_eval_sheet.
|
||||
*
|
||||
* @param ptr The pointer to free. Passing NULL is safe (no-op).
|
||||
* After this call the pointer is invalid.
|
||||
*/
|
||||
void calcpad_free_result(char *ptr);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* CALCPAD_H */
|
||||
5
calcpad-macos/Sources/CCalcPadEngine/module.modulemap
Normal file
5
calcpad-macos/Sources/CCalcPadEngine/module.modulemap
Normal file
@@ -0,0 +1,5 @@
|
||||
module CCalcPadEngine {
|
||||
header "calcpad.h"
|
||||
link "calcpad_engine"
|
||||
export *
|
||||
}
|
||||
11
calcpad-macos/Sources/CalcPad/App/CalcPadApp.swift
Normal file
11
calcpad-macos/Sources/CalcPad/App/CalcPadApp.swift
Normal file
@@ -0,0 +1,11 @@
|
||||
import SwiftUI
|
||||
|
||||
@main
|
||||
struct CalcPadApp: App {
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
ContentView()
|
||||
}
|
||||
.defaultSize(width: 800, height: 600)
|
||||
}
|
||||
}
|
||||
7
calcpad-macos/Sources/CalcPad/App/ContentView.swift
Normal file
7
calcpad-macos/Sources/CalcPad/App/ContentView.swift
Normal file
@@ -0,0 +1,7 @@
|
||||
import SwiftUI
|
||||
|
||||
struct ContentView: View {
|
||||
var body: some View {
|
||||
TwoColumnEditorView()
|
||||
}
|
||||
}
|
||||
62
calcpad-macos/Sources/CalcPad/Engine/CalculationEngine.swift
Normal file
62
calcpad-macos/Sources/CalcPad/Engine/CalculationEngine.swift
Normal file
@@ -0,0 +1,62 @@
|
||||
import Foundation
|
||||
|
||||
/// Protocol for a calculation engine that evaluates text expressions.
|
||||
/// The primary implementation is RustCalculationEngine (FFI bridge to calcpad-engine).
|
||||
/// StubCalculationEngine is provided for testing without the Rust library.
|
||||
protocol CalculationEngine: Sendable {
|
||||
/// Evaluate a single line expression and return the result string, or nil for blank/comment lines.
|
||||
func evaluateLine(_ line: String) -> LineResult
|
||||
|
||||
/// Evaluate an entire sheet (multi-line text) and return results for each line.
|
||||
func evaluateSheet(_ text: String) -> [LineResult]
|
||||
}
|
||||
|
||||
/// Stub engine for testing that handles basic arithmetic (+, -, *, /).
|
||||
/// Used when the Rust engine library is not available.
|
||||
final class StubCalculationEngine: CalculationEngine {
|
||||
|
||||
func evaluateLine(_ line: String) -> LineResult {
|
||||
evaluateSingleLine(line, lineNumber: 1)
|
||||
}
|
||||
|
||||
func evaluateSheet(_ text: String) -> [LineResult] {
|
||||
let lines = text.components(separatedBy: "\n")
|
||||
return lines.enumerated().map { index, line in
|
||||
evaluateSingleLine(line, lineNumber: index + 1)
|
||||
}
|
||||
}
|
||||
|
||||
private func evaluateSingleLine(_ line: String, lineNumber: Int) -> LineResult {
|
||||
let trimmed = line.trimmingCharacters(in: .whitespaces)
|
||||
|
||||
// Blank lines produce no result
|
||||
guard !trimmed.isEmpty else {
|
||||
return LineResult(id: lineNumber, expression: line, result: nil, isError: false)
|
||||
}
|
||||
|
||||
// Comment lines (starting with // or #) produce no result
|
||||
if trimmed.hasPrefix("//") || trimmed.hasPrefix("#") {
|
||||
return LineResult(id: lineNumber, expression: line, result: nil, isError: false)
|
||||
}
|
||||
|
||||
// Convert integer literals to floating-point so division produces decimals.
|
||||
// NSExpression does integer division for "7 / 2" -> 3, but we want 3.5.
|
||||
let floatExpr = trimmed.replacingOccurrences(
|
||||
of: #"\b(\d+)\b"#,
|
||||
with: "$1.0",
|
||||
options: .regularExpression
|
||||
)
|
||||
|
||||
// Try to evaluate as a basic arithmetic expression
|
||||
do {
|
||||
let expr = NSExpression(format: floatExpr)
|
||||
if let value = expr.expressionValue(with: nil, context: nil) as? NSNumber {
|
||||
let doubleValue = value.doubleValue
|
||||
let formatted = String(format: "%g", doubleValue)
|
||||
return LineResult(id: lineNumber, expression: line, result: formatted, isError: false)
|
||||
}
|
||||
}
|
||||
|
||||
return LineResult(id: lineNumber, expression: line, result: "Error", isError: true)
|
||||
}
|
||||
}
|
||||
107
calcpad-macos/Sources/CalcPad/Engine/FFIModels.swift
Normal file
107
calcpad-macos/Sources/CalcPad/Engine/FFIModels.swift
Normal file
@@ -0,0 +1,107 @@
|
||||
import Foundation
|
||||
|
||||
/// JSON response from `calcpad_eval_line` — wraps a single `CalcResult`.
|
||||
struct FFIResponse: Decodable {
|
||||
let schemaVersion: String
|
||||
let result: FFICalcResult
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case schemaVersion = "schema_version"
|
||||
case result
|
||||
}
|
||||
}
|
||||
|
||||
/// JSON response from `calcpad_eval_sheet` — wraps an array of `CalcResult`.
|
||||
struct FFISheetResponse: Decodable {
|
||||
let schemaVersion: String
|
||||
let results: [FFICalcResult]
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case schemaVersion = "schema_version"
|
||||
case results
|
||||
}
|
||||
}
|
||||
|
||||
/// A complete calculation result returned by the Rust engine.
|
||||
struct FFICalcResult: Decodable {
|
||||
let value: FFICalcValue
|
||||
let metadata: FFIResultMetadata
|
||||
}
|
||||
|
||||
/// Metadata attached to every evaluation result.
|
||||
struct FFIResultMetadata: Decodable {
|
||||
let span: FFISpan
|
||||
let resultType: String
|
||||
let display: String
|
||||
let rawValue: Double?
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case span
|
||||
case resultType = "result_type"
|
||||
case display
|
||||
case rawValue = "raw_value"
|
||||
}
|
||||
}
|
||||
|
||||
/// Source span (byte offsets).
|
||||
struct FFISpan: Decodable {
|
||||
let start: Int
|
||||
let end: Int
|
||||
}
|
||||
|
||||
/// Tagged union of possible calculation values.
|
||||
/// Rust serializes with `#[serde(tag = "kind")]`.
|
||||
enum FFICalcValue: Decodable {
|
||||
case number(value: Double)
|
||||
case unitValue(value: Double, unit: String)
|
||||
case currencyValue(amount: Double, currency: String)
|
||||
case dateTime(date: String)
|
||||
case timeDelta(days: Int64, description: String)
|
||||
case boolean(value: Bool)
|
||||
case error(message: String)
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case kind
|
||||
case value, unit, amount, currency, date, days, description, message, span
|
||||
}
|
||||
|
||||
init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
let kind = try container.decode(String.self, forKey: .kind)
|
||||
|
||||
switch kind {
|
||||
case "Number":
|
||||
let value = try container.decode(Double.self, forKey: .value)
|
||||
self = .number(value: value)
|
||||
case "UnitValue":
|
||||
let value = try container.decode(Double.self, forKey: .value)
|
||||
let unit = try container.decode(String.self, forKey: .unit)
|
||||
self = .unitValue(value: value, unit: unit)
|
||||
case "CurrencyValue":
|
||||
let amount = try container.decode(Double.self, forKey: .amount)
|
||||
let currency = try container.decode(String.self, forKey: .currency)
|
||||
self = .currencyValue(amount: amount, currency: currency)
|
||||
case "DateTime":
|
||||
let date = try container.decode(String.self, forKey: .date)
|
||||
self = .dateTime(date: date)
|
||||
case "TimeDelta":
|
||||
let days = try container.decode(Int64.self, forKey: .days)
|
||||
let desc = try container.decode(String.self, forKey: .description)
|
||||
self = .timeDelta(days: days, description: desc)
|
||||
case "Boolean":
|
||||
let value = try container.decode(Bool.self, forKey: .value)
|
||||
self = .boolean(value: value)
|
||||
case "Error":
|
||||
let message = try container.decode(String.self, forKey: .message)
|
||||
self = .error(message: message)
|
||||
default:
|
||||
self = .error(message: "Unknown result kind: \(kind)")
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether this value represents an error from the Rust engine.
|
||||
var isError: Bool {
|
||||
if case .error = self { return true }
|
||||
return false
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
import CCalcPadEngine
|
||||
import Foundation
|
||||
|
||||
/// Calculation engine backed by the Rust CalcPad engine via C FFI.
|
||||
///
|
||||
/// Thread safety: Each call creates its own `EvalContext` on the Rust side,
|
||||
/// so concurrent calls from different threads are safe. The C FFI functions
|
||||
/// are documented as safe to call from any thread.
|
||||
final class RustCalculationEngine: CalculationEngine, @unchecked Sendable {
|
||||
|
||||
private let decoder = JSONDecoder()
|
||||
|
||||
func evaluateLine(_ line: String) -> LineResult {
|
||||
evaluateSingleLine(line, lineNumber: 1)
|
||||
}
|
||||
|
||||
func evaluateSheet(_ text: String) -> [LineResult] {
|
||||
let lines = text.components(separatedBy: "\n")
|
||||
|
||||
// Build C string array for the FFI call
|
||||
let cStrings = lines.map { strdup($0) }
|
||||
defer { cStrings.forEach { free($0) } }
|
||||
|
||||
var pointers = cStrings.map { UnsafePointer<CChar>($0) }
|
||||
|
||||
let resultPtr = pointers.withUnsafeMutableBufferPointer { buffer -> UnsafeMutablePointer<CChar>? in
|
||||
calcpad_eval_sheet(buffer.baseAddress, Int32(lines.count))
|
||||
}
|
||||
|
||||
guard let resultPtr else {
|
||||
return lines.enumerated().map { index, line in
|
||||
LineResult(id: index + 1, expression: line, result: "Error", isError: true)
|
||||
}
|
||||
}
|
||||
defer { calcpad_free_result(resultPtr) }
|
||||
|
||||
let jsonString = String(cString: resultPtr)
|
||||
|
||||
guard let jsonData = jsonString.data(using: .utf8),
|
||||
let response = try? decoder.decode(FFISheetResponse.self, from: jsonData) else {
|
||||
return lines.enumerated().map { index, line in
|
||||
LineResult(id: index + 1, expression: line, result: "Error", isError: true)
|
||||
}
|
||||
}
|
||||
|
||||
return response.results.enumerated().map { index, calcResult in
|
||||
mapToLineResult(calcResult, expression: lines[index], lineNumber: index + 1)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
private func evaluateSingleLine(_ line: String, lineNumber: Int) -> LineResult {
|
||||
let resultPtr = calcpad_eval_line(line)
|
||||
|
||||
guard let resultPtr else {
|
||||
return LineResult(id: lineNumber, expression: line, result: "Error", isError: true)
|
||||
}
|
||||
defer { calcpad_free_result(resultPtr) }
|
||||
|
||||
let jsonString = String(cString: resultPtr)
|
||||
|
||||
guard let jsonData = jsonString.data(using: .utf8),
|
||||
let response = try? decoder.decode(FFIResponse.self, from: jsonData) else {
|
||||
return LineResult(id: lineNumber, expression: line, result: "Error", isError: true)
|
||||
}
|
||||
|
||||
return mapToLineResult(response.result, expression: line, lineNumber: lineNumber)
|
||||
}
|
||||
|
||||
private func mapToLineResult(_ calcResult: FFICalcResult, expression: String, lineNumber: Int) -> LineResult {
|
||||
switch calcResult.value {
|
||||
case .error(let message):
|
||||
// Blank/comment lines produce "empty expression" errors in the Rust engine.
|
||||
// Map these to nil result (matching the protocol's convention for non-evaluable lines).
|
||||
if isEmptyExpressionError(message) {
|
||||
return LineResult(id: lineNumber, expression: expression, result: nil, isError: false)
|
||||
}
|
||||
return LineResult(id: lineNumber, expression: expression, result: "Error: \(message)", isError: true)
|
||||
|
||||
default:
|
||||
return LineResult(id: lineNumber, expression: expression, result: calcResult.metadata.display, isError: false)
|
||||
}
|
||||
}
|
||||
|
||||
/// The Rust engine returns an error for blank lines, comments, and other
|
||||
/// non-evaluable input. We detect these to return nil (matching protocol convention).
|
||||
private func isEmptyExpressionError(_ message: String) -> Bool {
|
||||
let lower = message.lowercased()
|
||||
return lower.contains("empty") || lower.contains("no expression") || lower.contains("comment")
|
||||
}
|
||||
}
|
||||
15
calcpad-macos/Sources/CalcPad/Models/LineResult.swift
Normal file
15
calcpad-macos/Sources/CalcPad/Models/LineResult.swift
Normal file
@@ -0,0 +1,15 @@
|
||||
import Foundation
|
||||
|
||||
/// Represents the evaluation result for a single line in the editor.
|
||||
struct LineResult: Identifiable, Equatable {
|
||||
let id: Int
|
||||
let expression: String
|
||||
let result: String?
|
||||
let isError: Bool
|
||||
|
||||
/// Line number (1-based) corresponding to this result.
|
||||
var lineNumber: Int { id }
|
||||
|
||||
/// A blank/comment line that produces no result.
|
||||
var isEmpty: Bool { result == nil && !isError }
|
||||
}
|
||||
111
calcpad-macos/Sources/CalcPad/Views/AnswerColumnView.swift
Normal file
111
calcpad-macos/Sources/CalcPad/Views/AnswerColumnView.swift
Normal file
@@ -0,0 +1,111 @@
|
||||
import AppKit
|
||||
import SwiftUI
|
||||
|
||||
/// Displays calculation results in a vertical column, one result per line,
|
||||
/// aligned to match the corresponding editor lines.
|
||||
/// Uses NSViewRepresentable wrapping NSScrollView + NSTextView for pixel-perfect
|
||||
/// line height alignment with the editor.
|
||||
struct AnswerColumnView: NSViewRepresentable {
|
||||
let results: [LineResult]
|
||||
@Binding var scrollOffset: CGFloat
|
||||
var font: NSFont
|
||||
|
||||
func makeCoordinator() -> Coordinator {
|
||||
Coordinator()
|
||||
}
|
||||
|
||||
func makeNSView(context: Context) -> NSScrollView {
|
||||
let scrollView = NSScrollView()
|
||||
scrollView.hasVerticalScroller = false
|
||||
scrollView.hasHorizontalScroller = false
|
||||
scrollView.borderType = .noBorder
|
||||
scrollView.drawsBackground = false
|
||||
// Disable user scrolling — scroll is driven by the editor
|
||||
scrollView.verticalScrollElasticity = .none
|
||||
scrollView.horizontalScrollElasticity = .none
|
||||
|
||||
let textView = NSTextView()
|
||||
textView.isEditable = false
|
||||
textView.isSelectable = true
|
||||
textView.isRichText = true
|
||||
textView.usesFontPanel = false
|
||||
textView.drawsBackground = false
|
||||
textView.backgroundColor = .clear
|
||||
|
||||
// Match editor text container settings for alignment
|
||||
textView.textContainer?.lineFragmentPadding = 4
|
||||
textView.textContainerInset = NSSize(width: 8, height: 8)
|
||||
|
||||
// Disable line wrapping to match editor behavior
|
||||
textView.isHorizontallyResizable = true
|
||||
textView.textContainer?.widthTracksTextView = false
|
||||
textView.textContainer?.containerSize = NSSize(
|
||||
width: CGFloat.greatestFiniteMagnitude,
|
||||
height: CGFloat.greatestFiniteMagnitude
|
||||
)
|
||||
textView.maxSize = NSSize(
|
||||
width: CGFloat.greatestFiniteMagnitude,
|
||||
height: CGFloat.greatestFiniteMagnitude
|
||||
)
|
||||
|
||||
scrollView.documentView = textView
|
||||
context.coordinator.textView = textView
|
||||
context.coordinator.scrollView = scrollView
|
||||
|
||||
updateContent(textView: textView)
|
||||
return scrollView
|
||||
}
|
||||
|
||||
func updateNSView(_ scrollView: NSScrollView, context: Context) {
|
||||
guard let textView = scrollView.documentView as? NSTextView else { return }
|
||||
|
||||
updateContent(textView: textView)
|
||||
|
||||
// Sync scroll position from editor
|
||||
let currentOffset = scrollView.contentView.bounds.origin.y
|
||||
if abs(currentOffset - scrollOffset) > 0.5 {
|
||||
scrollView.contentView.scroll(to: NSPoint(x: 0, y: scrollOffset))
|
||||
scrollView.reflectScrolledClipView(scrollView.contentView)
|
||||
}
|
||||
}
|
||||
|
||||
private func updateContent(textView: NSTextView) {
|
||||
let attributedString = NSMutableAttributedString()
|
||||
|
||||
let paragraphStyle = NSMutableParagraphStyle()
|
||||
paragraphStyle.alignment = .right
|
||||
|
||||
let resultColor = NSColor.secondaryLabelColor
|
||||
let errorColor = NSColor.systemRed
|
||||
|
||||
for (index, lineResult) in results.enumerated() {
|
||||
let displayText = lineResult.result ?? ""
|
||||
let color = lineResult.isError ? errorColor : resultColor
|
||||
|
||||
let attributes: [NSAttributedString.Key: Any] = [
|
||||
.font: font,
|
||||
.foregroundColor: color,
|
||||
.paragraphStyle: paragraphStyle,
|
||||
]
|
||||
|
||||
let line = NSAttributedString(string: displayText, attributes: attributes)
|
||||
attributedString.append(line)
|
||||
|
||||
// Add newline between lines (but not after the last)
|
||||
if index < results.count - 1 {
|
||||
let newline = NSAttributedString(
|
||||
string: "\n",
|
||||
attributes: [.font: font]
|
||||
)
|
||||
attributedString.append(newline)
|
||||
}
|
||||
}
|
||||
|
||||
textView.textStorage?.setAttributedString(attributedString)
|
||||
}
|
||||
|
||||
final class Coordinator {
|
||||
weak var textView: NSTextView?
|
||||
weak var scrollView: NSScrollView?
|
||||
}
|
||||
}
|
||||
137
calcpad-macos/Sources/CalcPad/Views/EditorTextView.swift
Normal file
137
calcpad-macos/Sources/CalcPad/Views/EditorTextView.swift
Normal file
@@ -0,0 +1,137 @@
|
||||
import AppKit
|
||||
import SwiftUI
|
||||
|
||||
/// A SwiftUI wrapper around NSTextView for the editor pane.
|
||||
/// Uses NSViewRepresentable to bridge AppKit's NSTextView into SwiftUI,
|
||||
/// providing line-level control, scroll position access, and performance
|
||||
/// that SwiftUI's TextEditor cannot match.
|
||||
struct EditorTextView: NSViewRepresentable {
|
||||
@Binding var text: String
|
||||
@Binding var scrollOffset: CGFloat
|
||||
var font: NSFont
|
||||
|
||||
func makeCoordinator() -> Coordinator {
|
||||
Coordinator(self)
|
||||
}
|
||||
|
||||
func makeNSView(context: Context) -> NSScrollView {
|
||||
let scrollView = NSScrollView()
|
||||
scrollView.hasVerticalScroller = true
|
||||
scrollView.hasHorizontalScroller = false
|
||||
scrollView.autohidesScrollers = true
|
||||
scrollView.borderType = .noBorder
|
||||
scrollView.drawsBackground = false
|
||||
|
||||
let textView = NSTextView()
|
||||
textView.isEditable = true
|
||||
textView.isSelectable = true
|
||||
textView.allowsUndo = true
|
||||
textView.isRichText = false
|
||||
textView.usesFontPanel = false
|
||||
textView.isAutomaticQuoteSubstitutionEnabled = false
|
||||
textView.isAutomaticDashSubstitutionEnabled = false
|
||||
textView.isAutomaticTextReplacementEnabled = false
|
||||
textView.isAutomaticSpellingCorrectionEnabled = false
|
||||
|
||||
// Disable line wrapping — horizontal scroll instead
|
||||
textView.isHorizontallyResizable = true
|
||||
textView.textContainer?.widthTracksTextView = false
|
||||
textView.textContainer?.containerSize = NSSize(
|
||||
width: CGFloat.greatestFiniteMagnitude,
|
||||
height: CGFloat.greatestFiniteMagnitude
|
||||
)
|
||||
textView.maxSize = NSSize(
|
||||
width: CGFloat.greatestFiniteMagnitude,
|
||||
height: CGFloat.greatestFiniteMagnitude
|
||||
)
|
||||
|
||||
// Configure font and text color
|
||||
textView.font = font
|
||||
textView.textColor = .textColor
|
||||
textView.backgroundColor = .clear
|
||||
textView.drawsBackground = false
|
||||
textView.insertionPointColor = .textColor
|
||||
|
||||
// Set the text
|
||||
textView.string = text
|
||||
|
||||
// Configure the text container for consistent line spacing
|
||||
textView.textContainer?.lineFragmentPadding = 4
|
||||
textView.textContainerInset = NSSize(width: 8, height: 8)
|
||||
|
||||
scrollView.documentView = textView
|
||||
context.coordinator.textView = textView
|
||||
context.coordinator.scrollView = scrollView
|
||||
|
||||
// Observe text changes
|
||||
textView.delegate = context.coordinator
|
||||
|
||||
// Observe scroll changes
|
||||
scrollView.contentView.postsBoundsChangedNotifications = true
|
||||
NotificationCenter.default.addObserver(
|
||||
context.coordinator,
|
||||
selector: #selector(Coordinator.scrollViewDidScroll(_:)),
|
||||
name: NSView.boundsDidChangeNotification,
|
||||
object: scrollView.contentView
|
||||
)
|
||||
|
||||
return scrollView
|
||||
}
|
||||
|
||||
func updateNSView(_ scrollView: NSScrollView, context: Context) {
|
||||
guard let textView = scrollView.documentView as? NSTextView else { return }
|
||||
|
||||
// Update font if changed
|
||||
if textView.font != font {
|
||||
textView.font = font
|
||||
// Reapply font to entire text storage
|
||||
let range = NSRange(location: 0, length: textView.textStorage?.length ?? 0)
|
||||
textView.textStorage?.addAttribute(.font, value: font, range: range)
|
||||
}
|
||||
|
||||
// Update text only if it actually changed (avoid feedback loops)
|
||||
if textView.string != text {
|
||||
let selectedRanges = textView.selectedRanges
|
||||
textView.string = text
|
||||
textView.selectedRanges = selectedRanges
|
||||
}
|
||||
|
||||
// Update scroll position if driven externally
|
||||
context.coordinator.isUpdatingScroll = true
|
||||
let currentOffset = scrollView.contentView.bounds.origin.y
|
||||
if abs(currentOffset - scrollOffset) > 0.5 {
|
||||
scrollView.contentView.scroll(to: NSPoint(x: 0, y: scrollOffset))
|
||||
scrollView.reflectScrolledClipView(scrollView.contentView)
|
||||
}
|
||||
context.coordinator.isUpdatingScroll = false
|
||||
}
|
||||
|
||||
final class Coordinator: NSObject, NSTextViewDelegate {
|
||||
var parent: EditorTextView
|
||||
weak var textView: NSTextView?
|
||||
weak var scrollView: NSScrollView?
|
||||
var isUpdatingScroll = false
|
||||
|
||||
init(_ parent: EditorTextView) {
|
||||
self.parent = parent
|
||||
}
|
||||
|
||||
func textDidChange(_ notification: Notification) {
|
||||
guard let textView = notification.object as? NSTextView else { return }
|
||||
parent.text = textView.string
|
||||
}
|
||||
|
||||
@objc func scrollViewDidScroll(_ notification: Notification) {
|
||||
guard !isUpdatingScroll,
|
||||
let scrollView = scrollView else { return }
|
||||
let offset = scrollView.contentView.bounds.origin.y
|
||||
DispatchQueue.main.async { [weak self] in
|
||||
self?.parent.scrollOffset = offset
|
||||
}
|
||||
}
|
||||
|
||||
deinit {
|
||||
NotificationCenter.default.removeObserver(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
import Combine
|
||||
import SwiftUI
|
||||
|
||||
/// The main two-column editor layout: text editor on the left, results on the right.
|
||||
/// Scrolling is synchronized between both columns.
|
||||
struct TwoColumnEditorView: View {
|
||||
@State private var text: String = ""
|
||||
@State private var scrollOffset: CGFloat = 0
|
||||
@State private var results: [LineResult] = []
|
||||
@State private var evaluationTask: Task<Void, Never>?
|
||||
|
||||
/// Uses the Rust FFI engine. Falls back to StubCalculationEngine if the Rust
|
||||
/// library is not linked (e.g., during UI-only development).
|
||||
private let engine: CalculationEngine = RustCalculationEngine()
|
||||
|
||||
/// Debounce interval for re-evaluation after typing (seconds).
|
||||
private let evaluationDebounce: TimeInterval = 0.05
|
||||
|
||||
/// Font that respects the user's accessibility / Dynamic Type settings.
|
||||
private var editorFont: NSFont {
|
||||
// Use the system's preferred monospaced font size, which scales with
|
||||
// Accessibility > Display > Text Size in System Settings (macOS 14+).
|
||||
let baseSize = NSFont.systemFontSize
|
||||
// Scale with accessibility settings via the body text style size
|
||||
let preferredSize = NSFont.preferredFont(forTextStyle: .body, options: [:]).pointSize
|
||||
// Use the larger of system default or accessibility-preferred size
|
||||
let size = max(baseSize, preferredSize)
|
||||
return NSFont.monospacedSystemFont(ofSize: size, weight: .regular)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
HSplitView {
|
||||
// Left pane: Editor
|
||||
EditorTextView(
|
||||
text: $text,
|
||||
scrollOffset: $scrollOffset,
|
||||
font: editorFont
|
||||
)
|
||||
.frame(minWidth: 200)
|
||||
|
||||
// Divider is automatic with HSplitView
|
||||
|
||||
// Right pane: Answer column
|
||||
AnswerColumnView(
|
||||
results: results,
|
||||
scrollOffset: $scrollOffset,
|
||||
font: editorFont
|
||||
)
|
||||
.frame(minWidth: 120, idealWidth: 200)
|
||||
}
|
||||
.onChange(of: text) { _, newValue in
|
||||
scheduleEvaluation(newValue)
|
||||
}
|
||||
.onAppear {
|
||||
evaluateText(text)
|
||||
}
|
||||
}
|
||||
|
||||
/// Debounce evaluation so rapid typing doesn't cause excessive recalculation.
|
||||
private func scheduleEvaluation(_ newText: String) {
|
||||
evaluationTask?.cancel()
|
||||
evaluationTask = Task { @MainActor in
|
||||
try? await Task.sleep(for: .milliseconds(Int(evaluationDebounce * 1000)))
|
||||
guard !Task.isCancelled else { return }
|
||||
evaluateText(newText)
|
||||
}
|
||||
}
|
||||
|
||||
private func evaluateText(_ newText: String) {
|
||||
results = engine.evaluateSheet(newText)
|
||||
}
|
||||
}
|
||||
227
calcpad-macos/Tests/CalcPadTests/FFIModelTests.swift
Normal file
227
calcpad-macos/Tests/CalcPadTests/FFIModelTests.swift
Normal file
@@ -0,0 +1,227 @@
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable import CalcPad
|
||||
|
||||
@Suite("FFI JSON Model Decoding Tests")
|
||||
struct FFIModelTests {
|
||||
|
||||
// MARK: - Single Line Response
|
||||
|
||||
@Test("Decode number result")
|
||||
func decodeNumberResult() throws {
|
||||
let json = """
|
||||
{
|
||||
"schema_version": "1.0",
|
||||
"result": {
|
||||
"value": { "kind": "Number", "value": 42.0 },
|
||||
"metadata": {
|
||||
"span": { "start": 0, "end": 5 },
|
||||
"result_type": "Number",
|
||||
"display": "42",
|
||||
"raw_value": 42.0
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
let response = try JSONDecoder().decode(FFIResponse.self, from: Data(json.utf8))
|
||||
#expect(response.schemaVersion == "1.0")
|
||||
#expect(response.result.metadata.display == "42")
|
||||
#expect(response.result.metadata.resultType == "Number")
|
||||
#expect(response.result.metadata.rawValue == 42.0)
|
||||
if case .number(let value) = response.result.value {
|
||||
#expect(value == 42.0)
|
||||
} else {
|
||||
Issue.record("Expected .number variant")
|
||||
}
|
||||
}
|
||||
|
||||
@Test("Decode unit value result")
|
||||
func decodeUnitValueResult() throws {
|
||||
let json = """
|
||||
{
|
||||
"schema_version": "1.0",
|
||||
"result": {
|
||||
"value": { "kind": "UnitValue", "value": 5.0, "unit": "kg" },
|
||||
"metadata": {
|
||||
"span": { "start": 0, "end": 4 },
|
||||
"result_type": "UnitValue",
|
||||
"display": "5 kg",
|
||||
"raw_value": 5.0
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
let response = try JSONDecoder().decode(FFIResponse.self, from: Data(json.utf8))
|
||||
if case .unitValue(let value, let unit) = response.result.value {
|
||||
#expect(value == 5.0)
|
||||
#expect(unit == "kg")
|
||||
} else {
|
||||
Issue.record("Expected .unitValue variant")
|
||||
}
|
||||
}
|
||||
|
||||
@Test("Decode currency value result")
|
||||
func decodeCurrencyValueResult() throws {
|
||||
let json = """
|
||||
{
|
||||
"schema_version": "1.0",
|
||||
"result": {
|
||||
"value": { "kind": "CurrencyValue", "amount": 19.99, "currency": "USD" },
|
||||
"metadata": {
|
||||
"span": { "start": 0, "end": 6 },
|
||||
"result_type": "CurrencyValue",
|
||||
"display": "$19.99",
|
||||
"raw_value": 19.99
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
let response = try JSONDecoder().decode(FFIResponse.self, from: Data(json.utf8))
|
||||
if case .currencyValue(let amount, let currency) = response.result.value {
|
||||
#expect(amount == 19.99)
|
||||
#expect(currency == "USD")
|
||||
} else {
|
||||
Issue.record("Expected .currencyValue variant")
|
||||
}
|
||||
}
|
||||
|
||||
@Test("Decode datetime result")
|
||||
func decodeDateTimeResult() throws {
|
||||
let json = """
|
||||
{
|
||||
"schema_version": "1.0",
|
||||
"result": {
|
||||
"value": { "kind": "DateTime", "date": "2024-03-17" },
|
||||
"metadata": {
|
||||
"span": { "start": 0, "end": 10 },
|
||||
"result_type": "DateTime",
|
||||
"display": "2024-03-17",
|
||||
"raw_value": null
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
let response = try JSONDecoder().decode(FFIResponse.self, from: Data(json.utf8))
|
||||
if case .dateTime(let date) = response.result.value {
|
||||
#expect(date == "2024-03-17")
|
||||
} else {
|
||||
Issue.record("Expected .dateTime variant")
|
||||
}
|
||||
#expect(response.result.metadata.rawValue == nil)
|
||||
}
|
||||
|
||||
@Test("Decode time delta result")
|
||||
func decodeTimeDeltaResult() throws {
|
||||
let json = """
|
||||
{
|
||||
"schema_version": "1.0",
|
||||
"result": {
|
||||
"value": { "kind": "TimeDelta", "days": 30, "description": "30 days" },
|
||||
"metadata": {
|
||||
"span": { "start": 0, "end": 8 },
|
||||
"result_type": "TimeDelta",
|
||||
"display": "30 days",
|
||||
"raw_value": 30.0
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
let response = try JSONDecoder().decode(FFIResponse.self, from: Data(json.utf8))
|
||||
if case .timeDelta(let days, let desc) = response.result.value {
|
||||
#expect(days == 30)
|
||||
#expect(desc == "30 days")
|
||||
} else {
|
||||
Issue.record("Expected .timeDelta variant")
|
||||
}
|
||||
}
|
||||
|
||||
@Test("Decode boolean result")
|
||||
func decodeBooleanResult() throws {
|
||||
let json = """
|
||||
{
|
||||
"schema_version": "1.0",
|
||||
"result": {
|
||||
"value": { "kind": "Boolean", "value": true },
|
||||
"metadata": {
|
||||
"span": { "start": 0, "end": 5 },
|
||||
"result_type": "Boolean",
|
||||
"display": "true",
|
||||
"raw_value": null
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
let response = try JSONDecoder().decode(FFIResponse.self, from: Data(json.utf8))
|
||||
if case .boolean(let value) = response.result.value {
|
||||
#expect(value == true)
|
||||
} else {
|
||||
Issue.record("Expected .boolean variant")
|
||||
}
|
||||
}
|
||||
|
||||
@Test("Decode error result")
|
||||
func decodeErrorResult() throws {
|
||||
let json = """
|
||||
{
|
||||
"schema_version": "1.0",
|
||||
"result": {
|
||||
"value": { "kind": "Error", "message": "unexpected token", "span": { "start": 0, "end": 3 } },
|
||||
"metadata": {
|
||||
"span": { "start": 0, "end": 3 },
|
||||
"result_type": "Error",
|
||||
"display": "Error: unexpected token",
|
||||
"raw_value": null
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
let response = try JSONDecoder().decode(FFIResponse.self, from: Data(json.utf8))
|
||||
#expect(response.result.value.isError)
|
||||
if case .error(let message) = response.result.value {
|
||||
#expect(message == "unexpected token")
|
||||
} else {
|
||||
Issue.record("Expected .error variant")
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Sheet Response
|
||||
|
||||
@Test("Decode sheet response with multiple results")
|
||||
func decodeSheetResponse() throws {
|
||||
let json = """
|
||||
{
|
||||
"schema_version": "1.0",
|
||||
"results": [
|
||||
{
|
||||
"value": { "kind": "Number", "value": 4.0 },
|
||||
"metadata": { "span": { "start": 0, "end": 5 }, "result_type": "Number", "display": "4", "raw_value": 4.0 }
|
||||
},
|
||||
{
|
||||
"value": { "kind": "Error", "message": "empty expression", "span": { "start": 0, "end": 0 } },
|
||||
"metadata": { "span": { "start": 0, "end": 0 }, "result_type": "Error", "display": "Error: empty expression", "raw_value": null }
|
||||
},
|
||||
{
|
||||
"value": { "kind": "Number", "value": 30.0 },
|
||||
"metadata": { "span": { "start": 0, "end": 6 }, "result_type": "Number", "display": "30", "raw_value": 30.0 }
|
||||
}
|
||||
]
|
||||
}
|
||||
"""
|
||||
let response = try JSONDecoder().decode(FFISheetResponse.self, from: Data(json.utf8))
|
||||
#expect(response.results.count == 3)
|
||||
#expect(response.results[0].metadata.display == "4")
|
||||
#expect(response.results[1].value.isError)
|
||||
#expect(response.results[2].metadata.display == "30")
|
||||
}
|
||||
|
||||
// MARK: - FFICalcValue.isError
|
||||
|
||||
@Test("isError returns false for non-error values")
|
||||
func isErrorFalseForNonError() throws {
|
||||
let json = """
|
||||
{ "kind": "Number", "value": 42.0 }
|
||||
"""
|
||||
let value = try JSONDecoder().decode(FFICalcValue.self, from: Data(json.utf8))
|
||||
#expect(value.isError == false)
|
||||
}
|
||||
}
|
||||
64
calcpad-macos/Tests/CalcPadTests/PerformanceTests.swift
Normal file
64
calcpad-macos/Tests/CalcPadTests/PerformanceTests.swift
Normal file
@@ -0,0 +1,64 @@
|
||||
import Testing
|
||||
@testable import CalcPad
|
||||
|
||||
@Suite("Performance Tests")
|
||||
struct PerformanceTests {
|
||||
let engine = StubCalculationEngine()
|
||||
|
||||
@Test("Evaluate 1000+ line sheet completes in under 1 second")
|
||||
func largeSheetEvaluation() async throws {
|
||||
// Generate a 1500-line document mixing expressions, blanks, and comments
|
||||
var lines: [String] = []
|
||||
for i in 1...1500 {
|
||||
switch i % 5 {
|
||||
case 0: lines.append("") // blank
|
||||
case 1: lines.append("// Line \(i)") // comment
|
||||
case 2: lines.append("\(i) + \(i * 2)")
|
||||
case 3: lines.append("\(i) * 3")
|
||||
case 4: lines.append("\(i) / 7")
|
||||
default: lines.append("\(i)")
|
||||
}
|
||||
}
|
||||
let text = lines.joined(separator: "\n")
|
||||
|
||||
let start = ContinuousClock.now
|
||||
let results = engine.evaluateSheet(text)
|
||||
let elapsed = ContinuousClock.now - start
|
||||
|
||||
#expect(results.count == 1500)
|
||||
#expect(elapsed < .seconds(1), "Sheet evaluation took \(elapsed), expected < 1 second")
|
||||
}
|
||||
|
||||
@Test("Evaluate 5000 line sheet completes in under 3 seconds")
|
||||
func veryLargeSheetEvaluation() async throws {
|
||||
var lines: [String] = []
|
||||
for i in 1...5000 {
|
||||
lines.append("\(i) + \(i)")
|
||||
}
|
||||
let text = lines.joined(separator: "\n")
|
||||
|
||||
let start = ContinuousClock.now
|
||||
let results = engine.evaluateSheet(text)
|
||||
let elapsed = ContinuousClock.now - start
|
||||
|
||||
#expect(results.count == 5000)
|
||||
#expect(elapsed < .seconds(3), "Sheet evaluation took \(elapsed), expected < 3 seconds")
|
||||
}
|
||||
|
||||
@Test("LineResult array construction is efficient for large documents")
|
||||
func lineResultConstruction() {
|
||||
// Verify we get correct line numbering for large documents
|
||||
let lines = (1...1000).map { "\($0) + 1" }
|
||||
let text = lines.joined(separator: "\n")
|
||||
let results = engine.evaluateSheet(text)
|
||||
|
||||
#expect(results.count == 1000)
|
||||
#expect(results.first?.lineNumber == 1)
|
||||
#expect(results.last?.lineNumber == 1000)
|
||||
|
||||
// Spot-check results
|
||||
#expect(results[0].result == "2") // 1 + 1
|
||||
#expect(results[99].result == "101") // 100 + 1
|
||||
#expect(results[999].result == "1001") // 1000 + 1
|
||||
}
|
||||
}
|
||||
164
calcpad-macos/Tests/CalcPadTests/RustEngineTests.swift
Normal file
164
calcpad-macos/Tests/CalcPadTests/RustEngineTests.swift
Normal file
@@ -0,0 +1,164 @@
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable import CalcPad
|
||||
|
||||
@Suite("RustCalculationEngine Tests")
|
||||
struct RustEngineTests {
|
||||
let engine = RustCalculationEngine()
|
||||
|
||||
// MARK: - AC: evaluateLine returns LineResult (not raw pointer)
|
||||
|
||||
@Test("evaluateLine 2+2 returns LineResult with result 4")
|
||||
func evalLineBasicArithmetic() {
|
||||
let result = engine.evaluateLine("2 + 2")
|
||||
#expect(result.result == "4")
|
||||
#expect(result.isError == false)
|
||||
#expect(result.lineNumber == 1)
|
||||
}
|
||||
|
||||
@Test("evaluateLine multiplication")
|
||||
func evalLineMultiplication() {
|
||||
let result = engine.evaluateLine("6 * 7")
|
||||
#expect(result.result == "42")
|
||||
#expect(result.isError == false)
|
||||
}
|
||||
|
||||
@Test("evaluateLine subtraction")
|
||||
func evalLineSubtraction() {
|
||||
let result = engine.evaluateLine("10 - 4")
|
||||
#expect(result.result == "6")
|
||||
#expect(result.isError == false)
|
||||
}
|
||||
|
||||
@Test("evaluateLine division")
|
||||
func evalLineDivision() {
|
||||
let result = engine.evaluateLine("15 / 3")
|
||||
#expect(result.result == "5")
|
||||
#expect(result.isError == false)
|
||||
}
|
||||
|
||||
@Test("evaluateLine decimal result")
|
||||
func evalLineDecimal() {
|
||||
let result = engine.evaluateLine("7 / 2")
|
||||
#expect(result.result == "3.5")
|
||||
#expect(result.isError == false)
|
||||
}
|
||||
|
||||
// MARK: - AC: evaluateSheet returns array of LineResult, one per line
|
||||
|
||||
@Test("evaluateSheet returns one result per line")
|
||||
func evalSheetMultiLine() {
|
||||
let text = "2 + 2\n\n10 * 3"
|
||||
let results = engine.evaluateSheet(text)
|
||||
#expect(results.count == 3)
|
||||
#expect(results[0].lineNumber == 1)
|
||||
#expect(results[0].result == "4")
|
||||
#expect(results[1].lineNumber == 2)
|
||||
#expect(results[1].result == nil) // blank line
|
||||
#expect(results[2].lineNumber == 3)
|
||||
#expect(results[2].result == "30")
|
||||
}
|
||||
|
||||
@Test("evaluateSheet with comments and blanks")
|
||||
func evalSheetMixed() {
|
||||
let text = "// Title\n5 + 5\n\n// Section\n3 * 3"
|
||||
let results = engine.evaluateSheet(text)
|
||||
#expect(results.count == 5)
|
||||
#expect(results[0].result == nil) // comment
|
||||
#expect(results[0].isError == false)
|
||||
#expect(results[1].result == "10")
|
||||
#expect(results[2].result == nil) // blank
|
||||
#expect(results[3].result == nil) // comment
|
||||
#expect(results[4].result == "9")
|
||||
}
|
||||
|
||||
// MARK: - AC: Error handling — errors become LineResult, no crash
|
||||
|
||||
@Test("Malformed expression returns error LineResult, not crash")
|
||||
func evalLineError() {
|
||||
let result = engine.evaluateLine("2 + + 3")
|
||||
#expect(result.isError == true)
|
||||
#expect(result.result != nil) // has error message
|
||||
#expect(result.result?.starts(with: "Error") == true)
|
||||
}
|
||||
|
||||
@Test("Completely invalid input returns error, not crash")
|
||||
func evalLineInvalidInput() {
|
||||
let result = engine.evaluateLine("@#$%^&")
|
||||
// Should not crash — either error or some result
|
||||
#expect(result.id == 1)
|
||||
}
|
||||
|
||||
// MARK: - AC: Blank and comment lines
|
||||
|
||||
@Test("Blank line returns nil result")
|
||||
func blankLine() {
|
||||
let result = engine.evaluateLine("")
|
||||
#expect(result.result == nil)
|
||||
#expect(result.isError == false)
|
||||
}
|
||||
|
||||
@Test("Comment with // returns nil result")
|
||||
func commentLine() {
|
||||
let result = engine.evaluateLine("// this is a comment")
|
||||
#expect(result.result == nil)
|
||||
#expect(result.isError == false)
|
||||
}
|
||||
|
||||
@Test("# is not a comment in Rust engine — treated as identifier lookup")
|
||||
func hashNotComment() {
|
||||
let result = engine.evaluateLine("# header")
|
||||
// The Rust engine does not treat # as a comment. The # is skipped and
|
||||
// "header" is parsed as an identifier, resulting in an undefined variable error.
|
||||
#expect(result.isError == true)
|
||||
}
|
||||
|
||||
// MARK: - AC: Memory safety — no leaks with repeated calls
|
||||
|
||||
@Test("Repeated evaluateLine calls don't leak (1000 iterations)")
|
||||
func memoryStressLine() {
|
||||
for i in 0..<1000 {
|
||||
let result = engine.evaluateLine("\(i) + 1")
|
||||
#expect(result.isError == false)
|
||||
}
|
||||
}
|
||||
|
||||
@Test("Repeated evaluateSheet calls don't leak (100 iterations)")
|
||||
func memoryStressSheet() {
|
||||
let text = (1...10).map { "\($0) * 2" }.joined(separator: "\n")
|
||||
for _ in 0..<100 {
|
||||
let results = engine.evaluateSheet(text)
|
||||
#expect(results.count == 10)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - AC: Thread safety — concurrent calls
|
||||
|
||||
@Test("Concurrent evaluateLine calls from multiple threads")
|
||||
func threadSafety() async {
|
||||
await withTaskGroup(of: LineResult.self) { group in
|
||||
for i in 0..<50 {
|
||||
group.addTask {
|
||||
engine.evaluateLine("\(i) + 1")
|
||||
}
|
||||
}
|
||||
var count = 0
|
||||
for await result in group {
|
||||
#expect(result.isError == false)
|
||||
count += 1
|
||||
}
|
||||
#expect(count == 50)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - AC: Variables shared across sheet lines
|
||||
|
||||
@Test("evaluateSheet shares variables across lines")
|
||||
func evalSheetVariables() {
|
||||
let text = "x = 10\nx * 2"
|
||||
let results = engine.evaluateSheet(text)
|
||||
#expect(results.count == 2)
|
||||
#expect(results[0].result == "10")
|
||||
#expect(results[1].result == "20")
|
||||
}
|
||||
}
|
||||
115
calcpad-macos/Tests/CalcPadTests/StubEngineTests.swift
Normal file
115
calcpad-macos/Tests/CalcPadTests/StubEngineTests.swift
Normal file
@@ -0,0 +1,115 @@
|
||||
import Testing
|
||||
@testable import CalcPad
|
||||
|
||||
@Suite("StubCalculationEngine Tests")
|
||||
struct StubEngineTests {
|
||||
let engine = StubCalculationEngine()
|
||||
|
||||
@Test("Blank lines produce nil result")
|
||||
func blankLine() {
|
||||
let result = engine.evaluateLine("")
|
||||
#expect(result.result == nil)
|
||||
#expect(result.isError == false)
|
||||
#expect(result.isEmpty == true)
|
||||
}
|
||||
|
||||
@Test("Whitespace-only lines produce nil result")
|
||||
func whitespaceOnly() {
|
||||
let result = engine.evaluateLine(" ")
|
||||
#expect(result.result == nil)
|
||||
#expect(result.isError == false)
|
||||
}
|
||||
|
||||
@Test("Comment lines starting with // produce nil result")
|
||||
func doubleSlashComment() {
|
||||
let result = engine.evaluateLine("// this is a comment")
|
||||
#expect(result.result == nil)
|
||||
#expect(result.isError == false)
|
||||
}
|
||||
|
||||
@Test("Comment lines starting with # produce nil result")
|
||||
func hashComment() {
|
||||
let result = engine.evaluateLine("# header")
|
||||
#expect(result.result == nil)
|
||||
#expect(result.isError == false)
|
||||
}
|
||||
|
||||
@Test("Simple addition")
|
||||
func addition() {
|
||||
let result = engine.evaluateLine("2 + 3")
|
||||
#expect(result.result == "5")
|
||||
#expect(result.isError == false)
|
||||
}
|
||||
|
||||
@Test("Simple subtraction")
|
||||
func subtraction() {
|
||||
let result = engine.evaluateLine("10 - 4")
|
||||
#expect(result.result == "6")
|
||||
#expect(result.isError == false)
|
||||
}
|
||||
|
||||
@Test("Simple multiplication")
|
||||
func multiplication() {
|
||||
let result = engine.evaluateLine("6 * 7")
|
||||
#expect(result.result == "42")
|
||||
#expect(result.isError == false)
|
||||
}
|
||||
|
||||
@Test("Simple division")
|
||||
func division() {
|
||||
let result = engine.evaluateLine("15 / 3")
|
||||
#expect(result.result == "5")
|
||||
#expect(result.isError == false)
|
||||
}
|
||||
|
||||
@Test("Decimal result")
|
||||
func decimalResult() {
|
||||
let result = engine.evaluateLine("7 / 2")
|
||||
#expect(result.result == "3.5")
|
||||
#expect(result.isError == false)
|
||||
}
|
||||
|
||||
@Test("evaluateSheet returns one result per line")
|
||||
func evaluateSheet() {
|
||||
let text = "2 + 2\n\n10 * 3"
|
||||
let results = engine.evaluateSheet(text)
|
||||
#expect(results.count == 3)
|
||||
#expect(results[0].lineNumber == 1)
|
||||
#expect(results[0].result == "4")
|
||||
#expect(results[1].lineNumber == 2)
|
||||
#expect(results[1].result == nil)
|
||||
#expect(results[2].lineNumber == 3)
|
||||
#expect(results[2].result == "30")
|
||||
}
|
||||
|
||||
@Test("evaluateSheet with comments and blanks")
|
||||
func evaluateSheetMixed() {
|
||||
let text = "// Title\n5 + 5\n\n# Section\n3 * 3"
|
||||
let results = engine.evaluateSheet(text)
|
||||
#expect(results.count == 5)
|
||||
#expect(results[0].result == nil) // comment
|
||||
#expect(results[1].result == "10")
|
||||
#expect(results[2].result == nil) // blank
|
||||
#expect(results[3].result == nil) // comment
|
||||
#expect(results[4].result == "9")
|
||||
}
|
||||
|
||||
@Test("LineResult id equals line number")
|
||||
func lineResultId() {
|
||||
let result = LineResult(id: 5, expression: "2+2", result: "4", isError: false)
|
||||
#expect(result.lineNumber == 5)
|
||||
#expect(result.id == 5)
|
||||
}
|
||||
|
||||
@Test("LineResult isEmpty for nil results")
|
||||
func lineResultIsEmpty() {
|
||||
let empty = LineResult(id: 1, expression: "", result: nil, isError: false)
|
||||
#expect(empty.isEmpty == true)
|
||||
|
||||
let error = LineResult(id: 1, expression: "bad", result: nil, isError: true)
|
||||
#expect(error.isEmpty == false)
|
||||
|
||||
let hasResult = LineResult(id: 1, expression: "2+2", result: "4", isError: false)
|
||||
#expect(hasResult.isEmpty == false)
|
||||
}
|
||||
}
|
||||
37
calcpad-macos/build-rust.sh
Normal file
37
calcpad-macos/build-rust.sh
Normal file
@@ -0,0 +1,37 @@
|
||||
#!/bin/bash
|
||||
# Build the Rust CalcPad engine for macOS.
|
||||
#
|
||||
# Produces a static library that the Swift Package Manager links against.
|
||||
#
|
||||
# Usage:
|
||||
# ./build-rust.sh # Build release (default)
|
||||
# ./build-rust.sh debug # Build debug
|
||||
#
|
||||
# Prerequisites:
|
||||
# - Rust toolchain with targets: aarch64-apple-darwin, x86_64-apple-darwin
|
||||
# - Install targets: rustup target add aarch64-apple-darwin x86_64-apple-darwin
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
PROFILE="${1:-release}"
|
||||
ENGINE_DIR="$(cd "$(dirname "$0")/../calcpad-engine" && pwd)"
|
||||
OUTPUT_DIR="$ENGINE_DIR/target/$PROFILE"
|
||||
|
||||
if [ "$PROFILE" = "debug" ]; then
|
||||
CARGO_FLAG=""
|
||||
else
|
||||
CARGO_FLAG="--release"
|
||||
fi
|
||||
|
||||
echo "Building calcpad-engine ($PROFILE)..."
|
||||
|
||||
# Build for the current architecture
|
||||
cd "$ENGINE_DIR"
|
||||
cargo build $CARGO_FLAG
|
||||
|
||||
echo ""
|
||||
echo "Build complete!"
|
||||
echo "Static library: $OUTPUT_DIR/libcalcpad_engine.a"
|
||||
echo ""
|
||||
echo "To build and test the Swift package:"
|
||||
echo " cd calcpad-macos && swift test"
|
||||
Reference in New Issue
Block a user