// // 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 value: TestModel? // MARK: Test cases func testValue_whenNoDefaultValue_andEmptyStorage() throws { // GIVEN let keychainStorage = TestKeychainStorage_withNoDefaultValue_andEmptyStorage() // WHEN value = keychainStorage.keychainValue // THEN XCTAssertNil(value) } func testValue_whenDefaultValue_andEmptyStorage() throws { // GIVEN let keychainStorage = TestKeychainStorage_withDefaultValue_andEmptyStorage() // WHEN value = keychainStorage.keychainValue // THEN XCTAssertNotNil(value) } func testValue_whenNoDefaultValue_andValueInStorage() throws { // GIVEN let keychainStorage = TestKeychainStorage_withNoDefaultValue_andValueInStorage() // WHEN value = keychainStorage.keychainValue // THEN XCTAssertNotNil(value) } func testValue_whenNoDefaultValue_andNoValueInStorage() throws { // GIVEN let keychainStorage = TestKeychainStorage_withNoDefaultValue_andNoValueInStorage() // WHEN value = keychainStorage.keychainValue // THEN XCTAssertNil(value) } } // MARK: - Test classes private final class TestKeychainStorage_withNoDefaultValue_andEmptyStorage { @KeychainStorage( key: .Keys.someKey, keychain: KeychainStorageMock() ) var keychainValue: TestModel? } private final class TestKeychainStorage_withDefaultValue_andEmptyStorage { @KeychainStorage( key: .Keys.someKey, defaultValue: TestModel(), keychain: KeychainStorageMock() ) var keychainValue: TestModel? } private final class TestKeychainStorage_withNoDefaultValue_andValueInStorage { @KeychainStorage( key: .Keys.someKey, keychain: KeychainStorageMock(storage: [ .Keys.someKey: try! JSONEncoder().encode(TestModel()) ]) ) var keychainValue: TestModel? } private final class TestKeychainStorage_withNoDefaultValue_andNoValueInStorage { @KeychainStorage( key: .Keys.someKey, keychain: KeychainStorageMock(storage: [ .Keys.someOtherKey: try! JSONEncoder().encode(TestModel()) ]) ) var keychainValue: TestModel? }