Skip to content

Commit

Permalink
Merge pull request #3 from appunite/tests
Browse files Browse the repository at this point in the history
Tests
  • Loading branch information
SzymonMrozek authored Feb 7, 2018
2 parents 77ee30f + dbbe843 commit 3202fe3
Show file tree
Hide file tree
Showing 9 changed files with 746 additions and 4 deletions.
20 changes: 20 additions & 0 deletions Stefan.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
objects = {

/* Begin PBXBuildFile section */
AD06B8EE202758BF000A1736 /* ItemsLoadableStateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD06B8ED202758BF000A1736 /* ItemsLoadableStateTests.swift */; };
AD0C13962012B6A800CEA0D0 /* ReloadingType.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD0C13952012B6A800CEA0D0 /* ReloadingType.swift */; };
AD127C35202719FE00486A0A /* StefanDifferTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD127C34202719FE00486A0A /* StefanDifferTests.swift */; };
AD127C3720271A4100486A0A /* Fruit.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD127C3620271A4100486A0A /* Fruit.swift */; };
AD127C3920271A5700486A0A /* FruitStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD127C3820271A5700486A0A /* FruitStorage.swift */; };
AD46DDF3200E015A00D0A5B8 /* Stefan_iOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AD46DDE9200E015A00D0A5B8 /* Stefan_iOS.framework */; };
AD46DDF8200E015A00D0A5B8 /* StefanTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD46DDF7200E015A00D0A5B8 /* StefanTests.swift */; };
AD46DDFA200E015A00D0A5B8 /* Stefan.h in Headers */ = {isa = PBXBuildFile; fileRef = AD46DDEC200E015A00D0A5B8 /* Stefan.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -41,6 +45,7 @@
AD46DE4B200E06D500D0A5B8 /* FruitStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD46DE4A200E06D500D0A5B8 /* FruitStorage.swift */; };
AD46DE4E200E06F700D0A5B8 /* Fruit.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD46DE4D200E06F700D0A5B8 /* Fruit.swift */; };
AD46DE53200E143100D0A5B8 /* FruitTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD46DE52200E143100D0A5B8 /* FruitTableViewCell.swift */; };
AD5E9D55202B0AE300D0AD2B /* StefanTestController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD5E9D54202B0AE300D0AD2B /* StefanTestController.swift */; };
AD8ACAF020109B1B00CBDD0A /* Differ.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AD8ACAEF20109B1B00CBDD0A /* Differ.framework */; };
ADB93944202466D80085590A /* SectionatedStefanDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADB93943202466D80085590A /* SectionatedStefanDelegate.swift */; };
ADFB2E05202534440002A1FF /* UIView + LoadableStatePlaceholderPresentable.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADFB2E04202534440002A1FF /* UIView + LoadableStatePlaceholderPresentable.swift */; };
Expand All @@ -65,7 +70,11 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
AD06B8ED202758BF000A1736 /* ItemsLoadableStateTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ItemsLoadableStateTests.swift; sourceTree = "<group>"; };
AD0C13952012B6A800CEA0D0 /* ReloadingType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReloadingType.swift; sourceTree = "<group>"; };
AD127C34202719FE00486A0A /* StefanDifferTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StefanDifferTests.swift; sourceTree = "<group>"; };
AD127C3620271A4100486A0A /* Fruit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Fruit.swift; sourceTree = "<group>"; };
AD127C3820271A5700486A0A /* FruitStorage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FruitStorage.swift; sourceTree = "<group>"; };
AD46DDE9200E015A00D0A5B8 /* Stefan_iOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Stefan_iOS.framework; sourceTree = BUILT_PRODUCTS_DIR; };
AD46DDEC200E015A00D0A5B8 /* Stefan.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Stefan.h; sourceTree = "<group>"; };
AD46DDED200E015A00D0A5B8 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -104,6 +113,7 @@
AD46DE4A200E06D500D0A5B8 /* FruitStorage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FruitStorage.swift; sourceTree = "<group>"; };
AD46DE4D200E06F700D0A5B8 /* Fruit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Fruit.swift; sourceTree = "<group>"; };
AD46DE52200E143100D0A5B8 /* FruitTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FruitTableViewCell.swift; sourceTree = "<group>"; };
AD5E9D54202B0AE300D0AD2B /* StefanTestController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StefanTestController.swift; sourceTree = "<group>"; };
AD8ACAEF20109B1B00CBDD0A /* Differ.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Differ.framework; path = Carthage/Build/iOS/Differ.framework; sourceTree = "<group>"; };
ADB93943202466D80085590A /* SectionatedStefanDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SectionatedStefanDelegate.swift; sourceTree = "<group>"; };
ADFB2E04202534440002A1FF /* UIView + LoadableStatePlaceholderPresentable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIView + LoadableStatePlaceholderPresentable.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -177,8 +187,13 @@
AD46DDF6200E015A00D0A5B8 /* StefanTests */ = {
isa = PBXGroup;
children = (
AD127C3620271A4100486A0A /* Fruit.swift */,
AD127C3820271A5700486A0A /* FruitStorage.swift */,
AD46DDF7200E015A00D0A5B8 /* StefanTests.swift */,
AD46DDF9200E015A00D0A5B8 /* Info.plist */,
AD127C34202719FE00486A0A /* StefanDifferTests.swift */,
AD06B8ED202758BF000A1736 /* ItemsLoadableStateTests.swift */,
AD5E9D54202B0AE300D0AD2B /* StefanTestController.swift */,
);
path = StefanTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -460,6 +475,11 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
AD5E9D55202B0AE300D0AD2B /* StefanTestController.swift in Sources */,
AD06B8EE202758BF000A1736 /* ItemsLoadableStateTests.swift in Sources */,
AD127C3920271A5700486A0A /* FruitStorage.swift in Sources */,
AD127C3720271A4100486A0A /* Fruit.swift in Sources */,
AD127C35202719FE00486A0A /* StefanDifferTests.swift in Sources */,
AD46DDF8200E015A00D0A5B8 /* StefanTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
3 changes: 2 additions & 1 deletion Stefan.xcodeproj/xcshareddata/xcschemes/Stefan-iOS.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
language = ""
shouldUseLaunchSchemeArgsEnv = "YES">
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
<Testables>
<TestableReference
skipped = "NO">
Expand Down
23 changes: 23 additions & 0 deletions Stefan/Enums/ItemReloadingResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,26 @@ public enum ItemReloadingResult<ItemType: Equatable> {
case itemsAndPlaceholder(oldItems: [ItemType], newItems: [ItemType])

}

extension ItemReloadingResult: Equatable {

public static func == (lhs: ItemReloadingResult<ItemType>, rhs: ItemReloadingResult<ItemType>) -> Bool {

switch(lhs, rhs) {

case (.none, .none), (.placeholder, .placeholder):
return true
case (.items(let lOldItems, let lNewItems), .items(let rOldItems, let rNewItems)):
return lOldItems == rOldItems && lNewItems == rNewItems
case (.placeholderAndItems(let lOldItems, let lNewItems), .placeholderAndItems(let rOldItems, let rNewItems)):
return lOldItems == rOldItems && lNewItems == rNewItems
case (.itemsAndPlaceholder(let lOldItems, let lNewItems), .itemsAndPlaceholder(let rOldItems, let rNewItems)):
return lOldItems == rOldItems && lNewItems == rNewItems
default:
return false
}
}



}
37 changes: 37 additions & 0 deletions StefanTests/Fruit.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// Fruit.swift
// StefanTests
//
// Created by Szymon Mrozek on 04.02.2018.
// Copyright © 2018 AppUnite. All rights reserved.
//

import Foundation

enum FruitSize {

case small
case medium
case big
}

struct Fruit {

let name: String
let size: FruitSize

init(name: String, size: FruitSize) {
self.name = name
self.size = size
}

}

extension Fruit: Equatable {

static func == (lhs: Fruit, rhs: Fruit) -> Bool {
return lhs.name == rhs.name &&
lhs.size == rhs.size
}

}
55 changes: 55 additions & 0 deletions StefanTests/FruitStorage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// FruitStorage.swift
// StefanTests
//
// Created by Szymon Mrozek on 04.02.2018.
// Copyright © 2018 AppUnite. All rights reserved.
//

import Foundation

struct FruitStorage {

static var smallFruits: [Fruit] {
let fruitSize: FruitSize = .small

return [
Fruit(name: "Blackberry", size: fruitSize),
Fruit(name: "Grape", size: fruitSize),
Fruit(name: "Blackcurrant", size: fruitSize),
Fruit(name: "Gooseberry", size: fruitSize),
Fruit(name: "Raspberry", size: fruitSize),
Fruit(name: "Strawberry", size: fruitSize),
Fruit(name: "Cherry", size: fruitSize),
Fruit(name: "Plum", size: fruitSize)
]
}

static var mediumFruits: [Fruit] {
let fruitSize: FruitSize = .medium

return [
Fruit(name: "Apple", size: fruitSize),
Fruit(name: "Peach", size: fruitSize),
Fruit(name: "Pear", size: fruitSize),
Fruit(name: "Kiwi", size: fruitSize),
Fruit(name: "Maracuja", size: fruitSize),
Fruit(name: "Guava", size: fruitSize)
]
}

static var bigFruits: [Fruit] {
let fruitSize: FruitSize = .big

return [
Fruit(name: "Banana", size: fruitSize),
Fruit(name: "Mango", size: fruitSize),
Fruit(name: "Pineapple", size: fruitSize),
Fruit(name: "Grapefruit", size: fruitSize),
Fruit(name: "Watermelon", size: fruitSize),
Fruit(name: "Coconut", size: fruitSize),
Fruit(name: "Papaya", size: fruitSize)
]
}

}
197 changes: 197 additions & 0 deletions StefanTests/ItemsLoadableStateTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
//
// ItemsLoadableStateTests.swift
// StefanTests
//
// Created by Szymon Mrozek on 04.02.2018.
// Copyright © 2018 AppUnite. All rights reserved.
//

import XCTest

@testable import Stefan_iOS

class ItemsLoadableStateTests: XCTestCase {

enum TestError: Error {
case someError
}

override func setUp() {
super.setUp()
}

public func testItemsCount() {

let items = FruitStorage.smallFruits

var state = ItemsLoadableState<Fruit>.loaded(items: items)

let notEmptyItemsCount = items.count

XCTAssertEqual(state.itemsCount, notEmptyItemsCount)

state = .noContent

XCTAssertEqual(state.itemsCount, 0)
}

public func testGrabbingItemsInLoadedStateCorrectly() {

let state = ItemsLoadableState<Fruit>.loaded(items: FruitStorage.bigFruits)

do {
_ = try state.items()
} catch {
XCTFail("Should not throw error")
}
}

public func testGrabbingItemsInLoadedStateWith0Items() {

let state = ItemsLoadableState<Fruit>.loaded(items: [])

do {
_ = try state.items()
XCTFail("Should not return items without error")
} catch let error {
guard let stateError = error as? ItemsLoadableState<Fruit>.ItemsLoadableStateError, stateError == ItemsLoadableState<Fruit>.ItemsLoadableStateError.zeroItemsInLoadedState else {
XCTFail("Wrong error thrown")
return
}
}
}

public func testGrabbingItemsInRefreshingStateCorrectly() {

let state = ItemsLoadableState<Fruit>.refreshing(silent: true, items: FruitStorage.mediumFruits)

do {
_ = try state.items()
} catch {
XCTFail("Should not throw error")
}
}

public func testGrabbingItemsInSilentRefreshState() {

let state = ItemsLoadableState<Fruit>.refreshing(silent: false, items: FruitStorage.bigFruits)

do {
_ = try state.items()
XCTFail("Should not return items without error")
} catch let error {
guard let stateError = error as? ItemsLoadableState<Fruit>.ItemsLoadableStateError, stateError == ItemsLoadableState<Fruit>.ItemsLoadableStateError.wrongStateForReadingItems else {
XCTFail("Wrong error thrown")
return
}
}
}

public func testGrabbingItemsInWrongStates() {

let idleState = ItemsLoadableState<Fruit>.idle
let noContentState = ItemsLoadableState<Fruit>.noContent
let loadingState = ItemsLoadableState<Fruit>.loading

do {
_ = try idleState.items()
_ = try noContentState.items()
_ = try loadingState.items()
XCTFail("Should not return items without error")
} catch let error {
guard let stateError = error as? ItemsLoadableState<Fruit>.ItemsLoadableStateError, stateError == ItemsLoadableState<Fruit>.ItemsLoadableStateError.wrongStateForReadingItems else {
XCTFail("Wrong error thrown")
return
}
}
}

public func testInitWithItemsExplicity() {

let items = FruitStorage.mediumFruits

let state = ItemsLoadableState<Fruit>(items)

XCTAssertEqual(state, ItemsLoadableState<Fruit>.loaded(items: items))
}

public func testInitWithNoItems() {

let state = ItemsLoadableState<Fruit>([])
XCTAssertEqual(state, ItemsLoadableState<Fruit>.noContent)
}

public func testStatesComparingNoItems() {

var lState = ItemsLoadableState<Fruit>.idle
var rState = ItemsLoadableState<Fruit>.idle
XCTAssertEqual(lState, rState)


lState = ItemsLoadableState<Fruit>.loading
rState = ItemsLoadableState<Fruit>.loading
XCTAssertEqual(lState, rState)


lState = ItemsLoadableState<Fruit>.noContent
rState = ItemsLoadableState<Fruit>.noContent
XCTAssertEqual(lState, rState)


lState = ItemsLoadableState<Fruit>.error(TestError.someError)
rState = ItemsLoadableState<Fruit>.error(TestError.someError)
XCTAssertEqual(lState, rState)


lState = ItemsLoadableState<Fruit>.idle
rState = ItemsLoadableState<Fruit>.loading
XCTAssertNotEqual(lState, rState)

}

public func testStatesComparingRefreshing() {

let items = FruitStorage.smallFruits

var lState = ItemsLoadableState<Fruit>.refreshing(silent: true, items: items)
var rState = ItemsLoadableState<Fruit>.refreshing(silent: true, items: items)
XCTAssertEqual(lState, rState)


lState = ItemsLoadableState<Fruit>.refreshing(silent: false, items: items)
rState = ItemsLoadableState<Fruit>.refreshing(silent: false, items: items)
XCTAssertEqual(lState, rState)


lState = ItemsLoadableState<Fruit>.refreshing(silent: false, items: items)
rState = ItemsLoadableState<Fruit>.refreshing(silent: true, items: items)
XCTAssertNotEqual(lState, rState)


lState = ItemsLoadableState<Fruit>.refreshing(silent: true, items: [])
rState = ItemsLoadableState<Fruit>.refreshing(silent: true, items: items)
XCTAssertNotEqual(lState, rState)

}

public func testStatesComparingLoaded() {

let items = FruitStorage.smallFruits

var lState = ItemsLoadableState<Fruit>.loaded(items: items)
var rState = ItemsLoadableState<Fruit>.loaded(items: items)
XCTAssertEqual(lState, rState)


lState = ItemsLoadableState<Fruit>.loaded(items: [])
rState = ItemsLoadableState<Fruit>.loaded(items: items)
XCTAssertNotEqual(lState, rState)


lState = ItemsLoadableState<Fruit>.loaded(items: items)
rState = ItemsLoadableState<Fruit>.loaded(items: items + FruitStorage.mediumFruits)
XCTAssertNotEqual(lState, rState)

}

}
Loading

0 comments on commit 3202fe3

Please sign in to comment.