-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordCounterServiceTests.cs
108 lines (84 loc) · 3.35 KB
/
WordCounterServiceTests.cs
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
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Moq;
using NUnit.Framework;
namespace WordCounter.Tests
{
[TestFixture]
public class WordCounterServiceTests
{
Mock<IDataSource> m_DataSource;
WordCounterService m_WordCounterService;
static string[] s_NoData = new string[] { };
[SetUp]
public void BeforeEachTest()
{
m_DataSource = new Mock<IDataSource>();
m_WordCounterService = new WordCounterService(m_DataSource.Object);
}
[Test]
public async Task GetWordCountUpdates_NoWordsToCount_ReturnsEmptyResult()
{
SetupDataSource(s_NoData);
var result = await GetWordCountUpdate(s_NoData);
Assert.That(result, Is.Empty);
}
[Test]
public async Task GetWordCountUpdates_NoWordsDataInStream_EmptyResult()
{
SetupDataSource(s_NoData);
var result = await GetWordCountUpdate("foo", "bar");
Assert.That(result, Is.Empty);
}
[Test]
public async Task GetWordCountUpdates_WordMatchesInStream_ReturnsExpectedUpdates()
{
SetupDataSource("foo");
var result = (await GetWordCountUpdate("foo")).First();
Assert.That(result.OccurrencesCount, Is.EqualTo(1));
}
static IEnumerable<string> PunctuationTestCases()
{
return " ,.?!;:—\"\r\n".ToCharArray().Select(ch => $"foo{ch}foo");
}
[Test, TestCaseSource(nameof(PunctuationTestCases))]
public async Task GetWordCountUpdates_WordMatchesTwice_ReturnsExpectedUpdates(string streamContext)
{
SetupDataSource(streamContext);
var result = await GetWordCountUpdate("foo");
Assert.That(result.First().OccurrencesCount, Is.EqualTo(2));
}
[Test]
public async Task GetWordCountUpdates_TwoWordMatch_ReturnsExpectedUpdates()
{
SetupDataSource("foo", "bar");
var result = await GetWordCountUpdate("foo", "bar");
Assert.That(result.Select(x => x.Word), Is.EqualTo(new[] { "foo", "bar" }));
Assert.That(result.Select(x => x.OccurrencesCount), Is.EqualTo(new[] { 1, 1 }));
}
[Test]
public async Task GetWordCountUpdates_MultipleWordMatchOnMultipleLines_ReturnsExpectedUpdates()
{
SetupDataSource("foo", "bar", "foo;bar;baz foo", "baz;foo;bar");
var result = await GetWordCountUpdate("foo", "bar");
var sumFoo = result.Where(x => x.Word == "foo").Sum(x => x.OccurrencesCount);
var sumBar = result.Where(x => x.Word == "bar").Sum(x => x.OccurrencesCount);
Assert.That(sumFoo, Is.EqualTo(4));
Assert.That(sumBar, Is.EqualTo(3));
}
void SetupDataSource(params string[] data)
{
m_DataSource.Setup(x => x.GetData()).Returns(data.ToAsyncEnumerable());
}
async Task<List<WordCountUpdate>> GetWordCountUpdate(params string[] wordsToCount)
{
var result = new List<WordCountUpdate>();
await foreach (var update in m_WordCounterService.GetWordCountUpdates(wordsToCount))
{
result.Add(update);
}
return result;
}
}
}