-
Notifications
You must be signed in to change notification settings - Fork 303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Савельев Григорий #200
base: master
Are you sure you want to change the base?
Савельев Григорий #200
Changes from 1 commit
da15221
0780e96
73a2267
b22dab8
350b9c1
b3a8bfe
6fe9c1d
553449d
a216c4e
554883b
c9e8946
5579be7
9965d1e
5ecfab1
4892a1e
b2084cb
609ddca
f3c6de3
2f385c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using FluentAssertions; | ||
using NUnit.Framework; | ||
using TagsCloud.Formatters; | ||
|
||
namespace TagsCloud.Tests; | ||
|
||
[TestFixture] | ||
public class DefaultPostFormatterTests | ||
{ | ||
private readonly DefaultPostFormatter defaultFormatter = new(); | ||
|
||
[TestCase("Mercedes-Benz", "Mercedes")] | ||
[TestCase("Hello, world!", "Hello")] | ||
[TestCase("123", "")] | ||
[TestCase("Apple Orange Banana", "Apple")] | ||
[TestCase("", "")] | ||
[TestCase(" circle", "circle")] | ||
[TestCase("circle ", "circle")] | ||
[TestCase("$$$", "")] | ||
[TestCase("A", "A")] | ||
[TestCase(" ___ Juice", "")] | ||
[TestCase(" ", "")] | ||
public void Formatter_Should_CutLineToFirstNonLetterCharacter(string line, string expected) | ||
{ | ||
var actual = defaultFormatter.Format(line); | ||
actual.ShouldBeEquivalentTo(expected); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using FluentAssertions; | ||
using NUnit.Framework; | ||
using System.Reflection; | ||
using System.Text.RegularExpressions; | ||
using TagsCloud.Contracts; | ||
using TagsCloud.Formatters; | ||
|
||
namespace TagsCloud.Tests; | ||
|
||
[TestFixture] | ||
public partial class FileReadersTests | ||
{ | ||
[OneTimeSetUp] | ||
public void OneTimeSetUp() | ||
{ | ||
expectedLines = File.ReadAllLines($"{testDataPath}.txt") | ||
.Select(line => line.Split(separators, StringSplitOptions.RemoveEmptyEntries)) | ||
.Select(array => array[0]) | ||
.Where(word => char.IsLetter(word[0])) | ||
.ToArray(); | ||
} | ||
|
||
private readonly string testDataPath = Path.Join("TestData", "test_data"); | ||
private readonly IPostFormatter defaultFormatter = new DefaultPostFormatter(); | ||
private readonly char[] separators = { ' ', ',', '.', ':', ';', '!', '?' }; | ||
private string[] expectedLines; | ||
|
||
private readonly IFileReader[] fileReaders = | ||
Assembly | ||
.GetAssembly(typeof(IFileReader))! | ||
.GetTypes() | ||
.Where(type => type.GetInterfaces().Any(inter => inter == typeof(IFileReader))) | ||
.Select(reader => (IFileReader)Activator.CreateInstance(reader)!) | ||
.ToArray(); | ||
|
||
[Test] | ||
public void Readers_Should_ReadFileContentCorrectly() | ||
{ | ||
foreach (var reader in fileReaders) | ||
{ | ||
var actualLines = reader | ||
.ReadContent($"{testDataPath}.{reader.SupportedExtension}", defaultFormatter) | ||
.Where(line => !string.IsNullOrEmpty(line)); | ||
|
||
actualLines.ShouldAllBeEquivalentTo(expectedLines); | ||
} | ||
} | ||
|
||
[Test] | ||
public void ReadersNames_Should_MatchSupportedExtensions() | ||
{ | ||
foreach (var reader in fileReaders) | ||
{ | ||
var match = ReaderNamePattern().Match(reader.GetType().Name); | ||
|
||
match.Success.Should().Be(true); | ||
match.Groups[1].Value.ToLower().ShouldBeEquivalentTo(reader.SupportedExtension); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Сомнительный тест, т.к. по контракт по имени класса не самый сильный, но в целом пойдет, т.к. административные конвенции всё же встречаются. |
||
} | ||
} | ||
|
||
[GeneratedRegex("([A-Z]\\w*)FileReader")] | ||
private static partial Regex ReaderNamePattern(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
using FluentAssertions; | ||
using NUnit.Framework; | ||
using SixLabors.ImageSharp; | ||
using TagsCloud.Builders; | ||
using TagsCloud.Entities; | ||
using TagsCloudVisualization; | ||
|
||
namespace TagsCloud.Tests; | ||
|
||
[TestFixture] | ||
public class GlobalTest | ||
{ | ||
[OneTimeSetUp] | ||
public void OneTimeSetUp() | ||
{ | ||
var textParts = new HashSet<string> { "S" }; | ||
var colors = new HashSet<string> { "#de2114", "#07f727", "#7707f7" }; | ||
var (width, height) = (1920, 1080); | ||
|
||
var inputOptions = new InputOptionsBuilder() | ||
.SetWordsCase(CaseType.Upper) | ||
.SetCastPolitics(true) | ||
.SetExcludedWords(excludedWords) | ||
.SetLanguageParts(textParts) | ||
.SetLanguagePolitics(true) | ||
.BuildOptions(); | ||
|
||
var cloudOptions = new CloudOptionsBuilder() | ||
.SetColors(colors) | ||
.SetLayout( | ||
PointGeneratorType.Spiral, | ||
new PointF((float)width / 2, (float)height / 2), | ||
0.1f, | ||
(float)Math.PI / 180) | ||
.SetColoringStrategy(ColoringStrategy.AllRandom) | ||
.SetMeasurerType(MeasurerType.Linear) | ||
.SetFontFamily(string.Empty) | ||
.SetSortingType(SortType.Ascending) | ||
.SetFontSizeBounds(35, 100) | ||
.BuildOptions(); | ||
|
||
var outputOptions = new OutputOptionsBuilder() | ||
.SetImageFormat(ImageFormat.Jpeg) | ||
.SetImageSize(new Size(width, height)) | ||
.SetImageBackgroundColor("#ffffff") | ||
.BuildOptions(); | ||
|
||
var engine = new TagCloudEngine(inputOptions, cloudOptions, outputOptions); | ||
wordGroups = engine.GenerateTagCloud(Path.Join("TestData", "data.txt"), outputPath); | ||
} | ||
|
||
private readonly HashSet<string> excludedWords = new(StringComparer.OrdinalIgnoreCase) | ||
{ | ||
"Ноутбук", | ||
"Камера" | ||
}; | ||
|
||
private const string outputPath = "tagcloud_image.jpeg"; | ||
|
||
private readonly HashSet<string> verbs = new(StringComparer.OrdinalIgnoreCase) | ||
{ | ||
"Программировать", | ||
"Прыгать", | ||
"Бегать", | ||
"Играть" | ||
}; | ||
|
||
private readonly HashSet<string> englishWords = new(StringComparer.OrdinalIgnoreCase) | ||
{ | ||
"America", | ||
"Russia", | ||
"Germany", | ||
"Apple", | ||
"TV", | ||
"Join", | ||
"Split" | ||
}; | ||
|
||
private HashSet<WordTagGroup> wordGroups; | ||
|
||
[Test] | ||
public void WordGroups_Should_ContainOnlyRussianWords() | ||
{ | ||
wordGroups.Should().NotContain(group => englishWords.Contains(group.WordInfo.Text)); | ||
} | ||
|
||
[Test] | ||
public void WordGroups_ShouldNot_ContainVerbs() | ||
{ | ||
wordGroups.Should().NotContain(group => verbs.Contains(group.WordInfo.Text)); | ||
} | ||
|
||
[Test] | ||
public void WordGroups_ShouldNot_ContainExcludedWords() | ||
{ | ||
wordGroups.Should().NotContain(group => excludedWords.Contains(group.WordInfo.Text)); | ||
} | ||
|
||
[Test] | ||
public void WordGroupsWords_Should_BeUpperCase() | ||
{ | ||
foreach (var group in wordGroups) | ||
AreLettersUpperCase(group.WordInfo.Text).Should().Be(true); | ||
} | ||
|
||
[Test] | ||
public void TagCloud_Should_CreateImageFile() | ||
{ | ||
File.Exists(outputPath).Should().Be(true); | ||
} | ||
|
||
private static bool AreLettersUpperCase(string word) | ||
{ | ||
return word.Where(char.IsLetter).All(letter => letter == char.ToUpper(letter)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Программировать | ||
Играет | ||
Germany | ||
Бегает | ||
Прыгают | ||
Apple | ||
Join | ||
Split | ||
TV | ||
Бегать | ||
Бегать | ||
Russia | ||
Russia | ||
Russia | ||
Бегать | ||
Камера | ||
Камера | ||
Играть | ||
Играть | ||
Прыжок | ||
Ноутбук | ||
Планшет | ||
Яблоко | ||
America | ||
Ноутбук |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
Lorem , | ||
consectetur 1 2 3 4 5, | ||
Praesent, | ||
Nunc --->, | ||
Maecenas, | ||
Fusce, | ||
Nullam (5 + 5 = 10), | ||
Nulla world!, | ||
Vivamus Hello, | ||
Aenean, | ||
Mauris <<<-, | ||
Nullam, | ||
nec Microsoft Power point, | ||
Aliquam nec, |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
Lorem! ipsum dolor sit amet, | ||
consectetur :) adipiscing elit. | ||
--->>>>>>>>>>>>>>>>>>>>>> | ||
@@@@ | ||
Praesent blandit consequat tristique. Donec felis purus, consequat eget enim et, tempus porttitor felis. -> | ||
Nunc @@@ semper nulla a ante vulputate mattis. | ||
Maecenas pellentesque vestibulum nisl, ut elementum ante sagittis nec. | ||
Fusce semper finibus nibh at vehicula. | ||
Nullam, auctor euismod risus, ac interdum leo. Nulla pulvinar turpis eu interdum volutpat. | ||
Nulla, dignissim vestibulum leo, vel mollis quam. Pellentesque sed erat dictum, commodo arcu ut, euismod urna. | ||
###################### | ||
Vivamus, eu venenatis velit. | ||
Aenean rhoncus et lectus et aliquam. | ||
Mauris quis est odio. Vivamus id condimentum nunc. | ||
Nullam sagittis dictum leo, | ||
nec egestas ligula accumsan eget. | ||
<><><><> | ||
Aliquam enim dui, pharetra accumsan tincidunt id, imperdiet quis quam. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using FluentAssertions; | ||
using NUnit.Framework; | ||
using TagsCloud.TextAnalysisTools; | ||
using TagsCloudVisualization; | ||
|
||
namespace TagsCloud.Tests; | ||
|
||
[TestFixture] | ||
public class TextAnalyzerTests | ||
{ | ||
[Test] | ||
public void TextAnalyzer_Should_DistinguishRussianAndOtherWords() | ||
{ | ||
var testData = GetTestGroups().ToArray(); | ||
var groups = testData | ||
.Select(data => data.Group) | ||
.ToHashSet(); | ||
|
||
TextAnalyzer.FillWithAnalysis(groups); | ||
|
||
foreach (var (group, isEnglish) in testData) | ||
group.WordInfo.IsRussian.Should().Be(isEnglish); | ||
} | ||
|
||
private static IEnumerable<(WordTagGroup Group, bool isRussian)> GetTestGroups() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Кажется, проще было реализовать массив, чем прописывать |
||
{ | ||
yield return (new WordTagGroup("Apple", 1), false); | ||
yield return (new WordTagGroup("Игра", 1), true); | ||
yield return (new WordTagGroup("BMW", 1), false); | ||
yield return (new WordTagGroup("Богатырь", 1), true); | ||
yield return (new WordTagGroup("Математика", 1), true); | ||
yield return (new WordTagGroup("C#", 1), false); | ||
yield return (new WordTagGroup("Fibonacci", 1), false); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,10 +33,14 @@ public HashSet<WordTagGroup> CollectWordGroupsFromFile(string filename) | |
var reader = FindFileReader(filename); | ||
var wordGroups = reader | ||
.ReadContent(filename, postFormatter) | ||
.Where(line => !string.IsNullOrEmpty(line)) | ||
.GroupBy(line => line) | ||
.Select(group => new WordTagGroup(group.Key, group.Count())) | ||
.ToHashSet(); | ||
|
||
if (wordGroups.Count == 0) | ||
throw new ArgumentException("No words found! Check file structure."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно информативнее 🙃, у тебя на руках есть имя файла |
||
|
||
TextAnalyzer.FillWithAnalysis(wordGroups); | ||
filterConveyor.ApplyFilters(wordGroups); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Попробуй ещё сделать вот так, но перезаливать нет нужды, просто попробуй.
В
TestFixture
можно добавитьTestOf
. К методу можно добавить такой же атрибутTestOf
- тогда нет нужды в имя теста вписывать имя метода / сущности. Тестируемую систему часто называютsut