Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 44 lines (35 sloc) 1.43 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
-module(records_10).
-include_lib("eunit/include/eunit.hrl").
-record(purchase, {name, price, merchant=default}).

record_creation_test() ->
    P = #purchase{name="Giorgio",
                price=10.00,
                merchant=bakery},
    ?assertEqual("Giorgio", P#purchase.name).

record_default_value_test() ->
    P2 = #purchase{name="Giorgio",
                price=10.00},
    ?assertEqual(default, P2#purchase.merchant).

record_modification_test() ->
    P = #purchase{name="Giorgio", price=10.00},
    NewP = P#purchase{price=20.00},
    ?assertEqual(#purchase{name="Giorgio", price=20.00},
                 NewP).

record_pattern_matching_test() ->
    P = #purchase{name="Giorgio", price=10.00},
    ?assertEqual(2.0, fixed_tax(P)).

fixed_tax(#purchase{price=Price} = Purchase) ->
Price * 0.20.

record_multiple_pattern_matching_test() ->
    ?assertEqual(2.0, variable_tax(#purchase{name="Giorgio",
                                            price=10.00})),
    ?assertEqual(0.4, variable_tax(#purchase{name="Giorgio",
                                            price=10.00,
                                            merchant=bakery})).

variable_tax(#purchase{price=Price,merchant=bakery} = Purchase) ->
Price * 0.04;
variable_tax(#purchase{price=Price} = Purchase) ->
Price * 0.20.

record_info_on_fields_test() ->
    ?assertEqual([name, price, merchant],
                record_info(fields, purchase)).
Something went wrong with that request. Please try again.