diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c545112 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +xcuserdata/ diff --git a/SwiftRandom.xcodeproj/project.pbxproj b/SwiftRandom.xcodeproj/project.pbxproj index 717d087..566531a 100755 --- a/SwiftRandom.xcodeproj/project.pbxproj +++ b/SwiftRandom.xcodeproj/project.pbxproj @@ -7,18 +7,41 @@ objects = { /* Begin PBXBuildFile section */ + 53DC00941DA3313F00C3C823 /* SwiftRandomTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 53DC00931DA3313F00C3C823 /* SwiftRandomTests.swift */; }; + 53DC00961DA3313F00C3C823 /* SwiftRandom.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B537B0CB1BCA9E3E005725CE /* SwiftRandom.framework */; }; B537B0CF1BCA9E3F005725CE /* SwiftRandom.h in Headers */ = {isa = PBXBuildFile; fileRef = B537B0CE1BCA9E3F005725CE /* SwiftRandom.h */; settings = {ATTRIBUTES = (Public, ); }; }; B58CCF471BCA9E870033D848 /* Randoms.swift in Sources */ = {isa = PBXBuildFile; fileRef = B58CCF461BCA9E870033D848 /* Randoms.swift */; }; /* End PBXBuildFile section */ +/* Begin PBXContainerItemProxy section */ + 53DC00971DA3313F00C3C823 /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = B537B0C21BCA9E3E005725CE /* Project object */; + proxyType = 1; + remoteGlobalIDString = B537B0CA1BCA9E3E005725CE; + remoteInfo = "SwiftRandom-iOS"; + }; +/* End PBXContainerItemProxy section */ + /* Begin PBXFileReference section */ + 53DC00911DA3313F00C3C823 /* SwiftRandomTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SwiftRandomTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; + 53DC00931DA3313F00C3C823 /* SwiftRandomTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftRandomTests.swift; sourceTree = ""; }; + 53DC00951DA3313F00C3C823 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; B537B0CB1BCA9E3E005725CE /* SwiftRandom.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SwiftRandom.framework; sourceTree = BUILT_PRODUCTS_DIR; }; B537B0CE1BCA9E3F005725CE /* SwiftRandom.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SwiftRandom.h; sourceTree = ""; }; B537B0D01BCA9E3F005725CE /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - B58CCF461BCA9E870033D848 /* Randoms.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Randoms.swift; sourceTree = SOURCE_ROOT; }; + B58CCF461BCA9E870033D848 /* Randoms.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Randoms.swift; path = SwiftRandom/Randoms.swift; sourceTree = SOURCE_ROOT; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ + 53DC008E1DA3313F00C3C823 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + 53DC00961DA3313F00C3C823 /* SwiftRandom.framework in Frameworks */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; B537B0C71BCA9E3E005725CE /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; @@ -29,11 +52,21 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 53DC00921DA3313F00C3C823 /* SwiftRandomTests */ = { + isa = PBXGroup; + children = ( + 53DC00951DA3313F00C3C823 /* Info.plist */, + 53DC00931DA3313F00C3C823 /* SwiftRandomTests.swift */, + ); + path = SwiftRandomTests; + sourceTree = ""; + }; B537B0C11BCA9E3E005725CE = { isa = PBXGroup; children = ( B537B0CC1BCA9E3E005725CE /* Products */, B537B0CD1BCA9E3F005725CE /* SwiftRandom */, + 53DC00921DA3313F00C3C823 /* SwiftRandomTests */, ); sourceTree = ""; }; @@ -41,6 +74,7 @@ isa = PBXGroup; children = ( B537B0CB1BCA9E3E005725CE /* SwiftRandom.framework */, + 53DC00911DA3313F00C3C823 /* SwiftRandomTests.xctest */, ); name = Products; sourceTree = ""; @@ -69,6 +103,24 @@ /* End PBXHeadersBuildPhase section */ /* Begin PBXNativeTarget section */ + 53DC00901DA3313F00C3C823 /* SwiftRandomTests */ = { + isa = PBXNativeTarget; + buildConfigurationList = 53DC00991DA3313F00C3C823 /* Build configuration list for PBXNativeTarget "SwiftRandomTests" */; + buildPhases = ( + 53DC008D1DA3313F00C3C823 /* Sources */, + 53DC008E1DA3313F00C3C823 /* Frameworks */, + 53DC008F1DA3313F00C3C823 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + 53DC00981DA3313F00C3C823 /* PBXTargetDependency */, + ); + name = SwiftRandomTests; + productName = SwiftRandomTests; + productReference = 53DC00911DA3313F00C3C823 /* SwiftRandomTests.xctest */; + productType = "com.apple.product-type.bundle.unit-test"; + }; B537B0CA1BCA9E3E005725CE /* SwiftRandom-iOS */ = { isa = PBXNativeTarget; buildConfigurationList = B537B0D31BCA9E3F005725CE /* Build configuration list for PBXNativeTarget "SwiftRandom-iOS" */; @@ -93,9 +145,13 @@ B537B0C21BCA9E3E005725CE /* Project object */ = { isa = PBXProject; attributes = { - LastSwiftUpdateCheck = 0700; + LastSwiftUpdateCheck = 0800; LastUpgradeCheck = 0800; TargetAttributes = { + 53DC00901DA3313F00C3C823 = { + CreatedOnToolsVersion = 8.0; + ProvisioningStyle = Automatic; + }; B537B0CA1BCA9E3E005725CE = { CreatedOnToolsVersion = 7.0.1; LastSwiftMigration = 0800; @@ -115,11 +171,19 @@ projectRoot = ""; targets = ( B537B0CA1BCA9E3E005725CE /* SwiftRandom-iOS */, + 53DC00901DA3313F00C3C823 /* SwiftRandomTests */, ); }; /* End PBXProject section */ /* Begin PBXResourcesBuildPhase section */ + 53DC008F1DA3313F00C3C823 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; B537B0C91BCA9E3E005725CE /* Resources */ = { isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; @@ -130,6 +194,14 @@ /* End PBXResourcesBuildPhase section */ /* Begin PBXSourcesBuildPhase section */ + 53DC008D1DA3313F00C3C823 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 53DC00941DA3313F00C3C823 /* SwiftRandomTests.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; B537B0C61BCA9E3E005725CE /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; @@ -140,7 +212,51 @@ }; /* End PBXSourcesBuildPhase section */ +/* Begin PBXTargetDependency section */ + 53DC00981DA3313F00C3C823 /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = B537B0CA1BCA9E3E005725CE /* SwiftRandom-iOS */; + targetProxy = 53DC00971DA3313F00C3C823 /* PBXContainerItemProxy */; + }; +/* End PBXTargetDependency section */ + /* Begin XCBuildConfiguration section */ + 53DC009A1DA3313F00C3C823 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ANALYZER_NONNULL = YES; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_SUSPICIOUS_MOVES = YES; + DEVELOPMENT_TEAM = ""; + INFOPLIST_FILE = SwiftRandomTests/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 10.0; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = thellimist.SwiftRandom.SwiftRandomTests; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_VERSION = 3.0; + }; + name = Debug; + }; + 53DC009B1DA3313F00C3C823 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ANALYZER_NONNULL = YES; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_SUSPICIOUS_MOVES = YES; + DEVELOPMENT_TEAM = ""; + INFOPLIST_FILE = SwiftRandomTests/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 10.0; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = thellimist.SwiftRandom.SwiftRandomTests; + PRODUCT_NAME = "$(TARGET_NAME)"; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + SWIFT_VERSION = 3.0; + }; + name = Release; + }; B537B0D11BCA9E3F005725CE /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { @@ -271,6 +387,14 @@ /* End XCBuildConfiguration section */ /* Begin XCConfigurationList section */ + 53DC00991DA3313F00C3C823 /* Build configuration list for PBXNativeTarget "SwiftRandomTests" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 53DC009A1DA3313F00C3C823 /* Debug */, + 53DC009B1DA3313F00C3C823 /* Release */, + ); + defaultConfigurationIsVisible = 0; + }; B537B0C51BCA9E3E005725CE /* Build configuration list for PBXProject "SwiftRandom" */ = { isa = XCConfigurationList; buildConfigurations = ( diff --git a/SwiftRandom.xcodeproj/project.xcworkspace/xcuserdata/Sabatino.xcuserdatad/UserInterfaceState.xcuserstate b/SwiftRandom.xcodeproj/project.xcworkspace/xcuserdata/Sabatino.xcuserdatad/UserInterfaceState.xcuserstate deleted file mode 100644 index 304fb91..0000000 Binary files a/SwiftRandom.xcodeproj/project.xcworkspace/xcuserdata/Sabatino.xcuserdatad/UserInterfaceState.xcuserstate and /dev/null differ diff --git a/SwiftRandom.xcodeproj/project.xcworkspace/xcuserdata/cknight.xcuserdatad/UserInterfaceState.xcuserstate b/SwiftRandom.xcodeproj/project.xcworkspace/xcuserdata/cknight.xcuserdatad/UserInterfaceState.xcuserstate deleted file mode 100644 index ca300f2..0000000 Binary files a/SwiftRandom.xcodeproj/project.xcworkspace/xcuserdata/cknight.xcuserdatad/UserInterfaceState.xcuserstate and /dev/null differ diff --git a/SwiftRandom.xcodeproj/project.xcworkspace/xcuserdata/furkan.xcuserdatad/UserInterfaceState.xcuserstate b/SwiftRandom.xcodeproj/project.xcworkspace/xcuserdata/furkan.xcuserdatad/UserInterfaceState.xcuserstate deleted file mode 100644 index 11f978b..0000000 Binary files a/SwiftRandom.xcodeproj/project.xcworkspace/xcuserdata/furkan.xcuserdatad/UserInterfaceState.xcuserstate and /dev/null differ diff --git a/SwiftRandom.xcodeproj/xcshareddata/xcschemes/SwiftRandom-iOS.xcscheme b/SwiftRandom.xcodeproj/xcshareddata/xcschemes/SwiftRandom-iOS.xcscheme index b2da066..6713ebd 100644 --- a/SwiftRandom.xcodeproj/xcshareddata/xcschemes/SwiftRandom-iOS.xcscheme +++ b/SwiftRandom.xcodeproj/xcshareddata/xcschemes/SwiftRandom-iOS.xcscheme @@ -1,7 +1,7 @@ + version = "1.7"> @@ -28,6 +28,20 @@ selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" shouldUseLaunchSchemeArgsEnv = "YES"> + + + + + + - - - - SuppressBuildableAutocreation - - B537B0CA1BCA9E3E005725CE - - primary - - - - - diff --git a/SwiftRandom.xcodeproj/xcuserdata/cknight.xcuserdatad/xcschemes/xcschememanagement.plist b/SwiftRandom.xcodeproj/xcuserdata/cknight.xcuserdatad/xcschemes/xcschememanagement.plist deleted file mode 100644 index adc1591..0000000 --- a/SwiftRandom.xcodeproj/xcuserdata/cknight.xcuserdatad/xcschemes/xcschememanagement.plist +++ /dev/null @@ -1,14 +0,0 @@ - - - - - SuppressBuildableAutocreation - - B537B0CA1BCA9E3E005725CE - - primary - - - - - diff --git a/SwiftRandom.xcodeproj/xcuserdata/furkan.xcuserdatad/xcschemes/xcschememanagement.plist b/SwiftRandom.xcodeproj/xcuserdata/furkan.xcuserdatad/xcschemes/xcschememanagement.plist deleted file mode 100644 index adc1591..0000000 --- a/SwiftRandom.xcodeproj/xcuserdata/furkan.xcuserdatad/xcschemes/xcschememanagement.plist +++ /dev/null @@ -1,14 +0,0 @@ - - - - - SuppressBuildableAutocreation - - B537B0CA1BCA9E3E005725CE - - primary - - - - - diff --git a/Randoms.swift b/SwiftRandom/Randoms.swift similarity index 59% rename from Randoms.swift rename to SwiftRandom/Randoms.swift index c8d83ec..c02611b 100644 --- a/Randoms.swift +++ b/SwiftRandom/Randoms.swift @@ -18,26 +18,35 @@ public extension Bool { public extension Int { /// SwiftRandom extension - public static func random(range: Range) -> Int { - return random(range.upperBound, lower: range.lowerBound) + public static func random(_ range: Range) -> Int { + #if swift(>=3) + return random(range.lowerBound, range.upperBound - 1) + #else + return random(range.upperBound, range.lowerBound) + #endif } /// SwiftRandom extension - public static func random(lower: Int = 0, _ upper: Int = 100) -> Int { + public static func random(_ range: ClosedRange) -> Int { + return random(range.lowerBound, range.upperBound) + } + + /// SwiftRandom extension + public static func random(_ lower: Int = 0, _ upper: Int = 100) -> Int { return lower + Int(arc4random_uniform(UInt32(upper - lower + 1))) } } public extension Int32 { /// SwiftRandom extension - public static func random(range: Range) -> Int32 { - return random(range.upperBound, lower: range.lowerBound) + public static func random(_ range: Range) -> Int32 { + return random(range.upperBound, range.lowerBound) } /// SwiftRandom extension /// /// - note: Using `Int` as parameter type as we usually just want to write `Int32.random(13, 37)` and not `Int32.random(Int32(13), Int32(37))` - public static func random(lower: Int = 0, _ upper: Int = 100) -> Int32 { + public static func random(_ lower: Int = 0, _ upper: Int = 100) -> Int32 { let r = arc4random_uniform(UInt32(Int64(upper) - Int64(lower))) return Int32(Int64(r) + Int64(lower)) } @@ -45,47 +54,43 @@ public extension Int32 { public extension Double { /// SwiftRandom extension - public static func random(lower: Double = 0, _ upper: Double = 100) -> Double { + public static func random(_ lower: Double = 0, _ upper: Double = 100) -> Double { return (Double(arc4random()) / 0xFFFFFFFF) * (upper - lower) + lower } } public extension Float { /// SwiftRandom extension - public static func random(lower: Float = 0, _ upper: Float = 100) -> Float { + public static func random(_ lower: Float = 0, _ upper: Float = 100) -> Float { return (Float(arc4random()) / 0xFFFFFFFF) * (upper - lower) + lower } } public extension CGFloat { /// SwiftRandom extension - public static func random(lower: CGFloat = 0, _ upper: CGFloat = 1) -> CGFloat { + public static func random(_ lower: CGFloat = 0, _ upper: CGFloat = 1) -> CGFloat { return CGFloat(Float(arc4random()) / Float(UINT32_MAX)) * (upper - lower) + lower } } -public extension NSDate { +public extension Date { /// SwiftRandom extension - public static func randomWithinDaysBeforeToday(days: Int) -> NSDate { - let today = NSDate() - - guard let gregorian = Calendar(calendarIdentifier: Calendar.Identifier.gregorian) else { - print("no calendar \"NSCalendarIdentifierGregorian\" found") - return today - } + public static func randomWithinDaysBeforeToday(_ days: Int) -> Date { + let today = Date() + let gregorian = Calendar(identifier: Calendar.Identifier.gregorian) let r1 = arc4random_uniform(UInt32(days)) let r2 = arc4random_uniform(UInt32(23)) let r3 = arc4random_uniform(UInt32(59)) let r4 = arc4random_uniform(UInt32(59)) - let offsetComponents = NSDateComponents() + var offsetComponents = DateComponents() offsetComponents.day = Int(r1) * -1 offsetComponents.hour = Int(r2) offsetComponents.minute = Int(r3) offsetComponents.second = Int(r4) - guard let rndDate1 = gregorian.date(byAdding: offsetComponents as DateComponents, to: today as Date, options: []) else { + guard let rndDate1 = gregorian.date(byAdding: offsetComponents, to: today) else { print("randoming failed") return today } @@ -93,16 +98,16 @@ public extension NSDate { } /// SwiftRandom extension - public static func random() -> NSDate { + public static func random() -> Date { let randomTime = TimeInterval(arc4random_uniform(UInt32.max)) - return NSDate(timeIntervalSince1970: randomTime) + return Date(timeIntervalSince1970: randomTime) } } public extension UIColor { /// SwiftRandom extension - public static func random(randomAlpha: Bool = false) -> UIColor { + public static func random(_ randomAlpha: Bool = false) -> UIColor { let randomRed = CGFloat.random() let randomGreen = CGFloat.random() let randomBlue = CGFloat.random() @@ -129,17 +134,22 @@ public extension ArraySlice { guard self.count > 0 else { return nil } - - let index = Int.random(lower: self.startIndex, self.endIndex) + + #if swift(>=3) + let index = Int.random(self.startIndex, self.count - 1) + #else + let index = Int.random(self.startIndex, self.endIndex) + #endif + return self[index] } } -public extension NSURL { +public extension URL { /// SwiftRandom extension - public static func random() -> NSURL { + public static func random() -> URL { let urlList = ["http://www.google.com", "http://leagueoflegends.com/", "https://github.com/", "http://stackoverflow.com/", "https://medium.com/", "http://9gag.com/gag/6715049", "http://imgur.com/gallery/s9zoqs9", "https://www.youtube.com/watch?v=uelHwf8o7_U"] - return NSURL(string: urlList.randomItem())! + return URL(string: urlList.randomItem()!)! } } @@ -154,52 +164,52 @@ public struct Randoms { return Bool.random() } - public static func randomInt(range: Range) -> Int { - return Int.random(range: range) + public static func randomInt(_ range: Range) -> Int { + return Int.random(range) } - public static func randomInt(lower: Int = 0, _ upper: Int = 100) -> Int { - return Int.random(lower: lower, upper) + public static func randomInt(_ lower: Int = 0, _ upper: Int = 100) -> Int { + return Int.random(lower, upper) } - public static func randomInt32(range: Range) -> Int32 { - return Int32.random(range: range) + public static func randomInt32(_ range: Range) -> Int32 { + return Int32.random(range) } - public static func randomInt32(lower: Int = 0, _ upper: Int = 100) -> Int32{ - return Int32.random(lower, lower: upper) + public static func randomInt32(_ lower: Int = 0, _ upper: Int = 100) -> Int32 { + return Int32.random(lower, upper) } - public static func randomPercentageisOver(percentage: Int) -> Bool { + public static func randomPercentageisOver(_ percentage: Int) -> Bool { return Int.random() > percentage } - public static func randomDouble(lower: Double = 0, _ upper: Double = 100) -> Double { - return Double.random(lower, lower: upper) + public static func randomDouble(_ lower: Double = 0, _ upper: Double = 100) -> Double { + return Double.random(lower, upper) } - public static func randomFloat(lower: Float = 0, _ upper: Float = 100) -> Float { - return Float.random(lower, lower: upper) + public static func randomFloat(_ lower: Float = 0, _ upper: Float = 100) -> Float { + return Float.random(lower, upper) } - public static func randomCGFloat(lower: CGFloat = 0, _ upper: CGFloat = 1) -> CGFloat { - return CGFloat.random(lower, lower: upper) + public static func randomCGFloat(_ lower: CGFloat = 0, _ upper: CGFloat = 1) -> CGFloat { + return CGFloat.random(lower, upper) } - public static func randomDateWithinDaysBeforeToday(days: Int) -> NSDate { - return NSDate.randomWithinDaysBeforeToday(days: days) + public static func randomDateWithinDaysBeforeToday(_ days: Int) -> Date { + return Date.randomWithinDaysBeforeToday(days) } - public static func randomDate() -> NSDate { - return NSDate.random() + public static func randomDate() -> Date { + return Date.random() } - public static func randomColor(randomAlpha: Bool = false) -> UIColor { - return UIColor.random(randomAlpha: randomAlpha) + public static func randomColor(_ randomAlpha: Bool = false) -> UIColor { + return UIColor.random(randomAlpha) } - public static func randomNSURL() -> NSURL { - return NSURL.random() + public static func randomNSURL() -> URL { + return URL.random() } //========================================================================================================== @@ -209,7 +219,7 @@ public struct Randoms { public static func randomFakeName() -> String { let firstNameList = ["Henry", "William", "Geoffrey", "Jim", "Yvonne", "Jamie", "Leticia", "Priscilla", "Sidney", "Nancy", "Edmund", "Bill", "Megan"] let lastNameList = ["Pearson", "Adams", "Cole", "Francis", "Andrews", "Casey", "Gross", "Lane", "Thomas", "Patrick", "Strickland", "Nicolas", "Freeman"] - return firstNameList.randomItem() + " " + lastNameList.randomItem() + return firstNameList.randomItem()! + " " + lastNameList.randomItem()! } public static func randomFakeGender() -> String { @@ -217,23 +227,23 @@ public struct Randoms { } public static func randomFakeConversation() -> String { - let convoList = ["You embarrassed me this evening.","You don't think that was just lemonade in your glass, do you?","Do you ever think we should just stop doing this?","Why didn't he come and talk to me himself?","Promise me you'll look after your mother.","If you get me his phone, I might reconsider.","I think the room is bugged.","No! I'm tired of doing what you say.","For some reason, I'm attracted to you."] - return convoList.randomItem() + let convoList = ["You embarrassed me this evening.", "You don't think that was just lemonade in your glass, do you?", "Do you ever think we should just stop doing this?", "Why didn't he come and talk to me himself?", "Promise me you'll look after your mother.", "If you get me his phone, I might reconsider.", "I think the room is bugged.", "No! I'm tired of doing what you say.", "For some reason, I'm attracted to you."] + return convoList.randomItem()! } public static func randomFakeTitle() -> String { let titleList = ["CEO of Google", "CEO of Facebook", "VP of Marketing @Uber", "Business Developer at IBM", "Jungler @ Fanatic", "B2 Pilot @ USAF", "Student at Stanford", "Student at Harvard", "Mayor of Raccoon City", "CTO @ Umbrella Corporation", "Professor at Pallet Town University"] - return titleList.randomItem() + return titleList.randomItem()! } public static func randomFakeTag() -> String { let tagList = ["meta", "forum", "troll", "meme", "question", "important", "like4like", "f4f"] - return tagList.randomItem() + return tagList.randomItem()! } - private static func randomEnglishHonorific() -> String { + fileprivate static func randomEnglishHonorific() -> String { let englishHonorificsList = ["Mr.", "Ms.", "Dr.", "Mrs.", "Mz.", "Mx.", "Prof."] - return englishHonorificsList.randomItem() + return englishHonorificsList.randomItem()! } public static func randomFakeNameAndEnglishHonorific() -> String { @@ -245,13 +255,13 @@ public struct Randoms { public static func randomFakeCity() -> String { let cityPrefixes = ["North", "East", "West", "South", "New", "Lake", "Port"] let citySuffixes = ["town", "ton", "land", "ville", "berg", "burgh", "borough", "bury", "view", "port", "mouth", "stad", "furt", "chester", "mouth", "fort", "haven", "side", "shire"] - return cityPrefixes.randomItem() + citySuffixes.randomItem() + return cityPrefixes.randomItem()! + citySuffixes.randomItem()! } - + public static func randomCurrency() -> String { let currencyList = ["USD", "EUR", "GBP", "JPY", "AUD", "CAD", "ZAR", "NZD", "INR", "BRP", "CNY", "EGP", "KRW", "MXN", "SAR", "SGD",] - - return currencyList.randomItem() + + return currencyList.randomItem()! } public enum GravatarStyle: String { @@ -265,29 +275,28 @@ public struct Randoms { static let allValues = [Standard, MM, Identicon, MonsterID, Wavatar, Retro] } - public static func createGravatar(style: Randoms.GravatarStyle = .Standard, size: Int = 80, completion: ((image: UIImage?, error: NSError?) -> Void)?) { + public static func createGravatar(_ style: Randoms.GravatarStyle = .Standard, size: Int = 80, completion: ((_ image: UIImage?, _ error: Error?) -> Void)?) { var url = "https://secure.gravatar.com/avatar/thisimagewillnotbefound?s=\(size)" if style != .Standard { url += "&d=\(style.rawValue.lowercased())" } - let request = NSURLRequest(url: NSURL(string: url)! as URL, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 5.0) + let request = URLRequest(url: URL(string: url)! as URL, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 5.0) let session = URLSession.shared - session.dataTask(with: request as URLRequest, completionHandler: {(data, response, error) in - DispatchQueue.main.asynchronously() { + session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) in + DispatchQueue.main.async { if error == nil { - completion?(image: UIImage(data: data!), error: nil) + completion?(UIImage(data: data!), nil) } else { - completion?(image: nil, error: error) + completion?(nil, error) } } - }).resume() + }).resume() } - public static func randomGravatar(size: Int = 80, completion: ((image: UIImage?, error: NSError?) -> Void)?) { + public static func randomGravatar(_ size: Int = 80, completion: ((_ image: UIImage?, _ error: Error?) -> Void)?) { let options = Randoms.GravatarStyle.allValues - Randoms.createGravatar(style: options.randomItem(), size: size, completion: completion) + Randoms.createGravatar(options.randomItem()!, size: size, completion: completion) } } - diff --git a/SwiftRandomTests/Info.plist b/SwiftRandomTests/Info.plist new file mode 100644 index 0000000..6c6c23c --- /dev/null +++ b/SwiftRandomTests/Info.plist @@ -0,0 +1,22 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + BNDL + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/SwiftRandomTests/SwiftRandomTests.swift b/SwiftRandomTests/SwiftRandomTests.swift new file mode 100644 index 0000000..460e93c --- /dev/null +++ b/SwiftRandomTests/SwiftRandomTests.swift @@ -0,0 +1,127 @@ +// +// SwiftRandomTests.swift +// SwiftRandomTests +// +// Created by Rafael Ferreira on 10/3/16. +// +// + +import XCTest + +@testable import SwiftRandom + +/// Tests above `SwiftRandom` module. +class SwiftRandomTests: XCTestCase { + /// Tests if all static methods are working. + func testStaticMethods() { + XCTAssertNotNil(Randoms.randomInt(10, 20)) + XCTAssertNotNil(Randoms.randomCGFloat()) + XCTAssertNotNil(Randoms.randomPercentageisOver(70)) + XCTAssertNotNil(Randoms.randomBool()) + XCTAssertNotNil(Randoms.randomDateWithinDaysBeforeToday(5)) + XCTAssertNotNil(Randoms.randomDate()) + XCTAssertNotNil(Randoms.randomColor()) + XCTAssertNotNil(Randoms.randomFakeName()) + XCTAssertNotNil(Randoms.randomFakeGender()) + XCTAssertNotNil(Randoms.randomFakeConversation()) + XCTAssertNotNil(Randoms.randomFakeTitle()) + XCTAssertNotNil(Randoms.randomFakeTag()) + XCTAssertNotNil(Randoms.randomFakeNameAndEnglishHonorific()) + XCTAssertNotNil(Randoms.randomNSURL()) + } + + /// Tests for extensions applied directly above Swift types. + func testTypeExtensions() { + XCTAssertNotNil(Int.random()) + XCTAssertNotNil(Date.random()) + XCTAssertNotNil(Date.randomWithinDaysBeforeToday(7)) + XCTAssertNotNil(CGFloat.random()) + XCTAssertNotNil(Float.random()) + XCTAssertNotNil(Double.random()) + XCTAssertNotNil(UIColor.random()) + XCTAssertNotNil(URL.random()) + } + + /// Tests for SwiftRandom when applied above a list. + func testListItemRandom() { + let entry = ["hello", "world"] + + for _ in 0...10 { + XCTAssertNotNil(entry[0..