Skip to content

Commit

Permalink
Minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
eonist committed Jan 22, 2025
1 parent d8d273e commit af95797
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
13 changes: 12 additions & 1 deletion Sources/MockGen/MockGen+Getter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,20 @@ extension MockGen {
}
return String(first)
}
// fixme: add doc
/**
* Random Address Generator
* - Description: Generates a random street address using the `AddressGenerator` class.
* - Returns: A random street address string.
*/
public static var randomAddress: String {
return AddressGenerator.randomAddress()
}
// fixme: add doc
// fixme: add unit-test
func static func randomDate(from startDate: Date, to endDate: Date) -> Date {
let timeInterval = endDate.timeIntervalSince(startDate)
let randomInterval = TimeInterval.random(in: 0...timeInterval)
return startDate.addingTimeInterval(randomInterval)
}
}
#endif
45 changes: 45 additions & 0 deletions Sources/MockGen/assets/AddressGenerator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import Foundation

/**
* AddressGenerator is a utility class for generating random addresses.
* - Description: This class provides static methods and data arrays to generate random addresses,
* including street numbers, street names, street types, cities, states, and zip codes.
* It can be used for testing, mocking, or any other situation where random address data is needed.
*/
public class AddressGenerator {
/// An array of common street names.
private static let streetNames = ["Main", "High", "Maple", "Oak", "Pine", "Cedar", "Elm", "Washington", "Lake", "Hill"]

/// An array of common street types.
private static let streetTypes = ["St", "Ave", "Blvd", "Rd", "Ln", "Way"]

/// An array of major US cities.
private static let cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]

/// An array of US state abbreviations.
private static let states = ["NY", "CA", "IL", "TX", "AZ"]

/// An array of example zip codes corresponding to the cities.
private static let zipCodes = ["10001", "90001", "60601", "77001", "85001"]

/**
* Generates a random address string.
* - Description: Combines random elements from the street names, street types, cities, states, and zip codes arrays
* to create a realistic random address. The street number is randomly generated between 1 and 9999.
* - Returns: A string representing a random address in the format "1234 Main St, City, State ZIP".
* ## Examples:
* ```
* let address = AddressGenerator.randomAddress()
* print(address) // "123 Maple Ave, Chicago, IL 60601"
* ```
*/
public static func randomAddress() -> String {
let number = Int.random(in: 1...9999)
let streetName = streetNames.randomElement()!
let streetType = streetTypes.randomElement()!
let city = cities.randomElement()!
let state = states.randomElement()!
let zipCode = zipCodes.randomElement()!
return "\(number) \(streetName) \(streetType), \(city), \(state) \(zipCode)"
}
}
45 changes: 45 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[![Tests](https://github.com/sentryco/MockGen/actions/workflows/tests.yml/badge.svg)](https://github.com/sentryco/MockGen/actions/workflows/tests.yml)
[![codebeat badge](https://codebeat.co/badges/6f474052-1ae2-4c61-b72f-dcd23e442278)](https://codebeat.co/projects/github-com-sentryco-mockgen-main)
[![Swift Version](https://img.shields.io/badge/Swift-5.9-orange.svg)](https://swift.org)
![Platforms](https://img.shields.io/badge/platforms-macOS%20|%20iOS-blue.svg)

# 🧪 MockGen

Expand Down Expand Up @@ -84,6 +86,49 @@ let randomStreetAddress = MockGen.randomStreetAddress // Returns a random street
let randomCity = MockGen.randomCity // Returns a random city name.
let randomState = MockGen.randomState // Returns a random state abbreviation.
let randomZipCode = MockGen.randomZipCode // Returns a random zip code.
// e.g., "123 Main St, Springfield, IL 62704"
let randomAddress = MockGen.randomAddress // Returns a random street address.
let randomSSID = MockGen.randomSSID // Returns a random Wi-Fi SSID.
let randomIssuer = MockGen.randomIssuer // Returns a random issuer name.
let randomBrand = MockGen.randomBrand // Returns a random brand name.
let randomOTP = MockGen.randomOTP // Returns a random OTP URL.
```

## Generating Random User Profiles

MockGen can generate comprehensive user profiles combining multiple data points:

```swift
struct UserProfile {
let fullName: String
let email: String
let phoneNumber: String
let address: String
}

extension MockGen {
static var randomUserProfile: UserProfile {
let fullName = randomFullName ?? "John Doe"
let email = getEmail(name: fullName, brand: randomBrand ?? "example.com") ?? "[email protected]"
let phoneNumber = randomPhoneNumber()
let address = randomAddress ?? "123 Main St"
return UserProfile(fullName: fullName, email: email, phoneNumber: phoneNumber, address: address)
}
}

// Usage
let userProfile = MockGen.randomUserProfile
print(userProfile.fullName) // e.g., "Jane Smith"
print(userProfile.email) // e.g., "[email protected]"
print(userProfile.phoneNumber) // e.g., "(555) 123-4567"
print(userProfile.address) // e.g., "456 Elm St, Anytown, CA 90210"
```

The `SecRan` class provides methods for generating secure random secrets. Including usage examples will demonstrate how to generate cryptographically secure secrets.

```swift
let secret = SecRan.randomSecret(length: 16) // Generates a random secret string of length 16.
let secretInRange = SecRan.randomSecret(min: 8, max: 32) // Generates a random secret string with length between 8 and 32.
```

## FAQ:
Expand Down

0 comments on commit af95797

Please sign in to comment.