Skip to content

Commit

Permalink
Rearrange tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasf committed Nov 19, 2024
1 parent e20e20c commit 15edb9a
Show file tree
Hide file tree
Showing 21 changed files with 90 additions and 77 deletions.
75 changes: 37 additions & 38 deletions Tests/Tests/2D.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Testing

struct Geometry2DTests {
@Test func basic2D() {
let geometry = Union {
Union {
Rectangle(Vector2D(30, 10))
.aligned(at: .centerY)
.subtracting {
Expand All @@ -16,46 +16,45 @@ struct Geometry2DTests {
}
Arc(range: 80°..<280°, radius: 3.5)
}

#expect(geometry.code == scadFile("2dbasics"))
.expectCodeEquals(file: "2d/basics")
}

@Test func misc2D() {
let geometry = Rectangle(Vector2D(30, 10))
.aligned(at: .centerY)
.subtracting {
Circle(diameter: 8)
.scaled(x: 2)
Arc(range: 20°..<160°, radius: 4)
.translated(x: 15)
Teardrop(diameter: 5)
.translated(x: 22)
Teardrop(diameter: 4, angle: 30°, style: .bridged)
.translated(x: 27)
}
.rounded(amount: 0.35, side: . both)
.adding {
Rectangle(x: 10, y: 10)
.roundingRectangleCorners(.bottomLeft, radius: 5)
.roundingRectangleCorners(.bottomRight, radius: 3)
.roundingRectangleCorners(.topRight, radius: 2)
.aligned(at: .centerX)
.rotated(45°)
.translated(x: -3)

Text("SwiftSCAD")
.usingFont("Helvetica", size: 14)
.usingTextAlignment(horizontal: .left, vertical: .bottom)
.offset(amount: 0.4, style: .miter)
.translated(y: 5)
.sheared(.y, angle: 20°)
@Test func circular() {
Union {
Circle(diameter: 8)
.scaled(x: 2)
Arc(range: 20°..<160°, radius: 4)
.translated(x: 15)
Teardrop(diameter: 5)
.translated(x: 22)
Teardrop(diameter: 4, angle: 30°, style: .bridged)
.translated(x: 27)
CylinderBridge(bottomDiameter: 10, topDiameter: 6)
.translated(x: 15)
.repeated(in: 20°..<250°, count: 5)
.translated(x: 50, y: -10)
}
.expectCodeEquals(file: "2d/circular")
}

CylinderBridge(bottomDiameter: 10, topDiameter: 6)
.translated(x: 15)
.repeated(in: 20°..<250°, count: 5)
.translated(x: 50, y: -10)
}
@Test func roundedRectangle() {
Rectangle(x: 10, y: 10)
.roundingRectangleCorners(.bottomLeft, radius: 5)
.roundingRectangleCorners(.bottomRight, radius: 3)
.roundingRectangleCorners(.topRight, radius: 2)
.aligned(at: .centerX)
.rotated(45°)
.translated(x: -3)
.expectCodeEquals(file: "2d/rounded-rectangle")
}

#expect(geometry.code == scadFile("2dmisc"))
@Test func text() {
Text("SwiftSCAD")
.usingFont("Helvetica", size: 14)
.usingTextAlignment(horizontal: .left, vertical: .bottom)
.offset(amount: 0.4, style: .miter)
.translated(y: 5)
.sheared(.y, angle: 20°)
.expectCodeEquals(file: "2d/text")
}
}
20 changes: 8 additions & 12 deletions Tests/Tests/3D.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Testing

struct Geometry3DTests {
@Test func basic3D() {
let geometry = Box([20, 20, 20])
Box([20, 20, 20])
.aligned(at: .center)
.intersecting {
Sphere(diameter: 23)
Expand All @@ -14,20 +14,18 @@ struct Geometry3DTests {
.repeated(around: .x, in: 0°..<360°, count: 12)
.distributed(at: [0°, 90°], around: .z)
}

#expect(geometry.code == scadFile("3dbasics"))
.expectCodeEquals(file: "3d/basics")
}

@Test func empty3D() {
let geometry = Box([10, 20, 30])
Box([10, 20, 30])
.subtracting {}
.adding {}

#expect(geometry.code == scadFile("empty3d"))
.expectCodeEquals(file: "3d/empty")
}

@Test func roundedBoxes() {
let geometry = Stack(.x, spacing: 1) {
Stack(.x, spacing: 1) {
Box([10, 8, 5])
.roundingBoxCorners(radius: 2)
Box([10, 8, 5])
Expand All @@ -38,19 +36,17 @@ struct Geometry3DTests {
Box([8, 10, 6])
.roundingBoxCorners(.bottom, axis: .y, radius: 2.5)
}

#expect(geometry.code == scadFile("rounded-box"))
.expectCodeEquals(file: "3d/rounded-box")
}

@Test func cylinders() {
let geometry = Stack(.y, spacing: 1) {
Stack(.y, spacing: 1) {
Cylinder(bottomRadius: 3, topRadius: 6, height: 10)
Cylinder(largerDiameter: 10, apexAngle: 10°, height: 20)
Cylinder(largerDiameter: 20, apexAngle: -30°, height: 25)
Cylinder(smallerDiameter: 8, apexAngle: 60°, height: 10)
Cylinder(bottomDiameter: 10, topDiameter: 20, apexAngle: 20°)
}

#expect(geometry.code == scadFile("cylinders"))
.expectCodeEquals(file: "3d/cylinders")
}
}
5 changes: 2 additions & 3 deletions Tests/Tests/Color.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct ColorTests {
// around extrusions, projections.

@Test func colors() {
let geometry = Rectangle(10) // Declaration here (red)
Rectangle(10) // Declaration here (red)
.translated(x: 2)
.adding {
Circle(radius: 3) // Declaration here (blue). But because we're inside of an extrusion, no blue is actually shown.
Expand All @@ -32,7 +32,6 @@ struct ColorTests {
.adding {
Sphere(diameter: 3)
}

#expect(geometry.code == scadFile("colors"))
.expectCodeEquals(file: "colors")
}
}
29 changes: 26 additions & 3 deletions Tests/Tests/Common/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import Testing
import Foundation
@testable import SwiftSCAD

func scadFile(_ fileName: String) -> String {
let url = Bundle.module.url(forResource: fileName, withExtension: "scad", subdirectory: "SCAD")!
return try! String(contentsOf: url, encoding: .utf8).trimmingCharacters(in: .whitespacesAndNewlines)
fileprivate extension URL {
static func testSCADFile(named fileName: String) -> URL {
Bundle.module.url(forResource: fileName, withExtension: "scad", subdirectory: "SCAD")!
}

static func testSCADFileInSource(named fileName: String) -> URL {
URL.homeDirectory.appendingPathComponent("Documents/Projects/SwiftSCAD/Tests/Tests/SCAD/\(fileName).scad")
}
}

extension Geometry2D {
Expand All @@ -19,6 +24,15 @@ extension Geometry2D {
var bounds: BoundingBox2D? {
evaluated(in: .defaultEnvironment).boundary.boundingBox
}

func expectCodeEquals(file fileName: String) {
let correctCode = try! String(contentsOf: .testSCADFile(named: fileName), encoding: .utf8).trimmingCharacters(in: .whitespacesAndNewlines)
#expect(code == correctCode)
}

func setAsExpected(name: String) {
try! code.write(to: .testSCADFileInSource(named: name), atomically: true, encoding: .utf8)
}
}

extension Geometry3D {
Expand All @@ -35,6 +49,11 @@ extension Geometry3D {
#expect(code1 == code2, "Inconsistent code generation")
return code1
}

func expectCodeEquals(file fileName: String) {
let correctCode = try! String(contentsOf: .testSCADFile(named: fileName), encoding: .utf8).trimmingCharacters(in: .whitespacesAndNewlines)
#expect(code == correctCode)
}

var bounds: BoundingBox3D? {
evaluated(in: .defaultEnvironment).boundary.boundingBox
Expand All @@ -43,4 +62,8 @@ extension Geometry3D {
func triggerEvaluation() {
_ = evaluated(in: .defaultEnvironment)
}

func setAsExpected(name: String) {
try! code.write(to: .testSCADFileInSource(named: name), atomically: true, encoding: .utf8)
}
}
20 changes: 8 additions & 12 deletions Tests/Tests/Examples.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import Testing

struct ExampleTests {
@Test func example1(){
let geometry = Box([10, 20, 5])
Box([10, 20, 5])
.aligned(at: .centerY)
.rotated(y: -20°, z: 45°)

#expect(geometry.code == scadFile("example1"))
.expectCodeEquals(file: "examples/example1")
}

@Test func example2() {
let geometry = Circle(diameter: 10)
Circle(diameter: 10)
.usingFacets(count: 3)
.translated(x: 2)
.scaled(x: 2)
Expand All @@ -24,8 +23,7 @@ struct ExampleTests {
.rotated(x: 20°)
.highlighted()
}

#expect(geometry.code == scadFile("example2"))
.expectCodeEquals(file: "examples/example2")
}

struct Star: Shape2D {
Expand All @@ -46,24 +44,22 @@ struct ExampleTests {
}

@Test func example3() {
let geometry = Stack(.x, spacing: 1, alignment: .centerY) {
Stack(.x, spacing: 1, alignment: .centerY) {
Star(pointCount: 5, radius: 10, pointRadius: 1, centerSize: 4)
Star(pointCount: 6, radius: 8, pointRadius: 0, centerSize: 2)
}

#expect(geometry.code == scadFile("example3"))
.expectCodeEquals(file: "examples/example3")
}

@Test func example4() {
let path = BezierPath2D(startPoint: .zero)
.addingCubicCurve(controlPoint1: [10, 65], controlPoint2: [55, -20], end: [60, 40])

let geometry = Star(pointCount: 5, radius: 10, pointRadius: 1, centerSize: 4)
Star(pointCount: 5, radius: 10, pointRadius: 1, centerSize: 4)
.usingDefaultFacets()
.extruded(along: path)
.withPreviewConvexity(4)
.usingFacets(minAngle: 5°, minSize: 1)

#expect(geometry.code == scadFile("example4"))
.expectCodeEquals(file: "examples/example4")
}
}
5 changes: 2 additions & 3 deletions Tests/Tests/ExtrudePolygon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import Testing

struct ExtrudePolygonTest {
@Test func testHelix(){
let geometry = Polygon([[0, 3], [-1, 0], [1, 0]])
Polygon([[0, 3], [-1, 0], [1, 0]])
.transformed(.translation(x: 8))
.extrudedAlongHelix(pitch: 10, height: 20)

#expect(geometry.code == scadFile("helix"))
.expectCodeEquals(file: "helix")
}
}
File renamed without changes.
1 change: 1 addition & 0 deletions Tests/Tests/SCAD/2d/circular.scad
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let($fa=2.000000, $fn=0, $fs=0.150000) union() { scale(v=[2.000000, 1.000000]) circle(d=8.000000); translate(v=[15.000000, 0.000000]) polygon(points=[[0.000000, 0.000000], [3.758770, 1.368081], [3.704699, 1.508379], [3.645392, 1.646547], [3.580935, 1.782388], [3.511418, 1.915710], [3.436939, 2.046326], [3.357604, 2.174050], [3.273525, 2.298703], [3.184820, 2.420107], [3.091616, 2.538092], [2.994043, 2.652491], [2.892240, 2.763141], [2.786350, 2.869888], [2.676522, 2.972579], [2.562913, 3.071071], [2.445683, 3.165223], [2.324997, 3.254902], [2.201026, 3.339983], [2.073945, 3.420344], [1.943934, 3.495872], [1.811175, 3.566461], [1.675858, 3.632010], [1.538173, 3.692428], [1.398314, 3.747628], [1.256480, 3.797533], [1.112870, 3.842072], [0.967688, 3.881183], [0.821138, 3.914809], [0.673428, 3.942904], [0.524767, 3.965428], [0.375364, 3.982349], [0.225431, 3.993643], [0.075179, 3.999293], [-0.075179, 3.999293], [-0.225431, 3.993643], [-0.375364, 3.982349], [-0.524767, 3.965428], [-0.673428, 3.942904], [-0.821138, 3.914809], [-0.967688, 3.881183], [-1.112870, 3.842072], [-1.256480, 3.797533], [-1.398314, 3.747628], [-1.538173, 3.692428], [-1.675858, 3.632010], [-1.811175, 3.566461], [-1.943934, 3.495872], [-2.073945, 3.420344], [-2.201026, 3.339983], [-2.324997, 3.254902], [-2.445683, 3.165223], [-2.562913, 3.071071], [-2.676522, 2.972579], [-2.786350, 2.869888], [-2.892240, 2.763141], [-2.994043, 2.652491], [-3.091616, 2.538092], [-3.184820, 2.420107], [-3.273525, 2.298703], [-3.357604, 2.174050], [-3.436939, 2.046326], [-3.511418, 1.915710], [-3.580935, 1.782388], [-3.645392, 1.646547], [-3.704699, 1.508379], [-3.758770, 1.368081]]); translate(v=[22.000000, 0.000000]) union() { circle(d=5.000000); rotate(a=0.000000) intersection() { translate(v=[-1.767767, 1.767767]) rotate(a=-45.000000) square(size=[3.535534, 3.535534]); scale(v=[-1.000000, 1.000000]) translate(v=[-1.767767, 1.767767]) rotate(a=-45.000000) square(size=[3.535534, 3.535534]); } } translate(v=[27.000000, 0.000000]) union() { circle(d=4.000000); rotate(a=0.000000) intersection() { translate(v=[-1.732051, 1.000000]) rotate(a=-30.000000) square(size=[4.000000, 4.000000]); scale(v=[-1.000000, 1.000000]) translate(v=[-1.732051, 1.000000]) rotate(a=-30.000000) square(size=[4.000000, 4.000000]); translate(v=[-2.000000, -2.000000]) square(size=[4.000000, 4.000000]); } } translate(v=[50.000000, -10.000000]) union() { rotate(a=20.000000) translate(v=[15.000000, 0.000000]) difference() { circle(d=10.000000); union() { rotate(a=0.000000) translate(v=[3.000000, 0.000000]) translate(v=[0.000000, -5.000000]) square(size=[10.000000, 10.000000]); rotate(a=120.000000) translate(v=[3.000000, 0.000000]) translate(v=[0.000000, -5.000000]) square(size=[10.000000, 10.000000]); rotate(a=240.000000) translate(v=[3.000000, 0.000000]) translate(v=[0.000000, -5.000000]) square(size=[10.000000, 10.000000]); } } rotate(a=66.000000) translate(v=[15.000000, 0.000000]) difference() { circle(d=10.000000); union() { rotate(a=0.000000) translate(v=[3.000000, 0.000000]) translate(v=[0.000000, -5.000000]) square(size=[10.000000, 10.000000]); rotate(a=120.000000) translate(v=[3.000000, 0.000000]) translate(v=[0.000000, -5.000000]) square(size=[10.000000, 10.000000]); rotate(a=240.000000) translate(v=[3.000000, 0.000000]) translate(v=[0.000000, -5.000000]) square(size=[10.000000, 10.000000]); } } rotate(a=112.000000) translate(v=[15.000000, 0.000000]) difference() { circle(d=10.000000); union() { rotate(a=0.000000) translate(v=[3.000000, 0.000000]) translate(v=[0.000000, -5.000000]) square(size=[10.000000, 10.000000]); rotate(a=120.000000) translate(v=[3.000000, 0.000000]) translate(v=[0.000000, -5.000000]) square(size=[10.000000, 10.000000]); rotate(a=240.000000) translate(v=[3.000000, 0.000000]) translate(v=[0.000000, -5.000000]) square(size=[10.000000, 10.000000]); } } rotate(a=158.000000) translate(v=[15.000000, 0.000000]) difference() { circle(d=10.000000); union() { rotate(a=0.000000) translate(v=[3.000000, 0.000000]) translate(v=[0.000000, -5.000000]) square(size=[10.000000, 10.000000]); rotate(a=120.000000) translate(v=[3.000000, 0.000000]) translate(v=[0.000000, -5.000000]) square(size=[10.000000, 10.000000]); rotate(a=240.000000) translate(v=[3.000000, 0.000000]) translate(v=[0.000000, -5.000000]) square(size=[10.000000, 10.000000]); } } rotate(a=204.000000) translate(v=[15.000000, 0.000000]) difference() { circle(d=10.000000); union() { rotate(a=0.000000) translate(v=[3.000000, 0.000000]) translate(v=[0.000000, -5.000000]) square(size=[10.000000, 10.000000]); rotate(a=120.000000) translate(v=[3.000000, 0.000000]) translate(v=[0.000000, -5.000000]) square(size=[10.000000, 10.000000]); rotate(a=240.000000) translate(v=[3.000000, 0.000000]) translate(v=[0.000000, -5.000000]) square(size=[10.000000, 10.000000]); } } } }
1 change: 1 addition & 0 deletions Tests/Tests/SCAD/2d/rounded-rectangle.scad
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let($fa=2.000000, $fn=0, $fs=0.150000) translate(v=[-3.000000, 0.000000]) rotate(a=45.000000) translate(v=[-5.000000, 0.000000]) intersection() { intersection() { intersection() { square(size=[10.000000, 10.000000]); translate(v=[5.000000, 5.000000]) polygon(points=[[-5.000000, -0.000000], [-4.996954, -0.174497], [-4.987820, -0.348782], [-4.972609, -0.522642], [-4.951340, -0.695866], [-4.924039, -0.868241], [-4.890738, -1.039558], [-4.851479, -1.209609], [-4.806308, -1.378187], [-4.755283, -1.545085], [-4.698463, -1.710101], [-4.635919, -1.873033], [-4.567727, -2.033683], [-4.493970, -2.191856], [-4.414738, -2.347358], [-4.330127, -2.500000], [-4.240240, -2.649596], [-4.145188, -2.795965], [-4.045085, -2.938926], [-3.940054, -3.078307], [-3.830222, -3.213938], [-3.715724, -3.345653], [-3.596699, -3.473292], [-3.473292, -3.596699], [-3.345653, -3.715724], [-3.213938, -3.830222], [-3.078307, -3.940054], [-2.938926, -4.045085], [-2.795965, -4.145188], [-2.649596, -4.240240], [-2.500000, -4.330127], [-2.347358, -4.414738], [-2.191856, -4.493970], [-2.033683, -4.567727], [-1.873033, -4.635919], [-1.710101, -4.698463], [-1.545085, -4.755283], [-1.378187, -4.806308], [-1.209609, -4.851479], [-1.039558, -4.890738], [-0.868241, -4.924039], [-0.695866, -4.951340], [-0.522642, -4.972609], [-0.348782, -4.987820], [-0.174497, -4.996954], [-0.000000, -5.000000], [5.000000, -5.000000], [5.000000, 5.000000], [-5.000000, 5.000000]]); } translate(v=[5.000000, 5.000000]) polygon(points=[[-5.000000, -5.000000], [2.000000, -5.000000], [2.147203, -4.996386], [2.294051, -4.985554], [2.440191, -4.967530], [2.585271, -4.942356], [2.728941, -4.910094], [2.870854, -4.870821], [3.010670, -4.824632], [3.148050, -4.771639], [3.282665, -4.711968], [3.414190, -4.645764], [3.542308, -4.573186], [3.666711, -4.494409], [3.787098, -4.409623], [3.903180, -4.319031], [4.014677, -4.222853], [4.121320, -4.121320], [4.222853, -4.014677], [4.319031, -3.903180], [4.409623, -3.787098], [4.494409, -3.666711], [4.573186, -3.542308], [4.645764, -3.414190], [4.711968, -3.282665], [4.771639, -3.148050], [4.824632, -3.010670], [4.870821, -2.870854], [4.910094, -2.728941], [4.942356, -2.585271], [4.967530, -2.440191], [4.985554, -2.294051], [4.996386, -2.147203], [5.000000, -2.000000], [5.000000, 5.000000], [-5.000000, 5.000000]]); } translate(v=[5.000000, 5.000000]) polygon(points=[[-5.000000, -5.000000], [5.000000, -5.000000], [5.000000, 3.000000], [4.994408, 3.149460], [4.977662, 3.298085], [4.949856, 3.445042], [4.911146, 3.589510], [4.861747, 3.730682], [4.801938, 3.867767], [4.732051, 4.000000], [4.652478, 4.126640], [4.563663, 4.246980], [4.466104, 4.360345], [4.360345, 4.466104], [4.246980, 4.563663], [4.126640, 4.652478], [4.000000, 4.732051], [3.867767, 4.801938], [3.730682, 4.861747], [3.589510, 4.911146], [3.445042, 4.949856], [3.298085, 4.977662], [3.149460, 4.994408], [3.000000, 5.000000], [-5.000000, 5.000000]]); }
1 change: 1 addition & 0 deletions Tests/Tests/SCAD/2d/text.scad
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let($fa=2.000000, $fn=0, $fs=0.150000) multmatrix(m=[[1.000000, 0.363970, 0.000000, 0.000000], [0.000000, 1.000000, 0.000000, 0.000000], [0.000000, 0.000000, 1.000000, 0.000000], [0.000000, 0.000000, 0.000000, 1.000000]]) translate(v=[0.000000, 5.000000]) offset(delta=0.400000) text(font="Helvetica", halign="left", size=10.080000, text="SwiftSCAD", valign="bottom");
Loading

0 comments on commit 15edb9a

Please sign in to comment.