import Foundation import Testing import FoldFormat import FoldKitTestSupport @Test func decodesUnitSquareWithFourBoundaryEdges() throws { let f = try FoldFile(contentsOf: fixture("square.fold")) #expect(f.frames[0].verticesCoords.count == 4) #expect(f.frames[0].edgesAssignment.allSatisfy { $0 == .boundary }) #expect(f.stepCount == 0) } @Test func topologyLivesOnTheFileNotTheFrame() throws { let f = try FoldFile(contentsOf: fixture("square.fold")) #expect(f.edgesVertices.count == 4) #expect(f.facesVertices.count == 1) } @Test func aFrameCarryingTopologyIsRejected() { #expect(throws: FoldError.topologyInFrame(frame: 1)) { try FoldFile(contentsOf: fixture("topology-in-frame.fold")) } } @Test func decodesTwoElementCoordsAsZeroZ() throws { #expect(try FoldFile(contentsOf: fixture("square.fold")).frames[0].verticesCoords .allSatisfy { $0.z == 0 }) } @Test func frameNIsTheResultOfStepN() throws { let f = try FoldFile(contentsOf: fixture("half.fold")) #expect(f.stepCount == 3) #expect(f.frames[0].step == nil) #expect(f.frames[1].step?.kind == .fold) #expect(f.frames[2].step?.kind == .turnOver) #expect(f.frames[3].step?.rotateDegrees == 90) } @Test func decodesTheStepRecordIncludingItsInstructionAndViewpoint() throws { let step = try FoldFile(contentsOf: fixture("half.fold")).frames[1].step #expect(step?.axes.count == 1) #expect(step?.technique == .plain) #expect(step?.instruction?.isEmpty == false) #expect(step?.viewpoint == Viewpoint(eye: SIMD3(0, 0, 1), up: SIMD3(0, 1, 0))) } @Test func decodesAnAxisTreeWithWindowsAndACurve() throws { let json = #"{"edge": 3, "movingFaces": [2], "startAngle": 0, "endAngle": 180, "parent": 0,"# + #" "window": [0.25, 1], "angles": [0, 90, 180]}"# let axis = try JSONDecoder().decode(AxisRecord.self, from: Data(json.utf8)) #expect(axis.parent == 0) #expect(axis.window == [0.25, 1]) #expect(axis.angles == [0, 90, 180]) } @Test func decodesAPopRecord() throws { let json = #"{"spine": [0, 1], "openAngle": 30, "phases": [0.3333333333333333, 0.6666666666666666],"# + #" "openAxes": [], "popped": [[0, 0, 0], [1, 0, 0]], "closeAxes": []}"# let pop = try JSONDecoder().decode(PopRecord.self, from: Data(json.utf8)) #expect(pop.openAngle == 30) #expect(pop.popped.count == 2) } @Test func windowsDecodeOnAStaggeredStep() throws { let step = try FoldFile(contentsOf: fixture("letter-staggered.fold")).frames[1].step #expect(step?.axes.map(\.window) == [[0, 0.5], [0.5, 1]]) #expect(step?.nonRigid == false) } @Test func decodesFirstCreasedAtStepWithNullForBoundaries() throws { let f = try FoldFile(contentsOf: fixture("half.fold")) #expect(f.edgesFirstCreasedAtStep == [nil, nil, nil, nil, nil, nil, 1]) } @Test func roundTripsThroughEncodingUnchanged() throws { let f = try FoldFile(contentsOf: fixture("half.fold")) let again = try JSONDecoder().decode(FoldFile.self, from: JSONEncoder().encode(f)) #expect(again.edgesVertices == f.edgesVertices) #expect(again.frames.map(\.verticesCoords) == f.frames.map(\.verticesCoords)) #expect(again.frames.map(\.step) == f.frames.map(\.step)) } @Test func encodedFramesInheritFromTheKeyFrame() throws { let f = try FoldFile(contentsOf: fixture("half.fold")) let json = try JSONSerialization.jsonObject(with: JSONEncoder().encode(f)) as! [String: Any] let frames = json["file_frames"] as! [[String: Any]] #expect(frames.allSatisfy { $0["frame_parent"] as? Int == 0 && $0["frame_inherit"] as? Bool == true }) #expect(frames.allSatisfy { $0["edges_vertices"] == nil }) } @Test(arguments: ["C", "J"]) func rejectsCutAndJoinAssignments(_ code: String) { #expect(throws: FoldError.unsupportedAssignment(code)) { try JSONDecoder().decode(EdgeAssignment.self, from: Data("\"\(code)\"".utf8)) } } /// half.fold with `change` applied to its JSON object. private func decodeHalf(_ change: (inout [String: Any]) -> Void) throws -> FoldFile { var json = try JSONSerialization.jsonObject(with: Data(contentsOf: fixture("half.fold"))) as! [String: Any] change(&json) return try JSONDecoder().decode(FoldFile.self, from: JSONSerialization.data(withJSONObject: json)) } @Test func rejectsAnEdgeArrayOfTheWrongLength() { #expect(throws: FoldError.edgeArrayLengthMismatch(frame: 0, key: "edges_krz:firstCreasedAtStep")) { try decodeHalf { $0["edges_krz:firstCreasedAtStep"] = [NSNull(), 1] } } #expect(throws: FoldError.edgeArrayLengthMismatch(frame: 0, key: "edges_assignment")) { try decodeHalf { $0["edges_assignment"] = ["B", "B", "B", "B", "B", "B"] } } #expect(throws: FoldError.edgeArrayLengthMismatch(frame: 2, key: "edges_foldAngle")) { try decodeHalf { json in var frames = json["file_frames"] as! [[String: Any]] frames[1]["edges_foldAngle"] = [0, 0, 0, 0, 0, 0, 180, 0] json["file_frames"] = frames } } } @Test(arguments: [[0, 1], [0, 2, 1], [-1, 1, 1], [0, 1, 2], [0, 1, 1, 0]]) func rejectsAMalformedFaceOrder(_ triple: [Int]) { #expect(throws: FoldError.malformedFaceOrder(frame: 1, key: "faceOrders", entry: 1)) { try decodeHalf { json in var frames = json["file_frames"] as! [[String: Any]] frames[0]["faceOrders"] = [[0, 1, 1], triple] json["file_frames"] = frames } } #expect(throws: FoldError.malformedFaceOrder(frame: 1, key: "krz:faceOrders_midFold", entry: 0)) { try decodeHalf { json in var frames = json["file_frames"] as! [[String: Any]] frames[0]["krz:faceOrders_midFold"] = [triple] json["file_frames"] = frames } } }