Skip to content

Commit

Permalink
Clean up MockGen
Browse files Browse the repository at this point in the history
  • Loading branch information
eonist committed May 21, 2024
1 parent b43e9a9 commit 9ad0cd5
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 92 deletions.
28 changes: 13 additions & 15 deletions Sources/MockGen/MockGen+Const.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import FileSugar
* Data
*/
extension MockGen {
/**
/**
* Random bank name
*/
public static var randomBank: String? { Banks.line }
Expand All @@ -17,7 +17,6 @@ extension MockGen {
public static var randomNote: String? { Notes.line }
/**
* Random full name
*
* - Fixme: ⚠️️ Create firstNames, lastNames, fullNames? this is now in use MockGen.randomFirstName etc
*/
public static var randomFullName: String? { Names.line }
Expand All @@ -28,25 +27,19 @@ extension MockGen {
extension MockGen {
/**
* Random SSID generator
*
* This method generates a random SSID from the `SSIDS` array, which contains a list of common wifi network names. If the array is empty, the method returns `nil`.
*
* - Note: This method generates a random SSID from the `SSIDS` array, which contains a list of common wifi network names. If the array is empty, the method returns `nil`.
* - Returns: A random SSID string from the `SSIDS` array, or `nil` if the array is empty.
*/
public static var randomSSID: String? { SSIDS.line }
/**
* Random credit card issuer generator
*
* This method generates a random credit card issuer name from the `CreditCardIssuers` array, which contains a list of common credit card issuers. If the array is empty, the method returns `nil`.
*
* - Note: This method generates a random credit card issuer name from the `CreditCardIssuers` array, which contains a list of common credit card issuers. If the array is empty, the method returns `nil`.
* - Returns: A random credit card issuer name string from the `CreditCardIssuers` array, or `nil` if the array is empty.
*/
public static var randomIssuer: String? { CreditCardIssuers.line }
/**
* Random brand name generator
*
* This method generates a random brand name from the `Brands` array, which contains a list of common brand names. If the array is empty, the method returns `nil`.
*
* - Note: This method generates a random brand name from the `Brands` array, which contains a list of common brand names. If the array is empty, the method returns `nil`.
* - Returns: A random brand name string from the `Brands` array, or `nil` if the array is empty.
*/
public static var randomBrand: String? { Brands.brands.randomElement() }
Expand All @@ -64,10 +57,15 @@ extension MockGen {
* - Fixme: ⚠️️ Maybe use TWOFA lib to gen the secret? we cant access seclib here so no, using SecRan.randomSecret should work better
*/
public static var randomOTP: String? { // internal static let otps: [String] = ["otpauth://totp/test?secret=GEZDGNBV", "otpauth://hotp/test?secret=GEZDGNBV&algorithm=SHA512&digits=6&counter=1", "otpauth://totp/ACME%20Co:[email protected]?secret=GEZDGNBV&algorithm=SHA512&digits=6&period=30.0&issuer=ACME%20Co&image=https://www.images.com/image.png"]
// let secret: String = SecRan.randomSecret(min:8, max: 16) ?? "GEZDGNBV" //
let secret: String = CodeGen.generatePassword(length: 8, useLowercase: false, useNumbers: false, useSpecialChars: false) //
// let urlString: String = "otpauth://totp/test?secret=\(secret)"
// let urlString: String = "otpauth://hotp/test?secret=\(secret)&algorithm=SHA512&digits=6&counter=1"
// let secret: String = SecRan.randomSecret(min:8, max: 16) ?? "GEZDGNBV" //
let secret: String = CodeGen.generatePassword(
length: 8,
useLowercase: false,
useNumbers: false,
useSpecialChars: false
)
// let urlString: String = "otpauth://totp/test?secret=\(secret)"
// let urlString: String = "otpauth://hotp/test?secret=\(secret)&algorithm=SHA512&digits=6&counter=1"
let urlString: String = "otpauth://totp/ACME%20Co:[email protected]?secret=\(secret)&algorithm=SHA512&digits=6&period=30.0&issuer=ACME%20Co&image=https://www.images.com/image.png"
return urlString
// "otpauth://totp/test?secret=\(secret)" // GEZDGNBV
Expand Down
8 changes: 2 additions & 6 deletions Sources/MockGen/MockGen+Getter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
import Foundation
/**
* Email and website mock generators
*
* These methods generate mock email addresses and website URLs based on the specified name and brand. The `getEmail` method combines the lowercased name and website into an email address, while the `getWebsite` method returns the website for the specified brand. The `websites` dictionary contains a list of common brand names and their associated websites.
*
* - Note: These methods generate mock email addresses and website URLs based on the specified name and brand. The `getEmail` method combines the lowercased name and website into an email address, while the `getWebsite` method returns the website for the specified brand. The `websites` dictionary contains a list of common brand names and their associated websites.
* - Note: These methods are marked as `internal` to ensure they can only be accessed within the same module.
*/
extension MockGen {
/**
* Email mock generator
*
* - Parameters:
* - name: The name to use in the email address.
* - brand: The brand to use in the email address.
Expand All @@ -22,7 +19,6 @@ extension MockGen {
}
/**
* Website mock generator
*
* - Parameter brand: The brand to use in the website URL.
* - Returns: A mock website URL string based on the specified brand, or `nil` if the website for the brand is not found.
*/
Expand All @@ -35,7 +31,7 @@ extension MockGen {
}
/**
* Random first name
* - Fixme: ⚠️️ add random last name?
* - Fixme: ⚠️️ Add random last name?
*/
public static var randomFirstName: String? {
guard let fullName: String = randomFullName else { Swift.print("no name"); return nil }
Expand Down
15 changes: 6 additions & 9 deletions Sources/MockGen/MockGen+Util.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ import JSONSugar
import FileSugar
/**
* Utility methods
*
* These methods provide utility functionality for `MockGen`, such as generating random dates and boolean values with different probabilities of being true.
*
* - Description: These methods provide utility functionality for `MockGen`, such as generating random dates and boolean values with different probabilities of being true.
* - Note: These methods are marked as `internal` to ensure they can only be accessed within the same module. They are also wrapped in a `DEBUG` conditional compilation block to ensure they are only included in debug builds.
*/
extension MockGen {
/**
* Random date generator
* - Example: let randomDate = MockGen.randomDate // "06/23/2022"
* - Returns: A random date string in the short date format (MM/dd/yyyy).
* ## Examples:
* let randomDate = MockGen.randomDate // "06/23/2022"
* - Returns: A random date string in the short date format (MM/dd/yyyy)
*/
public static var randomDate: String {
let msec: Int64 = .init(arc4random_uniform(UInt32(600_000 - 300_000 + 1))) + 300_000 // Generate a random number of milliseconds between 300,000 and 600,000
Expand All @@ -23,11 +22,9 @@ extension MockGen {
}
/**
* Random boolean generator
*
* - Parameter chance: The probability of the boolean value being true, expressed as an integer where 1/chance is the probability (e.g. 3 = 1/16th chance, 4 = 1/32th chance).
* - Returns: A random boolean value with the specified probability of being true.
*
* Example usage:
* - Returns: A random boolean value with the specified probability of being true
* ## Examples:
* let randomBool = MockGen.getRandomBool(3) // 1/4 chance of returning true
*/
public static func getRandomBool(_ chance: Int) -> Bool {
Expand Down
7 changes: 3 additions & 4 deletions Sources/MockGen/MockGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import FileSugar // Provides convenience methods for working with files and dire
* MockGen
* This class is used for generating mock data for unit tests. It includes methods for generating random passwords, as well as adding user names, emails, and notes via JSON files. It's currently only used for debugging purposes, but could potentially be spun out into its own repository or framework and launched on a platform like Product Hunt.
* - Note: We can use this in other frameworks if we use `@testable import Account`
* - Fixme:
* - Add the random password methods from `UITests`. (or move them?)
* - Consider spinning MockGen out into its own repository or framework. (We can make the account an extension in the account lib etc)
* - Add support for user names, emails, and notes via JSON files.
* - Fixme: ⚠️️ Add the random password methods from `UITests`. (or move them?)
* - Fixme: ⚠️️ Consider spinning MockGen out into its own repository or framework. (We can make the account an extension in the account lib etc)
* - Fixme: ⚠️️ Add support for user names, emails, and notes via JSON files.
*/
public class MockGen {} // Fix: rename testing folder to mockgen
#endif
12 changes: 9 additions & 3 deletions Sources/MockGen/util/CSVKind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ import Foundation
* - Remark: The protocol defines a file name and an array of lines for the CSV kind.
*/
protocol CSVKind {
static var fileName: String { get } // The file name for the CSV kind
static var lines: [String] { get } // An array of lines for the CSV kind
/**
* The file name for the CSV kind
*/
static var fileName: String { get }
/**
* An array of lines for the CSV kind
*/
static var lines: [String] { get }
}
/**
* A helper extension for the CSVKind protocol.
* A helper extension for the CSVKind protocol
* - Remark: The extension provides a function for getting a random line from the array of lines for the CSV kind.
*/
extension CSVKind {
Expand Down
16 changes: 8 additions & 8 deletions Sources/MockGen/util/CSVUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ import Logger
*/
class CSVUtil {
/**
* Returns an array of strings representing the lines in the specified CSV file.
* - Parameters:
* - fileName: The name of the CSV file to read.
* - seperator: The separator used in the CSV file. Default is ",\n".
* - Returns: An array of strings representing the lines in the specified CSV file.
* Returns an array of strings representing the lines in the specified CSV file
* - Remark: The function reads the contents of the CSV file and splits it into an array of strings using the specified separator.
* - Parameters:
* - fileName: The name of the CSV file to read
* - seperator: The separator used in the CSV file. Default is ",\n"
* - Returns: An array of strings representing the lines in the specified CSV file
*/
static func list(fileName: String, seperator: String = ",\n") -> [String] {
content(fileName: fileName)?.components(separatedBy: seperator) ?? []
}
/**
* Returns the contents of the specified CSV file as a string.
* Returns the contents of the specified CSV file as a string
* - Remark: The function reads the contents of the CSV file and returns it as a string
* - Parameters:
* - fileName: The name of the CSV file to read.
* - Returns: The contents of the specified CSV file as a string.
* - Remark: The function reads the contents of the CSV file and returns it as a string.
* - Returns: The contents of the specified CSV file as a string
*/
static func content(fileName: String) -> String? {
guard let resourcePath: String = Foundation.Bundle.module.resourcePath else {
Expand Down
13 changes: 7 additions & 6 deletions Sources/MockGen/util/random/CodeGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import Foundation
public enum CodeGen {
/**
* A utility function for generating a random password string of a given length.
* The password string can include uppercase letters, lowercase letters, numbers, and special characters.
* - Parameter length: The length of the password string to generate.
* - Parameter useLowercase: Whether to include lowercase letters in the password string. Default is `true`.
* - Parameter useNumbers: Whether to include numbers in the password string. Default is `true`.
* - Parameter useSpecialChars: Whether to include special characters in the password string. Default is `true`.
* - Note: The password string can include uppercase letters, lowercase letters, numbers, and special characters.
* - Parameters:
* - length: The length of the password string to generate.
* - useLowercase: Whether to include lowercase letters in the password string. Default is `true`.
* - useNumbers: Whether to include numbers in the password string. Default is `true`.
* - useSpecialChars: Whether to include special characters in the password string. Default is `true`.
* - Returns: A random password string of the given length.
* ## Examples:
* CodeGen.generatePassword(length: Int.random(in: 2...48)) // $3K#j9@Lm
Expand Down Expand Up @@ -41,7 +42,7 @@ public enum CodeGen {
* Const
*/
extension CodeGen {
// fix we can also use Alphabet class. maybe copy it here? yepp!
// - Fixme: ⚠️️ we can also use Alphabet class. maybe copy it here? yepp!
private static let base: String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" // Define the base set of characters to use in the password string
private static let lowerChars: String = "abcdefghijklmnopqrstuvwxyz" // Define lowercase letters to use in the password string
private static let numberChars: String = "1234567890" // Define numbers to use in the password string
Expand Down
3 changes: 2 additions & 1 deletion Sources/MockGen/util/random/SecRan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public class SecRan {
}
/**
* Generates a random secret string of the specified length.
* - Fixme: ⚠️️ make it throw? and add max as an option?
* - Fixme: ⚠️️ Make it throw? and add max as an option?
* - Fixme: ⚠️️ Doc each line, use copilot
* ## Examples:
* - `randomSecret(length: 8)` returns "UGT7+4P2"
* - `randomSecret(length: 9)` returns "UZYVJ1OS2"
Expand Down
11 changes: 5 additions & 6 deletions Sources/MockGen/util/random/String+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ extension String {
*/
fileprivate static let pwdLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%&()0123456789"
/**
* Generates a random password string of the specified length.
* Generates a random password string of the specified length
* - Remark: The function generates a password string by randomly selecting characters from a set of letters that includes lowercase letters, uppercase letters, numbers, and special characters.
* - Fixme: ⚠️️ doc each line, use copilot
* - Parameters:
* - pwdLength: The length of the password string. Must be between 2 and 48.
* - Returns: A random password string of the specified length.
* - Remark: The function generates a password string by randomly selecting characters from a set of letters that includes lowercase letters, uppercase letters, numbers, and special characters.
* - Returns: A random password string of the specified length
* ## Examples:
* print(String.randomPassword(pwdLength: Int.random(in: 2...48))) // $3K#j9@Lm
*/
Expand All @@ -32,7 +33,7 @@ extension String {
fileprivate enum Statics {
/**
* A range of Unicode scalar values that includes lowercase letters, uppercase letters, and numbers.
* - Description: Define a range of Unicode scalar values that includes lowercase letters, uppercase letters, and numbers
* - Description: Define a range of Unicode scalar values that includes lowercase letters, uppercase letters, and numbers
* - Remark: The range is used to generate a set of characters that can be used to create a random string.
*/
static let scalars: FlattenSequence<[ClosedRange<UInt32>]> = [
Expand All @@ -48,10 +49,8 @@ extension String {
/**
* Generates a random string of the specified length.
* - Description: The function generates a random string by selecting characters from an array that includes lowercase letters, uppercase letters, and numbers.
*
* - Parameter length: The length of the random string.
* - Returns: A random string of the specified length.
*
* Example usage:
* let randomString = String.random(length: 10) // Generates a random string of length 10. aB48xhKk2Yc
*/
Expand Down
Loading

0 comments on commit 9ad0cd5

Please sign in to comment.