@@ -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
4445Test Suite ' All tests' 
4546Test Suite ' swift-libraryPackageTests.xctest' 
@@ -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