Swiftpack.co - Package - dankogai/swift-tap

build status

swift-tap

Test Anything Protocol (TAP) for Swift

Usage

Add this project to your Package.swift.

import PackageDescription
let package = Package(
  // ...
  dependencies: [
    .Package(
      url: "https://github.com/dankogai/swift-tap.git", from: "0.5.0"
    )
  ],
  // ...
)

  • write [main.swift] like below:
import TAP
let test = TAP(tests:3)
test.ok(42+0.195 == 42.195, "42 + 0.195 == 42.195") // ok 1 - 42 + 0.195 == 42.195
test.eq(42+0.195,   42.195, "42 + 0.195 is 42.195") // ok 2 - 42 + 0.195 is 42.195
test.ne(42+0.195,   42.195, "42 + 0.195 is not 42") // ok 3 - 42 + 0.195 is not 42
test.done() 
  • build and test like below:
$ swift run && prove .build/debug/main
./main .. ok   
All tests successful.
Files=1, Tests=2,  0 wallclock secs ( 0.02 usr +  0.00 sys =  0.02 CPU)
Result: PASS

./main actually prints the following:

1..3
ok 1 - 42 + 0.195 == 42.195
ok 2 - 42 + 0.195 is 42.195
ok 3 - 42 + 0.195 is not 42

test without plan

You should specify the number of tests to run via TAP(tests:Int) or .plan(_:Int) but this is optional. But when you do so you cannot omit .done() which prints 1..number_of_tests that the TAP protocol demands.

ok 1 - 42 + 0.195 == 42.195
ok 2 - 42 + 0.195 is 42.195
ok 3 - 42 + 0.195 is not 42
1..2

.ok, .eq and .ne

.ok

Checks if predicate is true.

func ok(@autoclosure predicate:()->Bool, _ message:String = "")->Bool

.eq

Checks if actual == expected. Analogous to .ok(actual == expected) but it is more informative when it is not ok.

On the other hand both actual and expected must be Equatable. So:

  • use .eq whenever you can.
  • use .ok as a last resort.
public func eq<T:Equatable>(actual:T, _ expected:T, _ message:String = "")->Bool
public func eq<T:Equatable>(actual:T?, _ expected:T?, _ message:String = "")->Bool
public func eq<T:Equatable>(actual:[T], _ expected:[T], _ message:String = "")->Bool
public func eq<K:Hashable,V:Equatable>(actual:[K:V], _ expected:[K:V], _ message:String = "")->Bool

As you can tell from the type signatures, .eq and .ne accepts arrays and dictionaries if they are equatable.

.ne

The opposite of .eq.

public func ne<T:Equatable>(actual:T, _ expected:T, _ message:String = "")->Bool
public func ne<T:Equatable>(actual:T?, _ expected:T?, _ message:String = "")->Bool
public func ne<T:Equatable>(actual:[T], _ expected:[T], _ message:String = "")->Bool
public func ne<K:Hashable,V:Equatable>(actual:[K:V], _ expected:[K:V], _ message:String = "")->Bool

Why NOT XCTest?

  • Still way too fancy and resource-demanding for some occasions

See Also

  • https://testanything.org
  • https://en.wikipedia.org/wiki/Test_Anything_Protocol

Github

link
Stars: 5
Help us keep the lights on

Dependencies

Used By

Total: 0