Skip to content

Commit

Permalink
Increase test coverage (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
billp committed Dec 29, 2021
1 parent 038b176 commit baae89f
Show file tree
Hide file tree
Showing 14 changed files with 289 additions and 52 deletions.
8 changes: 4 additions & 4 deletions Source/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ import Foundation
/// - max: The upper bound of interval.
public typealias MockDelayType = (min: TimeInterval, max: TimeInterval)

/// A configuration class that can be used with Environment, RouteConfiguration and Request.
/// A configuration class that can be used with Environment, Router, Route and Request.
/// A configuration object follows the following rules:
/// 1. When a Configuration object is passed to a Environment,
/// each Router (with its routes) will inherit this configuration.
/// 2. When a Configuration object is passed to Router, all its routes will inherit this configuration.
/// 1. When a Configuration object is passed to an Environment,
/// each Router with its routes and requests will inherit this configuration.
/// 2. When a Configuration object is passed to Router, all its routes and requests will inherit this configuration.
public final class Configuration {
// MARK: Public properties

Expand Down
4 changes: 0 additions & 4 deletions Source/Extensions/Data+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,4 @@ public extension Data {
}
return nil
}

internal func toString() -> String? {
return String(data: self, encoding: .utf8)
}
}
4 changes: 0 additions & 4 deletions Source/Extensions/URLRequest+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,4 @@ extension URLRequest {

return command.joined(separator: " \\\n\t")
}

init?(curlString: String) {
return nil
}
}
8 changes: 3 additions & 5 deletions Source/MultipartFormDataPartType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,15 @@ public enum MultipartFormDataPartType {
/// - value: The value of the multipart/form-data parameter.
case value(value: String)

/// Data case.
/// Data case with filename and content-type.
/// - Parameters
/// - data: The data to upload of the multipart/form-data parameter.
/// - filename: The filename value of the multipart/form-data parameter.
/// - contentType: The Content-Type of the multipart/form-data parameter.
case data(data: Data, filename: String?, contentType: String?)

/// URL case.
/// File URL case.
/// - Parameters
/// - file: The file URL that contains the data that will be uploaded.
/// - filename: The filename value of the multipart/form-data parameter.
/// - contentType: The Content-Type of the multipart/form-data parameter.
/// - url: The file URL that contains the data that will be uploaded.
case url(_ url: URL)
}
2 changes: 1 addition & 1 deletion Source/Operation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class Operation: Foundation.Operation {
_executing = executing
}

func finish(_ finished: Bool) {
func finished(_ finished: Bool) {
_finished = finished
}
}
14 changes: 7 additions & 7 deletions Source/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public final class Request: Operation {

/// Set executing to true in case it is not started
if !_executing {
_executing = true
executing(true)
}

dataTask?.cancel()
Expand Down Expand Up @@ -334,8 +334,8 @@ public final class Request: Operation {
queue.beforeEachRequestCallback?(self)
initializeInterceptorsChainIfNeeded()

_executing = true
_finished = false
executing(true)
finished(false)
startedAt = Date()

Log.logRequest(request: self,
Expand Down Expand Up @@ -442,8 +442,8 @@ public final class Request: Operation {
error: nil)
}

self._executing = false
self._finished = true
executing(false)
finished(true)

self.queue.afterOperationFinished(request: self,
data: data,
Expand All @@ -466,8 +466,8 @@ public final class Request: Operation {
self.queue.cancelAllOperations()
}

self._executing = false
self._finished = true
executing(false)
finished(true)

self.queue.afterOperationFinished(request: self,
data: data,
Expand Down
2 changes: 1 addition & 1 deletion Source/SessionTaskFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal class SessionTaskFactory {
return nil
}

/// Create mock response if needed
/// Create mocked response if needed
if request.shouldMockResponse() {
// return fake session
return URLSession(configuration: URLSession.shared.configuration)
Expand Down
3 changes: 1 addition & 2 deletions Source/SwiftUI/Image.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ final public class ImageLoader: ObservableObject {
/// Image is a SwiftUI component for downloading images.
@available(iOS 13.0, *)
public struct Image: View {
/// :no-doc
@ObservedObject public var imageLoader: ImageLoader
@ObservedObject internal var imageLoader: ImageLoader
@State var image = ImageType()

/// Main body
Expand Down
15 changes: 8 additions & 7 deletions TermiNetwork.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
F3116AA92460A06600DFE453 /* RouteProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3116AA72460A06600DFE453 /* RouteProtocol.swift */; };
F3116AAA2460A06600DFE453 /* RequestMiddlewareProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3116AA82460A06600DFE453 /* RequestMiddlewareProtocol.swift */; };
F3116AB62460A09E00DFE453 /* UIImageView+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3116AAC2460A09E00DFE453 /* UIImageView+Extensions.swift */; };
F3116AB72460A09E00DFE453 /* String+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3116AAD2460A09E00DFE453 /* String+Extensions.swift */; };
F3116AB82460A09E00DFE453 /* Request+Mock.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3116AAE2460A09E00DFE453 /* Request+Mock.swift */; };
F3116AB92460A09E00DFE453 /* URLRequest+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3116AAF2460A09E00DFE453 /* URLRequest+Extensions.swift */; };
F3116ABA2460A09E00DFE453 /* Request+FileOperations.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3116AB12460A09E00DFE453 /* Request+FileOperations.swift */; };
Expand Down Expand Up @@ -64,6 +63,7 @@
F355608F257527C40047D46D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F355607B257527C40047D46D /* Assets.xcassets */; };
F35B533A24422FAE00C1853A /* TestConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = F35B533924422FAE00C1853A /* TestConfiguration.swift */; };
F35B533E2443325400C1853A /* MockData.bundle in Resources */ = {isa = PBXBuildFile; fileRef = F35B533D2443325400C1853A /* MockData.bundle */; };
F362657A25C34FBB00CE4C5C /* TestCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = F362657925C34FBB00CE4C5C /* TestCache.swift */; };
F363508225894AC6002AF5DE /* FileUploader.swift in Sources */ = {isa = PBXBuildFile; fileRef = F363507F25894A80002AF5DE /* FileUploader.swift */; };
F37014B32598FCC80022B7D6 /* UnauthorizedInterceptor.swift in Sources */ = {isa = PBXBuildFile; fileRef = F37014B22598FCC80022B7D6 /* UnauthorizedInterceptor.swift */; };
F373779225C4B4720075EFA3 /* Reachability.swift in Sources */ = {isa = PBXBuildFile; fileRef = F373779125C4B4720075EFA3 /* Reachability.swift */; };
Expand Down Expand Up @@ -103,7 +103,7 @@
F38F34A225896ED500D5BB17 /* FileUploaderUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = F38F34A125896ED500D5BB17 /* FileUploaderUtils.swift */; };
F397EC9F244F74360082E12D /* CryptoMiddleware.swift in Sources */ = {isa = PBXBuildFile; fileRef = F397EC9D244F65730082E12D /* CryptoMiddleware.swift */; };
F397ECA2244F76330082E12D /* EncryptedModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = F397ECA0244F76300082E12D /* EncryptedModel.swift */; };
F398575225AC945A00DCCAAE /* TestMockRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = F398575125AC945A00DCCAAE /* TestMockRequest.swift */; };
F398575225AC945A00DCCAAE /* TestMockedRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = F398575125AC945A00DCCAAE /* TestMockedRequest.swift */; };
F3A0BC0F25769385005139D5 /* DemoEnvironment.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3A0BC07257692E6005139D5 /* DemoEnvironment.swift */; };
F3A0BC1825769434005139D5 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3A0BC12257693D2005139D5 /* AppDelegate.swift */; };
F3A0BC1C25769453005139D5 /* CityRoute.swift in Sources */ = {isa = PBXBuildFile; fileRef = F3A0BC1B25769453005139D5 /* CityRoute.swift */; };
Expand Down Expand Up @@ -171,7 +171,6 @@
F3116AA72460A06600DFE453 /* RouteProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RouteProtocol.swift; sourceTree = "<group>"; };
F3116AA82460A06600DFE453 /* RequestMiddlewareProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RequestMiddlewareProtocol.swift; sourceTree = "<group>"; };
F3116AAC2460A09E00DFE453 /* UIImageView+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIImageView+Extensions.swift"; sourceTree = "<group>"; };
F3116AAD2460A09E00DFE453 /* String+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+Extensions.swift"; sourceTree = "<group>"; };
F3116AAE2460A09E00DFE453 /* Request+Mock.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Request+Mock.swift"; sourceTree = "<group>"; };
F3116AAF2460A09E00DFE453 /* URLRequest+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "URLRequest+Extensions.swift"; sourceTree = "<group>"; };
F3116AB12460A09E00DFE453 /* Request+FileOperations.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Request+FileOperations.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -216,6 +215,7 @@
F3556082257527C40047D46D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
F35B533924422FAE00C1853A /* TestConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestConfiguration.swift; sourceTree = "<group>"; };
F35B533D2443325400C1853A /* MockData.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; path = MockData.bundle; sourceTree = "<group>"; };
F362657925C34FBB00CE4C5C /* TestCache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestCache.swift; sourceTree = "<group>"; };
F363507F25894A80002AF5DE /* FileUploader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileUploader.swift; sourceTree = "<group>"; };
F37014B22598FCC80022B7D6 /* UnauthorizedInterceptor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnauthorizedInterceptor.swift; sourceTree = "<group>"; };
F373779125C4B4720075EFA3 /* Reachability.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Reachability.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -258,7 +258,7 @@
F39474D725C8A1730084CB25 /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = System/Library/Frameworks/SystemConfiguration.framework; sourceTree = SDKROOT; };
F397EC9D244F65730082E12D /* CryptoMiddleware.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CryptoMiddleware.swift; sourceTree = "<group>"; };
F397ECA0244F76300082E12D /* EncryptedModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EncryptedModel.swift; sourceTree = "<group>"; };
F398575125AC945A00DCCAAE /* TestMockRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestMockRequest.swift; sourceTree = "<group>"; };
F398575125AC945A00DCCAAE /* TestMockedRequest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestMockedRequest.swift; sourceTree = "<group>"; };
F3A0BC07257692E6005139D5 /* DemoEnvironment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DemoEnvironment.swift; sourceTree = "<group>"; };
F3A0BC12257693D2005139D5 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
F3A0BC1B25769453005139D5 /* CityRoute.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CityRoute.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -358,7 +358,6 @@
isa = PBXGroup;
children = (
F3116AB02460A09E00DFE453 /* Operations */,
F3116AAD2460A09E00DFE453 /* String+Extensions.swift */,
F3116AAF2460A09E00DFE453 /* URLRequest+Extensions.swift */,
F3116AB32460A09E00DFE453 /* Request+Middleware.swift */,
F3116AAE2460A09E00DFE453 /* Request+Mock.swift */,
Expand Down Expand Up @@ -574,11 +573,12 @@
F3363E6425A6395800699E73 /* TestRequest.swift */,
F3850C86243D103700F7CC13 /* TestTNErrors.swift */,
F35B533924422FAE00C1853A /* TestConfiguration.swift */,
F398575125AC945A00DCCAAE /* TestMockRequest.swift */,
F398575125AC945A00DCCAAE /* TestMockedRequest.swift */,
F32C3D12243E499E004497BC /* TestPinning.swift */,
F387B0F6257958EB00A8C0C7 /* TestTransformers.swift */,
F3A4B14B2596963C004812B6 /* TestInterceptors.swift */,
F3354D7725C737F600E02BC3 /* TestReachability.swift */,
F362657925C34FBB00CE4C5C /* TestCache.swift */,
F3AA5146256EF53000D64D31 /* TestHelpers.swift */,
);
path = Tests;
Expand Down Expand Up @@ -981,7 +981,7 @@
buildActionMask = 2147483647;
files = (
F35B533A24422FAE00C1853A /* TestConfiguration.swift in Sources */,
F398575225AC945A00DCCAAE /* TestMockRequest.swift in Sources */,
F398575225AC945A00DCCAAE /* TestMockedRequest.swift in Sources */,
F3850C93243D103700F7CC13 /* TestTNErrors.swift in Sources */,
F3850C8E243D103700F7CC13 /* TestHeaders.swift in Sources */,
F3850C87243D103700F7CC13 /* TestExtensions.swift in Sources */,
Expand All @@ -1003,6 +1003,7 @@
F3AF556925987256000E01D8 /* DoNothingInterceptor.swift in Sources */,
F32BB57B25A8ADCE00E6E096 /* StatusCodeTransformer.swift in Sources */,
F3354D7825C737F600E02BC3 /* TestReachability.swift in Sources */,
F362657A25C34FBB00CE4C5C /* TestCache.swift in Sources */,
F32C3D13243E499E004497BC /* TestPinning.swift in Sources */,
F3AA5147256EF53000D64D31 /* TestHelpers.swift in Sources */,
F3850C8D243D103700F7CC13 /* TestJSONParams.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// String+Extensions.swift
// TestCache.swift
//
// Copyright © 2018-2021 Vasilis Panagiotopoulos. All rights reserved.
//
Expand All @@ -17,14 +17,26 @@
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import Foundation
import XCTest
@testable import TermiNetwork

extension String {
var fileName: String {
return URL(fileURLWithPath: self).deletingPathExtension().lastPathComponent
class TestCache: XCTestCase {

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

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

var fileExtension: String {
return URL(fileURLWithPath: self).pathExtension
func testConfigureCache() {
let count = 200
let size = 1024*100*100

Cache.shared.configureCache(countLimit: count, size: size)

XCTAssert(Cache.shared.cache.countLimit == count)
XCTAssert(Cache.shared.cache.totalCostLimit == size)
}
}
17 changes: 13 additions & 4 deletions Tests/TestExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import XCTest
import TermiNetwork
@testable import TermiNetwork
import SwiftUI

class TestExtensions: XCTestCase {
Expand All @@ -38,14 +38,22 @@ class TestExtensions: XCTestCase {
func testImageViewRemoteURL() {
let expectation = XCTestExpectation(description: "Test testImageViewRemoteURL")
var failed = true
let imageSize = CGSize(width: 86, height: 32)

let imageView = UIImageView()
imageView.tn_setRemoteImage(url: sampleImageURL,
defaultImage: nil,
resize: imageSize,
preprocessImage: { image in
return image
}, onFinish: { image, error in
failed = !(image != nil && error == nil)
guard let size = image?.size else {
expectation.fulfill()
return
}
failed = !(image != nil &&
[imageSize.width, imageSize.height].contains(max(size.width, size.height)) &&
error == nil)
expectation.fulfill()
})

Expand All @@ -60,10 +68,11 @@ class TestExtensions: XCTestCase {
let imageView = UIImageView()
imageView.tn_setRemoteImage(url: "abcdef",
defaultImage: nil,
resize: CGSize(width: 50, height: 50),
preprocessImage: { image in
return image
}, onFinish: { _, error in
failed = error == nil
}, onFinish: { image, error in
failed = !(image?.size == nil && error != nil)
expectation.fulfill()
})

Expand Down
Loading

0 comments on commit baae89f

Please sign in to comment.