Skip to content

Commit

Permalink
add tests for @LocalStorageEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaskollmer committed Jan 28, 2025
1 parent 6ca9e97 commit 17f9e47
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// This source file is part of the Stanford Spezi open-source project
//
// SPDX-FileCopyrightText: 2025 Stanford University and the project authors (see CONTRIBUTORS.md)
//
// SPDX-License-Identifier: MIT
//


import SpeziLocalStorage
import SwiftUI


extension LocalStorageKeys {

Check failure on line 14 in Tests/UITests/TestApp/LocalStorageTests/LocalStorageLiveUpdateTestView.swift

View workflow job for this annotation

GitHub Actions / SwiftLint / SwiftLint / SwiftLint

File Types Order Violation: An 'extension' should not be placed amongst the file type(s) 'supporting_type' (file_types_order)
static let number = LocalStorageKey<Int>("number")
}


struct LocalStorageLiveUpdateTestView: View {

Check failure on line 19 in Tests/UITests/TestApp/LocalStorageTests/LocalStorageLiveUpdateTestView.swift

View workflow job for this annotation

GitHub Actions / SwiftLint / SwiftLint / SwiftLint

File Types Order Violation: A 'main_type' should not be placed amongst the file type(s) 'supporting_type' (file_types_order)
@Environment(LocalStorage.self) private var localStorage

var body: some View {
Form {
RowView()
Section {
ForEach(0..<5) { number in
Button("\(number)") {
try! localStorage.store(number, for: .number)

Check failure on line 28 in Tests/UITests/TestApp/LocalStorageTests/LocalStorageLiveUpdateTestView.swift

View workflow job for this annotation

GitHub Actions / SwiftLint / SwiftLint / SwiftLint

Force Try Violation: Force tries should be avoided (force_try)
}
}
}
}
}
}


struct RowView: View {
@LocalStorageEntry(.number)
private var number

Check failure on line 39 in Tests/UITests/TestApp/LocalStorageTests/LocalStorageLiveUpdateTestView.swift

View workflow job for this annotation

GitHub Actions / SwiftLint / SwiftLint / SwiftLint

Attributes Violation: Attributes should be on their own lines in functions and types, but on the same line as variables and imports (attributes)

var body: some View {
LabeledContent("number", value: number.map(String.init) ?? "")
}
}

3 changes: 3 additions & 0 deletions Tests/UITests/TestApp/SpeziStorageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ import XCTestApp

enum SpeziStorageTests: String, TestAppTests {
case localStorage = "Local Storage"
case localStorageLiveUpdate = "Local Storage (Live Update)"
case secureStorage = "Secure Storage"


func view(withNavigationPath path: Binding<NavigationPath>) -> some View {
switch self {
case .localStorage:
LocalStorageTestsView()
case .localStorageLiveUpdate:
LocalStorageLiveUpdateTestView()
case .secureStorage:
SecureStorageTestsView()
}
Expand Down
17 changes: 15 additions & 2 deletions Tests/UITests/TestAppUITests/LocalStorageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,22 @@ final class LocalStorageTests: XCTestCase {
func testLocalStorage() throws {
let app = XCUIApplication()
app.launch()

app.buttons["Local Storage"].tap()

XCTAssertTrue(app.staticTexts["Passed"].waitForExistence(timeout: 2))
}


@MainActor
func testLocalStorageLiveUpdates() async throws {
let app = XCUIApplication()
app.launch()
app.buttons["Local Storage (Live Update)"].tap()
try await Task.sleep(for: .seconds(0.5))

let numbers = (0..<17).map { _ in Int.random(in: 0..<5) }
for number in numbers {
app.buttons["\(number)"].tap()
XCTAssertTrue(app.staticTexts.matching(NSPredicate(format: "label MATCHES %@", "number.*\(number)")).element.waitForExistence(timeout: 0.5))
}
}
}
4 changes: 4 additions & 0 deletions Tests/UITests/UITests.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
2F2D338729DE52EA00081B1D /* SpeziStorageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2F2D338629DE52EA00081B1D /* SpeziStorageTests.swift */; };
2F6D139A28F5F386007C25D6 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 2F6D139928F5F386007C25D6 /* Assets.xcassets */; };
2FA7382C290ADFAA007ACEB9 /* TestApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2FA7382B290ADFAA007ACEB9 /* TestApp.swift */; };
80F81BC22D492A2100F513C3 /* LocalStorageLiveUpdateTestView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 80F81BC12D492A2100F513C3 /* LocalStorageLiveUpdateTestView.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -47,6 +48,7 @@
2F6D13AC28F5F386007C25D6 /* TestAppUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = TestAppUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
2FA7382B290ADFAA007ACEB9 /* TestApp.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestApp.swift; sourceTree = "<group>"; };
2FB0758A299DDB9000C0B37F /* TestApp.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; path = TestApp.xctestplan; sourceTree = "<group>"; };
80F81BC12D492A2100F513C3 /* LocalStorageLiveUpdateTestView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalStorageLiveUpdateTestView.swift; sourceTree = "<group>"; };
971B61432B9849C100C0B0E2 /* TestApp.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = TestApp.entitlements; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -84,6 +86,7 @@
isa = PBXGroup;
children = (
2F2D336929DE0E7900081B1D /* LocalStorageTestsView.swift */,
80F81BC12D492A2100F513C3 /* LocalStorageLiveUpdateTestView.swift */,
2F2D336A29DE0E7900081B1D /* LocalStorageTests.swift */,
);
path = LocalStorageTests;
Expand Down Expand Up @@ -255,6 +258,7 @@
2FA7382C290ADFAA007ACEB9 /* TestApp.swift in Sources */,
2F2D336B29DE0E7900081B1D /* SecureStorageTestsView.swift in Sources */,
2F2D336C29DE0E7900081B1D /* SecureStorageTests.swift in Sources */,
80F81BC22D492A2100F513C3 /* LocalStorageLiveUpdateTestView.swift in Sources */,
2F2D338729DE52EA00081B1D /* SpeziStorageTests.swift in Sources */,
2F2D336E29DE0E7900081B1D /* LocalStorageTests.swift in Sources */,
);
Expand Down

0 comments on commit 17f9e47

Please sign in to comment.