Skip to content

Commit 74ec7ac

Browse files
Merge pull request #16 from code4teaching/inspector-helpers
Move inspector hints and Fallauswahl path helpers out of ContentView
2 parents 9bf2f73 + 69d6259 commit 74ec7ac

8 files changed

Lines changed: 1208 additions & 1108 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Foundation
2+
3+
extension VisuStructDocumentEditing {
4+
/// Innerste **Fallauswahl** (`multiway`), die `path` enthält (oder selbst ist) — für „Fall hinzufügen/entfernen“.
5+
public static func multiwayAncestorPath(for path: [Int]?, in doc: VisuStructDocument) -> [Int]? {
6+
guard let path, !path.isEmpty else { return nil }
7+
for len in stride(from: path.count, through: 1, by: -1) {
8+
let prefix = Array(path.prefix(len))
9+
if let el = try? element(at: prefix, in: doc),
10+
el.type == .fallauswahl {
11+
return prefix
12+
}
13+
}
14+
return nil
15+
}
16+
17+
/// Spaltenindex der Fallauswahl, wenn die Auswahl **in** einer Spalte liegt (`[…, fall, spalte, …]`).
18+
public static func caseIndex(of selectedPath: [Int]?, inMultiwayAt multiwayPath: [Int]) -> Int? {
19+
guard let sp = selectedPath, sp.count > multiwayPath.count else { return nil }
20+
return sp[multiwayPath.count]
21+
}
22+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import XCTest
2+
import VisuStructCore
3+
4+
final class MultiwayPathTests: XCTestCase {
5+
/// `[0]` Anweisung, `[1]` Fallauswahl mit zwei Fällen; im zweiten Fall eine innere Fallauswahl `[1, 1, 0]`.
6+
private func document() -> VisuStructDocument {
7+
let statement = VisuStructElement(type: .anweisung, textLines: ["x = 1"])
8+
let inner = VisuStructElement(
9+
type: .fallauswahl,
10+
textLines: ["y"],
11+
cases: [
12+
VisuStructCase(name: "1", elements: [statement]),
13+
VisuStructCase(name: "default", elements: [statement]),
14+
]
15+
)
16+
let outer = VisuStructElement(
17+
type: .fallauswahl,
18+
textLines: ["x"],
19+
cases: [
20+
VisuStructCase(name: "1", elements: [statement]),
21+
VisuStructCase(name: "default", elements: [inner]),
22+
]
23+
)
24+
var doc = VisuStructDocumentEditing.newEmpty()
25+
doc.elements = [statement, outer]
26+
return doc
27+
}
28+
29+
func testAncestorIsInnermostFallauswahl() {
30+
let doc = document()
31+
XCTAssertNil(VisuStructDocumentEditing.multiwayAncestorPath(for: nil, in: doc))
32+
XCTAssertNil(VisuStructDocumentEditing.multiwayAncestorPath(for: [0], in: doc))
33+
XCTAssertEqual(VisuStructDocumentEditing.multiwayAncestorPath(for: [1], in: doc), [1])
34+
XCTAssertEqual(VisuStructDocumentEditing.multiwayAncestorPath(for: [1, 0, 0], in: doc), [1])
35+
XCTAssertEqual(VisuStructDocumentEditing.multiwayAncestorPath(for: [1, 1, 0], in: doc), [1, 1, 0])
36+
XCTAssertEqual(VisuStructDocumentEditing.multiwayAncestorPath(for: [1, 1, 0, 1, 0], in: doc), [1, 1, 0])
37+
}
38+
39+
func testCaseIndex() {
40+
XCTAssertNil(VisuStructDocumentEditing.caseIndex(of: [1], inMultiwayAt: [1]))
41+
XCTAssertNil(VisuStructDocumentEditing.caseIndex(of: nil, inMultiwayAt: [1]))
42+
XCTAssertEqual(VisuStructDocumentEditing.caseIndex(of: [1, 1, 0], inMultiwayAt: [1]), 1)
43+
}
44+
}

0 commit comments

Comments
 (0)