From d7efe472f345920c33f45b5850daeeb23336d926 Mon Sep 17 00:00:00 2001 From: nilsnh Date: Tue, 10 Jul 2012 23:48:29 +0200 Subject: [PATCH] Finished the tutorial :) --- package.json | 5 +-- src/task.coffee | 35 +++++++++++++++++--- test/taskTest.coffee | 77 ++++++++++++++++++++++++++++++++++++++------ 3 files changed, 100 insertions(+), 17 deletions(-) diff --git a/package.json b/package.json index 2e4d3b0..75e4862 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/task.coffee b/src/task.coffee index 1659f01..853bcfc 100644 --- a/src/task.coffee +++ b/src/task.coffee @@ -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 \ No newline at end of file +root.Task = Task +root.TaskList = TaskList \ No newline at end of file diff --git a/test/taskTest.coffee b/test/taskTest.coffee index 0d614e4..940c1d7 100644 --- a/test/taskTest.coffee +++ b/test/taskTest.coffee @@ -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." \ No newline at end of file + 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