test(editor): validate editor state foundation
This commit is contained in:
parent
1e15778364
commit
9161b42d9d
2 changed files with 63 additions and 0 deletions
|
|
@ -62,6 +62,10 @@ let package = Package(
|
||||||
.testTarget(
|
.testTarget(
|
||||||
name: "SaplingCoreTests",
|
name: "SaplingCoreTests",
|
||||||
dependencies: ["SaplingCore"]
|
dependencies: ["SaplingCore"]
|
||||||
|
),
|
||||||
|
.testTarget(
|
||||||
|
name: "SaplingEditorTests",
|
||||||
|
dependencies: ["SaplingCore", "SaplingEditor"]
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
|
||||||
59
Tests/SaplingEditorTests/EditorStateTests.swift
Normal file
59
Tests/SaplingEditorTests/EditorStateTests.swift
Normal file
|
|
@ -0,0 +1,59 @@
|
||||||
|
import XCTest
|
||||||
|
import SaplingCore
|
||||||
|
@testable import SaplingEditor
|
||||||
|
|
||||||
|
final class EditorStateTests: XCTestCase {
|
||||||
|
func testActiveLineFollowsSelection() {
|
||||||
|
let source = "# Heading\nRendered **line**\nRaw source line"
|
||||||
|
let document = EditorDocument(
|
||||||
|
url: URL(fileURLWithPath: "/tmp/EditorStateTests.md"),
|
||||||
|
title: "EditorStateTests",
|
||||||
|
source: source
|
||||||
|
)
|
||||||
|
var state = EditorState(document: document)
|
||||||
|
|
||||||
|
let secondLineLocation = (source as NSString).range(of: "Rendered").location
|
||||||
|
state.updateSelection(EditorSelection(location: secondLineLocation, length: 0))
|
||||||
|
|
||||||
|
XCTAssertEqual(state.activeLineIndex, 1)
|
||||||
|
XCTAssertEqual(state.lines[0].mode, .rendered)
|
||||||
|
XCTAssertEqual(state.lines[1].mode, .source)
|
||||||
|
XCTAssertEqual(state.lines[2].mode, .rendered)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testUpdatingSourceTracksUnsavedChanges() {
|
||||||
|
let document = EditorDocument(
|
||||||
|
url: URL(fileURLWithPath: "/tmp/EditorStateTests.md"),
|
||||||
|
title: "EditorStateTests",
|
||||||
|
source: "Initial"
|
||||||
|
)
|
||||||
|
var state = EditorState(document: document)
|
||||||
|
|
||||||
|
state.updateSource("Initial\nChanged")
|
||||||
|
|
||||||
|
XCTAssertTrue(state.hasUnsavedChanges)
|
||||||
|
XCTAssertEqual(state.lines.count, 2)
|
||||||
|
|
||||||
|
state.markSaved()
|
||||||
|
|
||||||
|
XCTAssertFalse(state.hasUnsavedChanges)
|
||||||
|
}
|
||||||
|
|
||||||
|
@MainActor
|
||||||
|
func testViewModelSavesDocumentToDisk() throws {
|
||||||
|
let directory = FileManager.default.temporaryDirectory
|
||||||
|
.appendingPathComponent(UUID().uuidString, isDirectory: true)
|
||||||
|
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
|
||||||
|
let url = directory.appendingPathComponent("Note.md")
|
||||||
|
try "# Note\n".write(to: url, atomically: true, encoding: .utf8)
|
||||||
|
|
||||||
|
let document = try HybridMarkdownEditorViewModel.loadDocument(at: url)
|
||||||
|
let viewModel = HybridMarkdownEditorViewModel(document: document)
|
||||||
|
viewModel.updateSource("# Note\n\nUpdated")
|
||||||
|
try viewModel.save()
|
||||||
|
|
||||||
|
let saved = try String(contentsOf: url, encoding: .utf8)
|
||||||
|
XCTAssertEqual(saved, "# Note\n\nUpdated")
|
||||||
|
XCTAssertFalse(viewModel.hasUnsavedChanges)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue