Skip to content
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

Савельев Григорий #171

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions TagsCloud.Tests/BuildersTests/CloudOptionsBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
using FluentAssertions;
using NUnit.Framework;
using SixLabors.Fonts;
using SixLabors.ImageSharp;
using TagsCloud.Builders;
using TagsCloud.Entities;
using TagsCloud.Extensions;
using TagsCloud.Options;
using TagsCloudVisualization;
using static TagsCloud.Tests.TestConfiguration;

namespace TagsCloud.Tests.BuildersTests;

[TestFixture]
[TestOf(nameof(CloudOptionsBuilder))]
public class CloudOptionsBuilderTests
{
[SetUp]
public void SetUp()
{
optionsBuilder = new CloudOptionsBuilder();
}

private const string badFontFile = "404.ttf";
private const string badFontFamily = "mono-dotnet";
private const string fontName = "Vollkorn";

private CloudOptionsBuilder optionsBuilder;

[TestCase(0, 100)]
[TestCase(100, 0)]
public void Builder_Should_ReturnFailResult_When_FontBoundsLessOrEqualToZero(int lowerBound, int upperBound)
{
var builderResult = optionsBuilder
.SetFontBounds(lowerBound, upperBound);

TestHelper.AssertResultFail(builderResult);
}

[TestCase(150, 100)]
[TestCase(100, 100)]
public void Builder_Should_ReturnFailResult_When_UpperFontBoundLessOrEqualToLower(int lowerBound, int upperBound)
{
var builderResult = optionsBuilder
.SetFontBounds(lowerBound, upperBound);

TestHelper.AssertResultFail(builderResult);
}

[Test]
public void Builder_Should_ReturnFailResult_When_FontFileNotFound()
{
var builderResult = optionsBuilder
.SetFontFamily(badFontFile);

TestHelper.AssertResultFailAndErrorText(builderResult, $"{badFontFile} font file not found!");
}

[Test]
public void Builder_Should_ReturnFailResult_When_FontFamilyNotFoundInSystem()
{
var builderResult = optionsBuilder
.SetFontFamily(badFontFamily);

TestHelper.AssertResultFailAndErrorText(builderResult, $"{badFontFamily} family is unknown!");
}

[Test]
public void Builder_Should_ReturnFailResult_When_FontFamilyNotSet()
{
var builderResult = optionsBuilder
.AsResult()
.Then(builder => builder
.SetLayout(PointGeneratorType.Spiral, new PointF(), 0, 0))
.Then(builder => builder.BuildOptions());

TestHelper.AssertResultFailAndErrorText(
builderResult,
$"You must set {nameof(FontFamily)} before building options!");
}

[Test]
public void Builder_Should_ReturnFailResult_When_LayoutNotSet()
{
var builderResult = optionsBuilder
.AsResult()
.Then(builder => builder.SetFontFamily(Path.Join(TestDataPath, $"{fontName}-Regular.ttf")))
.Then(builder => builder.BuildOptions());

TestHelper.AssertResultFailAndErrorText(
builderResult,
$"You must set {nameof(Layout)} before building options!");
}

[Test]
public void Builder_Should_ReturnSuccessResult_When_CorrectInputValues()
{
var builderResult = optionsBuilder
.AsResult()
.Then(builder => builder.SetSortingType(SortType.Preserve))
.Then(builder => builder.SetMeasurerType(MeasurerType.Logarithmic))
.Then(builder => builder.SetColoringStrategy(ColoringStrategy.AllRandom))
.Then(builder => builder.SetColors(new HashSet<string> { "#FFD700" }))
.Then(builder => builder.SetFontBounds(40, 50))
.Then(builder => builder.SetFontFamily(
Path.Combine(TestDataPath, $"{fontName}-Regular.ttf")))
.Then(builder => builder
.SetLayout(PointGeneratorType.Spiral, new PointF(), 0, 0))
.Then(builder => builder.BuildOptions());

var expected = new CloudProcessorOptions
{
Colors = new[] { Color.Gold },
MinFontSize = 40,
MaxFontSize = 50,
ColoringStrategy = ColoringStrategy.AllRandom,
MeasurerType = MeasurerType.Logarithmic,
Sort = SortType.Preserve
};

TestHelper.AssertResultSuccess(builderResult);
builderResult.Value.ShouldBeEquivalentTo(expected,
config =>
config.Excluding(options => options.Layout)
.Using<FontFamily>(ctx =>
ctx.Subject.Name.Should().Be(fontName))
.WhenTypeIs<FontFamily>());
}
}
53 changes: 53 additions & 0 deletions TagsCloud.Tests/BuildersTests/InputOptionsBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using FluentAssertions;
using NUnit.Framework;
using TagsCloud.Builders;
using TagsCloud.Entities;
using TagsCloud.Extensions;
using TagsCloud.Options;

namespace TagsCloud.Tests.BuildersTests;

[TestFixture]
[TestOf(nameof(InputOptionsBuilder))]
public class InputOptionsBuilderTests
{
private readonly InputOptionsBuilder optionsBuilder = new();

[Test]
public void Builder_Should_ReturnFailResult_When_BadTextParts()
{
var textParts = new HashSet<string> { "S", "V", "CORS", "RPC", "MVC" };

var builderResult = optionsBuilder
.SetLanguageParts(textParts);

TestHelper.AssertResultFail(builderResult);
builderResult.Error.Should().Contain("CORS, RPC, MVC");
builderResult.Error.Should().NotContain("S, V");
}

[Test]
public void Builder_Should_ReturnSuccessResult_When_CorrectInputValues()
{
var builderResult = optionsBuilder
.AsResult()
.Then(builder => builder.SetCastPolitics(true))
.Then(builder => builder.SetWordsCase(CaseType.Lower))
.Then(builder => builder.SetLanguagePolitics(false))
.Then(builder => builder.SetExcludedWords(new HashSet<string> { "C++" }))
.Then(builder => builder.SetLanguageParts(new HashSet<string> { "S", "CONJ" }))
.Then(builder => builder.BuildOptions());

var expected = new InputProcessorOptions
{
WordsCase = CaseType.Lower,
ToInfinitive = true,
ExcludedWords = new HashSet<string> { "C++" },
LanguageParts = new HashSet<string> { "S", "CONJ" },
OnlyRussian = false
};

TestHelper.AssertResultSuccess(builderResult);
builderResult.Value.ShouldBeEquivalentTo(expected);
}
}
56 changes: 56 additions & 0 deletions TagsCloud.Tests/BuildersTests/OutputOptionsBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using FluentAssertions;
using NUnit.Framework;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Formats.Bmp;
using TagsCloud.Builders;
using TagsCloud.Entities;
using TagsCloud.Extensions;
using TagsCloud.Options;
using static TagsCloud.Tests.TestConfiguration;

namespace TagsCloud.Tests.BuildersTests;

[TestFixture]
[TestOf(nameof(OutputOptionsBuilder))]
public class OutputOptionsBuilderTests
{
private readonly OutputOptionsBuilder optionsBuilder = new();

[TestCase(1920, -1080)]
[TestCase(-1920, 1080)]
public void Builder_Should_ReturnFailResult_When_ImageWidthOrHeightBelowZero(int width, int height)
{
var builderResult = optionsBuilder
.SetImageSize(width, height);

TestHelper.AssertResultFailAndErrorText(builderResult);
}

[Test]
public void Builder_Should_ReturnSuccessResult_When_CorrectInputValues()
{
var builderResult = optionsBuilder
.AsResult()
.Then(builder => builder.SetImageFormat(ImageFormat.Bmp))
.Then(builder => builder.SetImageSize(WindowWidth, WindowHeight))
.Then(builder => builder.SetImageBackgroundColor("#FFD700"))
.Then(builder => builder.BuildOptions());

var expected = new OutputProcessorOptions
{
BackgroundColor = Color.Gold,
ImageSize = new Size(WindowWidth, WindowHeight),
ImageEncoder = new BmpEncoder()
};

TestHelper.AssertResultSuccess(builderResult);
builderResult.Value.ShouldBeEquivalentTo(expected,
opt
=> opt.Using<Color>(ctx => ctx.Subject.Should().Be(ctx.Expectation))
.WhenTypeIs<Color>()
.Using<IImageEncoder>(ctx =>
ctx.Subject.GetType().Should().Be(ctx.Expectation.GetType()))
.WhenTypeIs<IImageEncoder>());
}
}
29 changes: 29 additions & 0 deletions TagsCloud.Tests/OtherTests/DefaultPostFormatterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using FluentAssertions;
using NUnit.Framework;
using TagsCloud.Formatters;

namespace TagsCloud.Tests.OtherTests;

[TestFixture]
[TestOf(nameof(DefaultPostFormatter))]
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);
}
}
63 changes: 63 additions & 0 deletions TagsCloud.Tests/OtherTests/FileReadersTests.cs
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.FileReaders;
using TagsCloud.Formatters;

namespace TagsCloud.Tests.OtherTests;

[TestFixture]
[TestOf(nameof(FileReadersTests))]
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);
}
}

[GeneratedRegex("([A-Z]\\w*)FileReader")]
private static partial Regex ReaderNamePattern();
}
Loading