Skip to content

Commit 25d2b7d

Browse files
committed
wip
1 parent 5d9d532 commit 25d2b7d

File tree

2 files changed

+65
-6
lines changed

2 files changed

+65
-6
lines changed

getting-started/cli-tool/index.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ This line means that we can now use the `Figlet` module that the `swift-figlet`
9494

9595
## A small application
9696

97-
Now let’s write a small application with our new dependency. In our `main.swift`, add the following code:
97+
Now let’s write a small application with our new dependency. In our `hello_swift.swift`, add the following code:
9898

99-
```swift
99+
~~~swift
100100
import Figlet // from the previous step
101101

102102
@main
@@ -106,7 +106,7 @@ struct FigletTool {
106106
figlet.say("Hello, Swift!")
107107
}
108108
}
109-
```
109+
~~~
110110

111111
Once we save that, we can run our application with `swift run`
112112
Assuming everything went well, you should see your application print this to the screen:
@@ -157,7 +157,7 @@ let package = Package(
157157

158158
We can now import the argument parsing module provided by `swift-argument-parser` and use it in our application
159159

160-
```swift
160+
~~~swift
161161
import ArgumentParser
162162
import Figlet
163163

@@ -171,7 +171,7 @@ struct FigletTool: ParsableCommand {
171171
figlet.say(self.input)
172172
}
173173
}
174-
```
174+
~~~
175175

176176
For more information about how [swift-argument-parser](https://github.com/apple/swift-argument-parser) parses command line options, see [swift-argument-parser documentation](https://github.com/apple/swift-argument-parser) documentation.
177177

getting-started/library/index.md

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ We can run the tests by running `swift test` in our terminal.
4040

4141
~~~bash
4242
❯ swift test
43+
Building for debugging...
4344
[3/3] Linking swift-libraryPackageTests
4445
Test Suite 'All tests' started at 2023-01-03 10:57:52.659
4546
Test Suite 'swift-libraryPackageTests.xctest' started at 2023-01-03 10:57:52.660
@@ -56,4 +57,62 @@ Test Suite 'All tests' passed at 2023-01-03 10:57:52.664.
5657

5758
## A small library
5859

59-
Now let’s write a small library
60+
Now let’s write a small library.
61+
Replace the example content of `swift-library.swift` with the following code:
62+
63+
~~~swift
64+
import Foundation
65+
66+
struct Email {
67+
public init(_ emailString: String) throws {
68+
let regex = #"^\S+@\S+\.\S+$"#
69+
70+
guard let _ = emailString.range(of: regex, options: .regularExpression) else {
71+
throw InvalidEmailError(email: emailString)
72+
}
73+
}
74+
}
75+
76+
private struct InvalidEmailError: Error {
77+
let email: String
78+
}
79+
80+
~~~
81+
82+
Now lets add a unit test for this strongly types Email API.
83+
Replace the example content of `swift_libraryTests.swift` with the following code:
84+
85+
~~~swift
86+
@testable import swift_library
87+
import XCTest
88+
89+
final class swift_libraryTests: XCTestCase {
90+
func testEmail() throws {
91+
let email = try Email("[email protected]")
92+
XCTAssertEqual(email.description, "[email protected]")
93+
94+
XCTAssertThrowsError(try Email("invalid"))
95+
}
96+
}
97+
~~~
98+
99+
Once we save that, we can run our application with `swift run`
100+
Assuming everything went well, we can run the tests successfully again:
101+
102+
~~~no-highlight
103+
❯ swift test
104+
Building for debugging...
105+
[3/3] Linking swift-libraryPackageTests
106+
Build complete! (0.84s)
107+
Test Suite 'All tests' started at 2023-01-03 16:22:45.070
108+
Test Suite 'swift-libraryPackageTests.xctest' started at 2023-01-03 16:22:45.071
109+
Test Suite 'swift_libraryTests' started at 2023-01-03 16:22:45.071
110+
Test Case '-[swift_libraryTests.swift_libraryTests testEmail]' started.
111+
Test Case '-[swift_libraryTests.swift_libraryTests testEmail]' passed (0.005 seconds).
112+
Test Suite 'swift_libraryTests' passed at 2023-01-03 16:22:45.076.
113+
Executed 1 test, with 0 failures (0 unexpected) in 0.005 (0.005) seconds
114+
Test Suite 'swift-libraryPackageTests.xctest' passed at 2023-01-03 16:22:45.076.
115+
Executed 1 test, with 0 failures (0 unexpected) in 0.005 (0.005) seconds
116+
Test Suite 'All tests' passed at 2023-01-03 16:22:45.076.
117+
Executed 1 test, with 0 failures (0 unexpected) in 0.005 (0.007) seconds
118+
~~~

0 commit comments

Comments
 (0)