Skip to content

Commit bc39ff0

Browse files
ledger: add generator
1 parent fde4464 commit bc39ff0

File tree

4 files changed

+108
-65
lines changed

4 files changed

+108
-65
lines changed

exercises/practice/ledger/.meta/Example.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,15 @@ private static string FormatHeader(CultureInfo culture)
9393
private static string FormatDescription(string description) =>
9494
description.Length <= TruncateLength ? description : description.Substring(0, TruncateLength - TruncateSuffix.Length) + TruncateSuffix;
9595

96-
private static string FormatChange(IFormatProvider culture, decimal change) =>
97-
change < 0.0m ? change.ToString("C", culture) : change.ToString("C", culture) + " ";
96+
private static string FormatChange(IFormatProvider culture, decimal change)
97+
{
98+
var formatChange = change.ToString("C", culture);
99+
var suffix = change >= 0 || formatChange.Contains('-') ? " " : "";
100+
return $"{formatChange}{suffix}";
101+
}
98102

99103
private static string FormatEntry(IFormatProvider culture, LedgerEntry entry) =>
100-
string.Format("{0} | {1,-25} | {2,13}", FormatDate(culture, entry.Date), FormatDescription(entry.Description), FormatChange(culture, entry.Change));
104+
$"{FormatDate(culture, entry.Date)} | {FormatDescription(entry.Description),-25} | {FormatChange(culture, entry.Change),13}";
101105

102106
private static IEnumerable<LedgerEntry> OrderEntries(LedgerEntry[] entries) =>
103107
entries
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Xunit;
2+
3+
public class {{ testClass }}
4+
{
5+
{{- for test in tests }}
6+
[Fact]
7+
public void {{ test.testMethod }}()
8+
{
9+
var currency = {{ test.input.currency | string.literal }};
10+
var locale = {{ test.input.locale | string.replace "_" "-" | string.literal }};
11+
{{- if test.input.entries.empty? }}
12+
LedgerEntry[] entries = [];
13+
{{ else }}
14+
LedgerEntry[] entries = [
15+
{{- for entry in test.input.entries }}
16+
{{ testedClass }}.CreateEntry({{ entry.date | string.literal }}, {{ entry.description | string.literal }}, {{ entry.amountInCents }}){{- if !for.last }},{{ end -}}
17+
{{ end }}
18+
];
19+
{{ end -}}
20+
var expected =
21+
{{- for line in test.expected }}
22+
{{ if for.last -}}
23+
{{ line | string.literal -}};
24+
{{- else -}}
25+
{{ line | string.append "\n" | string.literal }} +
26+
{{- end -}}
27+
{{- end }}
28+
Assert.Equal(expected, {{ testedClass }}.Format(currency, locale, entries));
29+
}
30+
{{ end -}}
31+
}

exercises/practice/ledger/Ledger.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,20 @@ private static string Description(string desc)
115115

116116
private static string Change(IFormatProvider culture, decimal cgh)
117117
{
118-
return cgh < 0.0m ? cgh.ToString("C", culture) : cgh.ToString("C", culture) + " ";
118+
if (cgh < 0.0m)
119+
{
120+
var change = cgh.ToString("C", culture);
121+
if (change.Contains("-"))
122+
{
123+
return change + " ";
124+
}
125+
126+
return change;
127+
}
128+
else
129+
{
130+
return cgh.ToString("C", culture) + " ";
131+
}
119132
}
120133

121134
private static string PrintEntry(IFormatProvider culture, LedgerEntry entry)

exercises/practice/ledger/LedgerTests.cs

Lines changed: 56 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -7,162 +7,157 @@ public void Empty_ledger()
77
{
88
var currency = "USD";
99
var locale = "en-US";
10-
var entries = new LedgerEntry[0];
10+
LedgerEntry[] entries = [];
1111
var expected =
1212
"Date | Description | Change ";
13-
1413
Assert.Equal(expected, Ledger.Format(currency, locale, entries));
1514
}
1615

17-
[Fact(Skip = "Remove this Skip property to run this test")]
16+
[Fact]
1817
public void One_entry()
1918
{
2019
var currency = "USD";
2120
var locale = "en-US";
22-
var entries = new[]
23-
{
21+
LedgerEntry[] entries = [
2422
Ledger.CreateEntry("2015-01-01", "Buy present", -1000)
25-
};
23+
];
2624
var expected =
2725
"Date | Description | Change \n" +
2826
"01/01/2015 | Buy present | ($10.00)";
29-
3027
Assert.Equal(expected, Ledger.Format(currency, locale, entries));
3128
}
3229

33-
[Fact(Skip = "Remove this Skip property to run this test")]
30+
[Fact]
3431
public void Credit_and_debit()
3532
{
3633
var currency = "USD";
3734
var locale = "en-US";
38-
var entries = new[]
39-
{
35+
LedgerEntry[] entries = [
4036
Ledger.CreateEntry("2015-01-02", "Get present", 1000),
4137
Ledger.CreateEntry("2015-01-01", "Buy present", -1000)
42-
};
38+
];
4339
var expected =
4440
"Date | Description | Change \n" +
4541
"01/01/2015 | Buy present | ($10.00)\n" +
4642
"01/02/2015 | Get present | $10.00 ";
47-
48-
Assert.Equal(expected, Ledger.Format(currency, locale, entries));
49-
}
50-
51-
[Fact(Skip = "Remove this Skip property to run this test")]
52-
public void Multiple_entries_on_same_date_ordered_by_description()
53-
{
54-
var currency = "USD";
55-
var locale = "en-US";
56-
var entries = new[]
57-
{
58-
Ledger.CreateEntry("2015-01-01", "Buy present", -1000),
59-
Ledger.CreateEntry("2015-01-01", "Get present", 1000)
60-
};
61-
var expected =
62-
"Date | Description | Change \n" +
63-
"01/01/2015 | Buy present | ($10.00)\n" +
64-
"01/01/2015 | Get present | $10.00 ";
65-
6643
Assert.Equal(expected, Ledger.Format(currency, locale, entries));
6744
}
6845

69-
[Fact(Skip = "Remove this Skip property to run this test")]
46+
[Fact]
7047
public void Final_order_tie_breaker_is_change()
7148
{
7249
var currency = "USD";
7350
var locale = "en-US";
74-
var entries = new[]
75-
{
51+
LedgerEntry[] entries = [
7652
Ledger.CreateEntry("2015-01-01", "Something", 0),
7753
Ledger.CreateEntry("2015-01-01", "Something", -1),
7854
Ledger.CreateEntry("2015-01-01", "Something", 1)
79-
};
55+
];
8056
var expected =
8157
"Date | Description | Change \n" +
8258
"01/01/2015 | Something | ($0.01)\n" +
8359
"01/01/2015 | Something | $0.00 \n" +
8460
"01/01/2015 | Something | $0.01 ";
85-
8661
Assert.Equal(expected, Ledger.Format(currency, locale, entries));
8762
}
8863

89-
[Fact(Skip = "Remove this Skip property to run this test")]
90-
public void Overlong_descriptions()
64+
[Fact]
65+
public void Overlong_description_is_truncated()
9166
{
9267
var currency = "USD";
9368
var locale = "en-US";
94-
var entries = new[]
95-
{
69+
LedgerEntry[] entries = [
9670
Ledger.CreateEntry("2015-01-01", "Freude schoner Gotterfunken", -123456)
97-
};
71+
];
9872
var expected =
9973
"Date | Description | Change \n" +
10074
"01/01/2015 | Freude schoner Gotterf... | ($1,234.56)";
101-
10275
Assert.Equal(expected, Ledger.Format(currency, locale, entries));
10376
}
10477

105-
[Fact(Skip = "Remove this Skip property to run this test")]
78+
[Fact]
10679
public void Euros()
10780
{
10881
var currency = "EUR";
10982
var locale = "en-US";
110-
var entries = new[]
111-
{
83+
LedgerEntry[] entries = [
11284
Ledger.CreateEntry("2015-01-01", "Buy present", -1000)
113-
};
85+
];
11486
var expected =
11587
"Date | Description | Change \n" +
11688
"01/01/2015 | Buy present | (€10.00)";
117-
11889
Assert.Equal(expected, Ledger.Format(currency, locale, entries));
11990
}
12091

121-
[Fact(Skip = "Remove this Skip property to run this test")]
92+
[Fact]
12293
public void Dutch_locale()
12394
{
12495
var currency = "USD";
12596
var locale = "nl-NL";
126-
var entries = new[]
127-
{
97+
LedgerEntry[] entries = [
12898
Ledger.CreateEntry("2015-03-12", "Buy present", 123456)
129-
};
99+
];
130100
var expected =
131101
"Datum | Omschrijving | Verandering \n" +
132102
"12-03-2015 | Buy present | $ 1.234,56 ";
103+
Assert.Equal(expected, Ledger.Format(currency, locale, entries));
104+
}
133105

106+
[Fact]
107+
public void Dutch_locale_and_euros()
108+
{
109+
var currency = "EUR";
110+
var locale = "nl-NL";
111+
LedgerEntry[] entries = [
112+
Ledger.CreateEntry("2015-03-12", "Buy present", 123456)
113+
];
114+
var expected =
115+
"Datum | Omschrijving | Verandering \n" +
116+
"12-03-2015 | Buy present | € 1.234,56 ";
134117
Assert.Equal(expected, Ledger.Format(currency, locale, entries));
135118
}
136119

137-
[Fact(Skip = "Remove this Skip property to run this test")]
120+
[Fact]
138121
public void Dutch_negative_number_with_3_digits_before_decimal_point()
139122
{
140123
var currency = "USD";
141124
var locale = "nl-NL";
142-
var entries = new[]
143-
{
125+
LedgerEntry[] entries = [
144126
Ledger.CreateEntry("2015-03-12", "Buy present", -12345)
145-
};
127+
];
146128
var expected =
147129
"Datum | Omschrijving | Verandering \n" +
148-
"12-03-2015 | Buy present | $ -123,45";
149-
130+
"12-03-2015 | Buy present | $ -123,45 ";
150131
Assert.Equal(expected, Ledger.Format(currency, locale, entries));
151132
}
152133

153-
[Fact(Skip = "Remove this Skip property to run this test")]
134+
[Fact]
154135
public void American_negative_number_with_3_digits_before_decimal_point()
155136
{
156137
var currency = "USD";
157138
var locale = "en-US";
158-
var entries = new[]
159-
{
139+
LedgerEntry[] entries = [
160140
Ledger.CreateEntry("2015-03-12", "Buy present", -12345)
161-
};
141+
];
162142
var expected =
163143
"Date | Description | Change \n" +
164144
"03/12/2015 | Buy present | ($123.45)";
145+
Assert.Equal(expected, Ledger.Format(currency, locale, entries));
146+
}
165147

148+
[Fact]
149+
public void Multiple_entries_on_same_date_ordered_by_description()
150+
{
151+
var currency = "USD";
152+
var locale = "en-US";
153+
LedgerEntry[] entries = [
154+
Ledger.CreateEntry("2015-01-01", "Get present", 1000),
155+
Ledger.CreateEntry("2015-01-01", "Buy present", -1000)
156+
];
157+
var expected =
158+
"Date | Description | Change \n" +
159+
"01/01/2015 | Buy present | ($10.00)\n" +
160+
"01/01/2015 | Get present | $10.00 ";
166161
Assert.Equal(expected, Ledger.Format(currency, locale, entries));
167162
}
168-
}
163+
}

0 commit comments

Comments
 (0)