-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from appunite/tests
Tests
- Loading branch information
Showing
9 changed files
with
746 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
] | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.