Skip to content

Commit

Permalink
Merge branch 'swift-testing' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasf committed Oct 4, 2024
2 parents 90af93e + 0fd9425 commit 0984e82
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 233 deletions.
18 changes: 10 additions & 8 deletions Tests/Tests/2DTests.swift → Tests/Tests/2D.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import XCTest
import Testing
@testable import SwiftSCAD

final class Geometry2DTests: XCTestCase {
func test2DBasics() {
Union {
struct Geometry2DTests {
@Test func basic2D() {
let geometry = Union {
Rectangle(Vector2D(30, 10))
.aligned(at: .centerY)
.subtracting {
Expand All @@ -16,11 +16,12 @@ final class Geometry2DTests: XCTestCase {
}
Arc(range: 80°..<280°, radius: 3.5)
}
.assertEqual(toFile: "2dbasics")

#expect(geometry.code == scadFile("2dbasics"))
}

func test2DMisc() {
Rectangle(Vector2D(30, 10))
@Test func misc2D() {
let geometry = Rectangle(Vector2D(30, 10))
.aligned(at: .centerY)
.subtracting {
Circle(diameter: 8)
Expand Down Expand Up @@ -54,6 +55,7 @@ final class Geometry2DTests: XCTestCase {
.repeated(in: 20°..<250°, count: 5)
.translated(x: 50, y: -10)
}
.assertEqual(toFile: "2dmisc")

#expect(geometry.code == scadFile("2dmisc"))
}
}
27 changes: 27 additions & 0 deletions Tests/Tests/3D.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Testing
@testable import SwiftSCAD

struct Geometry3DTests {
@Test func basic3D() {
let geometry = Box([20, 20, 20])
.aligned(at: .center)
.intersection {
Sphere(diameter: 23)
}
.subtracting {
Cylinder(diameter: 2, height: 30)
.repeated(around: .x, in: 0°..<360°, count: 12)
.distributed(at: [0°, 90°], around: .z)
}

#expect(geometry.code == scadFile("3dbasics"))
}

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

#expect(geometry.code == scadFile("empty3d"))
}
}
50 changes: 0 additions & 50 deletions Tests/Tests/3DTests.swift

This file was deleted.

File renamed without changes.
57 changes: 32 additions & 25 deletions Tests/Tests/Bounds.swift
Original file line number Diff line number Diff line change
@@ -1,43 +1,47 @@
import XCTest
import Testing
@testable import SwiftSCAD

final class BoundsTests: XCTestCase {
func testBasic2DAlignment() {
Rectangle([10, 4])
struct BoundsTests {
@Test func basicAlignment2D() {
let geometry = Rectangle([10, 4])
.aligned(at: .centerX, .top)
.assertBoundsEqual(min: [-5, -4], max: [5, 0])

#expect(geometry.bounds .init(minimum: [-5, -4], maximum: [5, 0]))
}

func testConflictingAlignment() {
Rectangle([50, 20])
@Test func conflictingAlignment() {
let geometry = Rectangle([50, 20])
.aligned(at: .minX, .centerX, .centerY, .maxX)
.assertBoundsEqual(min: [-50, -10], max: [0, 10])

#expect(geometry.bounds .init(minimum: [-50, -10], maximum: [0, 10]))
}

func testRepeatedAlignment() {
Box([10, 8, 12])
@Test func repeatedAlignment() {
let geometry = Box([10, 8, 12])
.aligned(at: .minX)
.aligned(at: .maxY)
.aligned(at: .centerX, .centerY)
.aligned(at: .maxX)
.aligned(at: .centerXY)
.assertBoundsEqual(min: [-5, -4, 0], max: [5, 4, 12])

#expect(geometry.bounds .init(minimum: [-5, -4, 0], maximum: [5, 4, 12]))
}

func testTransformedBounds() {
Box([10, 8, 12])
@Test func transformedBounds() {
let base = Box([10, 8, 12])
.rotated(x: 90°)
.translated(y: 12)
.assertBoundsEqual {
Box([10, 12, 8])
}

let extended = base
.scaled(z: 1.25)
.sheared(.x, along: .y, factor: 1.5)
.assertBoundsEqual(min: [0, 0, 0], max: [28, 12, 10])

#expect(base.bounds Box([10, 12, 8]).bounds)
#expect(extended.bounds .init(minimum: .zero, maximum: [28, 12, 10]))
}

func testStack() {
Stack(.x, spacing: 1) {
@Test func stack() {
let geometry = Stack(.x, spacing: 1) {
Box([10, 8, 12])
RegularPolygon(sideCount: 8, apothem: 3)
.rotated(22.5°)
Expand All @@ -46,13 +50,14 @@ final class BoundsTests: XCTestCase {
.scaled(x: 2)
.extruded(height: 3)
}
.assertBoundsEqual(min: [0, 0, 0], max: [22, 8, 12])

#expect(geometry.bounds .init(minimum: .zero, maximum: [22, 8, 12]))
}

func testAnchors() {
@Test func anchors() {
let top = Anchor()
let side = Anchor()
Box([8,6,4])
let base = Box([8,6,4])
.aligned(at: .centerXY, .top)
.adding {
Box([2,2,1])
Expand All @@ -62,8 +67,10 @@ final class BoundsTests: XCTestCase {
.definingAnchor(side, at: .left, offset: [0, 0, -2], pointing: .left)
.anchored(to: top)
.anchored(to: side)
.assertBoundsEqual(min: [-2, -3, -8], max: [3, 3, 0])
.anchored(to: top)
.assertBoundsEqual(min: [-4, -3, -5], max: [4, 3, 0])

let extended = base.anchored(to: top)

#expect(base.bounds .init(minimum: [-2, -3, -8], maximum: [3, 3, 0]))
#expect(extended.bounds .init(minimum: [-4, -3, -5], maximum: [4, 3, 0]))
}
}
77 changes: 0 additions & 77 deletions Tests/Tests/Common.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import Foundation
import SwiftSCAD

infix operator
infix operator : ComparisonPrecedence

protocol ApproximatelyEquatable {
static func (_ lhs: Self, _ rhs: Self) -> Bool
}

extension Double: ApproximatelyEquatable {
static func (lhs: Self, rhs: Self) -> Bool {
abs(lhs - rhs) < 0.0001
abs(lhs - rhs) < 0.001
}
}

extension Optional: ApproximatelyEquatable where Wrapped: ApproximatelyEquatable {
static func (lhs: Self, rhs: Self) -> Bool {
switch (lhs, rhs) {
case (.none, .none): true
case (.none, .some), (.some, .none): false
case (.some(let a), .some(let b)): a b
}
}
}

Expand All @@ -23,8 +34,6 @@ extension Array: ApproximatelyEquatable where Element: ApproximatelyEquatable {}


extension Vector {
static var tolerance: Double { 0.001 }

static func (_ lhs: Self, _ rhs: Self) -> Bool {
lhs.elements rhs.elements
}
Expand All @@ -38,3 +47,9 @@ extension Angle: ApproximatelyEquatable {
lhs.radians rhs.radians
}
}

extension BoundingBox: ApproximatelyEquatable {
static func (_ lhs: Self, _ rhs: Self) -> Bool {
lhs.minimum rhs.minimum && lhs.maximum rhs.maximum
}
}
42 changes: 42 additions & 0 deletions Tests/Tests/Common/Helpers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import XCTest
@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)
}

extension Geometry2D {
var code: String {
usingDefaultFacets().codeFragment(in: .defaultEnvironment).scadCode
}

func triggerEvaluation() {
_ = codeFragment(in: .defaultEnvironment)
}

var bounds: BoundingBox2D? {
boundary(in: .defaultEnvironment).boundingBox
}
}

extension Geometry3D {
func readingOperation(_ action: @escaping (Environment.Operation) -> ()) -> any Geometry3D {
readingEnvironment { geo, environment in
action(environment.operation)
return self
}
}

var code: String {
usingDefaultFacets().codeFragment(in: .defaultEnvironment).scadCode
}

var bounds: BoundingBox3D? {
boundary(in: .defaultEnvironment).boundingBox
}

func triggerEvaluation() {
_ = codeFragment(in: .defaultEnvironment)
}
}
Loading

0 comments on commit 0984e82

Please sign in to comment.