@@ -2,7 +2,7 @@ import SwiftUI
22import XCTest
33@testable import Flow
44
5- final class TestSubview : Flow . Subview , CustomStringConvertible {
5+ class TestSubview : Flow . Subview , CustomStringConvertible {
66 var spacing = ViewSpacing ( )
77 var priority : Double = 1
88 var placement : ( position: CGPoint , size: CGSize ) ?
@@ -58,7 +58,22 @@ final class TestSubview: Flow.Subview, CustomStringConvertible {
5858 }
5959}
6060
61- extension [ TestSubview ] : Subviews { }
61+ final class WrappingText : TestSubview {
62+ override func sizeThatFits( _ proposal: ProposedViewSize ) -> CGSize {
63+ let area = idealSize. width * idealSize. height
64+ if let proposedWidth = proposal. width, idealSize. width > proposedWidth {
65+ let height = ( Int ( 1 ) ... ) . first { area <= proposedWidth * CGFloat( $0) } !
66+ return CGSize ( width: proposedWidth, height: CGFloat ( height) )
67+ }
68+ if let proposedHeight = proposal. height, idealSize. height > proposedHeight {
69+ let width = ( Int ( 1 ) ... ) . first { area <= proposedHeight * CGFloat( $0) } !
70+ return CGSize ( width: CGFloat ( width) , height: proposedHeight)
71+ }
72+ return super. sizeThatFits ( proposal)
73+ }
74+ }
75+
76+ extension [ TestSubview ] : Flow . Subviews { }
6277
6378typealias LayoutDescription = ( subviews: [ TestSubview ] , reportedSize: CGSize )
6479
@@ -92,6 +107,8 @@ func render(_ layout: LayoutDescription, border: Bool = true) -> String {
92107 struct Point : Hashable {
93108 let x , y : Int
94109 }
110+ let width = Int ( layout. reportedSize. width)
111+ let height = Int ( layout. reportedSize. height)
95112
96113 var positions : Set < Point > = [ ]
97114 for view in layout. subviews {
@@ -101,14 +118,13 @@ func render(_ layout: LayoutDescription, border: Bool = true) -> String {
101118 for x in Int ( point. x) ..< Int ( point. x + placement. size. width) {
102119 let result = positions. insert ( Point ( x: x, y: y) )
103120 precondition ( result. inserted, " Boxes should not overlap " )
121+ precondition ( x >= 0 && x < width && y >= 0 && y < height, " Out of bounds " )
104122 }
105123 }
106124 } else {
107125 fatalError ( " Should be placed " )
108126 }
109127 }
110- let width = Int ( layout. reportedSize. width)
111- let height = Int ( layout. reportedSize. height)
112128 var result = " "
113129 if border {
114130 result += " + " + String( repeating: " - " , count: width) + " + \n "
0 commit comments