# Copyright (c) 2006, Chris Parsons (chris@edendevelopment.co.uk) # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # * Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # * Neither the name of Chris Parsons nor the names of its contributors may be # used to endorse or promote products derived from this software without # specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES r OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. require 'invoice' context 'A new invoice' do setup do @invoice = new_invoice do code 'edi0123-2323' address 'Artswork, Fairways House, Mount Pleasant Road, Southampton, SO14 OQB' date '20th Dec' vat 17.5 work_at 25.00 do spent 1.5.on('Setting up') spent 1.5.on('Important work') spent 2.5.on('Making tea').on('14th Dec') end mileage_at 0.4 do drove 150.to('Meeting with client').on('23rd Dec') end expense 35.60.for('Another train ticket') end end specify 'should have the given address, date and vat' do @invoice.address.should_match(/Artswork/) @invoice.date.should_equal Time.parse('12/20/2006') @invoice.vat.should_equal 17.5 end specify 'should have one expense line' do @invoice.should_have(1).expenses end specify 'should have an item count of 4' do @invoice.should_have(4).items end specify 'should have the second item as "Important work"' do @invoice.items[1].what.should_equal 'Important work' end specify 'should have the third item with a date' do @invoice.items[2].date.should_not_be_nil @invoice.items[2].date.should_equal Time.parse('14 Dec 06') end specify 'should have mileage line with date' do @invoice.items.last.what.should_equal 'Mileage: Meeting with client' @invoice.items.last.date.should_not_be_nil @invoice.items.last.date.should_equal Time.parse('23 Dec') end specify 'should have correct total' do @invoice.total.should_equal 5.5 * 25 + (150*0.4) + 35.60 end specify 'should have correct vat value' do @invoice.total_vat.should_be_close((5.5 * 25 + 150 * 0.4) * 0.175, 0.01) end end context 'An invoice with timings' do setup do @invoice = new_invoice do work_at 200.dollars / day do spent 2.days.on('Procrastinating').on('7th Dec') end end end specify 'should have a total of 400' do @invoice.total.should_equal 400.dollars @invoice.total.should_not_equal 400.pounds "#{@invoice.total}".should_equal "400.00" end specify 'should have a "count" value in days' do @invoice.items.first.count.should_equal 2 end end context 'An invoice with differing currencies' do setup do @invoice = new_invoice do 1.pound('£') | 2.dollars('$') expense 25.pounds.on('A new bike') end end specify 'should have 2 pounds equals 4 dollars' do 2.pounds.should_equal 4.dollars 5.dollars.should_equal 2.5.pounds 2.dollars.should_equal 1.pound end specify 'should have a total of 25 pounds' do @invoice.total.should_equal 25.pounds end specify 'should have a total of 50 dollars' do @invoice.total.should_equal 50.dollars end specify 'when told is in dollars, only print in dollars'do @invoice.currency :dollars @invoice.total.currency_symbol.should_equal '$' @invoice.total.to_f.should_equal 50.0 end end context 'An invoice with fixed entries' do setup do @invoice = new_invoice do add 1.of('Iteration Three').costing('750') add 3.of('Conkers').costing('0.50') end end specify 'should have two items' do @invoice.should_have(2).items end specify 'should have first entry totalling 750' do @invoice.items.first.total.should_equal 750 end specify 'should have total 751.50' do @invoice.total.should_equal 751.50 end end