|
| 1 | +using System; |
| 2 | +using DiffViewer.Utility; |
| 3 | +using FluentAssertions; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace DiffViewer.Tests.Utility; |
| 7 | + |
| 8 | +public class PathListEditorTests |
| 9 | +{ |
| 10 | + private const string Dir = @"C:\Users\me\AppData\Local\DiffViewer\current"; |
| 11 | + |
| 12 | + // ---- Contains -------------------------------------------------------- |
| 13 | + |
| 14 | + [Fact] |
| 15 | + public void Contains_NullOrEmptyRaw_IsFalse() |
| 16 | + { |
| 17 | + PathListEditor.Contains(null, Dir).Should().BeFalse(); |
| 18 | + PathListEditor.Contains("", Dir).Should().BeFalse(); |
| 19 | + } |
| 20 | + |
| 21 | + [Fact] |
| 22 | + public void Contains_ExactSegment_IsTrue() |
| 23 | + => PathListEditor.Contains($@"C:\Windows;{Dir};C:\Tools", Dir).Should().BeTrue(); |
| 24 | + |
| 25 | + [Fact] |
| 26 | + public void Contains_IsCaseInsensitive() |
| 27 | + => PathListEditor.Contains(Dir.ToUpperInvariant(), Dir).Should().BeTrue(); |
| 28 | + |
| 29 | + [Fact] |
| 30 | + public void Contains_TrailingSeparatorMismatch_StillMatches() |
| 31 | + { |
| 32 | + PathListEditor.Contains($@"{Dir}\", Dir).Should().BeTrue(); |
| 33 | + PathListEditor.Contains(Dir, $@"{Dir}\").Should().BeTrue(); |
| 34 | + } |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public void Contains_SubstringOfAnotherSegment_DoesNotMatch() |
| 38 | + { |
| 39 | + // "...\current" must not match "...\current2". |
| 40 | + PathListEditor.Contains($@"{Dir}2", Dir).Should().BeFalse(); |
| 41 | + PathListEditor.Contains($@"C:\a\currentX;C:\b", @"C:\a\current").Should().BeFalse(); |
| 42 | + } |
| 43 | + |
| 44 | + [Fact] |
| 45 | + public void Contains_EnvironmentVariableForm_MatchesExpandedTarget() |
| 46 | + { |
| 47 | + var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); |
| 48 | + var expanded = System.IO.Path.Combine(localAppData, "DiffViewer", "current"); |
| 49 | + var rawWithVar = @"%LOCALAPPDATA%\DiffViewer\current"; |
| 50 | + |
| 51 | + PathListEditor.Contains(rawWithVar, expanded).Should().BeTrue(); |
| 52 | + } |
| 53 | + |
| 54 | + // ---- Add ------------------------------------------------------------- |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public void Add_NullRaw_ReturnsJustDirectory() |
| 58 | + => PathListEditor.Add(null, Dir).Should().Be(Dir); |
| 59 | + |
| 60 | + [Fact] |
| 61 | + public void Add_EmptyRaw_ReturnsJustDirectory() |
| 62 | + => PathListEditor.Add("", Dir).Should().Be(Dir); |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public void Add_WhitespaceRaw_ReturnsJustDirectory() |
| 66 | + => PathListEditor.Add(" ", Dir).Should().Be(Dir); |
| 67 | + |
| 68 | + [Fact] |
| 69 | + public void Add_AppendsWithSingleSeparator() |
| 70 | + => PathListEditor.Add(@"C:\Windows;C:\Tools", Dir).Should().Be($@"C:\Windows;C:\Tools;{Dir}"); |
| 71 | + |
| 72 | + [Fact] |
| 73 | + public void Add_RawEndingWithSeparator_DoesNotDoubleUp() |
| 74 | + => PathListEditor.Add(@"C:\Windows;", Dir).Should().Be($@"C:\Windows;{Dir}"); |
| 75 | + |
| 76 | + [Fact] |
| 77 | + public void Add_AlreadyPresent_ReturnsNull() |
| 78 | + => PathListEditor.Add($@"C:\Windows;{Dir}", Dir).Should().BeNull(); |
| 79 | + |
| 80 | + [Fact] |
| 81 | + public void Add_AlreadyPresentWithTrailingSlash_ReturnsNull() |
| 82 | + => PathListEditor.Add($@"C:\Windows;{Dir}\", Dir).Should().BeNull(); |
| 83 | + |
| 84 | + [Fact] |
| 85 | + public void Add_BlankDirectory_ReturnsNull() |
| 86 | + { |
| 87 | + PathListEditor.Add(@"C:\Windows", "").Should().BeNull(); |
| 88 | + PathListEditor.Add(@"C:\Windows", " ").Should().BeNull(); |
| 89 | + } |
| 90 | + |
| 91 | + [Fact] |
| 92 | + public void Add_TrimsTrailingSeparatorOnStoredEntry() |
| 93 | + => PathListEditor.Add(@"C:\Windows", $@"{Dir}\").Should().Be($@"C:\Windows;{Dir}"); |
| 94 | + |
| 95 | + // ---- Remove ---------------------------------------------------------- |
| 96 | + |
| 97 | + [Fact] |
| 98 | + public void Remove_NullOrEmptyRaw_ReturnsNull() |
| 99 | + { |
| 100 | + PathListEditor.Remove(null, Dir).Should().BeNull(); |
| 101 | + PathListEditor.Remove("", Dir).Should().BeNull(); |
| 102 | + } |
| 103 | + |
| 104 | + [Fact] |
| 105 | + public void Remove_NotPresent_ReturnsNull() |
| 106 | + => PathListEditor.Remove(@"C:\Windows;C:\Tools", Dir).Should().BeNull(); |
| 107 | + |
| 108 | + [Fact] |
| 109 | + public void Remove_FromMiddle_PreservesOtherSegments() |
| 110 | + => PathListEditor.Remove($@"C:\Windows;{Dir};C:\Tools", Dir).Should().Be(@"C:\Windows;C:\Tools"); |
| 111 | + |
| 112 | + [Fact] |
| 113 | + public void Remove_FromEnd() |
| 114 | + => PathListEditor.Remove($@"C:\Windows;{Dir}", Dir).Should().Be(@"C:\Windows"); |
| 115 | + |
| 116 | + [Fact] |
| 117 | + public void Remove_FromStart() |
| 118 | + => PathListEditor.Remove($@"{Dir};C:\Windows", Dir).Should().Be(@"C:\Windows"); |
| 119 | + |
| 120 | + [Fact] |
| 121 | + public void Remove_OnlyEntry_ReturnsEmptyStringNotNull() |
| 122 | + => PathListEditor.Remove(Dir, Dir).Should().Be(string.Empty); |
| 123 | + |
| 124 | + [Fact] |
| 125 | + public void Remove_DuplicateEntries_RemovesAll() |
| 126 | + => PathListEditor.Remove($@"{Dir};C:\Windows;{Dir}\", Dir).Should().Be(@"C:\Windows"); |
| 127 | + |
| 128 | + [Fact] |
| 129 | + public void Remove_PreservesInteriorEmptySegmentsVerbatim() |
| 130 | + => PathListEditor.Remove($@"C:\Windows;;{Dir};C:\Tools", Dir).Should().Be(@"C:\Windows;;C:\Tools"); |
| 131 | + |
| 132 | + [Fact] |
| 133 | + public void Remove_EnvironmentVariableForm_IsRemoved() |
| 134 | + { |
| 135 | + var localAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); |
| 136 | + var expanded = System.IO.Path.Combine(localAppData, "DiffViewer", "current"); |
| 137 | + |
| 138 | + PathListEditor.Remove($@"C:\Windows;%LOCALAPPDATA%\DiffViewer\current", expanded) |
| 139 | + .Should().Be(@"C:\Windows"); |
| 140 | + } |
| 141 | + |
| 142 | + [Fact] |
| 143 | + public void Remove_IsCaseInsensitive() |
| 144 | + => PathListEditor.Remove($@"C:\Windows;{Dir.ToUpperInvariant()}", Dir).Should().Be(@"C:\Windows"); |
| 145 | +} |
0 commit comments