Skip to content

Commit 459e76f

Browse files
committed
Support using Spectre in a Playground
Closes kylef#4
1 parent 56573bc commit 459e76f

17 files changed

+130
-32
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ MODULESDIR = .conche/modules
22
LIBDIR = .conche/lib
33
SWIFTFLAGS = -I $(MODULESDIR) -L $(LIBDIR)
44
SWIFTC := swiftc
5-
SOURCES := Assert Context GlobalContext Case Failure Reporter Reporters
5+
SOURCES := Assert Context GlobalContext Case Failure Reporter Reporters Global
66
SOURCE_FILES = $(foreach file,$(SOURCES),Spectre/$(file).swift)
77

88
all: spectre example

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
BDD Framework for Swift
66

7+
78
## Usage
89

910
```swift
@@ -89,6 +90,13 @@ a failure.
8990
for Spectre. You can simply add a `test_spec` to your Conche podspec depending
9091
on Spectre and it will run your tests with `conche test`.
9192

93+
### Playground
94+
95+
You can use Spectre in an Xcode Playground, open `Spectre.playground` in
96+
this repository, failures are printed in the console.
97+
98+
![Spectre in an Xcode Playground](Screenshots/Playground.png)
99+
92100
### Manually
93101

94102
You can build Spectre as a Framework or a library and link against it.

Screenshots/Playground.png

59.6 KB
Loading

Spectre.playground/Contents.swift

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
struct Person : CustomStringConvertible {
2+
let name:String
3+
4+
init(name:String) {
5+
self.name = name
6+
}
7+
8+
var description:String {
9+
return name
10+
}
11+
}
12+
13+
describe("a person") {
14+
let person = Person(name: "Kyle")
15+
16+
$0.it("has a name") {
17+
try equal(person.name, "Katie")
18+
}
19+
20+
$0.it("returns the name as description") {
21+
try equal(person.description, "Kyle")
22+
}
23+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../Spectre/Assert.swift

Spectre.playground/Sources/Case.swift

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../Spectre/Case.swift
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../Spectre/Context.swift
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../Spectre/Failure.swift
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../Spectre/GlobalContext.swift
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class PlaygroundReporter : ContextReporter {
2+
var depth = 0
3+
4+
func print(message:String) {
5+
let indentation = String(count: depth * 2, repeatedValue: " " as Character)
6+
Swift.print("\(indentation)\(message)")
7+
}
8+
9+
func report(name:String, @noescape closure:ContextReporter -> ()) {
10+
print("\(name):")
11+
12+
++depth
13+
closure(self)
14+
--depth
15+
16+
print("")
17+
}
18+
19+
func addSuccess(name:String) {
20+
print("\(name)")
21+
}
22+
23+
func addFailure(name:String, failure: Failure) {
24+
print("\(name)")
25+
}
26+
}
27+
28+
let reporter = PlaygroundReporter()
29+
30+
public func describe(name:String, closure:ContextType -> ()) {
31+
let context = Context(name: name)
32+
closure(context)
33+
context.run(reporter)
34+
}
35+
36+
public func it(name:String, closure:() throws -> ()) {
37+
let `case` = Case(name: name, closure: closure)
38+
`case`.run(reporter)
39+
}
40+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../Spectre/Reporter.swift
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../Spectre/Reporters.swift
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='osx' requires-full-environment='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Spectre.playground/playground.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
</TimelineItems>
6+
</Timeline>

Spectre/Global.swift

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import Darwin
2+
3+
let globalContext: GlobalContext = {
4+
atexit { run() }
5+
return GlobalContext()
6+
}()
7+
8+
public func describe(name:String, closure:ContextType -> ()) {
9+
globalContext.describe(name, closure: closure)
10+
}
11+
12+
public func it(name:String, closure:() throws -> ()) {
13+
globalContext.it(name, closure: closure)
14+
}
15+
16+
@noreturn public func run() {
17+
let reporter: Reporter
18+
19+
if Process.arguments.contains("-t") {
20+
reporter = DotReporter()
21+
} else {
22+
reporter = StandardReporter()
23+
}
24+
25+
run(reporter)
26+
}
27+
28+
@noreturn public func run(reporter:Reporter) {
29+
if globalContext.run(reporter) {
30+
exit(0)
31+
}
32+
exit(1)
33+
}
34+

Spectre/GlobalContext.swift

-31
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,3 @@ class GlobalContext {
2323
}
2424
}
2525

26-
let globalContext: GlobalContext = {
27-
atexit { run() }
28-
return GlobalContext()
29-
}()
30-
31-
public func describe(name:String, closure:ContextType -> ()) {
32-
globalContext.describe(name, closure: closure)
33-
}
34-
35-
public func it(name:String, closure:() throws -> ()) {
36-
globalContext.it(name, closure: closure)
37-
}
38-
39-
@noreturn public func run() {
40-
let reporter: Reporter
41-
if Process.arguments.contains("-t") {
42-
reporter = DotReporter()
43-
} else {
44-
reporter = StandardReporter()
45-
}
46-
47-
run(reporter)
48-
}
49-
50-
@noreturn public func run(reporter:Reporter) {
51-
if globalContext.run(reporter) {
52-
exit(0)
53-
}
54-
exit(1)
55-
}
56-

0 commit comments

Comments
 (0)