Skip to content

Commit

Permalink
Merge pull request #36 from niilohlin/refactor-constructor
Browse files Browse the repository at this point in the history
Refactor constructor
  • Loading branch information
niilohlin authored Dec 2, 2018
2 parents 760fc2d + 2b95037 commit f452ee5
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 0.3
- Add alphanumeric string
- Add default transform argument to combine
- Add one-of generator based on system RNG.

## 0.2
- Add NonEmpty generator
Expand Down
4 changes: 4 additions & 0 deletions Tenta.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
8E8901A16D46FA19CAE712B1 /* Shrink.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E890E369D88E63EFFB794CC /* Shrink.swift */; };
8E89023F542BF0B8F434F853 /* Optional+Generatable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E890084098C32A0D6269434 /* Optional+Generatable.swift */; };
8E89048932464D7A5538C79C /* Property.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E890FF115A2715C7DDE9499 /* Property.swift */; };
8E89053F2C5959343E8BAE39 /* Constructor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E8905DF2ED554C905DD8B6A /* Constructor.swift */; };
8E8905DFAC6D8436A93F8DC8 /* Double+Generatable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E89038F86E00A552B1B8373 /* Double+Generatable.swift */; };
8E8906B90C67FCA8540DD929 /* Generatable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E890BE14D9CEC77ED2315A7 /* Generatable.swift */; };
8E8907E3EB2B315C63D98D23 /* SeededRandomNumberGeneratorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8E89037A71F17CA04A21C5E7 /* SeededRandomNumberGeneratorTests.swift */; };
Expand Down Expand Up @@ -55,6 +56,7 @@
8E89031F831C25B5D6457A77 /* Bool+Generatable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Bool+Generatable.swift"; sourceTree = "<group>"; };
8E89037A71F17CA04A21C5E7 /* SeededRandomNumberGeneratorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SeededRandomNumberGeneratorTests.swift; sourceTree = "<group>"; };
8E89038F86E00A552B1B8373 /* Double+Generatable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Double+Generatable.swift"; sourceTree = "<group>"; };
8E8905DF2ED554C905DD8B6A /* Constructor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Constructor.swift; sourceTree = "<group>"; };
8E8907BD0DC238A3B3872B0A /* Character+Generatable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Character+Generatable.swift"; sourceTree = "<group>"; };
8E8907EA7C17DE23D1AF85AC /* String+Generatable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+Generatable.swift"; sourceTree = "<group>"; };
8E890A83B74300654007736F /* GeneratorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GeneratorTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -120,6 +122,7 @@
8E8902631B1619B438F3FCFD /* Combine.swift */,
8E890A0AC4A8E52CE096E084 /* Generators */,
8E890BDC993B78EC28625829 /* XCTest+Property.swift */,
8E8905DF2ED554C905DD8B6A /* Constructor.swift */,
);
path = Tenta;
sourceTree = "<group>";
Expand Down Expand Up @@ -282,6 +285,7 @@
8E89023F542BF0B8F434F853 /* Optional+Generatable.swift in Sources */,
8E890091C35AF754B7E1E33E /* Bool+Generatable.swift in Sources */,
8E890D49B3993428C1825D12 /* XCTest+Property.swift in Sources */,
8E89053F2C5959343E8BAE39 /* Constructor.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
23 changes: 23 additions & 0 deletions Tenta/Constructor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Created by Niil Öhlin on 2018-12-02.
// Copyright (c) 2018 Niil Öhlin. All rights reserved.
//

import Foundation

public struct Constructor {
public var size: Size
public var rng: SeededRandomNumberGenerator

/// Recommended initializer.
init(size: Size, rng: inout SeededRandomNumberGenerator) {
self.size = size
self.rng = rng
}

/// Only use this if you know what you are doing.
init(size: Size) {
self.size = size
self.rng = SeededRandomNumberGenerator(seed: UInt64.random(in: 0...UInt64.max))
}
}
14 changes: 14 additions & 0 deletions Tenta/Generatable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,17 @@ import Foundation
public protocol Generatable {
static var generator: Generator<Self> { get }
}

extension Generatable {

/// Generate a value without its shrink tree.
public static func generate(using constructor: inout Constructor) -> Self {
return generator.generate(constructor.size, &constructor.rng).root()
}

/// Discouraged generator. Has side effects and are not reproducable
public static func generate(size: Size = 10) -> Self {
var constructor = Constructor(size: size)
return generate(using: &constructor)
}
}
14 changes: 0 additions & 14 deletions Tenta/Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@ import XCTest

public typealias Size = UInt

public struct Constructor {
public var size: Size
public var rng: SeededRandomNumberGenerator
init(size: Size, rng: inout SeededRandomNumberGenerator) {
self.size = size
self.rng = rng
}
}

/**
Generator is a wrapper for a function that generates a value with accompanying shrink values in a `RoseTree`
*/
Expand All @@ -36,11 +27,6 @@ public struct Generator<ValueToTest> {
return RoseTree<ValueToTest>(root: value)
}
}

/// Generate a value without its shrink tree.
public func generate(using constructor: inout Constructor) -> ValueToTest {
return self.generate(constructor.size, &constructor.rng).root()
}
}

public extension Generator {
Expand Down
19 changes: 12 additions & 7 deletions TentaTests/GeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,13 @@ class GeneratorTests: XCTestCase {

func testSimpleGenerator() {
let generator = Generator.simple { constructor -> ComplexTest in
let firstName = String.generator.generate(using: &constructor)
let lastName = String.generator.generate(using: &constructor)
let age = Int.generator.generate(using: &constructor)
let email = String.generator.generate(using: &constructor)
let address = String.generator.generate(using: &constructor)
let zipCode = String.generator.generate(using: &constructor)
let sex = String.generator.generate(using: &constructor)
let firstName = String.generate(using: &constructor)
let lastName = String.generate(using: &constructor)
let age = Int.generate(using: &constructor)
let email = String.generate(using: &constructor)
let address = String.generate(using: &constructor)
let zipCode = String.generate(using: &constructor)
let sex = String.generate(using: &constructor)
return ComplexTest(
firstName: firstName,
lastName: lastName,
Expand All @@ -194,6 +194,11 @@ class GeneratorTests: XCTestCase {
}
}

func testOneOfGenerator() {
let int = Int.generate()
XCTAssertEqual(int, int)
}

func testGenerateSet() {
assert(generator: Set<Int>.generator, shrinksTo: Set([5]), predicate: { set in
!set.contains(5)
Expand Down

0 comments on commit f452ee5

Please sign in to comment.