Skip to content

Commit

Permalink
Merge pull request #6 from micheltlutz/feature/new-tags
Browse files Browse the repository at this point in the history
New Tags Span and Button
  • Loading branch information
micheltlutz authored Nov 28, 2024
2 parents 3b7b7fc + 183cf59 commit 59ea759
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ To add WingedSwift to your project, add the following line to your `Package.swif

```swift
dependencies: [
.package(url: "https://github.com/micheltlutz/Winged-Swift.git", from: "1.1.0")
.package(url: "https://github.com/micheltlutz/Winged-Swift.git", from: "1.2.2")
]
```

Expand Down
15 changes: 15 additions & 0 deletions Sources/WingedSwift/HTML/Commons/Button.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Foundation

/// Represents a <button> HTML tag.
public class Button: HTMLTag {
/// Initializes a new <button> tag.
///
/// - Parameters:
/// - attributes: The attributes of the <button> tag.
/// - children: The children tags of the <button> tag.
/// - content: The content of the <button> tag.
public init(attributes: [Attribute] = [], children: [HTMLTag] = [], content: String? = nil) {
super.init("button", attributes: attributes, children: children, content: content)
self.attributes.append(Attribute(key: "type", value: "button"))
}
}
14 changes: 14 additions & 0 deletions Sources/WingedSwift/HTML/Commons/Span.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Foundation

/// Represents a <span> HTML tag.
public class Span: HTMLTag {
/// Initializes a new <span> tag.
///
/// - Parameters:
/// - attributes: The attributes of the <span> tag.
/// - children: The children tags of the <span> tag.
/// - content: The content of the <span> tag.
public init(attributes: [Attribute] = [], children: [HTMLTag] = [], content: String? = nil) {
super.init("span", attributes: attributes, children: children, content: content)
}
}
2 changes: 1 addition & 1 deletion Sources/WingedSwift/WingedSwift.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation

public enum WingedSwift: String {
case VERSION = "1.1.0"
case VERSION = "1.2.2"
case AUTHOR = "Michel Anderson Lutz Teixeira"
case CONTACT = "https://micheltlutz.me"
}
60 changes: 60 additions & 0 deletions Tests/WingedSwiftTests/HTMLBuilderTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import XCTest
@testable import WingedSwift

final class HTMLBuilderTests: XCTestCase {
func testHTMLBuilderCreatesRootHTMLTag() {
// Given
let expectedTagName = "html"

// When
let document = html {
HTMLTag("body")
HTMLTag("head")
}

// Then
XCTAssertEqual(document.name, expectedTagName, "Root tag should be 'html'")
XCTAssertEqual(document.children.count, 2, "Root tag should have two children.")
XCTAssertEqual(document.children[0].name, "body", "First child should be 'body'")
XCTAssertEqual(document.children[1].name, "head", "Second child should be 'head'")
}

func testHTMLBuilderHandlesOptional() {
// Given
let optionalComponent: HTMLTag? = HTMLTag("optional")

// When
let document = html {
HTMLBuilder.buildOptional(optionalComponent)
}

// Then
XCTAssertEqual(document.name, "html", "Root tag should be 'html'")
XCTAssertEqual(document.children.count, 1, "Root tag should have one child.")
XCTAssertEqual(document.children[0].name, "optional", "Child tag should be 'optional'")
}

func testHTMLBuilderHandlesEitherFirst() {
// When
let document = html {
HTMLBuilder.buildEither(first: HTMLTag("first"))
}

// Then
XCTAssertEqual(document.name, "html", "Root tag should be 'html'")
XCTAssertEqual(document.children.count, 1, "Root tag should have one child.")
XCTAssertEqual(document.children[0].name, "first", "Child tag should be 'first'")
}

func testHTMLBuilderHandlesEitherSecond() {
// When
let document = html {
HTMLBuilder.buildEither(second: HTMLTag("second"))
}

// Then
XCTAssertEqual(document.name, "html", "Root tag should be 'html'")
XCTAssertEqual(document.children.count, 1, "Root tag should have one child.")
XCTAssertEqual(document.children[0].name, "second", "Child tag should be 'second'")
}
}
33 changes: 33 additions & 0 deletions Tests/WingedSwiftTests/HTMLTagTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,37 @@ final class HTMLTagTests: XCTestCase {
"""
XCTAssertEqual(document.render(), expected)
}

func testHTMLSpan() {
let htmlTag = Span()
.addAttribute(Attribute(key: "class", value: "text"))
.setContent("Hello, World!")

let expected = "<span class=\"text\">Hello, World!</span>"
XCTAssertEqual(htmlTag.render(), expected)
}

func testHTMLButton() {
let document = html {
Button(attributes: [Attribute(key: "class", value: "button-class")])
}

let expected = """
<html><button class="button-class" type="button"></button></html>
"""
XCTAssertEqual(document.render(), expected)
}

func testHTMLButtonChildren() {
let document = html {
Button(attributes: [Attribute(key: "class", value: "button-class")], children: [
Span(attributes: [Attribute(key: "class", value: "icon-bar")])
])
}

let expected = """
<html><button class="button-class" type="button"><span class="icon-bar"></span></button></html>
"""
XCTAssertEqual(document.render(), expected)
}
}

0 comments on commit 59ea759

Please sign in to comment.