Skip to content

Commit

Permalink
Merge pull request #29 from YusukeHosonuma/support/instance-method
Browse files Browse the repository at this point in the history
support: instance method
  • Loading branch information
YusukeHosonuma authored Mar 15, 2020
2 parents 201f107 + 10d8d73 commit 5e14301
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 62 deletions.
92 changes: 33 additions & 59 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,69 +84,43 @@ $ make snippets
import SwiftParamTest
import XCTest

///
/// Test to two argument function
///
func testIndent() {s
assert(to: indent) {
// basic
args(("Hello", 0), expect: "Hello")
args(("Hello", 2), expect: " Hello")
args(("Hello", 4), expect: " Hello")

// operator
expect(("Hello", 0) ==> "Hello")
expect(("Hello", 2) ==> " Hello")
expect(("Hello", 4) ==> " Hello")
}
}

///
/// Test for operator
///
func testOperator() {
assert(to: +) {
// basic
args((1, 1), expect: 2)
args((1, 2), expect: 3)
args((2, 2), expect: 4)

// operator
expect((1, 1) ==> 2)
expect((1, 2) ==> 3)
expect((2, 2) ==> 4)
}
}

///
/// Test for method of object
///
func testObject() {
let calc = Calculator(initialValue: 10)

assert(to: calc.add) {
// basic
args(1, expect: 11)
args(2, expect: 12)
args(3, expect: 13)

// operator
expect(1 ==> 11)
expect(2 ==> 12)
expect(3 ==> 13)
class ExampleTests: XCTestCase {

func testExample() {
//
// for `function`
//
assert(to: abs) {
args( 0, expect: 0)
args( 2, expect: 2)
args(-2, expect: 2)
}

assert(to: calc.subtraction) {
// basic
args(1, expect: 9)
args(2, expect: 8)
args(3, expect: 7)
//
// for `operator`
//
assert(to: +) {
args((1, 1), expect: 2)
args((1, 2), expect: 3)
args((2, 2), expect: 4)
}

// operator
expect(1 ==> 9)
expect(2 ==> 8)
expect(3 ==> 7)
//
// for `instance method` (when receiver is not fixed)
//
assert(to: String.hasPrefix, expect: [
args(("hello", "he"), expect: true),
args(("hello", "HE"), expect: false),
])

//
// for `instance method` (when receiver is fixed)
//
assert(to: "hello".hasPrefix) {
args("he", expect: true)
args("HE", expect: false)
}
}
}
```

Expand Down
31 changes: 30 additions & 1 deletion Sources/DSL/BasicDSL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,36 @@ public func assert<T, R: Equatable>(
with customAssertion: CustomAssertion<R>? = nil,
expect rows: [Row<T, R>]
) {
ParameterizedTest(target: target, customAssertion: customAssertion).execute(with: rows)
ParameterizedTest(target: target,
customAssertion: customAssertion).execute(with: rows)
}

/// Assert to `target` function with `rows` parameters.
/// - Parameters:
/// - instanceMethod: test target instance methods
/// - customAssertion: custom assert function (default: `XCTAssertEqual`)
/// - rows: test parameteres
public func assert<T, R: Equatable>(
to instanceMethod: @escaping (T) -> () -> R,
with customAssertion: CustomAssertion<R>? = nil,
expect rows: [Row<T, R>]
) {
ParameterizedTest(target: flatten(instanceMethod),
customAssertion: customAssertion).execute(with: rows)
}

/// Assert to `target` function with `rows` parameters.
/// - Parameters:
/// - instanceMethod: test target instance methods
/// - customAssertion: custom assert function (default: `XCTAssertEqual`)
/// - rows: test parameteres
public func assert<T1, T2, R: Equatable>(
to instanceMethod: @escaping (T1) -> (T2) -> R,
with customAssertion: CustomAssertion<R>? = nil,
expect rows: [Row<(T1, T2), R>]
) {
ParameterizedTest(target: flatten(instanceMethod),
customAssertion: customAssertion).execute(with: rows)
}

/// Definition test parameter and expected value.
Expand Down
33 changes: 31 additions & 2 deletions Sources/DSL/FunctionBuilderDSL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,38 @@ public struct ParameterBuilder<T, R> {
/// - customAssertion: custom assertion function
@available(swift 5.1)
public func assert<T, R: Equatable>(
to targetFunction: @escaping (T) -> R,
to function: @escaping (T) -> R,
with customAssertion: CustomAssertion<R>? = nil,
@ParameterBuilder <T, R> rows: () -> [Row<T, R>]
) {
ParameterizedTest(target: targetFunction, customAssertion: customAssertion).execute(with: rows())
ParameterizedTest(target: function, customAssertion:
customAssertion).execute(with: rows())
}

/// Create `Expect` for start parameterized-test.
/// - Parameters:
/// - instanceMethod: test target instance method
/// - customAssertion: custom assertion function
@available(swift 5.1)
public func assert<T, R: Equatable>(
to instanceMethod: @escaping (T) -> () -> R,
with customAssertion: CustomAssertion<R>? = nil,
@ParameterBuilder <T, R> rows: () -> [Row<T, R>]
) {
ParameterizedTest(target: flatten(instanceMethod),
customAssertion: customAssertion).execute(with: rows())
}

/// Create `Expect` for start parameterized-test.
/// - Parameters:
/// - instanceMethod: test target instance method
/// - customAssertion: custom assertion function
@available(swift 5.1)
public func assert<T1, T2, R: Equatable>(
to instanceMethod: @escaping (T1) -> (T2) -> R,
with customAssertion: CustomAssertion<R>? = nil,
@ParameterBuilder <(T1, T2), R> rows: () -> [Row<(T1, T2), R>]
) {
ParameterizedTest(target: flatten(instanceMethod),
customAssertion: customAssertion).execute(with: rows())
}
16 changes: 16 additions & 0 deletions Sources/Util.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Util.swift
// SwiftParamTest
//
// Created by Yusuke Hosonuma on 2020/03/15.
//

/// uncurry instance-method that has no arguments (e.g. `String#lowercase`)
func flatten<T, R: Equatable>(_ f: @escaping (T) -> () -> R) -> (T) -> R {
{ (t: T) -> R in f(t)() }
}

/// uncurry instance-method that has arguments (e.g. `String#hasPrefix`)
func flatten<T1, T2, R: Equatable>(_ f: @escaping (T1) -> (T2) -> R) -> ((T1, T2) -> R) {
{ (t1: T1, t2: T2) -> R in f(t1)(t2) }
}
4 changes: 4 additions & 0 deletions SwiftParamTest.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
/* End PBXAggregateTarget section */

/* Begin PBXBuildFile section */
E52C7079241E328800D6BE41 /* Util.swift in Sources */ = {isa = PBXBuildFile; fileRef = E52C7078241E328800D6BE41 /* Util.swift */; };
E55DBAB2240916C3009D5C20 /* FunctionBuilderDSLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E55DBAB1240916C3009D5C20 /* FunctionBuilderDSLTests.swift */; };
E5A38544241A3E62006501FA /* BasicDSLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E5A38543241A3E62006501FA /* BasicDSLTests.swift */; };
E5A38546241A3F20006501FA /* Function.swift in Sources */ = {isa = PBXBuildFile; fileRef = E5A38545241A3F20006501FA /* Function.swift */; };
Expand Down Expand Up @@ -50,6 +51,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
E52C7078241E328800D6BE41 /* Util.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Util.swift; sourceTree = "<group>"; };
E55DBAB1240916C3009D5C20 /* FunctionBuilderDSLTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FunctionBuilderDSLTests.swift; sourceTree = "<group>"; };
E5A0684B2407B00800FF06E4 /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Platforms/iPhoneOS.platform/Developer/Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; };
E5A38543241A3E62006501FA /* BasicDSLTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BasicDSLTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -169,6 +171,7 @@
children = (
E5A3854B241A41D4006501FA /* DSL */,
OBJ_8 /* Core.swift */,
E52C7078241E328800D6BE41 /* Util.swift */,
);
path = Sources;
sourceTree = SOURCE_ROOT;
Expand Down Expand Up @@ -260,6 +263,7 @@
OBJ_29 /* Core.swift in Sources */,
OBJ_30 /* FunctionBuilderDSL.swift in Sources */,
E5A3854A241A400D006501FA /* BasicDSL.swift in Sources */,
E52C7079241E328800D6BE41 /* Util.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
18 changes: 18 additions & 0 deletions Tests/SwiftParamTestTests/DSL/BasicDSLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ class BasicDSLTests: XCTestCase {
args((1, 2), expect: 3),
args((2, 2), expect: 4),
])

// instance method that has not argument
assert(to: String.lowercased, expect: [
args("hello", expect: "hello"),
args("HELLO", expect: "hello"),
])

// instance method that has arguments
assert(to: String.hasPrefix, expect: [
args(("hello", "he"), expect: true),
args(("hello", "HE"), expect: false),
])

// instance method that receiver is fixed
assert(to: "hello".hasPrefix, expect: [
args("he", expect: true),
args("HE", expect: false),
])
}

func testCustomAssertion() {
Expand Down
18 changes: 18 additions & 0 deletions Tests/SwiftParamTestTests/DSL/FunctionBuilderDSLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ class DSLTests: XCTestCase {
args((1, 2), expect: 3)
args((2, 2), expect: 4)
}

// instance method that has not argument
assert(to: String.lowercased) {
args("hello", expect: "hello")
args("HELLO", expect: "hello")
}

// instance method that has arguments
assert(to: String.hasPrefix) {
args(("hello", "he"), expect: true)
args(("hello", "HE"), expect: false)
}

// instance method that receiver is fixed
assert(to: "hello".hasPrefix) {
args("he", expect: true)
args("HE", expect: false)
}
}

func testCustomAssertion() {
Expand Down

0 comments on commit 5e14301

Please sign in to comment.