|
| 1 | +// |
| 2 | +// LayerTreet.Path+SubpathTests.swift |
| 3 | +// SwiftDraw |
| 4 | +// |
| 5 | +// Created by Simon Whitty on 21/8/22. |
| 6 | +// Copyright 2022 Simon Whitty |
| 7 | +// |
| 8 | +// Distributed under the permissive zlib license |
| 9 | +// Get the latest version from here: |
| 10 | +// |
| 11 | +// https://github.com/swhitty/SwiftDraw |
| 12 | +// |
| 13 | +// This software is provided 'as-is', without any express or implied |
| 14 | +// warranty. In no event will the authors be held liable for any damages |
| 15 | +// arising from the use of this software. |
| 16 | +// |
| 17 | +// Permission is granted to anyone to use this software for any purpose, |
| 18 | +// including commercial applications, and to alter it and redistribute it |
| 19 | +// freely, subject to the following restrictions: |
| 20 | +// |
| 21 | +// 1. The origin of this software must not be misrepresented; you must not |
| 22 | +// claim that you wrote the original software. If you use this software |
| 23 | +// in a product, an acknowledgment in the product documentation would be |
| 24 | +// appreciated but is not required. |
| 25 | +// |
| 26 | +// 2. Altered source versions must be plainly marked as such, and must not be |
| 27 | +// misrepresented as being the original software. |
| 28 | +// |
| 29 | +// 3. This notice may not be removed or altered from any source distribution. |
| 30 | +// |
| 31 | + |
| 32 | +@testable import SwiftDraw |
| 33 | +import XCTest |
| 34 | + |
| 35 | +final class LayerTreePathSubpathTests: XCTestCase { |
| 36 | + |
| 37 | + typealias Point = LayerTree.Point |
| 38 | + typealias Rect = LayerTree.Rect |
| 39 | + |
| 40 | + func testIgnoresRedundantMoves() { |
| 41 | + let path = LayerTree.Path([ |
| 42 | + .move(to: Point(-50, -50)), |
| 43 | + .move(to: Point(100, 100)), |
| 44 | + .line(to: Point(200, 200)), |
| 45 | + .move(to: Point(-50, -50)), |
| 46 | + .move(to: Point(0, 0)), |
| 47 | + .line(to: Point(0, 200)) |
| 48 | + ]) |
| 49 | + |
| 50 | + XCTAssertEqual( |
| 51 | + path.subpaths.count, 1 |
| 52 | + ) |
| 53 | + |
| 54 | + XCTAssertEqual( |
| 55 | + path.subpaths[0].segments, |
| 56 | + [.move(to: Point(100, 100)), |
| 57 | + .line(to: Point(200, 200)), |
| 58 | + .move(to: Point(0, 0)), |
| 59 | + .line(to: Point(0, 200))] |
| 60 | + ) |
| 61 | + } |
| 62 | + |
| 63 | + func testClosedPathsStartNewSubpath() { |
| 64 | + let path = LayerTree.Path([ |
| 65 | + .move(to: Point(100, 100)), |
| 66 | + .line(to: Point(200, 200)), |
| 67 | + .line(to: Point(200, 100)), |
| 68 | + .close, |
| 69 | + .line(to: Point(0, 200)), |
| 70 | + .line(to: Point(0, 100)), |
| 71 | + .close, |
| 72 | + .move(to: Point(500, 500)) |
| 73 | + ]) |
| 74 | + |
| 75 | + XCTAssertEqual( |
| 76 | + path.subpaths.count, 2 |
| 77 | + ) |
| 78 | + |
| 79 | + XCTAssertEqual( |
| 80 | + path.subpaths[0].segments.direction, |
| 81 | + .anticlockwise |
| 82 | + ) |
| 83 | + XCTAssertEqual( |
| 84 | + path.subpaths[0].segments, |
| 85 | + [.move(to: Point(100, 100)), |
| 86 | + .line(to: Point(200, 200)), |
| 87 | + .line(to: Point(200, 100)), |
| 88 | + .close] |
| 89 | + ) |
| 90 | + |
| 91 | + XCTAssertEqual( |
| 92 | + path.subpaths[1].segments.direction, |
| 93 | + .clockwise |
| 94 | + ) |
| 95 | + XCTAssertEqual( |
| 96 | + path.subpaths[1].segments, |
| 97 | + [.move(to: Point(100, 100)), |
| 98 | + .line(to: Point(0, 200)), |
| 99 | + .line(to: Point(0, 100)), |
| 100 | + .close] |
| 101 | + ) |
| 102 | + } |
| 103 | +} |
0 commit comments