|
| 1 | +// |
| 2 | +// ViewSpacingTests.swift |
| 3 | +// OpenSwiftUICoreTests |
| 4 | + |
| 5 | +import Numerics |
| 6 | +@_spi(ForOpenSwiftUIOnly) |
| 7 | +import OpenSwiftUICore |
| 8 | +import Testing |
| 9 | + |
| 10 | +struct ViewSpacingTests { |
| 11 | + // MARK: - Initialization Tests |
| 12 | + |
| 13 | + @Test |
| 14 | + func packageInitialization() { |
| 15 | + // Test package initializer with Spacing |
| 16 | + let spacing = Spacing.all(10) |
| 17 | + let viewSpacing = ViewSpacing(spacing) |
| 18 | + |
| 19 | + #expect(viewSpacing.description == #""" |
| 20 | + Spacing [ |
| 21 | + (default, top) : 10.0 |
| 22 | + (default, left) : 10.0 |
| 23 | + (default, bottom) : 10.0 |
| 24 | + (default, right) : 10.0 |
| 25 | + ] |
| 26 | + """#) |
| 27 | + |
| 28 | + // Test package initializer with Spacing and layout direction |
| 29 | + let rtlViewSpacing = ViewSpacing(spacing, layoutDirection: .rightToLeft) |
| 30 | + let ltrViewSpacing = ViewSpacing(spacing, layoutDirection: .leftToRight) |
| 31 | + |
| 32 | + // Different layout directions with symmetric spacing should behave the same |
| 33 | + let distance1 = rtlViewSpacing.distance(to: ViewSpacing.zero, along: .horizontal) |
| 34 | + let distance2 = ltrViewSpacing.distance(to: ViewSpacing.zero, along: .horizontal) |
| 35 | + #expect(distance1.isApproximatelyEqual(to: distance2)) |
| 36 | + } |
| 37 | + |
| 38 | + // MARK: - Layout Direction Tests |
| 39 | + |
| 40 | + @Test |
| 41 | + func layoutDirection() { |
| 42 | + // Create asymmetric spacing |
| 43 | + let leftSpacing = Spacing(minima: [ |
| 44 | + Spacing.Key(category: nil, edge: .left): .distance(2), |
| 45 | + Spacing.Key(category: nil, edge: .right): .distance(3), |
| 46 | + ]) |
| 47 | + |
| 48 | + // Create view spacing with different layout directions |
| 49 | + let ltrViewSpacing = ViewSpacing(leftSpacing, layoutDirection: .leftToRight) |
| 50 | + let rtlViewSpacing = ViewSpacing(leftSpacing, layoutDirection: .rightToLeft) |
| 51 | + |
| 52 | + // Test that distance calculation respects layout direction |
| 53 | + let targetSpacing = ViewSpacing(.zero) |
| 54 | + let ltrDistance = ltrViewSpacing.distance(to: targetSpacing, along: .horizontal) |
| 55 | + let rtlDistance = rtlViewSpacing.distance(to: targetSpacing, along: .horizontal) |
| 56 | + |
| 57 | + // The distances should be different due to different layout directions |
| 58 | + #expect(ltrDistance.isApproximatelyEqual(to: 3)) |
| 59 | + #expect(rtlDistance.isApproximatelyEqual(to: 2)) |
| 60 | + } |
| 61 | + |
| 62 | + // MARK: - Spacing Value Tests |
| 63 | + |
| 64 | + @Test |
| 65 | + func spacingValues() { |
| 66 | + let spacing = Spacing(minima: [ |
| 67 | + Spacing.Key(category: nil, edge: .top): .distance(10), |
| 68 | + Spacing.Key(category: nil, edge: .left): .distance(20), |
| 69 | + Spacing.Key(category: nil, edge: .bottom): .distance(30), |
| 70 | + Spacing.Key(category: nil, edge: .right): .distance(40), |
| 71 | + ]) |
| 72 | + |
| 73 | + let viewSpacing = ViewSpacing(spacing) |
| 74 | + |
| 75 | + let horizontalViewSpacing = ViewSpacing(.zero) |
| 76 | + let verticalViewSpacing = ViewSpacing(.zero) |
| 77 | + |
| 78 | + let horizontalDistance = viewSpacing.distance(to: horizontalViewSpacing, along: .horizontal) |
| 79 | + let verticalDistance = viewSpacing.distance(to: verticalViewSpacing, along: .vertical) |
| 80 | + |
| 81 | + #expect(horizontalDistance.isApproximatelyEqual(to: 40)) |
| 82 | + #expect(verticalDistance.isApproximatelyEqual(to: 30)) |
| 83 | + } |
| 84 | + |
| 85 | + // MARK: - Default Spacing Value Tests |
| 86 | + |
| 87 | + @Test |
| 88 | + func defaultSpacing() { |
| 89 | + // Create custom spacing with no common edges |
| 90 | + let spacing1 = Spacing(minima: [ |
| 91 | + Spacing.Key(category: .textToText, edge: .top): .distance(888), |
| 92 | + ]) |
| 93 | + let spacing2 = Spacing(minima: [ |
| 94 | + Spacing.Key(category: .textBaseline, edge: .bottom): .distance(999), |
| 95 | + ]) |
| 96 | + |
| 97 | + let viewSpacing1 = ViewSpacing(spacing1) |
| 98 | + let viewSpacing2 = ViewSpacing(spacing2) |
| 99 | + |
| 100 | + // Since there are no common edges, the default spacing value should be used |
| 101 | + let horizontalDistance = viewSpacing1.distance(to: viewSpacing2, along: .horizontal) |
| 102 | + let verticalDistance = viewSpacing1.distance(to: viewSpacing2, along: .vertical) |
| 103 | + |
| 104 | + #expect(horizontalDistance.isApproximatelyEqual(to: defaultSpacingValue.width)) |
| 105 | + #expect(verticalDistance.isApproximatelyEqual(to: defaultSpacingValue.height)) |
| 106 | + } |
| 107 | + |
| 108 | + // MARK: - Description Tests |
| 109 | + |
| 110 | + @Test |
| 111 | + func description() { |
| 112 | + let zeroDescription = ViewSpacing.zero.description |
| 113 | + #expect(zeroDescription.description == #""" |
| 114 | + Spacing [ |
| 115 | + (default, top) : 0.0 |
| 116 | + (default, left) : 0.0 |
| 117 | + (default, bottom) : 0.0 |
| 118 | + (default, right) : 0.0 |
| 119 | + ] |
| 120 | + """#) |
| 121 | + |
| 122 | + let customSpacing = Spacing(minima: [ |
| 123 | + Spacing.Key(category: nil, edge: .left): .distance(42), |
| 124 | + ]) |
| 125 | + #expect(ViewSpacing(customSpacing).description == #""" |
| 126 | + Spacing [ |
| 127 | + (default, left) : 42.0 |
| 128 | + ] |
| 129 | + """#) |
| 130 | + |
| 131 | + let emptySpacing = Spacing(minima: [:]) |
| 132 | + #expect(emptySpacing.description == #""" |
| 133 | + Spacing (empty) |
| 134 | + """#) |
| 135 | + } |
| 136 | + |
| 137 | + // MARK: - Edge Incorporation Tests |
| 138 | + |
| 139 | + @Test |
| 140 | + func edgeIncorporation() { |
| 141 | + // Create spacing with values for specific edges |
| 142 | + let spacing1 = Spacing(minima: [ |
| 143 | + Spacing.Key(category: nil, edge: .top): .distance(10), |
| 144 | + Spacing.Key(category: nil, edge: .left): .distance(20), |
| 145 | + ]) |
| 146 | + |
| 147 | + let spacing2 = Spacing(minima: [ |
| 148 | + Spacing.Key(category: nil, edge: .top): .distance(30), |
| 149 | + Spacing.Key(category: nil, edge: .right): .distance(40), |
| 150 | + ]) |
| 151 | + |
| 152 | + var viewSpacing1 = ViewSpacing(spacing1) |
| 153 | + let viewSpacing2 = ViewSpacing(spacing2) |
| 154 | + |
| 155 | + // Incorporate only the top edge |
| 156 | + viewSpacing1.formUnion(viewSpacing2, edges: .top) |
| 157 | + |
| 158 | + // Top should be taken from spacing2 (larger), but left should remain from spacing1 |
| 159 | + let result = viewSpacing1.spacing |
| 160 | + |
| 161 | + let topValue = result.minima[Spacing.Key(category: nil, edge: .top)] |
| 162 | + let leftValue = result.minima[Spacing.Key(category: nil, edge: .left)] |
| 163 | + let rightValue = result.minima[Spacing.Key(category: nil, edge: .right)] |
| 164 | + |
| 165 | + #expect(topValue?.value?.isApproximatelyEqual(to: 30.0) == true) |
| 166 | + #expect(leftValue?.value?.isApproximatelyEqual(to: 20.0) == true) |
| 167 | + #expect(rightValue == nil) // Right edge should not be incorporated |
| 168 | + } |
| 169 | +} |
0 commit comments