Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dreymonde committed Feb 21, 2018
1 parent cc7be36 commit 1e20038
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 44 deletions.
24 changes: 16 additions & 8 deletions Shallows.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
7C4B84082010D4D700502FB0 /* DiskStorageSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C4B84062010D4D600502FB0 /* DiskStorageSpec.swift */; };
7C4B84092010D4D700502FB0 /* DiskStorageSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C4B84062010D4D600502FB0 /* DiskStorageSpec.swift */; };
7C4B840A2010D4D700502FB0 /* DiskStorageSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C4B84062010D4D600502FB0 /* DiskStorageSpec.swift */; };
7C4B840B2010D4D700502FB0 /* ShallowsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C4B84072010D4D700502FB0 /* ShallowsTests.swift */; };
7C4B840C2010D4D700502FB0 /* ShallowsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C4B84072010D4D700502FB0 /* ShallowsTests.swift */; };
7C4B840D2010D4D700502FB0 /* ShallowsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C4B84072010D4D700502FB0 /* ShallowsTests.swift */; };
7C4B840B2010D4D700502FB0 /* TestExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C4B84072010D4D700502FB0 /* TestExtensions.swift */; };
7C4B840C2010D4D700502FB0 /* TestExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C4B84072010D4D700502FB0 /* TestExtensions.swift */; };
7C4B840D2010D4D700502FB0 /* TestExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C4B84072010D4D700502FB0 /* TestExtensions.swift */; };
7C4B84162010D5F900502FB0 /* Failure.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C4B840E2010D5F800502FB0 /* Failure.swift */; };
7C4B84172010D5F900502FB0 /* Failure.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C4B840E2010D5F800502FB0 /* Failure.swift */; };
7C4B84182010D5F900502FB0 /* Failure.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7C4B840E2010D5F800502FB0 /* Failure.swift */; };
Expand Down Expand Up @@ -109,6 +109,9 @@
7CEC8AC9203D987000393104 /* Deprecated.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CEC8AC7203D987000393104 /* Deprecated.swift */; };
7CEC8ACA203D987000393104 /* Deprecated.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CEC8AC7203D987000393104 /* Deprecated.swift */; };
7CEC8ACB203D987000393104 /* Deprecated.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CEC8AC7203D987000393104 /* Deprecated.swift */; };
7CEC8ACD203D9DDE00393104 /* AdditionalSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CEC8ACC203D9DDE00393104 /* AdditionalSpec.swift */; };
7CEC8ACE203D9DDE00393104 /* AdditionalSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CEC8ACC203D9DDE00393104 /* AdditionalSpec.swift */; };
7CEC8ACF203D9DDE00393104 /* AdditionalSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7CEC8ACC203D9DDE00393104 /* AdditionalSpec.swift */; };
DD7502881C68FEDE006590AF /* Shallows.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 52D6DA0F1BF000BD002C0205 /* Shallows.framework */; };
DD7502921C690C7A006590AF /* Shallows.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 52D6D9F01BEFFFBE002C0205 /* Shallows.framework */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -154,7 +157,7 @@
7C4B83DB2010D48400502FB0 /* WriteOnlyStorage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WriteOnlyStorage.swift; sourceTree = "<group>"; };
7C4B83DD2010D48400502FB0 /* DiskStorage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DiskStorage.swift; sourceTree = "<group>"; };
7C4B84062010D4D600502FB0 /* DiskStorageSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DiskStorageSpec.swift; sourceTree = "<group>"; };
7C4B84072010D4D700502FB0 /* ShallowsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ShallowsTests.swift; sourceTree = "<group>"; };
7C4B84072010D4D700502FB0 /* TestExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestExtensions.swift; sourceTree = "<group>"; };
7C4B840E2010D5F800502FB0 /* Failure.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Failure.swift; sourceTree = "<group>"; };
7C4B840F2010D5F800502FB0 /* Expectation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Expectation.swift; sourceTree = "<group>"; };
7C4B84102010D5F800502FB0 /* Reporter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Reporter.swift; sourceTree = "<group>"; };
Expand All @@ -173,6 +176,7 @@
7CD789C92010F3EC0057F6E8 /* MemoryStorageSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemoryStorageSpec.swift; sourceTree = "<group>"; };
7CEC8AC2203D95E100393104 /* Composition.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Composition.swift; sourceTree = "<group>"; };
7CEC8AC7203D987000393104 /* Deprecated.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Deprecated.swift; sourceTree = "<group>"; };
7CEC8ACC203D9DDE00393104 /* AdditionalSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdditionalSpec.swift; sourceTree = "<group>"; };
AD2FAA261CD0B6D800659CF4 /* Shallows.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Shallows.plist; sourceTree = "<group>"; };
AD2FAA281CD0B6E100659CF4 /* ShallowsTests.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = ShallowsTests.plist; sourceTree = "<group>"; };
DD75027A1C68FCFC006590AF /* Shallows-macOS Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "Shallows-macOS Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -328,10 +332,11 @@
7C4B84312010D68300502FB0 /* XCTest.swift */,
7C4B84062010D4D600502FB0 /* DiskStorageSpec.swift */,
7CD789C92010F3EC0057F6E8 /* MemoryStorageSpec.swift */,
7C4B84072010D4D700502FB0 /* ShallowsTests.swift */,
7CC675ED20335951004557EB /* ResultSpec.swift */,
7CC675F120335CC0004557EB /* ZipSpec.swift */,
7CC675F5203364A7004557EB /* CompositionSpec.swift */,
7CEC8ACC203D9DDE00393104 /* AdditionalSpec.swift */,
7C4B84072010D4D700502FB0 /* TestExtensions.swift */,
);
name = Tests;
path = Tests/ShallowsTests;
Expand Down Expand Up @@ -657,7 +662,8 @@
7C4B84192010D5F900502FB0 /* Expectation.swift in Sources */,
7C4B841C2010D5F900502FB0 /* Reporter.swift in Sources */,
7CC675F220335CC0004557EB /* ZipSpec.swift in Sources */,
7C4B840B2010D4D700502FB0 /* ShallowsTests.swift in Sources */,
7CEC8ACD203D9DDE00393104 /* AdditionalSpec.swift in Sources */,
7C4B840B2010D4D700502FB0 /* TestExtensions.swift in Sources */,
7C4B841F2010D5F900502FB0 /* Context.swift in Sources */,
7CC675F6203364A7004557EB /* CompositionSpec.swift in Sources */,
7C4B84322010D68300502FB0 /* XCTest.swift in Sources */,
Expand Down Expand Up @@ -742,7 +748,8 @@
7C4B841A2010D5F900502FB0 /* Expectation.swift in Sources */,
7C4B841D2010D5F900502FB0 /* Reporter.swift in Sources */,
7CC675F320335CC0004557EB /* ZipSpec.swift in Sources */,
7C4B840C2010D4D700502FB0 /* ShallowsTests.swift in Sources */,
7CEC8ACE203D9DDE00393104 /* AdditionalSpec.swift in Sources */,
7C4B840C2010D4D700502FB0 /* TestExtensions.swift in Sources */,
7C4B84202010D5F900502FB0 /* Context.swift in Sources */,
7CC675F7203364A7004557EB /* CompositionSpec.swift in Sources */,
7C4B84332010D68300502FB0 /* XCTest.swift in Sources */,
Expand All @@ -764,7 +771,8 @@
7C4B841B2010D5F900502FB0 /* Expectation.swift in Sources */,
7C4B841E2010D5F900502FB0 /* Reporter.swift in Sources */,
7CC675F420335CC0004557EB /* ZipSpec.swift in Sources */,
7C4B840D2010D4D700502FB0 /* ShallowsTests.swift in Sources */,
7CEC8ACF203D9DDE00393104 /* AdditionalSpec.swift in Sources */,
7C4B840D2010D4D700502FB0 /* TestExtensions.swift in Sources */,
7C4B84212010D5F900502FB0 /* Context.swift in Sources */,
7CC675F8203364A7004557EB /* CompositionSpec.swift in Sources */,
7C4B84342010D68300502FB0 /* XCTest.swift in Sources */,
Expand Down
8 changes: 4 additions & 4 deletions Sources/Shallows/NSCacheStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ public final class NSCacheStorage<Key : NSObject, Value : AnyObject> : StoragePr
case noValue(Key)
}

public let storage: NSCache<Key, Value>
public let cache: NSCache<Key, Value>
public let storageName: String = "nscache"

public init(storage: NSCache<Key, Value> = NSCache()) {
self.storage = storage
self.cache = storage
}

public func set(_ value: Value, forKey key: Key, completion: @escaping (Result<Void>) -> ()) {
storage.setObject(value, forKey: key)
cache.setObject(value, forKey: key)
completion(.success)
}

public func retrieve(forKey key: Key, completion: @escaping (Result<Value>) -> ()) {
if let object = storage.object(forKey: key) {
if let object = cache.object(forKey: key) {
completion(.success(object))
} else {
completion(.failure(Error.noValue(key)))
Expand Down
64 changes: 32 additions & 32 deletions Sources/Shallows/Zip.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,38 @@

import Dispatch

public func zip<Key, Value1, Value2>(_ lhs: ReadOnlyStorage<Key, Value1>, _ rhs: ReadOnlyStorage<Key, Value2>) -> ReadOnlyStorage<Key, (Value1, Value2)> {
return ReadOnlyStorage(storageName: lhs.storageName + "+" + rhs.storageName, retrieve: { (key, completion) in
let container = CompletionContainer<Result<Value1>, Result<Value2>>() { left, right in
completion(zip(left, right))
}
lhs.retrieve(forKey: key, completion: { container.completeLeft(with: $0) })
rhs.retrieve(forKey: key, completion: { container.completeRight(with: $0) })
})
}

public func zip<Key, Value1, Value2>(_ lhs: WriteOnlyStorage<Key, Value1>, _ rhs: WriteOnlyStorage<Key, Value2>) -> WriteOnlyStorage<Key, (Value1, Value2)> {
return WriteOnlyStorage(storageName: lhs.storageName + "+" + rhs.storageName, set: { (value, key, completion) in
let container = CompletionContainer<Result<Void>, Result<Void>>(completion: { (left, right) in
let zipped = zip(left, right)
switch zipped {
case .success:
completion(.success)
case .failure(let error):
completion(.failure(error))
}
})
lhs.set(value.0, forKey: key, completion: { container.completeLeft(with: $0) })
rhs.set(value.1, forKey: key, completion: { container.completeRight(with: $0) })
})
}

public func zip<Storage1 : StorageProtocol, Storage2 : StorageProtocol>(_ lhs: Storage1, _ rhs: Storage2) -> Storage<Storage1.Key, (Storage1.Value, Storage2.Value)> where Storage1.Key == Storage2.Key {
let readOnlyZipped = zip(lhs.asReadOnlyStorage(), rhs.asReadOnlyStorage())
let writeOnlyZipped = zip(lhs.asWriteOnlyStorage(), rhs.asWriteOnlyStorage())
return Storage(read: readOnlyZipped, write: writeOnlyZipped)
}

fileprivate final class CompletionContainer<Left, Right> {

private var left: Left?
Expand Down Expand Up @@ -62,35 +94,3 @@ public func zip<Value1, Value2>(_ lhs: Result<Value1>, _ rhs: Result<Value2>) ->
return Result.failure(ZippedResultError(left: lhs.error, right: rhs.error))
}
}

public func zip<Key, Value1, Value2>(_ lhs: ReadOnlyStorage<Key, Value1>, _ rhs: ReadOnlyStorage<Key, Value2>) -> ReadOnlyStorage<Key, (Value1, Value2)> {
return ReadOnlyStorage(storageName: lhs.storageName + "+" + rhs.storageName, retrieve: { (key, completion) in
let container = CompletionContainer<Result<Value1>, Result<Value2>>() { left, right in
completion(zip(left, right))
}
lhs.retrieve(forKey: key, completion: { container.completeLeft(with: $0) })
rhs.retrieve(forKey: key, completion: { container.completeRight(with: $0) })
})
}

public func zip<Key, Value1, Value2>(_ lhs: WriteOnlyStorage<Key, Value1>, _ rhs: WriteOnlyStorage<Key, Value2>) -> WriteOnlyStorage<Key, (Value1, Value2)> {
return WriteOnlyStorage(storageName: lhs.storageName + "+" + rhs.storageName, set: { (value, key, completion) in
let container = CompletionContainer<Result<Void>, Result<Void>>(completion: { (left, right) in
let zipped = zip(left, right)
switch zipped {
case .success:
completion(.success)
case .failure(let error):
completion(.failure(error))
}
})
lhs.set(value.0, forKey: key, completion: { container.completeLeft(with: $0) })
rhs.set(value.1, forKey: key, completion: { container.completeRight(with: $0) })
})
}

public func zip<Storage1 : StorageProtocol, Storage2 : StorageProtocol>(_ lhs: Storage1, _ rhs: Storage2) -> Storage<Storage1.Key, (Storage1.Value, Storage2.Value)> where Storage1.Key == Storage2.Key {
let readOnlyZipped = zip(lhs.asReadOnlyStorage(), rhs.asReadOnlyStorage())
let writeOnlyZipped = zip(lhs.asWriteOnlyStorage(), rhs.asWriteOnlyStorage())
return Storage(read: readOnlyZipped, write: writeOnlyZipped)
}
39 changes: 39 additions & 0 deletions Tests/ShallowsTests/AdditionalSpec.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// AdditionalSpec.swift
// Shallows
//
// Created by Олег on 21.02.2018.
// Copyright © 2018 Shallows. All rights reserved.
//

import Shallows

func testAdditional() {

describe("renaming") {
$0.it("renames read-only storage") {
let r1 = ReadOnlyStorage<Int, Int>.empty()
let r2 = r1.renaming(to: "r2")
try expect(r2.storageName) == "r2"
}
$0.it("renames write-only storage") {
let w1 = WriteOnlyStorage<Int, Int>.empty()
let w2 = w1.renaming(to: "w2")
try expect(w2.storageName) == "w2"
}
$0.describe("storage") {
let s1 = Storage<Int, Int>.empty()
let s2 = s1.renaming(to: "s2")
$0.it("renames it") {
try expect(s2.storageName) == "s2"
}
$0.it("renames asReadOnlyStorage") {
try expect(s2.asReadOnlyStorage().storageName) == "s2"
}
$0.it("renames asWriteOnlyStorage") {
try expect(s2.asWriteOnlyStorage().storageName) == "s2"
}
}
}

}
File renamed without changes.
1 change: 1 addition & 0 deletions Tests/ShallowsTests/XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class ShallowsXCTests : XCTestCase {
testResult()
testZip()
testComposition()
testAdditional()
}

}
Expand Down

0 comments on commit 1e20038

Please sign in to comment.