Add tests and fix layout discrepencies

This commit is contained in:
Carson Katri 2023-02-04 17:19:02 -05:00
parent 8aca895422
commit 3478c9df19
7 changed files with 456 additions and 16 deletions

View File

@ -144,12 +144,16 @@ public extension _LazyGridLayout {
maxMainAxisSize = size[keyPath: mainAxis]
}
if subviews.indices.contains(index + cache.resolvedItems.count) {
let spacing = subview.spacing.distance(
to: subviews[index + cache.resolvedItems.count].spacing,
along: .vertical
)
if spacing > mainAxisSpacing {
if let spacing {
mainAxisSpacing = spacing
} else {
let spacing = subview.spacing.distance(
to: subviews[index + cache.resolvedItems.count].spacing,
along: .vertical
)
if spacing > mainAxisSpacing {
mainAxisSpacing = spacing
}
}
}
if itemIndex == cache.resolvedItems.count - 1 {

View File

@ -381,10 +381,9 @@ public extension Grid where Content == EmptyView {
horizontalSpacing: CGFloat? = nil,
verticalSpacing: CGFloat? = nil
) {
self.init(
alignment: alignment,
horizontalSpacing: horizontalSpacing,
verticalSpacing: verticalSpacing
) { EmptyView() }
self.alignment = alignment
self.horizontalSpacing = horizontalSpacing
self.verticalSpacing = verticalSpacing
content = EmptyView()
}
}

View File

@ -118,12 +118,12 @@ extension ScrollView: Layout {
subview.place(
at: .init(x: bounds.midX, y: bounds.midY),
anchor: .center,
proposal: proposal
proposal: contentProposal
)
} else {
subview.place(
at: bounds.origin,
proposal: proposal
proposal: contentProposal
)
}
}

View File

@ -48,7 +48,7 @@ benchmark("render Text (StackReconciler)") {
}
benchmark("render Text (FiberReconciler)") {
StaticHTMLFiberRenderer().render(Text("Hello, world!"))
_ = StaticHTMLFiberRenderer().render(Text("Hello, world!"))
}
benchmark("render ForEach(100) (StackReconciler)") {
@ -56,7 +56,7 @@ benchmark("render ForEach(100) (StackReconciler)") {
}
benchmark("render ForEach(100) (FiberReconciler)") {
StaticHTMLFiberRenderer().render(ForEach(1..<100) { Text("\($0)") })
_ = StaticHTMLFiberRenderer().render(ForEach(1..<100) { Text("\($0)") })
}
benchmark("render ForEach(1000) (StackReconciler)") {
@ -64,7 +64,7 @@ benchmark("render ForEach(1000) (StackReconciler)") {
}
benchmark("render ForEach(1000) (FiberReconciler)") {
StaticHTMLFiberRenderer().render(ForEach(1..<1000) { Text("\($0)") })
_ = StaticHTMLFiberRenderer().render(ForEach(1..<1000) { Text("\($0)") })
}
struct RecursiveView: View {
@ -88,7 +88,7 @@ benchmark("render RecursiveView(1000) (StackReconciler)") {
}
benchmark("render RecursiveView(1000) (FiberReconciler)") {
StaticHTMLFiberRenderer().render(RecursiveView(1000))
_ = StaticHTMLFiberRenderer().render(RecursiveView(1000))
}
Benchmark.main()

View File

@ -0,0 +1,285 @@
// Copyright 2022 Tokamak contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Created by Carson Katri on 2/4/23.
//
#if os(macOS)
import SwiftUI
import TokamakStaticHTML
import XCTest
final class GridTests: XCTestCase {
@available(macOS 13.0, *)
func testGrid() async {
await compare(size: .init(width: 500, height: 500)) {
SwiftUI.Grid(alignment: .bottomTrailing, horizontalSpacing: 10, verticalSpacing: 10) {
GridRow(alignment: .top) {
SwiftUI.Rectangle()
.fill(Color(white: 0))
SwiftUI.Rectangle()
.fill(Color(white: 0.25))
SwiftUI.Rectangle()
.fill(Color(white: 0.4))
}
GridRow(alignment: .top) {
SwiftUI.Rectangle()
.fill(Color(white: 0))
SwiftUI.Rectangle()
.fill(Color(white: 0.25))
SwiftUI.Rectangle()
.fill(Color(white: 0.4))
SwiftUI.Rectangle()
.fill(Color(white: 0.6))
}
}
} to: {
TokamakStaticHTML.Grid(
alignment: .bottomTrailing,
horizontalSpacing: 10,
verticalSpacing: 10
) {
GridRow(alignment: .top) {
Rectangle()
.fill(Color(white: 0))
Rectangle()
.fill(Color(white: 0.25))
Rectangle()
.fill(Color(white: 0.4))
}
GridRow(alignment: .top) {
Rectangle()
.fill(Color(white: 0))
Rectangle()
.fill(Color(white: 0.25))
Rectangle()
.fill(Color(white: 0.4))
Rectangle()
.fill(Color(white: 0.6))
}
}
}
}
@available(macOS 13.0, *)
func testGridCellColumns() async {
await compare(size: .init(width: 500, height: 500)) {
SwiftUI.Grid(alignment: .bottomTrailing, horizontalSpacing: 10, verticalSpacing: 10) {
GridRow(alignment: .top) {
SwiftUI.Rectangle()
.fill(Color(white: 0))
SwiftUI.Rectangle()
.fill(Color(white: 0.25))
.gridCellColumns(2)
SwiftUI.Rectangle()
.fill(Color(white: 0.4))
}
GridRow(alignment: .top) {
SwiftUI.Rectangle()
.fill(Color(white: 0))
SwiftUI.Rectangle()
.fill(Color(white: 0.25))
SwiftUI.Rectangle()
.fill(Color(white: 0.4))
SwiftUI.Rectangle()
.fill(Color(white: 0.6))
}
}
} to: {
TokamakStaticHTML.Grid(
alignment: .bottomTrailing,
horizontalSpacing: 10,
verticalSpacing: 10
) {
GridRow(alignment: .top) {
Rectangle()
.fill(Color(white: 0))
Rectangle()
.fill(Color(white: 0.25))
.gridCellColumns(2)
Rectangle()
.fill(Color(white: 0.4))
}
GridRow(alignment: .top) {
Rectangle()
.fill(Color(white: 0))
Rectangle()
.fill(Color(white: 0.25))
Rectangle()
.fill(Color(white: 0.4))
Rectangle()
.fill(Color(white: 0.6))
}
}
}
}
@available(macOS 13.0, *)
func testGridColumnAlignment() async {
await compare(size: .init(width: 500, height: 500)) {
SwiftUI.Grid(alignment: .bottomTrailing, horizontalSpacing: 10, verticalSpacing: 10) {
GridRow(alignment: .top) {
SwiftUI.Rectangle()
.fill(Color(white: 0))
SwiftUI.Rectangle()
.fill(Color(white: 0.25))
.frame(width: 15)
.gridColumnAlignment(.trailing)
SwiftUI.Rectangle()
.fill(Color(white: 0.4))
}
GridRow(alignment: .top) {
SwiftUI.Rectangle()
.fill(Color(white: 0))
SwiftUI.Rectangle()
.fill(Color(white: 0.25))
SwiftUI.Rectangle()
.fill(Color(white: 0.4))
}
}
} to: {
TokamakStaticHTML.Grid(
alignment: .bottomTrailing,
horizontalSpacing: 10,
verticalSpacing: 10
) {
GridRow(alignment: .top) {
Rectangle()
.fill(Color(white: 0))
Rectangle()
.fill(Color(white: 0.25))
.frame(width: 15)
.gridColumnAlignment(.trailing)
Rectangle()
.fill(Color(white: 0.4))
}
GridRow(alignment: .top) {
Rectangle()
.fill(Color(white: 0))
Rectangle()
.fill(Color(white: 0.25))
Rectangle()
.fill(Color(white: 0.4))
}
}
}
}
@available(macOS 13.0, *)
func testGridCellAnchor() async {
await compare(size: .init(width: 500, height: 500)) {
SwiftUI.Grid(alignment: .topLeading, horizontalSpacing: 10, verticalSpacing: 10) {
GridRow(alignment: .top) {
SwiftUI.Rectangle()
.fill(Color(white: 0))
.frame(width: 15, height: 15)
.gridCellAnchor(.bottomTrailing)
SwiftUI.Rectangle()
.fill(Color(white: 0))
SwiftUI.Rectangle()
.fill(Color(white: 0))
}
GridRow(alignment: .top) {
SwiftUI.Rectangle()
.fill(Color(white: 0))
SwiftUI.Rectangle()
.fill(Color(white: 0))
SwiftUI.Rectangle()
.fill(Color(white: 0))
.frame(width: 15, height: 15)
.gridCellAnchor(.leading)
}
}
} to: {
TokamakStaticHTML.Grid(alignment: .topLeading, horizontalSpacing: 10, verticalSpacing: 10) {
GridRow(alignment: .top) {
Rectangle()
.fill(Color(white: 0))
.frame(width: 15, height: 15)
.gridCellAnchor(.bottomTrailing)
Rectangle()
.fill(Color(white: 0))
Rectangle()
.fill(Color(white: 0))
}
GridRow(alignment: .top) {
Rectangle()
.fill(Color(white: 0))
Rectangle()
.fill(Color(white: 0))
Rectangle()
.fill(Color(white: 0))
.frame(width: 15, height: 15)
.gridCellAnchor(.leading)
}
}
}
}
@available(macOS 13.0, *)
func testGridCellUnsizedAxes() async {
await compare(size: .init(width: 500, height: 500)) {
SwiftUI.Grid(alignment: .bottomTrailing, horizontalSpacing: 10, verticalSpacing: 10) {
GridRow(alignment: .top) {
SwiftUI.Rectangle()
.fill(Color(white: 0))
.frame(width: 100, height: 100)
SwiftUI.Rectangle()
.fill(Color(white: 0.25))
.frame(width: 100, height: 100)
}
Rectangle()
.fill(Color(white: 0))
.frame(height: 5)
.gridCellUnsizedAxes(.horizontal)
GridRow(alignment: .top) {
SwiftUI.Rectangle()
.fill(Color(white: 0))
.frame(width: 100, height: 100)
SwiftUI.Rectangle()
.fill(Color(white: 0.25))
.frame(width: 100, height: 100)
}
}
} to: {
TokamakStaticHTML.Grid(
alignment: .bottomTrailing,
horizontalSpacing: 10,
verticalSpacing: 10
) {
GridRow(alignment: .top) {
Rectangle()
.fill(Color(white: 0))
.frame(width: 100, height: 100)
Rectangle()
.fill(Color(white: 0.25))
.frame(width: 100, height: 100)
}
Rectangle()
.fill(Color(white: 0))
.frame(height: 5)
.gridCellUnsizedAxes(.horizontal)
GridRow(alignment: .top) {
Rectangle()
.fill(Color(white: 0))
.frame(width: 100, height: 100)
Rectangle()
.fill(Color(white: 0.25))
.frame(width: 100, height: 100)
}
}
}
}
}
#endif

View File

@ -0,0 +1,112 @@
// Copyright 2022 Tokamak contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Created by Carson Katri on 2/4/23.
//
#if os(macOS)
import SwiftUI
import TokamakStaticHTML
import XCTest
final class LazyGridTests: XCTestCase {
@available(macOS 13.0, *)
func testLazyVGrid() async {
await compare(size: .init(width: 500, height: 500)) {
SwiftUI.LazyVGrid(columns: [.init(.adaptive(minimum: 100))], spacing: 10) {
ForEach(0..<10) { _ in
Rectangle()
.fill(Color(white: 0))
.frame(height: 100)
}
}
} to: {
TokamakStaticHTML.LazyVGrid(columns: [.init(.adaptive(minimum: 100))], spacing: 10) {
ForEach(0..<10) { _ in
Rectangle()
.fill(Color(white: 0))
.frame(height: 100)
}
}
}
await compare(size: .init(width: 500, height: 500)) {
SwiftUI.LazyVGrid(
columns: [.init(.fixed(100)), .init(.fixed(100), spacing: 0), .init(.flexible())],
spacing: 10
) {
ForEach(0..<10) { _ in
Rectangle()
.fill(Color(white: 0))
.frame(height: 100)
}
}
} to: {
TokamakStaticHTML.LazyVGrid(
columns: [.init(.fixed(100)), .init(.fixed(100), spacing: 0), .init(.flexible())],
spacing: 10
) {
ForEach(0..<10) { _ in
Rectangle()
.fill(Color(white: 0))
.frame(height: 100)
}
}
}
}
@available(macOS 13.0, *)
func testLazyHGrid() async {
await compare(size: .init(width: 500, height: 500)) {
SwiftUI.LazyHGrid(rows: [.init(.adaptive(minimum: 100))], spacing: 10) {
ForEach(0..<10) { _ in
Rectangle()
.fill(Color(white: 0))
.frame(width: 100)
}
}
} to: {
TokamakStaticHTML.LazyHGrid(rows: [.init(.adaptive(minimum: 100))], spacing: 10) {
ForEach(0..<10) { _ in
Rectangle()
.fill(Color(white: 0))
.frame(width: 100)
}
}
}
await compare(size: .init(width: 500, height: 500)) {
SwiftUI.LazyHGrid(
rows: [.init(.fixed(100)), .init(.fixed(100), spacing: 0), .init(.flexible())],
spacing: 10
) {
ForEach(0..<10) { _ in
Rectangle()
.fill(Color(white: 0))
.frame(width: 100)
}
}
} to: {
TokamakStaticHTML.LazyHGrid(
rows: [.init(.fixed(100)), .init(.fixed(100), spacing: 0), .init(.flexible())],
spacing: 10
) {
ForEach(0..<10) { _ in
Rectangle()
.fill(Color(white: 0))
.frame(width: 100)
}
}
}
}
}
#endif

View File

@ -0,0 +1,40 @@
// Copyright 2022 Tokamak contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Created by Carson Katri on 2/4/23.
//
#if os(macOS)
import SwiftUI
import TokamakStaticHTML
import XCTest
final class LinkTests: XCTestCase {
func testLink() async {
await compare(size: .init(width: 500, height: 500)) {
SwiftUI.Link(destination: URL(string: "https://tokamak.dev")!) {
Rectangle()
.fill(Color(white: 0))
.frame(width: 250, height: 100)
}
} to: {
TokamakStaticHTML.Link(destination: URL(string: "https://tokamak.dev")!) {
Rectangle()
.fill(TokamakStaticHTML.Color(white: 0))
.frame(width: 250, height: 100)
}
}
}
}
#endif