// // KeychainStorage+InitTests.swift // KeychainStorageTests // // Created by Javier Cicchelli on 11/12/2022. // Copyright © 2022 Röck+Cöde. All rights reserved. // import Foundation import KeychainStorage import XCTest final class KeychainStorageInitTests: XCTestCase { // MARK: Properties private var keychainStorage: TestKeychyainStorage! private var value: TestModel? // MARK: Test cases func testValue_whenNoDefaultValue_andEmptyStorage() throws { // GIVEN keychainStorage = TestKeychainStorage_withNoDefaultValue_andEmptyStorage() // WHEN value = keychainStorage.keychainValue // THEN XCTAssertNil(value) } func testValue_whenDefaultValue_andEmptyStorage() throws { // GIVEN keychainStorage = TestKeychainStorage_withDefaultValue_andEmptyStorage() // WHEN value = keychainStorage.keychainValue // THEN XCTAssertNotNil(value) } func testValue_whenNoDefaultValue_andValueInStorage() throws { // GIVEN keychainStorage = TestKeychainStorage_withNoDefaultValue_andValueInStorage() // WHEN value = keychainStorage.keychainValue // THEN XCTAssertNotNil(value) } func testValue_whenNoDefaultValue_andNoValueInStorage() throws { // GIVEN keychainStorage = TestKeychainStorage_withNoDefaultValue_andNoValueInStorage() // WHEN value = keychainStorage.keychainValue // THEN XCTAssertNil(value) } } // MARK: - Test protocols private protocol TestKeychyainStorage { var keychainValue: TestModel? { get set } } // MARK: - Test structs private struct TestKeychainStorage_withNoDefaultValue_andEmptyStorage: TestKeychyainStorage { @KeychainStorage( key: .Keys.someKey, keychain: KeychainStorageMock() ) var keychainValue: TestModel? } private struct TestKeychainStorage_withDefaultValue_andEmptyStorage: TestKeychyainStorage { @KeychainStorage( key: .Keys.someKey, defaultValue: TestModel(), keychain: KeychainStorageMock() ) var keychainValue: TestModel? } private struct TestKeychainStorage_withNoDefaultValue_andValueInStorage: TestKeychyainStorage { @KeychainStorage( key: .Keys.someKey, keychain: KeychainStorageMock(storage: [ .Keys.someKey: try! JSONEncoder().encode(TestModel()) ]) ) var keychainValue: TestModel? } private struct TestKeychainStorage_withNoDefaultValue_andNoValueInStorage: TestKeychyainStorage { @KeychainStorage( key: .Keys.someKey, keychain: KeychainStorageMock(storage: [ .Keys.someOtherKey: try! JSONEncoder().encode(TestModel()) ]) ) var keychainValue: TestModel? }