Skip to content

Commit 204b0e4

Browse files
updated staticHTML() implementations; added a unit test and...
- added custom rendering for some libraries due to result limitations
1 parent e8b29b4 commit 204b0e4

File tree

11 files changed

+147
-33
lines changed

11 files changed

+147
-33
lines changed

Benchmarks/Benchmarks/Elementary/Elementary.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,34 @@ package struct ElementaryTests : HTMLGenerator {
1212
package init() {}
1313

1414
package func staticHTML() -> String {
15-
html { body { h1 { "Swift HTML Benchmarks" }} }.render()
15+
StaticView().render()
1616
}
1717
package func dynamicHTML(_ context: HTMLContext) -> String {
1818
html {
1919
body {
2020
h1 { context.heading }
21-
div(attributes: [.id("desc")]) {
21+
div(attributes: [.id(context.desc_id)]) {
2222
p { context.string }
2323
}
2424
h2 { context.user.details_heading }
2525
h3 { context.user.qualities_heading }
26-
ul(attributes: [.id("user-qualities")]) {
26+
ul(attributes: [.id(context.user.qualities_id)]) {
2727
for quality in context.user.qualities {
2828
li { quality }
2929
}
3030
}
3131
}
3232
}.render()
3333
}
34+
}
35+
36+
struct StaticView : HTMLDocument {
37+
var title:String = "StaticView"
38+
39+
var head : some HTML {
40+
""
41+
}
42+
var body : some HTML {
43+
h1 { "Swift HTML Benchmarks" }
44+
}
3445
}

Benchmarks/Benchmarks/Plot/Plot.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ package struct PlotTests : HTMLGenerator {
1313

1414
package func staticHTML() -> String {
1515
HTML(
16+
.head(
17+
.element(named: "title", text: "StaticView")
18+
),
1619
.body(
1720
.h1("Swift HTML Benchmarks")
1821
)
@@ -42,9 +45,9 @@ struct Context {
4245

4346
init(_ context: Utilities.HTMLContext) {
4447
heading = H1(context.heading)
45-
desc = Div(Paragraph(context.string).id("desc"))
48+
desc = Div(Paragraph(context.string).id(context.desc_id))
4649
details_heading = H2(context.user.details_heading)
4750
qualities_heading = H3(context.user.qualities_heading)
48-
qualities = List(context.user.qualities).id("user-qualities")
51+
qualities = List(context.user.qualities).id(context.user.qualities_id)
4952
}
5053
}

Benchmarks/Benchmarks/SwiftHTMLBB/SwiftHTMLBB.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ package struct SwiftHTMLBBTests : HTMLGenerator {
1717
package func staticHTML() -> String {
1818
renderer.render(Document(.html) {
1919
Html {
20+
Head {
21+
Title("StaticView")
22+
}
2023
Body {
2124
H1("Swift HTML Benchmarks")
2225
}
@@ -31,12 +34,12 @@ package struct SwiftHTMLBBTests : HTMLGenerator {
3134
H1(context.heading)
3235
Div {
3336
P(context.string)
34-
}.id("desc")
37+
}.id(context.desc_id)
3538
H2(context.user.details_heading)
3639
H3(context.user.qualities_heading)
3740
Ul {
3841
context.user.qualities.map({ Li($0) })
39-
}.id("user-qualities")
42+
}.id(context.user.qualities_id)
4043
}
4144
}
4245
})

Benchmarks/Benchmarks/SwiftHTMLKit/SwiftHTMLKit.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ package struct SwiftHTMLKitTests : HTMLGenerator {
1313

1414
package func staticHTML() -> String {
1515
#html([
16+
#head([
17+
#title(["StaticView"])
18+
]),
1619
#body([
1720
#h1(["Swift HTML Benchmarks"])
1821
])
@@ -23,12 +26,12 @@ package struct SwiftHTMLKitTests : HTMLGenerator {
2326
return #html([
2427
#body([
2528
#h1(["\(context.heading)"]),
26-
#div(attributes: [.id("desc")], [
29+
#div(attributes: [.id(context.desc_id)], [
2730
#p(["\(context.string)"])
2831
]),
2932
#h2(["\(context.user.details_heading)"]),
3033
#h3(["\(context.user.qualities_heading)"]),
31-
#ul(attributes: [.id("user-qualities")], ["\(qualities)"])
34+
#ul(attributes: [.id(context.user.qualities_id)], ["\(qualities)"])
3235
])
3336
])
3437
}

Benchmarks/Benchmarks/SwiftHTMLPF/SwiftHTMLPF.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,16 @@ package struct SwiftHTMLPFTests : HTMLGenerator {
1212
package init() {}
1313

1414
package func staticHTML() -> String {
15-
render(.document(.html(.body(.h1("Swift HTML Benchmarks")))))
15+
render(
16+
.document(
17+
.html(
18+
.head(
19+
.title("StaticView")
20+
),
21+
.body(.h1("Swift HTML Benchmarks"))
22+
)
23+
)
24+
)
1625
}
1726

1827
package func dynamicHTML(_ context: HTMLContext) -> String {
@@ -21,10 +30,10 @@ package struct SwiftHTMLPFTests : HTMLGenerator {
2130
.html(
2231
.body(
2332
.h1(.raw(context.heading)),
24-
.div(attributes: [.id("desc")], .p(.raw(context.string))),
33+
.div(attributes: [.id(context.desc_id)], .p(.raw(context.string))),
2534
.h2(.raw(context.user.details_heading)),
2635
.h3(.raw(context.user.qualities_heading)),
27-
.ul(attributes: [.id("user-qualities")], .fragment(context.user.qualities.map({ quality in .li(.raw(quality)) })))
36+
.ul(attributes: [.id(context.user.qualities_id)], .fragment(context.user.qualities.map({ quality in .li(.raw(quality)) })))
2837
)
2938
)
3039
)

Benchmarks/Benchmarks/Swim/Swim.swift

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@ import Utilities
99
import Swim
1010

1111
extension Node {
12-
var rendered: String {
13-
var result = ""
14-
write(to: &result)
15-
return result
12+
var rendered : String {
13+
switch self {
14+
case .element(let name, let attributes, let child):
15+
let attributes_string:String = attributes.isEmpty ? "" : " " + attributes.map({ $0 + "=\"" + $1 + "\"" }).joined(separator: " ")
16+
return (name == "html" ? "<!DOCTYPE html>" : "") + "<" + name + attributes_string + ">" + (child?.rendered ?? "") + "</" + name + ">"
17+
case .text(let string): return string
18+
case .raw(let string): return string
19+
case .comment(let _): return ""
20+
case .documentType(let string): return string
21+
case .fragment(let children): return children.map({ $0.rendered }).joined()
22+
case .trim: return ""
23+
}
1624
}
1725
}
1826

@@ -21,6 +29,9 @@ package struct SwimTests : HTMLGenerator {
2129

2230
package func staticHTML() -> String {
2331
html {
32+
head {
33+
title { "StaticView" }
34+
}
2435
body {
2536
h1 {
2637
"Swift HTML Benchmarks"
@@ -32,12 +43,12 @@ package struct SwimTests : HTMLGenerator {
3243
html {
3344
body {
3445
h1 { context.heading }
35-
div(id: "desc") {
46+
div(id: context.desc_id) {
3647
p { context.string }
3748
}
3849
h2 { context.user.details_heading }
3950
h3 { context.user.qualities_heading }
40-
ul(id: "user-qualities") {
51+
ul(id: context.user.qualities_id) {
4152
context.user.qualities.map({ quality in li { quality} })
4253
}
4354
}

Benchmarks/Benchmarks/UnitTests/UnitTests.swift

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,44 @@
77

88
import Testing
99
import Utilities
10+
import SwiftHTMLKit
1011

12+
import TestElementary
13+
import TestPlot
14+
import TestSwiftHTMLBB
15+
import TestSwiftHTMLKit
16+
import TestSwiftHTMLPF
17+
import TestSwim
18+
import TestToucan
19+
import TestVaporHTMLKit
1120
import TestVaux
1221

1322
struct UnitTests {
14-
@Test func vaux() {
15-
#expect(VauxTests().staticHTML() != "")
23+
let libraries:[String:HTMLGenerator] = [
24+
"BinaryBirds" : SwiftHTMLBBTests(),
25+
"Elementary" : ElementaryTests(),
26+
"Plot" : PlotTests(),
27+
"Pointfreeco" : SwiftHTMLPFTests(),
28+
"SwiftHTMLKit" : SwiftHTMLKitTests(),
29+
"Swim" : SwimTests(),
30+
"VaporHTMLKit" : VaporHTMLKitTests(),
31+
"Vaux" : VauxTests()
32+
]
33+
@Test func staticHTML() {
34+
let expected_value:String = #html([
35+
#head([
36+
#title(["StaticView"])
37+
]),
38+
#body([
39+
#h1(["Swift HTML Benchmarks"])
40+
])
41+
])
42+
for (key, value) in libraries {
43+
var string:String = value.staticHTML()
44+
if key == "Swim" {
45+
string = string.replacingOccurrences(of: "\n", with: "")
46+
}
47+
#expect(string == expected_value, Comment(rawValue: key))
48+
}
1649
}
1750
}

Benchmarks/Benchmarks/Utilities/Utilities.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ package actor Cache {
3535

3636
// MARK: HTMLContext
3737
package struct HTMLContext {
38-
package let heading:String
38+
package let heading:String, desc_id:String
3939
package let string:String, integer:Int, double:Double, float:Float, boolean:Bool
4040
package let now:Date
4141
package let user:User
4242

4343
package init() {
4444
heading = "Dynamic HTML Benchmark"
45+
desc_id = "desc"
4546
// 1 paragraph of lorem ipsum
4647
string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam tempor euismod arcu, sed elementum erat lobortis vel. Fusce nec orci purus. Maecenas a rutrum elit, in pellentesque nisl. Cras nec dapibus turpis. Donec finibus auctor arcu, vehicula maximus eros tincidunt et. Praesent nulla urna, imperdiet quis nunc id, auctor varius justo. Integer fringilla tincidunt lectus, et egestas massa molestie ut. Aliquam at augue pulvinar ante dignissim dignissim a at augue. Donec nisi elit, faucibus a ante a, interdum ultrices lacus. Fusce faucibus odio at est imperdiet, id sodales ipsum hendrerit. Nullam vehicula velit non metus malesuada ornare. Proin consequat id nulla sed porttitor."
4748
integer = 293785
@@ -55,7 +56,7 @@ package struct HTMLContext {
5556
}
5657
}
5758
package struct User {
58-
package let details_heading:String, qualities_heading:String
59+
package let details_heading:String, qualities_heading:String, qualities_id:String
5960

6061
package let id:UInt64, email:String, username:String
6162
package let qualities:[String]
@@ -64,6 +65,7 @@ package struct User {
6465
init() {
6566
details_heading = "User Details"
6667
qualities_heading = "Qualities"
68+
qualities_id = "user-qualities"
6769

6870
id = 63821
6971

Benchmarks/Benchmarks/VaporHTMLKit/VaporHTMLKit.swift

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,25 @@ package struct VaporHTMLKitTests : HTMLGenerator {
1313
let renderer:Renderer
1414
package init() {
1515
renderer = Renderer()
16-
try! renderer.add(layout: SimpleView())
16+
try! renderer.add(layout: StaticView())
1717
try! renderer.add(layout: DynamicView(context: Utilities.HTMLContext()))
1818
}
1919

2020
package func staticHTML() -> String {
21-
return try! renderer.render(layout: SimpleView.self)
21+
return try! renderer.render(layout: StaticView.self)
2222
}
2323
package func dynamicHTML(_ context: Utilities.HTMLContext) -> String {
2424
return try! renderer.render(layout: DynamicView.self, with: context)
2525
}
2626
}
2727

28-
struct SimpleView : View {
28+
struct StaticView : View {
2929
var body : AnyContent {
3030
Document(.html5)
3131
Html {
32+
Head {
33+
Title { "StaticView" }
34+
}
3235
Body {
3336
Heading1 { "Swift HTML Benchmarks" }
3437
}
@@ -46,12 +49,12 @@ struct DynamicView : View {
4649
Heading1 { context.heading }
4750
Div {
4851
P { context.string }
49-
}.id("desc")
52+
}.id(context.desc_id)
5053
Heading2 { context.user.details_heading }
5154
Heading3 { context.user.qualities_heading }
5255
Ul {
5356
context.user.qualities.map({ quality in Li { quality } })
54-
}.id("user-qualities")
57+
}.id(context.user.qualities_id)
5558
}
5659
}
5760
}

Benchmarks/Benchmarks/Vaux/Vaux.swift

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,36 @@
66
//
77

88
import Utilities
9-
import Vaux
9+
@testable import Vaux
1010
import Foundation
1111

1212
extension HTML {
13-
var rendered : String { String(describing: self) }
13+
var render : String {
14+
if let node:HTMLNode = self as? HTMLNode {
15+
return node.rendered
16+
} else if let node:MultiNode = self as? MultiNode {
17+
return node.children.map({ $0.render }).joined()
18+
} else {
19+
return String(describing: self)
20+
}
21+
}
22+
}
23+
24+
extension HTMLNode {
25+
var rendered : String {
26+
guard let tag:String = getTag() else { return String(describing: self) }
27+
var string:String
28+
if tag == "html" {
29+
string = "<!DOCTYPE html>"
30+
} else {
31+
string = ""
32+
}
33+
string += "<" + tag + ">"
34+
if let child = self.child {
35+
string += child.render
36+
}
37+
return string + "</" + tag + ">" // Vaux doesn't take into account void elements
38+
}
1439
}
1540

1641
package struct VauxTests : HTMLGenerator {
@@ -22,12 +47,15 @@ package struct VauxTests : HTMLGenerator {
2247

2348
package func staticHTML() -> String {
2449
html {
50+
head {
51+
title("StaticView")
52+
}
2553
body {
2654
heading(.h1) {
2755
"Swift HTML Benchmarks"
2856
}
2957
}
30-
}.rendered
58+
}.render
3159
}
3260

3361
package func dynamicHTML(_ context: HTMLContext) -> String {
@@ -36,15 +64,15 @@ package struct VauxTests : HTMLGenerator {
3664
heading(.h1) { context.heading }
3765
div {
3866
paragraph { context.string }
39-
}.id("desc")
67+
}.id(context.desc_id)
4068
heading(.h2) { context.user.details_heading }
4169
heading(.h3) { context.user.qualities_heading }
4270
list {
4371
forEach(context.user.qualities) {
4472
listItem(label: $0)
4573
}
46-
}.id("user-qualities")
74+
}.id(context.user.qualities_id)
4775
}
48-
}.rendered
76+
}.render
4977
}
5078
}

0 commit comments

Comments
 (0)