Skip to content

Instantly share code, notes, and snippets.

@mythz
Created May 21, 2011 02:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mythz/984160 to your computer and use it in GitHub Desktop.
Save mythz/984160 to your computer and use it in GitHub Desktop.
Peepcode cash resgister example of using Jasmine with CoffeeScript
describe "Dish", ->
describe "constructor", ->
beforeEach ->
@dish = new Dish 'Sirloin Steak $18.99 mains'
it "extracts title", ->
(expect @dish.title).toEqual('Sirloin Steak')
it "extracts price", ->
(expect @dish.price.cents).toEqual 1899
(expect @dish.price.toString()).toEqual '$18.99'
it "extracts tag", ->
(expect @dish.tag).toEqual 'mains'
describe "constructor with partial data", ->
beforeEach ->
@dish = new Dish 'Sirloin Steak $18.99'
it "extracts title", ->
(expect @dish.title).toEqual 'Sirloin Steak'
it "extracts price", ->
(expect @dish.price.toString()).toEqual '$18.99'
it "leaves tag blank", ->
(expect @dish.tag).toEqual ''
describe "constructor with invalid data", ->
beforeEach ->
@dish = new Dish 'Sirloin Steak'
it "extracts title", ->
(expect @dish.title).toEqual 'Sirloin Steak'
it "leaves price blank", ->
(expect @dish.price.cents).toEqual 0
it "leaves tag blank", ->
(expect @dish.tag).toEqual ''
describe "constructor with null description", ->
beforeEach ->
@dish = new Dish
it "sets title to null", ->
(expect @dish.title).toEqual ''
describe "Money", ->
describe "constructor", ->
describe "valid value", ->
beforeEach ->
@money = new Money '$15.99'
it "extracts dollar and cents", ->
(expect @money.cents).toEqual 1599
it "formats as string", ->
(expect @money.toString()).toEqual '$15.99'
describe "invalid value", ->
it "clips fractional part of cents to .99", ->
@money = new Money '$15.909'
(expect @money.cents).toEqual 1599
it "uses zero if a money value can't be parsed", ->
@money = new Money 'NOT A MONETARY VALUE'
(expect @money.cents).toEqual 0
describe "Meal", ->
beforeEach ->
@donut = new Dish 'Maple Bacon Donut $1.99 #breakfast'
@fish = new Dish 'Salmon Filet $14.99 #main'
@dishes = [@donut, @fish]
describe "starting blank", ->
beforeEach ->
@meal = new Meal
it "adds a single dish", ->
@meal.add @donut
(expect @meal.dishes.length).toEqual(1)
it "calculates total price", ->
@meal.add @donut, @fish
(expect @meal.totalPrice().cents).toEqual 1698
it "updates total price when a new dish is added", ->
(expect @meal.totalPrice().cents).toEqual 0
@meal.add @donut
(expect @meal.totalPrice().cents).toEqual 199
@meal.add @fish
(expect @meal.totalPrice().cents).toEqual 1698
it "adds a list of dishes", ->
@meal.add @donut, @fish
(expect @meal.dishes.length).toEqual 2
(function() {
describe("Dish", function() {
describe("constructor", function() {
beforeEach(function() {
return this.dish = new Dish('Sirloin Steak $18.99 mains');
});
it("extracts title", function() {
return (expect(this.dish.title)).toEqual('Sirloin Steak');
});
it("extracts price", function() {
(expect(this.dish.price.cents)).toEqual(1899);
return (expect(this.dish.price.toString())).toEqual('$18.99');
});
return it("extracts tag", function() {
return (expect(this.dish.tag)).toEqual('mains');
});
});
describe("constructor with partial data", function() {
beforeEach(function() {
return this.dish = new Dish('Sirloin Steak $18.99');
});
it("extracts title", function() {
return (expect(this.dish.title)).toEqual('Sirloin Steak');
});
it("extracts price", function() {
return (expect(this.dish.price.toString())).toEqual('$18.99');
});
return it("leaves tag blank", function() {
return (expect(this.dish.tag)).toEqual('');
});
});
describe("constructor with invalid data", function() {
beforeEach(function() {
return this.dish = new Dish('Sirloin Steak');
});
it("extracts title", function() {
return (expect(this.dish.title)).toEqual('Sirloin Steak');
});
it("leaves price blank", function() {
return (expect(this.dish.price.cents)).toEqual(0);
});
return it("leaves tag blank", function() {
return (expect(this.dish.tag)).toEqual('');
});
});
return describe("constructor with null description", function() {
beforeEach(function() {
return this.dish = new Dish;
});
return it("sets title to null", function() {
return (expect(this.dish.title)).toEqual('');
});
});
});
describe("Money", function() {
return describe("constructor", function() {
describe("valid value", function() {
beforeEach(function() {
return this.money = new Money('$15.99');
});
it("extracts dollar and cents", function() {
return (expect(this.money.cents)).toEqual(1599);
});
return it("formats as string", function() {
return (expect(this.money.toString())).toEqual('$15.99');
});
});
return describe("invalid value", function() {
it("clips fractional part of cents to .99", function() {
this.money = new Money('$15.909');
return (expect(this.money.cents)).toEqual(1599);
});
return it("uses zero if a money value can't be parsed", function() {
this.money = new Money('NOT A MONETARY VALUE');
return (expect(this.money.cents)).toEqual(0);
});
});
});
});
describe("Meal", function() {
beforeEach(function() {
this.donut = new Dish('Maple Bacon Donut $1.99 #breakfast');
this.fish = new Dish('Salmon Filet $14.99 #main');
return this.dishes = [this.donut, this.fish];
});
return describe("starting blank", function() {
beforeEach(function() {
return this.meal = new Meal;
});
it("adds a single dish", function() {
this.meal.add(this.donut);
return (expect(this.meal.dishes.length)).toEqual(1);
});
it("calculates total price", function() {
this.meal.add(this.donut, this.fish);
return (expect(this.meal.totalPrice().cents)).toEqual(1698);
});
it("updates total price when a new dish is added", function() {
(expect(this.meal.totalPrice().cents)).toEqual(0);
this.meal.add(this.donut);
(expect(this.meal.totalPrice().cents)).toEqual(199);
this.meal.add(this.fish);
return (expect(this.meal.totalPrice().cents)).toEqual(1698);
});
return it("adds a list of dishes", function() {
this.meal.add(this.donut, this.fish);
return (expect(this.meal.dishes.length)).toEqual(2);
});
});
});
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment