-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSentencesParser_Tests.cs
163 lines (147 loc) · 5.68 KB
/
SentencesParser_Tests.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using NUnit.Framework;
namespace TextAnalysis
{
[TestFixture]
public class SentencesParser_Tests
{
[Test]
[Order(00)]
public void ReturnCorrectResult_OnTextWithOneSentenceWithOneWord()
{
var text = "abc";
var expected = new List<List<string>> { new List<string> { "abc" } };
var actual = SentencesParser.ParseSentences(text);
AssertAllSentencesEqual(expected, actual, text);
}
[Test]
[Order(10)]
public void ReturnCorrectResult_OnTextWithOneSentenceWithTwoWords()
{
var text = "b, c";
var expected = new List<List<string>> { new List<string> { "b", "c" } };
var actual = SentencesParser.ParseSentences(text);
AssertAllSentencesEqual(expected, actual, text);
}
[Test]
[Order(20)]
public void ReturnCorrectResult_OnTextWithOneSentence_WithWordContainingApostrophe()
{
var text = "it's";
var expected = new List<List<string>> { new List<string> { "it's" } };
var actual = SentencesParser.ParseSentences(text);
AssertAllSentencesEqual(expected, actual, text);
}
[Test]
[Order(30)]
public void CorrectlyParse_SentenceDelimiters()
{
var text = "a.b!c?d:e;f(g)h;i";
var expected = new List<List<string>>
{
new List<string> {"a"},
new List<string> {"b"},
new List<string> {"c"},
new List<string> {"d"},
new List<string> {"e"},
new List<string> {"f"},
new List<string> {"g"},
new List<string> {"h"},
new List<string> {"i"}
};
var actual = SentencesParser.ParseSentences(text);
AssertAllSentencesEqual(expected, actual, text);
}
[Test]
[Order(40)]
public void CorrectlyParse_SpecialCharacters()
{
var originalText = "b;\tc;\rd;\ne;\r\nf;\r\n\r\ng";
var escapedText = Regex.Escape(originalText);
var expected = new List<List<string>>
{
new List<string> {"b"},
new List<string> {"c"},
new List<string> {"d"},
new List<string> {"e"},
new List<string> {"f"},
new List<string> {"g"}
};
var actual = SentencesParser.ParseSentences(originalText);
AssertAllSentencesEqual(expected, actual, escapedText);
}
[Test]
[Order(50)]
public void CorrectlyParse_OneSentenceWithWordDelimiter(
[Values('^', '#', '$', '-', '+', '1', '=', ' ', '\t', '\n', '\r')]
char delimiter)
{
var text = "x" + delimiter + "y";
var expected = new List<List<string>>
{
new List<string> {"x", "y"}
};
var actual = SentencesParser.ParseSentences(text);
AssertAllSentencesEqual(expected, actual, text);
}
[Test]
[Order(60)]
public void ReturnResult_InLowerCase()
{
var text = "B.C.D";
var expected = new List<List<string>>
{
new List<string> {"b"},
new List<string> {"c"},
new List<string> {"d"}
};
var actual = SentencesParser.ParseSentences(text);
AssertAllSentencesEqual(expected, actual, text);
}
[Test]
[Order(80)]
public void NotReturnEmptySentence([Values("..", "...!!?","")]
string text)
{
var expected = new List<List<string>>();
var actual = SentencesParser.ParseSentences(text);
AssertAllSentencesEqual(expected, actual, text);
}
//[Test]
//[Order(90)]
//public void First()
//{
// string text = "See more books in http://www.e-reading.biz";
// var expected = new List<List<string>>
// {
// new List<string> { "there's", "talent", "ah", "my", "goodness"},
// };
// var actual = SentencesParser.ParseSentences(text);
// AssertAllSentencesEqual(expected, actual, text);
//}
protected static void AssertAllSentencesEqual(
List<List<string>> expectedSentences,
List<List<string>> actualSentences,
string text)
{
var actualLines = actualSentences.Select(sentence => string.Join(" ", sentence)).ToArray();
var expectedLines = expectedSentences.Select(sentence => string.Join(" ", sentence)).ToArray();
for (var i = 0; i < Math.Min(expectedSentences.Count, actualSentences.Count); i++)
if (actualLines[i] != expectedLines[i])
AssertSentenceEuqal(expectedSentences[i], actualSentences[i], text, i);
CollectionAssert.AreEqual(expectedSentences, actualSentences,
$"Input text: [{text}].\nWrong number of sentences.");
}
protected static void AssertSentenceEuqal(
List<string> expected,
List<string> actual,
string text,
int sentenceNumber)
{
CollectionAssert.AreEqual(expected, actual, $"Input text: [{text}]\nSentence #{sentenceNumber} is wrong");
}
}
}