Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Swift 6]: Port log-lines #828

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions exercises/concept/log-lines/.docs/hints.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
- The keyword `self` refers to the enum value inside a method's body.

[string-docs]: https://developer.apple.com/documentation/swift/string/
[switch-statement]: https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html#ID129
[enum-raw-values]: https://docs.swift.org/swift-book/LanguageGuide/Enumerations.html#ID149
[switch-statement]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/controlflow/#Switch
[enum-raw-values]: https://docs.swift.org/swift-book/documentation/the-swift-programming-language/enumerations/#Raw-Values
9 changes: 5 additions & 4 deletions exercises/concept/log-lines/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ Next, implement the `LogLevel` initializer to parse the log level of a log line:

```swift
LogLevel("[INF]: File deleted")
// => LogLevel.info
// returns LogLevel.info
LogLevel("Something went wrong!")
// => LogLevel.unknown
// returns LogLevel.unknown
```

## 3. Convert log line to short format

The log level of a log line is quite verbose. To reduce the disk space needed to store the log lines, a short format is developed: `"<ENCODED_LEVEL>:<MESSAGE>"`.
The log level of a log line is quite verbose.
To reduce the disk space needed to store the log lines, a short format is developed: `"<ENCODED_LEVEL>:<MESSAGE>"`.

The encoded log level is simple mapping of a log level to a number:

Expand All @@ -57,5 +58,5 @@ Implement the `shortFormat(message: String) -> String` method that can output th
```swift
let overflow = LogLevel.error
overflow.shortFormat(message: "Stack overflow")
// => "6:Stack overflow"
// returns "6:Stack overflow"
```
44 changes: 22 additions & 22 deletions exercises/concept/log-lines/Package.swift
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
// swift-tools-version:5.3
// swift-tools-version:6.0
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "LogLines",
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "LogLines",
targets: ["LogLines"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "LogLines",
dependencies: []),
.testTarget(
name: "LogLinesTests",
dependencies: ["LogLines"]),
]
name: "LogLines",
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "LogLines",
targets: ["LogLines"])
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "LogLines",
dependencies: []),
.testTarget(
name: "LogLinesTests",
dependencies: ["LogLines"]),
]
)
98 changes: 50 additions & 48 deletions exercises/concept/log-lines/Tests/LogLinesTests/LogLinesTests.swift
Original file line number Diff line number Diff line change
@@ -1,98 +1,100 @@
import XCTest
import Foundation
import Testing

@testable import LogLines

final class LogLinesTests: XCTestCase {
let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false
let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false

@Suite struct LogLinesTests {
@Test("init trace")
func testInitTrace() {
let line = "[TRC]: Line 84 - Console.WriteLine('Hello World');"
XCTAssertEqual(LogLevel(line), LogLevel.trace)
#expect(LogLevel(line) == LogLevel.trace)
}

func testInitDebug() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("init debug", .enabled(if: RUNALL))
func testInitDebug() {
let line = "[DBG]: ; expected"
XCTAssertEqual(LogLevel(line), LogLevel.debug)
#expect(LogLevel(line) == LogLevel.debug)
}

func testInitInfo() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("init info", .enabled(if: RUNALL))
func testInitInfo() {
let line = "[INF]: Timezone changed"
XCTAssertEqual(LogLevel(line), LogLevel.info)
#expect(LogLevel(line) == LogLevel.info)
}

func testInitWarning() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("init warning", .enabled(if: RUNALL))
func testInitWarning() {
let line = "[WRN]: Timezone not set"
XCTAssertEqual(LogLevel(line), LogLevel.warning)
#expect(LogLevel(line) == LogLevel.warning)
}

func testInitError() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("init error", .enabled(if: RUNALL))
func testInitError() {
let line = "[ERR]: Disk full"
XCTAssertEqual(LogLevel(line), LogLevel.error)
#expect(LogLevel(line) == LogLevel.error)
}

func testInitFatal() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("init fatal", .enabled(if: RUNALL))
func testInitFatal() {
let line = "[FTL]: Not enough memory"
XCTAssertEqual(LogLevel(line), LogLevel.fatal)
#expect(LogLevel(line) == LogLevel.fatal)
}

func testInitUnknownEmpty() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("init unknown empty", .enabled(if: RUNALL))
func testInitUnknownEmpty() {
let line = "Something terrible has happened!"
XCTAssertEqual(LogLevel(line), LogLevel.unknown)
#expect(LogLevel(line) == LogLevel.unknown)
}

func testInitUnknownNonStandard() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("init unknown non-standard", .enabled(if: RUNALL))
func testInitUnknownNonStandard() {
let line = "[XYZ]: Gibberish message.. beep boop.."
XCTAssertEqual(LogLevel(line), LogLevel.unknown)
#expect(LogLevel(line) == LogLevel.unknown)
}

func testShortTrace() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("short trace", .enabled(if: RUNALL))
func testShortTrace() {
let message = "Line 13 - int myNum = 42;"
XCTAssertEqual(LogLevel.trace.shortFormat(message: message), "0:Line 13 - int myNum = 42;")
#expect(LogLevel.trace.shortFormat(message: message) == "0:Line 13 - int myNum = 42;")
}

func testShortDebug() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("short debug", .enabled(if: RUNALL))
func testShortDebug() {
let message = "The name 'LogLevel' does not exist in the current context"
XCTAssertEqual(
LogLevel.debug.shortFormat(message: message),
"1:The name 'LogLevel' does not exist in the current context")
#expect(
LogLevel.debug.shortFormat(message: message)
== "1:The name 'LogLevel' does not exist in the current context")
}

func testShortInfo() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("short info", .enabled(if: RUNALL))
func testShortInfo() {
let message = "File moved"
XCTAssertEqual(LogLevel.info.shortFormat(message: message), "4:File moved")
#expect(LogLevel.info.shortFormat(message: message) == "4:File moved")
}

func testShortWarning() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("short warning", .enabled(if: RUNALL))
func testShortWarning() {
let message = "Unsafe password"
XCTAssertEqual(LogLevel.warning.shortFormat(message: message), "5:Unsafe password")
#expect(LogLevel.warning.shortFormat(message: message) == "5:Unsafe password")
}

func testShortError() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("short error", .enabled(if: RUNALL))
func testShortError() {
let message = "Stack overflow"
XCTAssertEqual(LogLevel.error.shortFormat(message: message), "6:Stack overflow")
#expect(LogLevel.error.shortFormat(message: message) == "6:Stack overflow")
}

func testShortFatal() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("short fatal", .enabled(if: RUNALL))
func testShortFatal() {
let message = "Dumping all files"
XCTAssertEqual(LogLevel.fatal.shortFormat(message: message), "7:Dumping all files")
#expect(LogLevel.fatal.shortFormat(message: message) == "7:Dumping all files")
}

func testShortUnknownEmpty() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
@Test("short unknown empty", .enabled(if: RUNALL))
func testShortUnknownEmpty() {
let message = "Wha happon?"
XCTAssertEqual(LogLevel.unknown.shortFormat(message: message), "42:Wha happon?")
#expect(LogLevel.unknown.shortFormat(message: message) == "42:Wha happon?")
}
}