|
| 1 | +// Copyright (c) ktsu.dev |
| 2 | +// All rights reserved. |
| 3 | +// Licensed under the MIT license. |
| 4 | + |
| 5 | +namespace KtsuBuild.Tests.Metadata; |
| 6 | + |
| 7 | +using KtsuBuild.Metadata; |
| 8 | +using KtsuBuild.Tests.Helpers; |
| 9 | + |
| 10 | +[TestClass] |
| 11 | +public class TagsParserTests |
| 12 | +{ |
| 13 | + private static readonly string[] ThreeTopics = ["extensions", "collection", "utility"]; |
| 14 | + private static readonly string[] HyphenatedTopics = ["my-library", "code-helper"]; |
| 15 | + private static readonly string[] SanitizedTopics = ["c", "dotnet", "mytag", "valid-tag"]; |
| 16 | + private static readonly string[] TwoTopics = ["dotnet", "csharp"]; |
| 17 | + |
| 18 | + private string _tempDir = null!; |
| 19 | + |
| 20 | + [TestInitialize] |
| 21 | + public void Setup() => _tempDir = TestHelpers.CreateTempDir("TagsParser"); |
| 22 | + |
| 23 | + [TestCleanup] |
| 24 | + public void Cleanup() |
| 25 | + { |
| 26 | + if (Directory.Exists(_tempDir)) |
| 27 | + { |
| 28 | + Directory.Delete(_tempDir, recursive: true); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + [TestMethod] |
| 33 | + public async Task ParseAsync_SemicolonSeparated_ReturnsParsedTopics() |
| 34 | + { |
| 35 | + string tagsFile = Path.Combine(_tempDir, "TAGS.md"); |
| 36 | + await File.WriteAllTextAsync(tagsFile, "extensions;collection;utility").ConfigureAwait(false); |
| 37 | + |
| 38 | + IReadOnlyList<string> topics = await TagsParser.ParseAsync(tagsFile).ConfigureAwait(false); |
| 39 | + |
| 40 | + CollectionAssert.AreEqual(ThreeTopics, topics.ToList()); |
| 41 | + } |
| 42 | + |
| 43 | + [TestMethod] |
| 44 | + public async Task ParseAsync_TrimsWhitespace() |
| 45 | + { |
| 46 | + string tagsFile = Path.Combine(_tempDir, "TAGS.md"); |
| 47 | + await File.WriteAllTextAsync(tagsFile, " extensions ; collection ; utility ").ConfigureAwait(false); |
| 48 | + |
| 49 | + IReadOnlyList<string> topics = await TagsParser.ParseAsync(tagsFile).ConfigureAwait(false); |
| 50 | + |
| 51 | + CollectionAssert.AreEqual(ThreeTopics, topics.ToList()); |
| 52 | + } |
| 53 | + |
| 54 | + [TestMethod] |
| 55 | + public async Task ParseAsync_ConvertsToLowercase() |
| 56 | + { |
| 57 | + string tagsFile = Path.Combine(_tempDir, "TAGS.md"); |
| 58 | + await File.WriteAllTextAsync(tagsFile, "Extensions;COLLECTION;Utility").ConfigureAwait(false); |
| 59 | + |
| 60 | + IReadOnlyList<string> topics = await TagsParser.ParseAsync(tagsFile).ConfigureAwait(false); |
| 61 | + |
| 62 | + CollectionAssert.AreEqual(ThreeTopics, topics.ToList()); |
| 63 | + } |
| 64 | + |
| 65 | + [TestMethod] |
| 66 | + public async Task ParseAsync_ReplacesSpacesWithHyphens() |
| 67 | + { |
| 68 | + string tagsFile = Path.Combine(_tempDir, "TAGS.md"); |
| 69 | + await File.WriteAllTextAsync(tagsFile, "my library;code helper").ConfigureAwait(false); |
| 70 | + |
| 71 | + IReadOnlyList<string> topics = await TagsParser.ParseAsync(tagsFile).ConfigureAwait(false); |
| 72 | + |
| 73 | + CollectionAssert.AreEqual(HyphenatedTopics, topics.ToList()); |
| 74 | + } |
| 75 | + |
| 76 | + [TestMethod] |
| 77 | + public async Task ParseAsync_RemovesInvalidCharacters() |
| 78 | + { |
| 79 | + string tagsFile = Path.Combine(_tempDir, "TAGS.md"); |
| 80 | + await File.WriteAllTextAsync(tagsFile, "c#;dotnet!;my_tag;valid-tag").ConfigureAwait(false); |
| 81 | + |
| 82 | + IReadOnlyList<string> topics = await TagsParser.ParseAsync(tagsFile).ConfigureAwait(false); |
| 83 | + |
| 84 | + CollectionAssert.AreEqual(SanitizedTopics, topics.ToList()); |
| 85 | + } |
| 86 | + |
| 87 | + [TestMethod] |
| 88 | + public async Task ParseAsync_FileNotFound_ReturnsEmptyList() |
| 89 | + { |
| 90 | + IReadOnlyList<string> topics = await TagsParser.ParseAsync(Path.Combine(_tempDir, "nonexistent.md")).ConfigureAwait(false); |
| 91 | + |
| 92 | + Assert.AreEqual(0, topics.Count); |
| 93 | + } |
| 94 | + |
| 95 | + [TestMethod] |
| 96 | + public async Task ParseAsync_EmptyFile_ReturnsEmptyList() |
| 97 | + { |
| 98 | + string tagsFile = Path.Combine(_tempDir, "TAGS.md"); |
| 99 | + await File.WriteAllTextAsync(tagsFile, "").ConfigureAwait(false); |
| 100 | + |
| 101 | + IReadOnlyList<string> topics = await TagsParser.ParseAsync(tagsFile).ConfigureAwait(false); |
| 102 | + |
| 103 | + Assert.AreEqual(0, topics.Count); |
| 104 | + } |
| 105 | + |
| 106 | + [TestMethod] |
| 107 | + public async Task ParseAsync_DeduplicatesTopics() |
| 108 | + { |
| 109 | + string tagsFile = Path.Combine(_tempDir, "TAGS.md"); |
| 110 | + await File.WriteAllTextAsync(tagsFile, "dotnet;csharp;dotnet;csharp").ConfigureAwait(false); |
| 111 | + |
| 112 | + IReadOnlyList<string> topics = await TagsParser.ParseAsync(tagsFile).ConfigureAwait(false); |
| 113 | + |
| 114 | + CollectionAssert.AreEqual(TwoTopics, topics.ToList()); |
| 115 | + } |
| 116 | + |
| 117 | + [TestMethod] |
| 118 | + public void Parse_WhitespaceOnly_ReturnsEmptyList() |
| 119 | + { |
| 120 | + IReadOnlyList<string> topics = TagsParser.Parse(" "); |
| 121 | + |
| 122 | + Assert.AreEqual(0, topics.Count); |
| 123 | + } |
| 124 | + |
| 125 | + [TestMethod] |
| 126 | + public void Parse_EmptySemicolonSegments_FilteredOut() |
| 127 | + { |
| 128 | + IReadOnlyList<string> topics = TagsParser.Parse(";;dotnet;;;csharp;;"); |
| 129 | + |
| 130 | + CollectionAssert.AreEqual(TwoTopics, topics.ToList()); |
| 131 | + } |
| 132 | +} |
0 commit comments