Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 50 additions & 50 deletions DOM/Tests/DOM+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,59 +33,59 @@
import Foundation

extension DOM {

static func createLine() -> DOM.Line {
return DOM.Line(x1: 0, y1: 1, x2: 3, y2: 4)
}

static func createCircle() -> DOM.Circle {
return DOM.Circle(cx: 0, cy: 1, r: 2)
}

static func createEllipse() -> DOM.Ellipse {
return DOM.Ellipse(cx: 0, cy: 1, rx: 2, ry: 3)
}

static func createRect() -> DOM.Rect {
return DOM.Rect(x: 0, y: 1, width: 2, height: 3)
}

static func createPolygon() -> DOM.Polygon {
return DOM.Polygon(0, 1, 2, 3, 4, 5)
}

static func createPolyline() -> DOM.Polyline {
return DOM.Polyline(0, 1, 2, 3, 4, 5)
}

static func createText() -> DOM.Text {
return DOM.Text(y: 1, value: "The quick brown fox")
}

static func createPath() -> DOM.Path {
let path = DOM.Path(x: 0, y: 1)
path.segments.append(.move(x: 10, y: 10, space: .absolute))
path.segments.append(.horizontal(x: 10, space: .absolute))
return path
}

static func createGroup() -> DOM.Group {
let group = DOM.Group()
group.childElements.append(createLine())
group.childElements.append(createPolygon())
group.childElements.append(createCircle())
group.childElements.append(createPath())
group.childElements.append(createRect())
group.childElements.append(createEllipse())
return group
}
static func createLine() -> DOM.Line {
return DOM.Line(x1: 0, y1: 1, x2: 3, y2: 4)
}
static func createCircle() -> DOM.Circle {
return DOM.Circle(cx: 0, cy: 1, r: 2)
}
static func createEllipse() -> DOM.Ellipse {
return DOM.Ellipse(cx: 0, cy: 1, rx: 2, ry: 3)
}
static func createRect() -> DOM.Rect {
return DOM.Rect(x: 0, y: 1, width: 2, height: 3)
}
static func createPolygon() -> DOM.Polygon {
return DOM.Polygon(0, 1, 2, 3, 4, 5)
}
static func createPolyline() -> DOM.Polyline {
return DOM.Polyline(0, 1, 2, 3, 4, 5)
}
static func createText() -> DOM.Text {
return DOM.Text(y: 1, value: "The quick brown fox")
}
static func createPath() -> DOM.Path {
let path = DOM.Path(x: 0, y: 1)
path.segments.append(.move(x: 10, y: 10, space: .absolute))
path.segments.append(.horizontal(x: 10, space: .absolute))
return path
}
static func createGroup() -> DOM.Group {
let group = DOM.Group()
group.childElements.append(createLine())
group.childElements.append(createPolygon())
group.childElements.append(createCircle())
group.childElements.append(createPath())
group.childElements.append(createRect())
group.childElements.append(createEllipse())
return group
}
}

// Equatable just for tests

extension DOM.GraphicsElement: Swift.Equatable {
static func ==(lhs: DOM.GraphicsElement, rhs: DOM.GraphicsElement) -> Bool {
let toString: (Any) -> String = { var text = ""; dump($0, to: &text); return text }
return toString(lhs) == toString(rhs)
}
static func ==(lhs: DOM.GraphicsElement, rhs: DOM.GraphicsElement) -> Bool {
let toString: (Any) -> String = { var text = ""; dump($0, to: &text); return text }
return toString(lhs) == toString(rhs)
}
}
106 changes: 50 additions & 56 deletions DOM/Tests/DOM.PresentationAttributesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,100 +29,97 @@
// 3. This notice may not be removed or altered from any source distribution.
//

import XCTest
import Testing
@testable import SwiftDrawDOM

final class PresentationAttributesTests: XCTestCase {
struct PresentationAttributesTests {

typealias Attributes = DOM.PresentationAttributes
typealias StyleSheet = DOM.StyleSheet

func testOpacityIsApplied() {
XCTAssertNil(
@Test
func opacityIsApplied() {
#expect(
Attributes(opacity: nil)
.applyingAttributes(Attributes(opacity: nil))
.opacity
.opacity == nil
)

XCTAssertEqual(
#expect(
Attributes(opacity: 5)
.applyingAttributes(Attributes(opacity: nil))
.opacity,
5
.opacity == 5
)

XCTAssertEqual(
#expect(
Attributes(opacity: 5)
.applyingAttributes(Attributes(opacity: 10))
.opacity,
10
.opacity == 10
)
}

func testDisplayIsApplied() {
XCTAssertNil(
@Test
func displayIsApplied() {
#expect(
Attributes(display: nil)
.applyingAttributes(Attributes(display: nil))
.display
.display == nil
)

XCTAssertEqual(
#expect(
Attributes(display: DOM.DisplayMode.none)
.applyingAttributes(Attributes(display: nil))
.display,
DOM.DisplayMode.none
.display == DOM.DisplayMode.none
)

XCTAssertEqual(
#expect(
Attributes(display: DOM.DisplayMode.none)
.applyingAttributes(Attributes(display: .inline))
.display,
.inline
.display == .inline
)
}

func testColorIsApplied() {
XCTAssertNil(
@Test
func colorIsApplied() {
#expect(
Attributes(color: nil)
.applyingAttributes(Attributes(color: nil))
.color
.color == nil
)

XCTAssertEqual(
#expect(
Attributes(color: .keyword(.green))
.applyingAttributes(Attributes(color: nil))
.color,
.keyword(.green)
.color == .keyword(.green)
)

XCTAssertEqual(
#expect(
Attributes(color: .keyword(.green))
.applyingAttributes(Attributes(color: .currentColor))
.color,
.currentColor
.color == .currentColor
)
}

func testSelectors() {
XCTAssertEqual(
DOM.makeSelectors(for: .circle()),
[.element("circle")]
@Test
func selectors() {
#expect(
DOM.makeSelectors(for: .circle()) == [.element("circle")]
)

XCTAssertEqual(
DOM.makeSelectors(for: .circle(id: "c1")),
#expect(
DOM.makeSelectors(for: .circle(id: "c1")) ==
[.element("circle"),
.id("c1")]
)

XCTAssertEqual(
DOM.makeSelectors(for: .circle(class: "c")),
#expect(
DOM.makeSelectors(for: .circle(class: "c")) ==
[.element("circle"),
.class("c")]
)

XCTAssertEqual(
DOM.makeSelectors(for: .circle(id: "c1 ", class: "a b c")),
#expect(
DOM.makeSelectors(for: .circle(id: "c1 ", class: "a b c")) ==
[.element("circle"),
.class("a"),
.class("b"),
Expand All @@ -131,7 +128,8 @@ final class PresentationAttributesTests: XCTestCase {
)
}

func testLastSheetAttributesAreUsed() {
@Test
func lastSheetAttributesAreUsed() {
var sheet = StyleSheet()
sheet[.id("b")].opacity = 0
sheet[.id("a")].opacity = 1
Expand All @@ -140,45 +138,41 @@ final class PresentationAttributesTests: XCTestCase {
another[.id("b")].opacity = 0.1
another[.id("a")].opacity = 0.5

XCTAssertEqual(
#expect(
DOM.makeAttributes(for: .id("a"), styles: [sheet])
.opacity,
1
.opacity == 1
)

XCTAssertEqual(
#expect(
DOM.makeAttributes(for: .id("a"), styles: [sheet, another])
.opacity,
0.5
.opacity == 0.5
)
}

func testSelectorPrecedence() {
@Test
func selectorPrecedence() {
var sheet = StyleSheet()
sheet[.element("circle")].opacity = 1
sheet[.id("c1")].opacity = 0.5
sheet[.class("b")].opacity = 0.1
sheet[.class("c")].opacity = 0.2

XCTAssertEqual(
#expect(
DOM.presentationAttributes(for: .circle(id: "c1", class: "b c"),
styles: [sheet])
.opacity,
0.5
.opacity == 0.5
)

XCTAssertEqual(
#expect(
DOM.presentationAttributes(for: .circle(id: "c2", class: "b c"),
styles: [sheet])
.opacity,
0.2
.opacity == 0.2
)

XCTAssertEqual(
#expect(
DOM.presentationAttributes(for: .circle(id: "c2", class: "z"),
styles: [sheet])
.opacity,
1
.opacity == 1
)
}
}
Expand Down
Loading