diff --git a/Codemine.xcodeproj/project.pbxproj b/Codemine.xcodeproj/project.pbxproj index 4052e43..16aeb45 100644 --- a/Codemine.xcodeproj/project.pbxproj +++ b/Codemine.xcodeproj/project.pbxproj @@ -61,6 +61,18 @@ 291272CF1C75F32200FB1BBD /* Operators.swift in Sources */ = {isa = PBXBuildFile; fileRef = 291272CE1C75F32200FB1BBD /* Operators.swift */; }; 293490EE1C6CAFD500E8305E /* Application.swift in Sources */ = {isa = PBXBuildFile; fileRef = 293490ED1C6CAFD500E8305E /* Application.swift */; }; 293490F01C6CAFF200E8305E /* Then.swift in Sources */ = {isa = PBXBuildFile; fileRef = 293490EF1C6CAFF200E8305E /* Then.swift */; }; + 83A5BEBC1D981F3500C74312 /* UIImageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83A5BEBB1D981F3500C74312 /* UIImageTests.swift */; }; + 83A5BEBD1D98215F00C74312 /* UIImageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83A5BEBB1D981F3500C74312 /* UIImageTests.swift */; }; + 83A5BEBE1D98216000C74312 /* UIImageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83A5BEBB1D981F3500C74312 /* UIImageTests.swift */; }; + 83A5BEC01D98226800C74312 /* add.png in Resources */ = {isa = PBXBuildFile; fileRef = 83A5BEBF1D98226800C74312 /* add.png */; }; + 83A5BEC11D98226800C74312 /* add.png in Resources */ = {isa = PBXBuildFile; fileRef = 83A5BEBF1D98226800C74312 /* add.png */; }; + 83A5BEC21D98226800C74312 /* add.png in Resources */ = {isa = PBXBuildFile; fileRef = 83A5BEBF1D98226800C74312 /* add.png */; }; + 83A5BEC41D98228300C74312 /* alert.png in Resources */ = {isa = PBXBuildFile; fileRef = 83A5BEC31D98228300C74312 /* alert.png */; }; + 83A5BEC51D98228300C74312 /* alert.png in Resources */ = {isa = PBXBuildFile; fileRef = 83A5BEC31D98228300C74312 /* alert.png */; }; + 83A5BEC61D98228300C74312 /* alert.png in Resources */ = {isa = PBXBuildFile; fileRef = 83A5BEC31D98228300C74312 /* alert.png */; }; + 83A5BEC91D98249500C74312 /* URLImageAssetSizeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83A5BEC81D98249500C74312 /* URLImageAssetSizeTests.swift */; }; + 83A5BECA1D98249500C74312 /* URLImageAssetSizeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83A5BEC81D98249500C74312 /* URLImageAssetSizeTests.swift */; }; + 83A5BECB1D98249500C74312 /* URLImageAssetSizeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83A5BEC81D98249500C74312 /* URLImageAssetSizeTests.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -112,6 +124,10 @@ 291272CE1C75F32200FB1BBD /* Operators.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Operators.swift; sourceTree = ""; }; 293490ED1C6CAFD500E8305E /* Application.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Application.swift; sourceTree = ""; }; 293490EF1C6CAFF200E8305E /* Then.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Then.swift; sourceTree = ""; }; + 83A5BEBB1D981F3500C74312 /* UIImageTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UIImageTests.swift; sourceTree = ""; }; + 83A5BEBF1D98226800C74312 /* add.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = add.png; sourceTree = ""; }; + 83A5BEC31D98228300C74312 /* alert.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = alert.png; sourceTree = ""; }; + 83A5BEC81D98249500C74312 /* URLImageAssetSizeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = URLImageAssetSizeTests.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -208,8 +224,11 @@ 275BCAAD1C57D1B500FF3647 /* CodemineTests */ = { isa = PBXGroup; children = ( - 275BCAAE1C57D1B500FF3647 /* CodemineTests.swift */, + 83A5BEC71D98228F00C74312 /* Resources */, 275BCAB01C57D1B500FF3647 /* Info.plist */, + 275BCAAE1C57D1B500FF3647 /* CodemineTests.swift */, + 83A5BEBB1D981F3500C74312 /* UIImageTests.swift */, + 83A5BEC81D98249500C74312 /* URLImageAssetSizeTests.swift */, ); path = CodemineTests; sourceTree = ""; @@ -240,6 +259,15 @@ path = "Supporting Files"; sourceTree = ""; }; + 83A5BEC71D98228F00C74312 /* Resources */ = { + isa = PBXGroup; + children = ( + 83A5BEBF1D98226800C74312 /* add.png */, + 83A5BEC31D98228300C74312 /* alert.png */, + ); + name = Resources; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXHeadersBuildPhase section */ @@ -474,6 +502,8 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + 83A5BEC51D98228300C74312 /* alert.png in Resources */, + 83A5BEC11D98226800C74312 /* add.png in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -488,6 +518,8 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + 83A5BEC61D98228300C74312 /* alert.png in Resources */, + 83A5BEC21D98226800C74312 /* add.png in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -509,6 +541,8 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + 83A5BEC41D98228300C74312 /* alert.png in Resources */, + 83A5BEC01D98226800C74312 /* add.png in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -535,6 +569,8 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 83A5BEBD1D98215F00C74312 /* UIImageTests.swift in Sources */, + 83A5BECA1D98249500C74312 /* URLImageAssetSizeTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -562,6 +598,8 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 83A5BEBE1D98216000C74312 /* UIImageTests.swift in Sources */, + 83A5BECB1D98249500C74312 /* URLImageAssetSizeTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -607,7 +645,9 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 83A5BEC91D98249500C74312 /* URLImageAssetSizeTests.swift in Sources */, 275BCAAF1C57D1B500FF3647 /* CodemineTests.swift in Sources */, + 83A5BEBC1D981F3500C74312 /* UIImageTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Codemine/Application.swift b/Codemine/Application.swift index 893772d..32df5a9 100644 --- a/Codemine/Application.swift +++ b/Codemine/Application.swift @@ -23,7 +23,7 @@ import Foundation public struct Application { - private static func getString(_ key: String) -> String { + fileprivate static func getString(_ key: String) -> String { guard let infoDictionary = Bundle.main.infoDictionary, let value = infoDictionary[key] as? String else { return "" } diff --git a/Codemine/Extensions/NSURL+AssetSize.swift b/Codemine/Extensions/NSURL+AssetSize.swift index 7228478..5252caf 100644 --- a/Codemine/Extensions/NSURL+AssetSize.swift +++ b/Codemine/Extensions/NSURL+AssetSize.swift @@ -41,8 +41,8 @@ public extension URL { - widthParameterName: the name of the width paramter. Default is 'h' - Returns: `URL` as a `NSURL`. */ - public func urlByAppendingAssetSize(_ size: CGSize, mode: ImageUrlMode = .Default, heightParameterName : String = "h", widthParameterName : String = "w") -> URL { - guard var urlComponents = URLComponents(url: self, resolvingAgainstBaseURL: false) else { return self } + public func urlByAppendingAssetSize(_ size: CGSize, mode: ImageUrlMode = .Default, heightParameterName : String = "h", widthParameterName : String = "w") -> URL? { + guard var urlComponents = URLComponents(url: self, resolvingAgainstBaseURL: false) else { return nil } var queryItems:[URLQueryItem] = urlComponents.queryItems ?? [] queryItems.append(URLQueryItem(name: widthParameterName, value: "\(Int(size.width * UIScreen.main.scale ))")) @@ -51,6 +51,6 @@ public extension URL { queryItems.append(URLQueryItem(name: "mode", value: mode.rawValue)) } urlComponents.queryItems = queryItems - return urlComponents.url ?? self + return urlComponents.url } } diff --git a/Codemine/Then.swift b/Codemine/Then.swift index 20c01ba..a14c668 100644 --- a/Codemine/Then.swift +++ b/Codemine/Then.swift @@ -25,7 +25,7 @@ import Foundation public protocol Then {} extension Then { - public func then( _ block: @noescape (Self) -> Void) -> Self { + public func then( _ block: (Self) -> Void) -> Self { block(self) return self } diff --git a/CodemineTests/CodemineTests.swift b/CodemineTests/CodemineTests.swift index 7972893..02a8fc3 100644 --- a/CodemineTests/CodemineTests.swift +++ b/CodemineTests/CodemineTests.swift @@ -189,25 +189,7 @@ class CodemineTests: XCTestCase { XCTAssertEqual(magenta, UIColor.magenta) XCTAssertNotEqual(red, UIColor.yellow) } - - // MARK: - NSURL extension test - func testURLWithSize() { - guard let url = URL(string: "https://example.com/image.png") else { XCTAssertTrue(false, "Failed to create NSURL"); return } - let size = CGSize(width: 512, height: 256) - let heightParameterName = "height" - let widthParameterName = "width" - - let url2 = url.urlByAppendingAssetSize(size, mode: .Default, heightParameterName: heightParameterName, widthParameterName: widthParameterName) - XCTAssertEqual(url2.absoluteString, url.absoluteString + "?\(widthParameterName)=\(Int(size.width * UIScreen.main.scale ))&\(heightParameterName)=\(Int(size.height * UIScreen.main.scale))") - - let url3 = url.urlByAppendingAssetSize(size) - XCTAssertEqual(url3.absoluteString, url.absoluteString + "?w=\(Int(size.width * UIScreen.main.scale ))&h=\(Int(size.height * UIScreen.main.scale))") - let url4 = url.urlByAppendingAssetSize(size, mode: .Crop) - XCTAssertEqual(url4.absoluteString, url.absoluteString + "?w=\(Int(size.width * UIScreen.main.scale ))&h=\(Int(size.height * UIScreen.main.scale))&mode=crop") - - } - func testError() { let domain = "Domain" let code = 123 diff --git a/CodemineTests/UIImageTests.swift b/CodemineTests/UIImageTests.swift new file mode 100644 index 0000000..f8ab579 --- /dev/null +++ b/CodemineTests/UIImageTests.swift @@ -0,0 +1,37 @@ +// +// UIImageTests.swift +// Codemine +// +// Created by Chris on 25/09/2016. +// Copyright © 2016 Nodes. All rights reserved. +// + +import XCTest +@testable import Codemine + +class UIImageTests: XCTestCase { + + override func setUp() { + super.setUp() + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + func testImageNamed(name: String) -> UIImage { + let bundle = Bundle(for: type(of: self)) + let path = bundle.path(forResource: name, ofType: "png")! + return UIImage(contentsOfFile: path)! + } + + func testUIImageFromColor() { + XCTAssertNotNil(UIImage.imageFromColor(.red, size: CGSize(width: 20, height: 20), cornerRadius: 1.0), "Failed to convert color to image") + } + + func testImageImbed() { + XCTAssertNotNil(UIImage.imageByEmbeddingIconIn(testImageNamed(name: "add"), icon: testImageNamed(name: "alert")), "Failed to embed image") + } + + func testImageRotation() { + XCTAssertNotNil(testImageNamed(name: "add").rotationCorrectedImage(), "Failed to rotate image") + } + +} diff --git a/CodemineTests/URLImageAssetSizeTests.swift b/CodemineTests/URLImageAssetSizeTests.swift new file mode 100644 index 0000000..057c5f5 --- /dev/null +++ b/CodemineTests/URLImageAssetSizeTests.swift @@ -0,0 +1,48 @@ +// +// URLImageAssetSizeTests.swift +// Codemine +// +// Created by Chris on 25/09/2016. +// Copyright © 2016 Nodes. All rights reserved. +// + +import XCTest +@testable import Codemine + +class URLImageAssetSizeTests: XCTestCase { + + override func setUp() { + super.setUp() + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + + func testURLWithSize() { + guard let url = URL(string: "https://example.com/image.png") else { XCTAssertTrue(false, "Failed to create NSURL"); return } + let size = CGSize(width: 512, height: 256) + let heightParameterName = "height" + let widthParameterName = "width" + + let url2 = url.urlByAppendingAssetSize(size, mode: .Default, heightParameterName: heightParameterName, widthParameterName: widthParameterName) + XCTAssertEqual(url2?.absoluteString, url.absoluteString + "?\(widthParameterName)=\(Int(size.width * UIScreen.main.scale ))&\(heightParameterName)=\(Int(size.height * UIScreen.main.scale))") + + let url3 = url.urlByAppendingAssetSize(size) + XCTAssertEqual(url3?.absoluteString, url.absoluteString + "?w=\(Int(size.width * UIScreen.main.scale ))&h=\(Int(size.height * UIScreen.main.scale))") + + let url4 = url.urlByAppendingAssetSize(size, mode: .Crop) + XCTAssertEqual(url4?.absoluteString, url.absoluteString + "?w=\(Int(size.width * UIScreen.main.scale ))&h=\(Int(size.height * UIScreen.main.scale))&mode=crop") + + } + + func testBadUrl() { + let url = URL(string: "http://example.com:-80/")! + + print(URLComponents(url: url, resolvingAgainstBaseURL: false)) + + let newUrl = url.urlByAppendingAssetSize(CGSize(width: 10, height: 10)) + XCTAssertNil(newUrl?.absoluteString, "Bad URL did not return nil") + } + + + +} diff --git a/CodemineTests/add.png b/CodemineTests/add.png new file mode 100644 index 0000000..3d00875 Binary files /dev/null and b/CodemineTests/add.png differ diff --git a/CodemineTests/alert.png b/CodemineTests/alert.png new file mode 100644 index 0000000..8292381 Binary files /dev/null and b/CodemineTests/alert.png differ