Skip to content

Commit a6f8af1

Browse files
committed
add more side-effects
1 parent eef112c commit a6f8af1

File tree

9 files changed

+256
-170
lines changed

9 files changed

+256
-170
lines changed

code/.idea/.idea.CSharpRefactor/.idea/contentModel.xml

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/.idea/.idea.CSharpRefactor/.idea/workspace.xml

Lines changed: 23 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

code/CSharpRefactor.Tests/InvoicesTest.cs renamed to code/CSharpRefactor.Tests/InvoicesParserTest.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,26 @@
66

77
namespace CSharpRefactor.Tests
88
{
9-
public class InvoicesTest
9+
public class InvoicesParserTest
1010
{
1111
[Fact]
1212
public void ReadInvoices__No_Input_Files__Empty_Result()
1313
{
14-
var actual = Invoices.ReadInvoices(new string[] {}, null, () => false);
14+
var sut = new InvoicesParser(new string[] {}, null, null);
15+
var actual = sut.ReadAndParseInvoices();
1516
Assert.Empty(actual);
1617
}
1718

1819
[Fact]
1920
public void ReadInvoices__Valid_Input_File_No_Discount__Amount_Is_Read()
2021
{
2122
const string path = "invoice1.txt";
22-
var actual = Invoices.ReadInvoices(new string[]
23-
{
24-
path
25-
}, null, () => false);
23+
var sut = new InvoicesParser(new string[] {path}, null, null);
24+
var actual = sut.ReadAndParseInvoices();
2625

27-
var expected = new Dictionary<string, ParseResult>()
26+
var expected = new Dictionary<string, InvoiceParseResult>()
2827
{
29-
{path, new ParseResult(42m, null)}
28+
{path, new InvoiceParseResult(0, 42m, null)}
3029
};
3130

3231
Assert.Equal(expected, actual);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using Xunit;
6+
7+
namespace CSharpRefactor.Tests
8+
{
9+
public class InvoicesSumTest
10+
{
11+
[Fact]
12+
public void Sum__Empty_Input__Zero()
13+
{
14+
var sut = new InvoiceSum(new InvoiceParseResult[] {});
15+
var actual = sut.Sum();
16+
var expected = new InvoicesSum(0m, 0m);
17+
Assert.Equal(expected, actual);
18+
}
19+
}
20+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
namespace CSharpRefactor
2+
{
3+
public class InvoiceParseResult
4+
{
5+
public int Id;
6+
public decimal? Amount;
7+
public decimal? DiscountedAmount;
8+
public string ErrorText;
9+
10+
public InvoiceParseResult(int id, decimal? amount, decimal? discountedAmount)
11+
{
12+
Id = id;
13+
Amount = amount;
14+
DiscountedAmount = discountedAmount;
15+
ErrorText = null;
16+
}
17+
18+
public InvoiceParseResult(int id, string errorText)
19+
{
20+
Id = id;
21+
Amount = null;
22+
DiscountedAmount = null;
23+
ErrorText = errorText;
24+
}
25+
26+
public override int GetHashCode()
27+
{
28+
unchecked // Overflow is fine, just wrap
29+
{
30+
int hash = 17;
31+
hash = hash * 23 + Id.GetHashCode();
32+
33+
// Suitable nullity checks etc, of course :)
34+
if (Amount.HasValue)
35+
{
36+
hash = hash * 23 + Amount.Value.GetHashCode();
37+
}
38+
39+
if (DiscountedAmount.HasValue)
40+
{
41+
hash = hash * 23 + DiscountedAmount.Value.GetHashCode();
42+
}
43+
44+
if (ErrorText != null)
45+
{
46+
hash = hash * 23 + ErrorText.GetHashCode();
47+
}
48+
return hash;
49+
}
50+
}
51+
52+
public override bool Equals(object obj)
53+
{
54+
var item = obj as InvoiceParseResult;
55+
56+
if (item == null)
57+
{
58+
return false;
59+
}
60+
61+
return
62+
Id.Equals(item.Id)
63+
&& (Amount.HasValue && item.Amount.HasValue && Amount.Equals(item.Amount))
64+
|| (!Amount.HasValue && !item.Amount.HasValue)
65+
&& (DiscountedAmount.HasValue && item.DiscountedAmount.HasValue && DiscountedAmount.Equals(item.DiscountedAmount))
66+
|| (!DiscountedAmount.HasValue && !item.DiscountedAmount.HasValue)
67+
&& ErrorText.Equals(item.ErrorText);
68+
}
69+
}
70+
}

code/CSharpRefactor/InvoiceSum.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Collections.Generic;
2+
3+
namespace CSharpRefactor
4+
{
5+
public class InvoiceSum
6+
{
7+
private readonly IEnumerable<InvoiceParseResult> _invoices;
8+
9+
public InvoiceSum(IEnumerable<InvoiceParseResult> invoices)
10+
{
11+
_invoices = invoices;
12+
}
13+
14+
public InvoicesSum Sum()
15+
{
16+
var sum = 0m;
17+
var discountedSum = 0m;
18+
19+
// what should happen if errorText is non-empty, but there is an amount nonetheless?
20+
foreach (var invoice in _invoices)
21+
{
22+
sum += invoice.Amount ?? 0;
23+
discountedSum += invoice.DiscountedAmount ?? 0;
24+
}
25+
26+
return new InvoicesSum(sum, discountedSum);
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)