Skip to content

Commit

Permalink
Merge pull request #108 from guseducampos/BuildOnLinux
Browse files Browse the repository at this point in the history
Making build and run tests on Linux
  • Loading branch information
vadymmarkov authored Mar 18, 2019
2 parents a04a210 + 0777445 commit c4edd97
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 21 deletions.
13 changes: 10 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
// swift-tools-version:4.0
// swift-tools-version:4.2

import PackageDescription

let package = Package(
name: "Fakery",
products: [
.library(name: "Fakery", targets: ["Fakery"])
.library(name: "Fakery", targets: ["Fakery"])
],
dependencies: [
// Test dependencies
.package(url: "https://github.com/Quick/Quick.git", from: "2.0.0"),
.package(url: "https://github.com/Quick/Nimble.git", from: "8.0.0"),

],
targets: [
.target(name: "Fakery")
.target(name: "Fakery"),
.testTarget(name: "FakeryTests", dependencies: ["Fakery","Quick", "Nimble"], path: "Tests/Fakery")
]
)
5 changes: 3 additions & 2 deletions Sources/Fakery/Data/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,14 @@ public final class Parser {

func parse(_ template: String, forSubject subject: String) -> String {
var text = ""
let string = template as NSString
let string = NSString(string: template)
var regex: NSRegularExpression

do {
try regex = NSRegularExpression(pattern: "(\\(?)#\\{([A-Za-z]+\\.)?([^\\}]+)\\}([^#]+)?",
options: .caseInsensitive)
let matches = regex.matches(in: string as String,

let matches = regex.matches(in: template,
options: .reportCompletion,
range: NSRange(location: 0, length: string.length))

Expand Down
7 changes: 5 additions & 2 deletions Sources/Fakery/Extensions/ArrayExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ extension Array {
guard count > 0 else {
return nil
}

return self[Int(arc4random_uniform(UInt32(count)))]
#if swift(>=4.2)
return self.randomElement()
#else
return self[Int(arc4random_uniform(UInt32(count)))]
#endif
}
}
17 changes: 15 additions & 2 deletions Sources/Fakery/Generators/Address.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import Foundation
#if os(Linux)
public struct Location {
let latitude: Double
let longitude: Double
}
#else
import CoreLocation
public typealias Location = CLLocationCoordinate2D
#endif

public final class Address: Generator {
public func city() -> String {
Expand Down Expand Up @@ -80,16 +88,21 @@ public final class Address: Generator {
return drand48() * 360.0 - 180.0
}

public func coordinate(inRadius radius: Double, fromCenter center:CLLocationCoordinate2D) -> CLLocationCoordinate2D {
public func coordinate(inRadius radius: Double, fromCenter center: Location) -> Location {
let y0 = center.latitude
let x0 = center.longitude

// Convert meters to degrees for radius
let radiusInDegrees = radius / 111300.0

// Random point in circle
#if swift(>=4.2)
let u = Double.random(in: 0..<Double.greatestFiniteMagnitude) / 0xFFFFFFFF
let v = Double.random(in: 0..<Double.greatestFiniteMagnitude) / 0xFFFFFFFF
#else
let u = Double(arc4random()) / 0xFFFFFFFF
let v = Double(arc4random()) / 0xFFFFFFFF
#endif
let w = radiusInDegrees * sqrt(u)
let t = 2 * .pi * v
let x = w * cos(t)
Expand All @@ -103,6 +116,6 @@ public final class Address: Generator {
let foundLatitude = y + y0
let foundLongitude = newx + x0

return CLLocationCoordinate2D.init(latitude: foundLatitude, longitude: foundLongitude)
return Location(latitude: foundLatitude, longitude: foundLongitude)
}
}
9 changes: 8 additions & 1 deletion Sources/Fakery/Generators/Commerce.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ public final class Commerce: Generator {
}

public func department(maximum: Int = 3, fixedAmount: Bool = false) -> String {
#if swift(>=4.2)
let amount = fixedAmount ? maximum : 1 + Int.random(in: 0..<maximum)
#else
let amount = fixedAmount ? maximum : 1 + Int(arc4random_uniform(UInt32(maximum)))

#endif
let fetchedCategories = categories(amount)
let count = fetchedCategories.count

Expand All @@ -30,7 +33,11 @@ public final class Commerce: Generator {

public func price() -> Double {
let arc4randoMax: Double = 0x100000000
#if swift(>=4.2)
return floor(Double((Double(UInt32.random(in: 0..<UInt32.max)) / arc4randoMax) * 100.0) * 100) / 100.0
#else
return floor(Double((Double(arc4random()) / arc4randoMax) * 100.0) * 100) / 100.0
#endif
}

// MARK: - Helpers
Expand Down
6 changes: 5 additions & 1 deletion Sources/Fakery/Generators/Company.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ public final class Company: Generator {
}

public func logo() -> String {
let number = Int(arc4random_uniform(13)) + 1
#if swift(>=4.2)
let number = Int.random(in: 0..<13) + 1
#else
let number = Int(arc4random_uniform(13)) + 1
#endif
return "https://pigment.github.io/fake-logos/logos/medium/color/\(number).png"
}
}
11 changes: 9 additions & 2 deletions Sources/Fakery/Generators/Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,24 @@ public class Generator {
let count = UInt32(Constants.numbers.count)

return String(string.enumerated().map { (index, item) in
let numberIndex = index == 0 ? arc4random_uniform(count - 1) :
arc4random_uniform(count)
#if swift(>=4.2)
let numberIndex = index == 0 ? UInt32.random(in: 0..<(count - 1)) : UInt32.random(in: 0..<count)
#else
let numberIndex = index == 0 ? arc4random_uniform(count - 1) : arc4random_uniform(count)
#endif
let char = Constants.numbers[Int(numberIndex)]
return String(item) == "#" ? char : item
})
}

public func letterify(_ string: String) -> String {
return String(string.enumerated().map { _, item in
#if swift(>=4.2)
let char = Constants.uppercaseLetters.randomElement() ?? Character("")
#else
let count = UInt32(Constants.uppercaseLetters.count)
let char = Constants.uppercaseLetters[Int(arc4random_uniform(count))]
#endif
return String(item) == "?" ? char : item
})
}
Expand Down
38 changes: 33 additions & 5 deletions Sources/Fakery/Generators/Internet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,23 @@ public final class Internet: Generator {
}

public func username(separator: String? = nil) -> String {
#if swift(>=4.2)
let lastRandomComponent = Int.random(in: 0..<10000)
#else
let lastRandomComponent = arc4random_uniform(10000)
#endif
var components: [String] = [
generate("name.first_name"),
generate("name.last_name"),
"\(arc4random_uniform(10000))"
"\(lastRandomComponent)"
]

let randomCount = UInt32(components.count) - 1
let count = Int(arc4random_uniform(randomCount) + randomCount)
let randomCount = components.count - 1
#if swift(>=4.2)
let count = Int.random(in: 0..<randomCount) + randomCount
#else
let count = Int(arc4random_uniform(UInt32(randomCount)) + UInt32(randomCount))
#endif

var gap = ""
if let sep = separator {
Expand Down Expand Up @@ -61,8 +70,12 @@ public final class Internet: Generator {

public func safeEmail() -> String {
let topLevelDomains = ["org", "com", "net"]
#if swift(>=4.2)
let topLevelDomain = topLevelDomains.randomElement() ?? ""
#else
let count = UInt32(topLevelDomains.count)
let topLevelDomain = topLevelDomains[Int(arc4random_uniform(count))]
#endif

return [username(), "example." + topLevelDomain].joined(separator: "@")
}
Expand All @@ -72,26 +85,41 @@ public final class Internet: Generator {
let diffLength = maximumLength - minimumLength

if diffLength > 0 {
#if swift(>=4.2)
let diffRandom = Int.random(in: 0..<diffLength + 1)
#else
let diffRandom = Int(arc4random_uniform(UInt32(diffLength + 1)))
#endif
temp += lorem.characters(amount: diffRandom)
}

return temp
}

public func ipV4Address() -> String {
#if swift(>=4.2)
let randomNumber = UInt32.random(in: 0..<UInt32.max) % 253
#else
let randomNumber = arc4random() % 253
#endif
let ipRand = {
2 + arc4random() % 253
2 + randomNumber
}

return String(format: "%d.%d.%d.%d", ipRand(), ipRand(), ipRand(), ipRand())
}

public func ipV6Address() -> String {
#if swift(>=4.2)
let randomNumber = UInt32.random(in: 0..<UInt32.max) % 65536
#else
let randomNumber = arc4random() % 65536
#endif

var components: [String] = []

for _ in 0..<8 {
components.append(String(format: "%X", arc4random() % 65536))
components.append(String(format: "%X", randomNumber))
}

return components.joined(separator: ":")
Expand Down
6 changes: 5 additions & 1 deletion Sources/Fakery/Generators/Lorem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ public final class Lorem: Generator {

if amount > 0 {
for _ in 0..<amount {
let char = Character(UnicodeScalar(arc4random() % (122-97) + 97)!)
#if swift(>=4.2)
let char = Character(UnicodeScalar(Int.random(in: 0..<Int.max) % (122-97) + 97)!)
#else
let char = Character(UnicodeScalar(arc4random() % (122-97) + 97)!)
#endif
chars.append(char)
}
}
Expand Down
24 changes: 22 additions & 2 deletions Sources/Fakery/Generators/Number.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Foundation
#if !os(Linux)
import CoreGraphics
#endif

public final class Number {
fileprivate var lastUsedId: Int64 = 0
Expand All @@ -10,8 +12,12 @@ public final class Number {

public func randomInt(min: Int = 0, max: Int = 1000) -> Int {
var i: Int = 0
#if swift(>=4.2)
i = Int.random(in: i..<Int.max)
#else
arc4random_buf(&i, MemoryLayout.size(ofValue: i))
i = i & Int.max // Make the number positive
#endif

if max >= 0 && max - Int.max >= min {
return min + i
Expand All @@ -21,19 +27,33 @@ public final class Number {
}

public func randomFloat(min: Float = 0, max: Float = 1000) -> Float {
#if swift(>=4.2)
return (Float.random(in: 0..<Float.greatestFiniteMagnitude) / Float.greatestFiniteMagnitude) * (max - min) + min
#else
return (Float(arc4random()) / Float(UInt32.max)) * (max - min) + min
#endif
}

#if !os(Linux)
public func randomCGFloat(min: CGFloat = 0, max: CGFloat = 1000) -> CGFloat {
return CGFloat(Float(arc4random()) / Float(UInt32.max)) * (max - min) + min
}
#endif

public func randomDouble(min: Double = 0, max: Double = 1000) -> Double {
#if swift(>=4.2)
return (Double.random(in: 0..<Double.greatestFiniteMagnitude) / Double.greatestFiniteMagnitude) * (max - min) + min
#else
return (Double(arc4random()) / Double(UInt32.max)) * (max - min) + min
#endif
}

public func increasingUniqueId() -> Int {
OSAtomicIncrement64(&lastUsedId)
#if os(Linux)
// increasing just by one on linux due to the lack of an method like OSAtomicIncrement64
lastUsedId += 1
#else
OSAtomicIncrement64(&lastUsedId)
#endif
return Int(lastUsedId)
}
}
1 change: 1 addition & 0 deletions Tests/Fakery/Generators/DateSpec.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Quick
import Nimble
import Foundation
@testable import Fakery

final class DateSpec: QuickSpec {
Expand Down
2 changes: 2 additions & 0 deletions Tests/Fakery/Generators/NumberSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ final class NumberSpec: QuickSpec {
expect(number.randomFloat(min:5, max:7)) >= 5
}

#if !os(Linux)
it("creates random CGFloats") {
expect(number.randomCGFloat()) <= 1000
expect(number.randomCGFloat()) >= 0
Expand All @@ -59,6 +60,7 @@ final class NumberSpec: QuickSpec {
expect(number.randomCGFloat(min:5, max:7)) <= 7
expect(number.randomCGFloat(min:5, max:7)) >= 5
}
#endif

it("creates random Doubles") {
expect(number.randomDouble()) <= 1000
Expand Down
32 changes: 32 additions & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// LinuxMain.swift
//
//
// Created by Gustavo Campos on 3/16/19.
//

import XCTest
import Quick

@testable import FakeryTests

QCKMain([
ConfigSpec.self,
FakerSpec.self,
ParserSpec.self,
ProviderSpec.self,
AddressSpec.self,
AppSpec.self,
BusinessSpec.self,
CommerceSpec.self,
CompanySpec.self,
GeneratorSpec.self,
InternetSpec.self,
LoremSpec.self,
NameSpec.self,
NumberSpec.self,
PhoneNumberSpec.self,
TeamSpec.self,
BankSpec.self,
DateSpec.self
])

0 comments on commit c4edd97

Please sign in to comment.