Packages/FoldKit/Tests/FoldFormatTests/DecodingTests.swift

137 lines · 5801 bytes

16 symbols in this file
  1import Foundation
  2import Testing
  3import FoldFormat
  4import FoldKitTestSupport
  5
  6@Test func decodesUnitSquareWithFourBoundaryEdges() throws {
  7    let f = try FoldFile(contentsOf: fixture("square.fold"))
  8    #expect(f.frames[0].verticesCoords.count == 4)
  9    #expect(f.frames[0].edgesAssignment.allSatisfy { $0 == .boundary })
 10    #expect(f.stepCount == 0)
 11}
 12
 13@Test func topologyLivesOnTheFileNotTheFrame() throws {
 14    let f = try FoldFile(contentsOf: fixture("square.fold"))
 15    #expect(f.edgesVertices.count == 4)
 16    #expect(f.facesVertices.count == 1)
 17}
 18
 19@Test func aFrameCarryingTopologyIsRejected() {
 20    #expect(throws: FoldError.topologyInFrame(frame: 1)) {
 21        try FoldFile(contentsOf: fixture("topology-in-frame.fold"))
 22    }
 23}
 24
 25@Test func decodesTwoElementCoordsAsZeroZ() throws {
 26    #expect(try FoldFile(contentsOf: fixture("square.fold")).frames[0].verticesCoords
 27        .allSatisfy { $0.z == 0 })
 28}
 29
 30@Test func frameNIsTheResultOfStepN() throws {
 31    let f = try FoldFile(contentsOf: fixture("half.fold"))
 32    #expect(f.stepCount == 3)
 33    #expect(f.frames[0].step == nil)
 34    #expect(f.frames[1].step?.kind == .fold)
 35    #expect(f.frames[2].step?.kind == .turnOver)
 36    #expect(f.frames[3].step?.rotateDegrees == 90)
 37}
 38
 39@Test func decodesTheStepRecordIncludingItsInstructionAndViewpoint() throws {
 40    let step = try FoldFile(contentsOf: fixture("half.fold")).frames[1].step
 41    #expect(step?.axes.count == 1)
 42    #expect(step?.technique == .plain)
 43    #expect(step?.instruction?.isEmpty == false)
 44    #expect(step?.viewpoint == Viewpoint(eye: SIMD3(0, 0, 1), up: SIMD3(0, 1, 0)))
 45}
 46
 47@Test func decodesAnAxisTreeWithWindowsAndACurve() throws {
 48    let json = #"{"edge": 3, "movingFaces": [2], "startAngle": 0, "endAngle": 180, "parent": 0,"# +
 49               #" "window": [0.25, 1], "angles": [0, 90, 180]}"#
 50    let axis = try JSONDecoder().decode(AxisRecord.self, from: Data(json.utf8))
 51    #expect(axis.parent == 0)
 52    #expect(axis.window == [0.25, 1])
 53    #expect(axis.angles == [0, 90, 180])
 54}
 55
 56@Test func decodesAPopRecord() throws {
 57    let json = #"{"spine": [0, 1], "openAngle": 30, "phases": [0.3333333333333333, 0.6666666666666666],"# +
 58               #" "openAxes": [], "popped": [[0, 0, 0], [1, 0, 0]], "closeAxes": []}"#
 59    let pop = try JSONDecoder().decode(PopRecord.self, from: Data(json.utf8))
 60    #expect(pop.openAngle == 30)
 61    #expect(pop.popped.count == 2)
 62}
 63
 64@Test func windowsDecodeOnAStaggeredStep() throws {
 65    let step = try FoldFile(contentsOf: fixture("letter-staggered.fold")).frames[1].step
 66    #expect(step?.axes.map(\.window) == [[0, 0.5], [0.5, 1]])
 67    #expect(step?.nonRigid == false)
 68}
 69
 70@Test func decodesFirstCreasedAtStepWithNullForBoundaries() throws {
 71    let f = try FoldFile(contentsOf: fixture("half.fold"))
 72    #expect(f.edgesFirstCreasedAtStep == [nil, nil, nil, nil, nil, nil, 1])
 73}
 74
 75@Test func roundTripsThroughEncodingUnchanged() throws {
 76    let f = try FoldFile(contentsOf: fixture("half.fold"))
 77    let again = try JSONDecoder().decode(FoldFile.self, from: JSONEncoder().encode(f))
 78    #expect(again.edgesVertices == f.edgesVertices)
 79    #expect(again.frames.map(\.verticesCoords) == f.frames.map(\.verticesCoords))
 80    #expect(again.frames.map(\.step) == f.frames.map(\.step))
 81}
 82
 83@Test func encodedFramesInheritFromTheKeyFrame() throws {
 84    let f = try FoldFile(contentsOf: fixture("half.fold"))
 85    let json = try JSONSerialization.jsonObject(with: JSONEncoder().encode(f)) as! [String: Any]
 86    let frames = json["file_frames"] as! [[String: Any]]
 87    #expect(frames.allSatisfy { $0["frame_parent"] as? Int == 0 && $0["frame_inherit"] as? Bool == true })
 88    #expect(frames.allSatisfy { $0["edges_vertices"] == nil })
 89}
 90
 91@Test(arguments: ["C", "J"])
 92func rejectsCutAndJoinAssignments(_ code: String) {
 93    #expect(throws: FoldError.unsupportedAssignment(code)) {
 94        try JSONDecoder().decode(EdgeAssignment.self, from: Data("\"\(code)\"".utf8))
 95    }
 96}
 97
 98/// half.fold with `change` applied to its JSON object.
 99private func decodeHalf(_ change: (inout [String: Any]) -> Void) throws -> FoldFile {
100    var json = try JSONSerialization.jsonObject(with: Data(contentsOf: fixture("half.fold"))) as! [String: Any]
101    change(&json)
102    return try JSONDecoder().decode(FoldFile.self, from: JSONSerialization.data(withJSONObject: json))
103}
104
105@Test func rejectsAnEdgeArrayOfTheWrongLength() {
106    #expect(throws: FoldError.edgeArrayLengthMismatch(frame: 0, key: "edges_krz:firstCreasedAtStep")) {
107        try decodeHalf { $0["edges_krz:firstCreasedAtStep"] = [NSNull(), 1] }
108    }
109    #expect(throws: FoldError.edgeArrayLengthMismatch(frame: 0, key: "edges_assignment")) {
110        try decodeHalf { $0["edges_assignment"] = ["B", "B", "B", "B", "B", "B"] }
111    }
112    #expect(throws: FoldError.edgeArrayLengthMismatch(frame: 2, key: "edges_foldAngle")) {
113        try decodeHalf { json in
114            var frames = json["file_frames"] as! [[String: Any]]
115            frames[1]["edges_foldAngle"] = [0, 0, 0, 0, 0, 0, 180, 0]
116            json["file_frames"] = frames
117        }
118    }
119}
120
121@Test(arguments: [[0, 1], [0, 2, 1], [-1, 1, 1], [0, 1, 2], [0, 1, 1, 0]])
122func rejectsAMalformedFaceOrder(_ triple: [Int]) {
123    #expect(throws: FoldError.malformedFaceOrder(frame: 1, key: "faceOrders", entry: 1)) {
124        try decodeHalf { json in
125            var frames = json["file_frames"] as! [[String: Any]]
126            frames[0]["faceOrders"] = [[0, 1, 1], triple]
127            json["file_frames"] = frames
128        }
129    }
130    #expect(throws: FoldError.malformedFaceOrder(frame: 1, key: "krz:faceOrders_midFold", entry: 0)) {
131        try decodeHalf { json in
132            var frames = json["file_frames"] as! [[String: Any]]
133            frames[0]["krz:faceOrders_midFold"] = [triple]
134            json["file_frames"] = frames
135        }
136    }
137}