Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
27 changes: 27 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
root = true

# Apply to all files
[*]
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
charset = utf-8
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

# In the interest of keeping code samples compact in the limited space of the
# printed book, I reduced the indentation from the Apple default of 4 spaces to
# just 2.
[*.swift]
indent_size = 2

[{*.h,*.m}]
indent_size = 4

# Ruby specific rules
[{*.rb,Fastfile,Gemfile}]
indent_style = space
indent_size = 2
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
xcuserdata/
.build/
DerivedData/
8 changes: 3 additions & 5 deletions 01-introduction/fizz_buzz.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env xcrun swift
#!/usr/bin/env swift

func fizzBuzz(_ number: Int) -> String {
let divisibleBy3 = number % 3 == 0
Expand All @@ -7,7 +7,7 @@ func fizzBuzz(_ number: Int) -> String {
case (false, false): return "\(number)"
case (true, false): return "fizz"
case (false, true): return "buzz"
case (true, true): return "fizz buzz"
case (true, true): return "fizz-buzz"
}
}

Expand All @@ -23,7 +23,7 @@ func testFizzBuzz() {
test(value: fizzBuzz(1), matches: "1")
test(value: fizzBuzz(3), matches: "fizz")
test(value: fizzBuzz(5), matches: "buzz")
test(value: fizzBuzz(15), matches: "fizz buzz")
test(value: fizzBuzz(15), matches: "fizz-buzz")
}

func main() {
Expand All @@ -42,7 +42,5 @@ func main() {
print(fizzBuzz(number))
}

// TODO: explain why we need to either run main or test? Alternatively, extract
// the tests in a dedicated file?
main()
// testFizzBuzz()
2 changes: 1 addition & 1 deletion 01-introduction/hello_world.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env xcrun swift
#!/usr/bin/env swift

func main() {
guard CommandLine.argc > 1 else {
Expand Down
8 changes: 4 additions & 4 deletions 01-introduction/test_fizz_buzz.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#!/usr/bin/env xcrun swift
#!/usr/bin/env swift

// There is currently (5 years later!) no way for a Swift script to import
// another script.
//
// What follows is a bit of a hack where we test the fizz-buzz implementation
// from fizz_buzz.swift by calling the script directly.
//
// I find this acceptable in the context of providing an harness for showing
// I find this acceptable in the context of providing a harness for showing
// how to write code that tests other code as part of the introduction of
// Test-Driven Development in Swift (https://tddinswift.com). It definitely is
// not a scalabe solution for testing Swift scripts, but, again, that's not
// not a scalable solution for testing Swift scripts, but, again, that's not
// what the examples in the book introduction are about.

import Foundation
Expand Down Expand Up @@ -49,7 +49,7 @@ func testFizzBuzz() {
test(value: fizzBuzz(1), matches: "1")
test(value: fizzBuzz(3), matches: "fizz")
test(value: fizzBuzz(5), matches: "buzz")
test(value: fizzBuzz(15), matches: "fizz buzz")
test(value: fizzBuzz(15), matches: "fizz-buzz")
}

testFizzBuzz()
8 changes: 8 additions & 0 deletions 03-getting-started-with-tdd/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "2600"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES"
buildArchitectures = "Automatic">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "LeapYear"
BuildableName = "LeapYear"
BlueprintName = "LeapYear"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "ProductsManager"
BuildableName = "ProductsManager"
BlueprintName = "ProductsManager"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "LeapYearTests"
BuildableName = "LeapYearTests"
BlueprintName = "LeapYearTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "ProductsManagerTests"
BuildableName = "ProductsManagerTests"
BlueprintName = "ProductsManagerTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "LeapYear"
BuildableName = "LeapYear"
BlueprintName = "LeapYear"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "2600"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES"
buildArchitectures = "Automatic">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "LeapYear"
BuildableName = "LeapYear"
BlueprintName = "LeapYear"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "LeapYear"
BuildableName = "LeapYear"
BlueprintName = "LeapYear"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "2600"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES"
buildArchitectures = "Automatic">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "ProductsManager"
BuildableName = "ProductsManager"
BlueprintName = "ProductsManager"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<TestPlans>
<TestPlanReference
reference = "container:Tests/ProductsManagerTests/ProductsManager.xctestplan"
default = "YES">
</TestPlanReference>
</TestPlans>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "ProductsManager"
BuildableName = "ProductsManager"
BlueprintName = "ProductsManager"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
33 changes: 33 additions & 0 deletions 03-getting-started-with-tdd/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// swift-tools-version: 5.10

import PackageDescription

let package = Package(
name: "LeapYear",
products: [
.library(
name: "LeapYear",
targets: ["LeapYear"]
),
.library(
name: "ProductsManager",
targets: ["ProductsManager"]
),
],
targets: [
.target(
name: "LeapYear"
),
.testTarget(
name: "LeapYearTests",
dependencies: ["LeapYear"]
),
.target(
name: "ProductsManager"
),
.testTarget(
name: "ProductsManagerTests",
dependencies: ["ProductsManager"]
),
]
)
5 changes: 5 additions & 0 deletions 03-getting-started-with-tdd/Sources/LeapYear/LeapYear.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
func isLeap(_ year: Int) -> Bool {
guard year % 400 != 0 else { return true }
guard year % 100 != 0 else { return false }
return year % 4 == 0
}
Loading