Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node.js + CoffeeScript + jasmine #2

Merged
merged 1 commit into from
Nov 13, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions gonzalo123/nodejs/potter.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Potter
constructor: () ->
@discounts = [0, 1, 0.95, 0.9, 0.8, 0.75]
@basePrice = 8
@_blankBooks = [0, 0, 0, 0, 0]

@_price = (shopingCart) ->
size = @_calcSize shopingCart
return 0 if size is 0
newBooks = @_changeShoppingCart(shopingCart, size)
size * @discounts[size] + @_price(newBooks)

@_calcSize = (shopingCart) ->
size = size2 = 0
for bookAmount in shopingCart
size++ if bookAmount > 0
size2++ if bookAmount > 1
if size2 is 3 and size is 5
return 4
size

@_changeShoppingCart = (shopingCart, toReduce) ->
nextshopingCart = @_blankBooks
i = 0
for bookAmount in shopingCart
nextshopingCart[i] = bookAmount
if bookAmount > 0 and toReduce > 0
--toReduce
nextshopingCart[i] = bookAmount - 1
i++
nextshopingCart

price: (books) ->
shopingCart = @_blankBooks
for book in books
shopingCart[book]++
@basePrice * @_price(shopingCart)

exports.Potter = Potter
62 changes: 62 additions & 0 deletions gonzalo123/nodejs/potter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions gonzalo123/nodejs/spec/potter_spec.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Potter = require('../potter').Potter
potter = new Potter

describe 'jasmine-node', ->

it 'testBasics', ->
expect(potter.price []).toEqual 0
expect(potter.price [0]).toEqual 8
expect(potter.price [1]).toEqual 8
expect(potter.price [2]).toEqual 8
expect(potter.price [3]).toEqual 8
expect(potter.price [4]).toEqual 8

expect(potter.price [0, 0]).toEqual 8 * 2
expect(potter.price [1, 1, 1]).toEqual 8 * 3

it 'testSimpleDiscounts', ->
expect(potter.price [0, 1]).toEqual 8 * 2 * 0.95
expect(potter.price [0, 2, 4]).toEqual 8 * 3 * 0.9
expect(potter.price [0, 1, 2, 4]).toEqual 8 * 4 * 0.8
expect(potter.price [0, 1, 2, 3, 4]).toEqual 8 * 5 * 0.75

it 'testSeveralDiscounts', ->
expect(potter.price [0, 0, 1]).toEqual 8 + (8 * 2 * 0.95)
expect(potter.price [0, 0, 1, 1]).toEqual 2 * (8 * 2 * 0.95)
expect(potter.price [0, 0, 1, 2, 2, 3]).toEqual((8 * 4 * 0.8) + (8 * 2 * 0.95))
expect(potter.price [0, 1, 1, 2, 3, 4]).toEqual 8 + (8 * 5 * 0.75)

it 'testEdgeCases', ->
expect(potter.price [0, 0, 1, 1, 2, 2, 3, 4]).toEqual 2 * (8 * 4 * 0.8)
expect(potter.price [0, 0, 0, 0, 0, 1, 1, 1, 1, 1,2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4]).toEqual(3 * (8 * 5 * 0.75) + 2 * (8 * 4 * 0.8))
33 changes: 33 additions & 0 deletions gonzalo123/nodejs/spec/potter_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(function() {
var Potter, potter;
Potter = require('../potter').Potter;
potter = new Potter;
describe('jasmine-node', function() {
it('testBasics', function() {
expect(potter.price([])).toEqual(0);
expect(potter.price([0])).toEqual(8);
expect(potter.price([1])).toEqual(8);
expect(potter.price([2])).toEqual(8);
expect(potter.price([3])).toEqual(8);
expect(potter.price([4])).toEqual(8);
expect(potter.price([0, 0])).toEqual(8 * 2);
return expect(potter.price([1, 1, 1])).toEqual(8 * 3);
});
it('testSimpleDiscounts', function() {
expect(potter.price([0, 1])).toEqual(8 * 2 * 0.95);
expect(potter.price([0, 2, 4])).toEqual(8 * 3 * 0.9);
expect(potter.price([0, 1, 2, 4])).toEqual(8 * 4 * 0.8);
return expect(potter.price([0, 1, 2, 3, 4])).toEqual(8 * 5 * 0.75);
});
it('testSeveralDiscounts', function() {
expect(potter.price([0, 0, 1])).toEqual(8 + (8 * 2 * 0.95));
expect(potter.price([0, 0, 1, 1])).toEqual(2 * (8 * 2 * 0.95));
expect(potter.price([0, 0, 1, 2, 2, 3])).toEqual((8 * 4 * 0.8) + (8 * 2 * 0.95));
return expect(potter.price([0, 1, 1, 2, 3, 4])).toEqual(8 + (8 * 5 * 0.75));
});
return it('testEdgeCases', function() {
expect(potter.price([0, 0, 1, 1, 2, 2, 3, 4])).toEqual(2 * (8 * 4 * 0.8));
return expect(potter.price([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4])).toEqual(3 * (8 * 5 * 0.75) + 2 * (8 * 4 * 0.8));
});
});
}).call(this);