forked from ANT0071/ShapeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImportExportTests.swift
More file actions
128 lines (119 loc) · 3.43 KB
/
ImportExportTests.swift
File metadata and controls
128 lines (119 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//
// ImportExportTests.swift
// ShapeScriptTests
//
// Created by Nick Lockwood on 19/06/2022.
// Copyright © 2022 Nick Lockwood. All rights reserved.
//
@testable import Euclid
@testable import ShapeScript
import XCTest
#if canImport(SceneKit)
import SceneKit
#endif
class ImportExportTests: XCTestCase {
// MARK: Geometry
func testCog() throws {
let source = """
define cog {
option teeth 6
path {
define step 1 / teeth
for 1 to teeth {
point -0.02 0.8
point 0.05 1
rotate step
point -0.05 1
point 0.02 0.8
rotate step
}
point -0.02 0.8
}
}
difference {
extrude {
size 1 1 0.5
cog { teeth 8 }
}
rotate 0 0 0.5
cylinder
}
"""
let program = try parse(source)
let context = EvaluationContext(source: program.source, delegate: nil)
XCTAssertNoThrow(try program.evaluate(in: context))
let geometry = try XCTUnwrap(context.children.first?.value as? Geometry)
XCTAssert(geometry.build { true })
let mesh = try XCTUnwrap(geometry.mesh)
let polygons = mesh.polygons
XCTAssertEqual(polygons.count, 80)
XCTAssert(polygons.areWatertight)
let triangles = mesh.triangulate().polygons
XCTAssertEqual(triangles.count, 256)
XCTAssert(triangles.areWatertight)
#if canImport(SceneKit)
geometry.scnBuild(with: .default)
let node = SCNNode(geometry)
let geometry2 = try Geometry(node)
XCTAssert(geometry2.build { true })
let mesh2 = try XCTUnwrap(geometry2.mesh)
XCTAssertEqual(mesh2.polygons.count, 256)
XCTAssert(mesh2.isWatertight)
#endif
}
// MARK: JSON
func testParseJSONValues() throws {
let source = """
[
"hello",
3,
3.5,
true,
null,
[1, 2, 3],
{
"foo": 2,
"bar": true,
}
]
"""
let json = try JSONSerialization
.jsonObject(with: source.data(using: .utf8)!)
let value = Value(json: json)
XCTAssertEqual(value, [
"hello",
3,
3.5,
true,
[],
[1, 2, 3],
[
"bar": true,
"foo": 2,
],
])
XCTAssertEqual(value["seventh"]?["foo"], 2)
}
func testMalformedJSON() throws {
let json = """
[
"🙃,
"foo"
]
"""
XCTAssertThrowsError(try Value(jsonData: json.data(using: .utf8)!)) { error in
let error = try? XCTUnwrap(error as? ParserError)
guard case let .custom(message, _, range)? = error?.type else {
XCTFail()
return
}
if let range = range {
XCTAssertEqual(message, "Unescaped control character")
XCTAssertEqual(range.lowerBound, json.range(of: "🙃,")?.upperBound)
} else {
XCTAssert(message.hasPrefix("Unescaped control character") ||
message.hasPrefix("Badly formed array"))
}
}
}
}