feat: Moves core functionality into it's own library.

This commit is contained in:
2024-12-05 09:59:30 -05:00
parent 33356d8648
commit e7bbbec7c2
24 changed files with 464 additions and 387 deletions

View File

@@ -0,0 +1,136 @@
@testable import CliDocCore
@preconcurrency import Rainbow
import Testing
@Suite("CliDocCore Tests")
struct CliDocCoreTests {
// Ensure that rainbow is setup, for test comparisons to work properly.
let setupRainbow: Bool = {
Rainbow.enabled = true
Rainbow.outputTarget = .console
return true
}()
@Test
func testGroup() {
#expect(setupRainbow)
let group = Group {
"foo"
"bar"
if setupRainbow {
"baz".color(.blue)
}
}
#expect(group.render() == "foobar\("baz".blue)")
}
@Test
func testHStack() {
#expect(setupRainbow)
let stack = HStack {
"foo"
"bar"
}
#expect(stack.render() == "foo bar")
}
@Test
func testVStack() {
#expect(setupRainbow)
let stack = VStack {
"foo"
"bar"
}
#expect(stack.render() == """
foo
bar
""")
}
@Test
func testOptionalTextNode() {
let someNode = String?.some("string")
#expect(someNode.body.render() == "string")
#expect(someNode.render() == "string")
let noneNode = String?.none
#expect(noneNode.body.render() == "")
#expect(noneNode.render() == "")
}
@Test
func testArrayTextNode() {
let array = ["foo", " ", "bar"]
#expect(array.body.render() == "foo bar")
#expect(array.render() == "foo bar")
}
@Test(
arguments: SectionArg.arguments
)
func testSection(arg: SectionArg) {
#expect(setupRainbow)
printIfNotEqual(arg.section.render(), arg.expected)
#expect(arg.section.render() == arg.expected)
}
struct SectionArg: @unchecked Sendable {
let section: any TextNode
let expected: String
static var arguments: [Self] {
[
.init(
section: Section {
"Content"
} header: {
"Header"
} footer: {
"Footer"
},
expected: """
Header
Content
Footer
"""
),
.init(
section: Section {
"Content"
} footer: {
"Footer"
},
expected: """
Content
Footer
"""
),
.init(
section: Section {
"Content"
} header: {
"Header"
},
expected: """
Header
Content
"""
)
]
}
}
}
@discardableResult
func printIfNotEqual(_ lhs: String, _ rhs: String) -> Bool {
guard lhs == rhs else {
print(lhs)
print(rhs)
return false
}
return true
}

View File

@@ -1,154 +1,79 @@
@testable import CliDoc
@testable import CliDocCore
@preconcurrency import Rainbow
import Testing
// Ensure that rainbow is setup, for test comparisons to work properly.
let setupRainbow: Bool = {
Rainbow.enabled = true
Rainbow.outputTarget = .console
return true
}()
@Suite("CliDoc tests")
struct CliDocTests {
// Ensure that rainbow is setup, for test comparisons to work properly.
let setupRainbow: Bool = {
Rainbow.enabled = true
Rainbow.outputTarget = .console
return true
}()
@Test
func testGroup() {
#expect(setupRainbow)
let group = Group {
"foo"
"bar"
@Test
func testNote() {
#expect(setupRainbow)
let note = Note(content: "Some note.")
let expected = """
\("NOTE:".yellow.bold) Some note.
"""
#expect(note.render() == expected)
}
#expect(group.render() == "foobar")
}
@Test
func testHStack() {
#expect(setupRainbow)
let stack = HStack {
"foo"
"bar"
@Test
func testExamples() {
#expect(setupRainbow)
let examples = ExampleSection(
"Examples:",
label: "Some common usage examples.",
examples: [("First", "ls -lah"), ("Second", "find . -name foo")]
)
let expected = """
\("Examples:".yellow.bold)\(" ")\("Some common usage examples.".italic)
\("First".green.bold)
$ \("ls -lah".italic)
\("Second".green.bold)
$ \("find . -name foo".italic)
"""
let result = printIfNotEqual(examples.render(), expected)
#expect(result)
}
#expect(stack.render() == "foo bar")
}
@Test
func testVStack() {
#expect(setupRainbow)
let stack = VStack {
"foo"
"bar"
}
#expect(stack.render() == """
foo
bar
""")
}
@Test
func testExamplesWithCustomExampleOnlyStyle() {
#expect(setupRainbow)
let examples = ExampleSection(
"Examples:",
label: "Some common usage examples.",
examples: [("First", "ls -lah"), ("Second", "find . -name foo")]
)
// .exampleStyle(CustomExampleOnlyStyle())
@Test
func testNote() {
#expect(setupRainbow)
let note = Note(content: "Some note.")
let expected = """
\("NOTE:".yellow.bold) Some note.
"""
#expect(note.render() == expected)
}
let expected = """
\("Examples:".applyingStyle(.bold).applyingColor(.yellow)) \("Some common usage examples.".italic)
@Test
func testExamples() {
#expect(setupRainbow)
let examples = ExampleSection(
"Examples:",
label: "Some common usage examples.",
examples: [("First", "ls -lah"), ("Second", "find . -name foo")]
)
\("First".red)
$ \("ls -lah".italic)
let expected = """
\("Examples:".yellow.bold)\(" ")\("Some common usage examples.".italic)
\("Second".red)
$ \("find . -name foo".italic)
"""
let result = printIfNotEqual(
examples.exampleStyle(CustomExampleOnlyStyle()).render(),
expected
)
#expect(result)
\("First".green.bold)
$ \("ls -lah".italic)
\("Second".green.bold)
$ \("find . -name foo".italic)
"""
let result = printIfNotEqual(examples.render(), expected)
#expect(result)
}
@Test
func testExamplesWithCustomExampleOnlyStyle() {
#expect(setupRainbow)
let examples = ExampleSection(
"Examples:",
label: "Some common usage examples.",
examples: [("First", "ls -lah"), ("Second", "find . -name foo")]
)
.exampleStyle(CustomExampleOnlyStyle())
let expected = """
\("Examples:".applyingStyle(.bold).applyingColor(.yellow)) \("Some common usage examples.".italic)
\("First".red)
$ \("ls -lah".italic)
\("Second".red)
$ \("find . -name foo".italic)
"""
let result = printIfNotEqual(examples.render(), expected)
#expect(result)
}
@Test(
arguments: SectionArg.arguments
)
func testSection(arg: SectionArg) {
#expect(setupRainbow)
printIfNotEqual(arg.section.render(), arg.expected)
#expect(arg.section.render() == arg.expected)
}
struct SectionArg: @unchecked Sendable {
let section: any TextNode
let expected: String
static var arguments: [Self] {
[
.init(
section: Section {
"Header"
} content: {
"Content"
} footer: {
"Footer"
},
expected: """
Header
Content
Footer
"""
),
.init(
section: Section {
"Content"
} footer: {
"Footer"
},
expected: """
Content
Footer
"""
),
.init(
section: Section {
"Header"
} content: {
"Content"
},
expected: """
Header
Content
"""
)
]
let result2 = printIfNotEqual(
examples.style(.custom).render(),
expected
)
#expect(result2)
}
}
@@ -162,6 +87,12 @@ func printIfNotEqual(_ lhs: String, _ rhs: String) -> Bool {
return true
}
extension ExampleSectionStyle where Self == DefaultExampleSectionStyle<CustomExampleOnlyStyle> {
static var custom: Self {
.default(exampleStyle: CustomExampleOnlyStyle())
}
}
struct CustomExampleOnlyStyle: ExampleStyle {
func render(content: ExampleConfiguration) -> some TextNode {
VStack(spacing: 2) {