-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #198 from TelemetryDeck/feature/swift-testing
Migrate from XCTest framework to Swift Testing + get rid of all warnings
- Loading branch information
Showing
7 changed files
with
148 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,41 @@ | ||
@testable import TelemetryDeck | ||
import XCTest | ||
import Testing | ||
#if canImport(CryptoKit) | ||
import CryptoKit | ||
#endif | ||
|
||
final class CryptoHashingTests: XCTestCase { | ||
struct CryptoHashingTests { | ||
#if canImport(CryptoKit) | ||
func testCryptoKitAndCommonCryptoHaveSameDigestStringResults() { | ||
@Test | ||
func cryptoKitAndCommonCryptoHaveSameDigestStringResults() { | ||
let stringToHash = "how do i get cowboy paint off a dog ." | ||
let dataToHash = stringToHash.data(using: .utf8)! | ||
|
||
let expectedDigestString = "5b8fab7cf45fcece0e99a05950611b7b355917e4fb6daa73fd3d7590764fa53b" | ||
|
||
XCTAssertEqual(expectedDigestString, CryptoHashing.sha256(string: stringToHash, salt: "")) | ||
XCTAssertEqual(expectedDigestString, CryptoHashing.commonCryptoSha256(strData: dataToHash)) | ||
#expect(expectedDigestString == CryptoHashing.sha256(string: stringToHash, salt: "")) | ||
#expect(expectedDigestString == CryptoHashing.commonCryptoSha256(strData: dataToHash)) | ||
|
||
// even though we already test if we can import CryptoKit, somehow this still fails on iOS 12, | ||
// so we're gating it to iOS 13 et al. | ||
if #available(watchOS 7, iOS 13, macOS 10.14, tvOS 13, *) { | ||
// calling directly to prove that CryptoKit produces same reult, as ``sha256(str:)`` can fall back, | ||
XCTAssertEqual(expectedDigestString, SHA256.hash(data: dataToHash).compactMap { String(format: "%02x", $0) }.joined()) | ||
#expect(expectedDigestString == SHA256.hash(data: dataToHash).compactMap { String(format: "%02x", $0) }.joined()) | ||
} | ||
} | ||
|
||
func testSaltedResultsAreDifferentFromUnsaltedResults() { | ||
@Test | ||
func saltedResultsAreDifferentFromUnsaltedResults() { | ||
let stringToHash = "how do i get cowboy paint off a dog ." | ||
let salt = "q8wMvgVW3LzGCRQiLSLk" | ||
let expectedDigestString = "d46208db801b09cf055fedd7ae0390e9797fc00d1bcdcb3589ea075ca157e0d6" | ||
|
||
let secondSalt = "x21MTSq3MRSmLjVFsYIe" | ||
let expectedSecondDigestString = "acb027bb031c0f73de26c6b8d0441d9c98449d582a538014c44ca49b4c299aa8" | ||
|
||
XCTAssertEqual(expectedDigestString, CryptoHashing.sha256(string: stringToHash, salt: salt)) | ||
XCTAssertEqual(expectedSecondDigestString, CryptoHashing.sha256(string: stringToHash, salt: secondSalt)) | ||
XCTAssertNotEqual(CryptoHashing.sha256(string: stringToHash, salt: salt), CryptoHashing.sha256(string: stringToHash, salt: secondSalt)) | ||
#expect(expectedDigestString == CryptoHashing.sha256(string: stringToHash, salt: salt)) | ||
#expect(expectedSecondDigestString == CryptoHashing.sha256(string: stringToHash, salt: secondSalt)) | ||
#expect(CryptoHashing.sha256(string: stringToHash, salt: salt) != CryptoHashing.sha256(string: stringToHash, salt: secondSalt)) | ||
} | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,56 @@ | ||
@testable import TelemetryDeck | ||
import XCTest | ||
import Testing | ||
|
||
final class LogHandlerTests: XCTestCase { | ||
actor LogHandlerTests { | ||
var counter: Int = 0 | ||
var lastLevel: LogHandler.LogLevel? | ||
|
||
func testLogHandler_stdoutLogLevelDefined() { | ||
XCTAssertEqual(LogHandler.standard(.error).logLevel, .error) | ||
@Test | ||
func logHandler_stdoutLogLevelDefined() { | ||
#expect(LogHandler.standard(.error).logLevel == .error) | ||
} | ||
|
||
func testLogHandler_logLevelRespected() { | ||
|
||
@Test | ||
func logHandler_logLevelRespected() async throws { | ||
let handler = LogHandler(logLevel: .info) { _, _ in | ||
self.counter += 1 | ||
Task { | ||
await self.increment() | ||
} | ||
} | ||
|
||
XCTAssertEqual(counter, 0) | ||
#expect(counter == 0) | ||
|
||
handler.log(.debug, message: "") | ||
XCTAssertEqual(counter, 0) | ||
try await Task.sleep(nanoseconds: 10_000_000) // 10 milliseconds | ||
#expect(counter == 0) | ||
|
||
handler.log(.info, message: "") | ||
XCTAssertEqual(counter, 1) | ||
try await Task.sleep(nanoseconds: 10_000_000) // 10 milliseconds | ||
#expect(counter == 1) | ||
|
||
handler.log(.error, message: "") | ||
XCTAssertEqual(counter, 2) | ||
try await Task.sleep(nanoseconds: 10_000_000) // 10 milliseconds | ||
#expect(counter == 2) | ||
} | ||
|
||
func testLogHandler_defaultLogLevel() { | ||
|
||
@Test | ||
func logHandler_defaultLogLevel() async throws { | ||
let handler = LogHandler(logLevel: .debug) { level, _ in | ||
self.lastLevel = level | ||
Task { | ||
await self.setLastLevel(level) | ||
} | ||
} | ||
|
||
handler.log(message: "") | ||
XCTAssertEqual(lastLevel, .info) | ||
try await Task.sleep(nanoseconds: 10_000_000) // 10 milliseconds | ||
#expect(lastLevel == .info) | ||
} | ||
|
||
private func increment() { | ||
self.counter += 1 | ||
} | ||
|
||
private func setLastLevel(_ lastLevel: LogHandler.LogLevel?) { | ||
self.lastLevel = lastLevel | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.