import Foundation import Testing import FoldFormat import Folding import FoldKitTestSupport @Test func stepIndicesCountFromOne() throws { let steps = try readSteps(FoldFile(contentsOf: fixture("half.fold"))) #expect(steps.map(\.index) == [1, 2, 3]) } @Test func simpleValleyFoldReadsOneAxis() throws { let step = try readSteps(FoldFile(contentsOf: fixture("half.fold")))[0] #expect(step.axes.count == 1) #expect(step.axes[0].endAngle == 180) } @Test func letterFoldReadsTwoSimultaneousAxes() throws { let step = try readSteps(FoldFile(contentsOf: fixture("letter.fold")))[0] #expect(step.axes.count == 2) } @Test(arguments: ["half.fold", "letter.fold", "letter-sequential.fold", "quarters.fold", "occlusion.fold"]) func movingFaceSetsWithinOneStepAreDisjoint(_ name: String) throws { for step in try readSteps(FoldFile(contentsOf: fixture(name))) { let union = step.axes.reduce(into: Set()) { $0.formUnion($1.movingFaces) } #expect(union.count == step.axes.reduce(0) { $0 + $1.movingFaces.count }) } } @Test func turnOverStepHasNoAxesAndAnAxisToTurnAbout() throws { let turn = try readSteps(FoldFile(contentsOf: fixture("half.fold")))[1] #expect(turn.kind == .turnOver) #expect(turn.axes.isEmpty) #expect(turn.turnOverAxis == .vertical) } @Test func rotateStepCarriesItsDegrees() throws { #expect(try readSteps(FoldFile(contentsOf: fixture("half.fold")))[2].rotateDegrees == 90) } @Test func foldAndUnfoldPeaksMidwayAndReturns() throws { let s = try readSteps(FoldFile(contentsOf: fixture("quarters.fold")))[0] #expect(s.kind == .foldAndUnfold) #expect(s.axes[0].angle(at: 0.5, kind: s.kind) == 180) #expect(s.axes[0].angle(at: 1, kind: s.kind) == 0) } @Test func axisIsDirectedAsTheMovingFaceTraversesIt() throws { // f1 = [v1, v2, v3, v4] traverses e6 = [v1, v4] from v4 to v1 let axis = try readSteps(FoldFile(contentsOf: fixture("half.fold")))[0].axes[0] #expect(axis.origin == SIMD3(1, 2, 0)) #expect(axis.direction == SIMD3(0, -1, 0)) } @Test func instructionIsReadVerbatim() throws { let f = try FoldFile(contentsOf: fixture("half.fold")) #expect(try readSteps(f)[0].instruction == f.frames[1].step?.instruction) } @Test func aWindowedAxisWaitsAndThenHolds() throws { let s = try readSteps(FoldFile(contentsOf: fixture("letter-staggered.fold")))[0] #expect(s.axes[0].angle(at: 0.25, kind: s.kind) == 90) #expect(s.axes[1].angle(at: 0.25, kind: s.kind) == 0) #expect(s.axes[0].angle(at: 0.75, kind: s.kind) == 180) #expect(s.axes[1].angle(at: 0.75, kind: s.kind) == 90) } @Test func aCurveIsInterpolatedBetweenItsSamples() throws { let axis = try readSteps(degreeFourVertex()).first!.axes.first { $0.curve != nil }! let t = 0.5 + 0.5 / 256 // halfway between two samples #expect(abs(axis.angle(at: t, kind: .fold) - degreeFourExactAngle(axis.edge, at: t)) < 1e-6) } @Test func aNonRigidStepNeedsItsPop() { #expect(throws: StepError.nonRigidWithoutPop(step: 1)) { try readSteps(FoldFile(contentsOf: fixture("nonrigid-no-pop.fold"))) } } @Test func rotateWithoutDegreesThrows() { #expect(throws: StepError.rotateWithoutDegrees(step: 3)) { try readSteps(FoldFile(contentsOf: fixture("rotate-no-degrees.fold"))) } } @Test func movingFaceOutOfRangeThrows() { #expect(throws: StepError.movingFacesOutOfRange(step: 1)) { try readSteps(FoldFile(contentsOf: fixture("bad-moving-face.fold"))) } } @Test func parentsComeBeforeTheirChildren() throws { // list the carried 0° axis first; readSteps must move its parent ahead of it var file = degreeFourVertex() let record = file.frames[1].step! let reordered = [record.axes[2], record.axes[0], record.axes[1]].map { a in AxisRecord(edge: a.edge, movingFaces: a.movingFaces, startAngle: a.startAngle, endAngle: a.endAngle, peakAngle: a.peakAngle, parent: a.parent.map { _ in 2 }, window: a.window, angles: a.angles) } file.frames[1].step = StepRecord(kind: record.kind, technique: record.technique, instruction: record.instruction, axes: reordered, viewpoint: record.viewpoint) let axes = try readSteps(file)[0].axes for (i, axis) in axes.enumerated() { if let parent = axis.parent { #expect(parent < i) } } let child = try #require(axes.first { $0.parent != nil }) #expect(axes[child.parent!].edge == record.axes[record.axes[2].parent!].edge) } @Test func theDegreeFourVertexIsAValidModel() { #expect(validate(degreeFourVertex()) == []) #expect(validate(degreeFourVertex(linear: true)) == []) } @Test func aFoldAndUnfoldThatDoesNotReturnThrows() throws { var file = try FoldFile(contentsOf: fixture("quarters.fold")) let record = file.frames[1].step! let a = record.axes[0] file.frames[1].step = StepRecord(kind: record.kind, instruction: record.instruction, axes: [AxisRecord(edge: a.edge, movingFaces: a.movingFaces, startAngle: a.startAngle, endAngle: 180, peakAngle: a.peakAngle)], viewpoint: record.viewpoint) #expect(throws: StepError.foldAndUnfoldDoesNotReturn(step: 1)) { try readSteps(file) } } @Test func aFoldAndUnfoldAxisWithoutAPeakPeaksAtItsEndAngle() { let axis = FoldAxis(edge: 0, origin: .zero, direction: SIMD3(0, 1, 0), movingFaces: [1], startAngle: 0, endAngle: 90) #expect(axis.angle(at: 0.5, kind: .foldAndUnfold) == 90) let step = FoldStep(index: 1, kind: .foldAndUnfold, instruction: "Fold and unfold.", axes: [axis], pop: Pop(spine: [], openAngle: 30, phases: 0.25...0.75, openAxes: [], popped: [], closeAxes: []), viewpoint: Viewpoint(eye: SIMD3(0, 0, 1), up: SIMD3(0, 1, 0))) #expect(step.movingFaceUnion == [1]) } @Test func aStepReadsWithoutTheFramesAfterIt() throws { var file = try FoldFile(contentsOf: fixture("quarters.fold")) let whole = try readSteps(file) file.frames.removeLast() #expect(try readStep(2, in: file) == whole[1]) }