summaryrefslogtreecommitdiff
path: root/tests/amounts.h
blob: a72e57d23da3900fdf692e6b826714360b094978 (plain)
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#ifndef __TESTAMOUNT_H
#define __TESTAMOUNT_H

#include <cxxtest/TestSuite.h>

#include <amount.h>

using namespace ledger;

class TestAmount : public CxxTest::TestSuite
{
public:
    void testCreateAmountWithoutCommodityFromInteger()
    {
	amount_t a((long int)42);
	TS_ASSERT_EQUALS("", a.commodity().symbol());
	TS_ASSERT_EQUALS("", a.commodity().base_symbol());
	TS_ASSERT_EQUALS("42", a.quantity_string());
    }

    void testCreateAmountWithoutCommodity()
    {
	amount_t a("42");
	TS_ASSERT_EQUALS("", a.commodity().symbol());
	TS_ASSERT_EQUALS("", a.commodity().base_symbol());
	TS_ASSERT_EQUALS("42", a.quantity_string());
    }

    void testCreateAmountWithPrefixCommodity()
    {
	amount_t *a;
	a = new amount_t("$ 42");
	TS_ASSERT_EQUALS("$", a->commodity().symbol());
	TS_ASSERT_EQUALS("$", a->commodity().base_symbol());
	TS_ASSERT_EQUALS("42", a->quantity_string());
    }

    void testCreateAmountWithPostfixCommodity()
    {
	amount_t *a;
	a = new amount_t("42 GLD");
	TS_ASSERT_EQUALS("GLD", a->commodity().symbol());
	TS_ASSERT_EQUALS("GLD", a->commodity().base_symbol());
	TS_ASSERT_EQUALS("42", a->quantity_string());
    }

    void testCreateAmountWithPrefixCommodityContainingSpace()
    {
	amount_t *a;
	a = new amount_t("\"one dollar\" 42");
	TS_ASSERT_EQUALS("\"one dollar\"", a->commodity().symbol());
	TS_ASSERT_EQUALS("one dollar", a->commodity().base_symbol());
	TS_ASSERT_EQUALS("42", a->quantity_string());
    }

    void testCreateAmountWithPostfixCommodityContainingSpace()
    {
	amount_t *a;
	a = new amount_t("42 \"swedish crowns\"");
	TS_ASSERT_EQUALS("\"swedish crowns\"", a->commodity().symbol());
	TS_ASSERT_EQUALS("swedish crowns", a->commodity().base_symbol());
	TS_ASSERT_EQUALS("42", a->quantity_string());
    }

    void testCreateAmountWithDirectPrefixCommodity()
    {
	amount_t *a;
	a = new amount_t("$42");
	TS_ASSERT_EQUALS("$", a->commodity().symbol());
	TS_ASSERT_EQUALS("$", a->commodity().base_symbol());
	TS_ASSERT_EQUALS("42", a->quantity_string());
    }

    void testCreateAmountWithDirectPostfixCommodity()
    {
	amount_t *a;
	a = new amount_t("42GLD");
	TS_ASSERT_EQUALS("GLD", a->commodity().symbol());
	TS_ASSERT_EQUALS("GLD", a->commodity().base_symbol());
	TS_ASSERT_EQUALS("42", a->quantity_string());
    }

    void testCannotCreateAmountWithoutQuantity()
    {
	TS_ASSERT_THROWS(new amount_t("$"), amount_error*);
    }

    void testCreateBiiigIntegerAmount()
    {
	amount_t a("12345678901234567890123456789012345678901234567890");
	TS_ASSERT_EQUALS("12345678901234567890123456789012345678901234567890",
			a.quantity_string());
    }

    void testCreateBiiigDecimalAmount()
    {
	amount_t a("12345678.901234567890123456789012345678901234567890");
	TS_ASSERT_EQUALS("12345678.901234567890123456789012345678901234567890",
			a.quantity_string());
    }

    void testCreateCommodityAnnotatedWithEntry()
    {
	amount_t a("10 AAPL (entry 6)");
	TS_ASSERT_EQUALS("10", a.quantity_string());
	commodity_t c = a.commodity();
	TS_ASSERT_EQUALS("AAPL", c.symbol());
	TS_ASSERT_EQUALS("AAPL", c.base_symbol());
	TS_ASSERT(c.annotated);
	//TODO: check entry annotation
    }

    void testCreateCommodityAnnotatedWithEntry2()
    {
	amount_t *a = new amount_t("10 AAPL (entry 6)");
	TS_ASSERT_EQUALS("10", a->quantity_string());
	commodity_t c = a->commodity();
	TS_ASSERT_EQUALS("AAPL", c.symbol());
	TS_ASSERT_EQUALS("AAPL", c.base_symbol());
	TS_ASSERT(c.annotated);
	//TODO: check entry annotation
    }

    void testAddTwoAmountsWithoutCommodity()
    {
	amount_t a("6");
	amount_t b("9");
	TS_ASSERT_EQUALS(* new amount_t((long int)15), a+b);
    }

    void testAddTwoAmountsWithSameCommodity()
    {
	amount_t a("$ 6");
	amount_t b("$ 9");
	TS_ASSERT_EQUALS(* new amount_t("$ 15"), a+b);
    }

    void testCannotAddTwoAmountsWithDifferentCommodities()
    {
	amount_t a("$ 6");
	amount_t b("9 GLD");
	TS_ASSERT_THROWS(a+b, amount_error*);
    }

    void testCompareTwoAmountsWithSameCommodity()
    {
	amount_t a("6 SCOX");
	amount_t b("9 SCOX");
	TS_ASSERT(a < b);
	TS_ASSERT(a <= b);
	TS_ASSERT(!(a > b));
	TS_ASSERT(!(a >= b));
	TS_ASSERT(!(a == b));
    }

    void testCannotCompareTwoAmountsWithDifferentCommodities()
    {
	amount_t a("$ 6");
	amount_t b("9 GLD");

	TS_ASSERT_THROWS(a < b, amount_error*);
	TS_ASSERT_THROWS(a <= b, amount_error*);
	TS_ASSERT_THROWS(a > b, amount_error*);
	TS_ASSERT_THROWS(a >= b, amount_error*);
	TS_ASSERT(!(a == b));
    }
};

#endif // __TESTAMOUNT_H