Skip to content
Open
Show file tree
Hide file tree
Changes from 7 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
82 changes: 82 additions & 0 deletions TagCloudGenerator.Tests/ConverterTests/BoringWordFilterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Collections.Generic;
using FluentAssertions;
using NUnit.Framework;
using TagsCloudVisualization;

namespace TagCloudGenerator.FileConverter.Tests;

[TestFixture]
public class BoringWordFilterTests
{
[Test]
public void Filter_RemovesShortHighFrequencyAndLowCount()
{
var raw = new Dictionary<string, int>()
{
{"a", 10},
{"hello", 1},
{"word", 2}
};
var totalWords = 13;

var filter = new BoringWordFilter();
var result = filter.Filter(raw, totalWords, null);

result.Should().ContainKey("word");
result["word"].Should().Be(2);
result.Should().NotContainKey("a");
result.Should().NotContainKey("hello");
}

[Test]
public void Filter_RemovesEmptyOrNullWord()
{
var raw = new Dictionary<string, int>()
{
{"", 5},
{"good", 3}
};

var filter = new BoringWordFilter();
var result = filter.Filter(raw, 8, null);

result.Should().ContainKey("good");
result["good"].Should().Be(3);
result.Should().NotContainKey("");
}

[Test]
public void Filter_RemovesShortWordAtMinimalCount()
{
var raw = new Dictionary<string, int>()
{
{"it", 2},
{"content", 10}
};

var totalWords = 20;

var filter = new BoringWordFilter();
var result = filter.Filter(raw, totalWords, null);

result.Should().NotContainKey("it");
result.Should().ContainKey("content");
}
[Test]
public void Filter_RespectsProvidedBlacklist()
{
var raw = new Dictionary<string, int>()
{
{"skipme", 10},
{"keep", 5}
};

var filter = new BoringWordFilter();
var blacklist = new HashSet<string>() { "skipme" };
var result = filter.Filter(raw, 15, blacklist);

result.Should().ContainKey("keep");
result.Should().NotContainKey("skipme");
}
}
90 changes: 90 additions & 0 deletions TagCloudGenerator.Tests/ConverterTests/FileConverterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.IO;
using System.Collections.Generic;
using FluentAssertions;
using NUnit.Framework;
using TagsCloudVisualization;

namespace TagCloudGenerator.FileConverter.Tests;

[TestFixture]
public class FileConverterTests
{
private string CreateTempFile(IEnumerable<string> lines)
{
var path = Path.GetTempFileName();
File.WriteAllLines(path, lines);
return path;
}

[Test]
public void Convert_WhenOk_ReturnsCalculatedSizes()
{
var lines = new[] { "Hello", "hello", "World", "world", "content" };
var path = CreateTempFile(lines);

var counter = new FileWordCounter();
var filter = new BoringWordFilter();
var calculator = new WordFontSizeCalculator();

var converter = new TagsCloudVisualization.FileConverter(counter, filter, calculator, new DefaultAppPaths());
var result = converter.Convert(path);

result.Should().ContainKey("hello");
result["hello"].Should().Be(72f);
result.Should().ContainKey("world");
result["world"].Should().Be(72f);
result.Should().NotContainKey("content");

File.Delete(path);
}

[Test]
public void Convert_WhenFilterRemovesAll_ThrowsFromCalculator()
{
var lines = new[] { "one", "two" };
var path = CreateTempFile(lines);

var counter = new FileWordCounter();
var emptyFilter = new BoringWordFilter();
var calculator = new WordFontSizeCalculator();

var converter = new TagsCloudVisualization.FileConverter(counter, emptyFilter, calculator, new DefaultAppPaths());

FluentActions.Invoking(() => converter.Convert(path))
.Should().Throw<InvalidOperationException>();

File.Delete(path);
}

[Test]
public void Convert_WhenFileDoesNotExist_ThrowsFileNotFoundException()
{
var counter = new FileWordCounter();
var filter = new BoringWordFilter();
var calculator = new WordFontSizeCalculator();

var converter = new TagsCloudVisualization.FileConverter(counter, filter, calculator, new DefaultAppPaths());

FluentActions.Invoking(() => converter.Convert("nonexistent_file_12345.tmp"))
.Should().Throw<FileNotFoundException>();
}

[Test]
public void Convert_NotContain_WordsFromBlackList()
{
var lines = new[] { "Hello", "hello", "World", "world", "content" };
var blackList = new[] { "hello" };
var blackPath = CreateTempFile(blackList);
var path = CreateTempFile(lines);
var counter = new FileWordCounter();
var filter = new BoringWordFilter();
var calculator = new WordFontSizeCalculator();

var converter = new TagsCloudVisualization.FileConverter(counter, filter, calculator, new DefaultAppPaths());
var result = converter.Convert(path,blackPath);
result.Should().NotContainKey("hello");
result.Should().NotContainKey("Hello");
File.Delete(path);
}
}
86 changes: 86 additions & 0 deletions TagCloudGenerator.Tests/ConverterTests/FileWordCounterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.IO;
using System.Collections.Generic;
using FluentAssertions;
using NUnit.Framework;
using TagsCloudVisualization;

namespace TagCloudGenerator.FileConverter.Tests;

[TestFixture]
public class FileWordCounterTests
{
private string CreateTempFile(IEnumerable<string> lines)
{
var path = Path.GetTempFileName();
File.WriteAllLines(path, lines);
return path;
}

[Test]
public void CountWords_NormalCountsAndTotal()
{
var lines = new[] { "Hello", "world", "hello" };
var path = CreateTempFile(lines);

var counter = new FileWordCounter();
var counts = counter.CountWords(path, out var total);

total.Should().Be(3);
counts.Should().ContainKey("hello");
counts["hello"].Should().Be(2);
counts.Should().ContainKey("world");
counts["world"].Should().Be(1);

File.Delete(path);
}

[Test]
public void CountWords_IgnoresNonLettersAndWhitespace()
{
var lines = new[] { "", "123", "abc!", "Good" };
var path = CreateTempFile(lines);

var counter = new FileWordCounter();
var counts = counter.CountWords(path, out var total);

total.Should().Be(1);
counts.Should().ContainKey("good");
counts["good"].Should().Be(1);
counts.Should().NotContainKey("123");
counts.Should().NotContainKey("abc!");

File.Delete(path);
}

[Test]
public void CountWords_EmptyFile_ReturnsEmptyAndZero()
{
var path = CreateTempFile(Array.Empty<string>());

var counter = new FileWordCounter();
var counts = counter.CountWords(path, out var total);

total.Should().Be(0);
counts.Should().BeEmpty();

File.Delete(path);
}

[Test]
public void CountWords_IgnoresLinesWithMultipleWords()
{
var lines = new[] { "hello world", "ok", "hello world" };
var path = CreateTempFile(lines);

var counter = new FileWordCounter();
var counts = counter.CountWords(path, out var total);

total.Should().Be(1);
counts.Should().ContainKey("ok");
counts["ok"].Should().Be(1);
counts.Should().NotContainKey("hello world");

File.Delete(path);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using FluentAssertions;
using NUnit.Framework;
using TagsCloudVisualization;

namespace TagCloudGenerator.FileConverter.Tests;

[TestFixture]
public class WordFontSizeCalculatorTests
{
[Test]
public void CalculateFontSizes_NormalScaling_ReturnsExpectedSizes()
{
var calculator = new WordFontSizeCalculator();
var input = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase)
{
{"a", 2},
{"b", 1}
};

var result = calculator.CalculateFontSizes(input);

result.Should().ContainKey("a");
result["a"].Should().Be(72f);
result.Should().ContainKey("b");
result["b"].Should().Be(40f);
}

[Test]
public void CalculateFontSizes_EmptyDictionary_ThrowsInvalidOperationException()
{
var calculator = new WordFontSizeCalculator();
var empty = new Dictionary<string, int>();

FluentActions.Invoking(() => calculator.CalculateFontSizes(empty))
.Should().Throw<InvalidOperationException>();
}
}
Loading