From df9ede25ca888daa5eb691c4244ef434fbeb6718 Mon Sep 17 00:00:00 2001 From: James Walker Date: Sun, 16 Sep 2018 16:52:49 -0400 Subject: [PATCH] swift - hello-world --- swift/hello-world/.gitignore | 4 +++ swift/hello-world/Package.swift | 5 +++ swift/hello-world/README.md | 29 ++++++++++++++++ swift/hello-world/Sources/HelloWorld.swift | 6 ++++ .../HelloWorldTests/HelloWorldTests.swift | 34 +++++++++++++++++++ swift/hello-world/Tests/LinuxMain.swift | 6 ++++ 6 files changed, 84 insertions(+) create mode 100644 swift/hello-world/.gitignore create mode 100644 swift/hello-world/Package.swift create mode 100644 swift/hello-world/README.md create mode 100644 swift/hello-world/Sources/HelloWorld.swift create mode 100644 swift/hello-world/Tests/HelloWorldTests/HelloWorldTests.swift create mode 100644 swift/hello-world/Tests/LinuxMain.swift diff --git a/swift/hello-world/.gitignore b/swift/hello-world/.gitignore new file mode 100644 index 0000000..02c0875 --- /dev/null +++ b/swift/hello-world/.gitignore @@ -0,0 +1,4 @@ +.DS_Store +/.build +/Packages +/*.xcodeproj diff --git a/swift/hello-world/Package.swift b/swift/hello-world/Package.swift new file mode 100644 index 0000000..12d5586 --- /dev/null +++ b/swift/hello-world/Package.swift @@ -0,0 +1,5 @@ +import PackageDescription + +let package = Package( + name: "HelloWorld" +) diff --git a/swift/hello-world/README.md b/swift/hello-world/README.md new file mode 100644 index 0000000..619f137 --- /dev/null +++ b/swift/hello-world/README.md @@ -0,0 +1,29 @@ +# Hello World + +The classical introductory exercise. Just say "Hello, World!". + +["Hello, World!"](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program) is +the traditional first program for beginning programming in a new language +or environment. + +The objectives are simple: + +- Write a function that returns the string "Hello, World!". +- Run the test suite and make sure that it succeeds. +- Submit your solution and check it at the website. + +If everything goes well, you will be ready to fetch your first real exercise. + +## Setup + +Go through the project setup instructions for Xcode using Swift: + +http://exercism.io/languages/swift + + +## Source + +This is an exercise to introduce users to using Exercism [http://en.wikipedia.org/wiki/%22Hello,_world!%22_program](http://en.wikipedia.org/wiki/%22Hello,_world!%22_program) + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/swift/hello-world/Sources/HelloWorld.swift b/swift/hello-world/Sources/HelloWorld.swift new file mode 100644 index 0000000..b27c098 --- /dev/null +++ b/swift/hello-world/Sources/HelloWorld.swift @@ -0,0 +1,6 @@ +//Solution goes in Sources +class HelloWorld { + class func hello(_ name: String = "World") -> String { + return "Hello, \(name)!" + } +} diff --git a/swift/hello-world/Tests/HelloWorldTests/HelloWorldTests.swift b/swift/hello-world/Tests/HelloWorldTests/HelloWorldTests.swift new file mode 100644 index 0000000..7c49d43 --- /dev/null +++ b/swift/hello-world/Tests/HelloWorldTests/HelloWorldTests.swift @@ -0,0 +1,34 @@ +import XCTest +@testable import HelloWorld + +class HelloWorldTests: XCTestCase { + + func testNoName() { + let expected = "Hello, World!" + XCTAssertEqual(HelloWorld.hello(), expected, "When given no name, we should greet the world!") + } + + func testSampleName() { + let expected = "Hello, Alice!" + XCTAssertEqual(HelloWorld.hello("Alice"), expected, "When given 'Alice' we should greet Alice!") + } + + func testOtherSampleName() { + let expected = "Hello, Bob!" + XCTAssertEqual(HelloWorld.hello("Bob"), expected, "When given 'Bob' we should greet Bob!") + } + + func testNoStrangeName() { + let expected = "Hello, !" + XCTAssertEqual(HelloWorld.hello(""), expected, "When given an empty string, it is strange, but should have a space and punctuation") + } + + static var allTests: [(String, (HelloWorldTests) -> () throws -> Void)] { + return [ + ("testNoName", testNoName), + ("testSampleName", testSampleName), + ("testOtherSampleName", testOtherSampleName), + ("testNoStrangeName", testNoStrangeName), + ] + } +} diff --git a/swift/hello-world/Tests/LinuxMain.swift b/swift/hello-world/Tests/LinuxMain.swift new file mode 100644 index 0000000..e1fb7cd --- /dev/null +++ b/swift/hello-world/Tests/LinuxMain.swift @@ -0,0 +1,6 @@ +import XCTest +@testable import HelloWorldTests + +XCTMain([ + testCase(HelloWorldTests.allTests), + ])