Finished the tutorial :)

This commit is contained in:
nilsnh 2012-07-10 23:48:29 +02:00
parent 1400be4a16
commit d7efe472f3
3 changed files with 100 additions and 17 deletions

View File

@ -1,5 +1,5 @@
{
"name": "Better Coffe-script testing with Mocha"
"name": "bettercoffeescripttestingtutorial"
, "version": "0.0.1"
, "private": true
, "dependencies": {
@ -7,6 +7,7 @@
}
, "devDependencies": {
"mocha": "1.3.0",
"should": "0.6.3"
"should": "0.6.3",
"expect": "0.0.2"
}
}

View File

@ -1,17 +1,42 @@
class Task
class Task
constructor: (@name) ->
@status = 'incomplete'
complete: ->
if @parent? and @parent.status isnt 'complete'
throw "Dependent task '#{@parent.name}' is not completed."
throw "Dependent task '#{@parent.name}' is not completed."
@status = 'complete'
true
dependsOn: (@parent) ->
@parent.child = @
@status = 'dependent'
class TaskList
constructor: () ->
@tasks = []
@length = 0
add: (@name) ->
if typeof @name is 'string'
task = new Task 'take out garbage'
@tasks.push task
else
@tasks.push @name
@length++
remove: (task) ->
i = @tasks.indexOf task
@tasks = @tasks[0...i].concat @tasks[i+1..] if i > -1
@length = @tasks.length
print: ->
str = "Tasks\n\n"
for task in @tasks
str += "- #{task.name}"
str += " (depends on '#{task.parent.name}')" if task.parent?
str += " (completed)" if task.status is 'complete'
str += "\n"
str
root = exports ? window
root.Task = Task
root.Task = Task
root.TaskList = TaskList

View File

@ -1,14 +1,18 @@
{Tasklist, Task} = require '../src/task'
chai = require 'chai'
chai.should()
expect = chai.expect
{TaskList, Task} = require '../src/task'
describe 'Task instance', ->
task1 = task2 = null
it 'has name', ->
task1 = new Task 'feed the cat'
task1.name.should.equal 'feed the cat'
it 'is initially incomplete', ->
task1.status.should.equal 'incomplete'
it 'is able to be completed', ->
task1.complete().should.be.true
task1.status.should.equal 'complete'
@ -24,12 +28,65 @@ describe 'Task instance', ->
task1.child.should.equal task2
it 'refuses completed if it is dependent on an uncompleted task', ->
task1 = new Task 'wash dishes'
task2 = new Task 'dry dishes'
task1 = new Task 'wash dishes'
task2 = new Task 'dry dishes'
task2.dependsOn task1
task2.dependsOn task1
task2.status.should.equal 'dependent'
task2.parent.should.equal task1
task1.child.should.equal task2
-> task2.complete().should.throw "Dependent task 'wash dishes' is not completed."
task2.status.should.equal 'dependent'
task2.parent.should.equal task1
task1.child.should.equal task2
-> task2.complete().should.throw "Dependent task 'wash dishes' is not completed."
describe 'TaskList', ->
taskList = null
it 'starts with no tasks', ->
taskList = new TaskList
taskList.tasks.length.should.equal 0
taskList.length.should.equal 0
it 'accepts new tasks as tasks', ->
task = new Task 'buy milk'
taskList.add task
taskList.tasks[0].name.should.equal 'buy milk'
taskList.length.should.equal 1
it 'accepts new tasks as string', ->
taskList.add 'take out garbage'
taskList.tasks[1].name.should.equal 'take out garbage'
taskList.length.should.equal 2
it 'removes tasks', ->
i = taskList.length - 1
taskList.remove taskList.tasks[i]
#expect(taskList.tasks[i]).to.be `undefined`
expect(taskList.tasks[i]).to.not.be.ok
it 'prints out the list', ->
taskList = new TaskList
task0 = new Task 'buy milk'
task1 = new Task 'go to store'
task2 = new Task 'another task'
task3 = new Task 'sub-task'
task4 = new Task 'sub-sub-task'
taskList.add task0
taskList.add task1
taskList.add task2
taskList.add task3
taskList.add task4
task0.dependsOn task1
task4.dependsOn task3
task3.dependsOn task2
task1.complete()
desiredOutput = """Tasks
- buy milk (depends on 'go to store')
- go to store (completed)
- another task
- sub-task (depends on 'another task')
- sub-sub-task (depends on 'sub-task')
"""
taskList.print().should.equal desiredOutput