Skip to content

A Swiss Army knife for developers.

Added unit tests

Etienne Baudouxe2fc2af59281
diff --git a/src/Directory.Packages.props b/src/Directory.Packages.props
index 773b723f5..404809c17 100644
--- a/src/Directory.Packages.props
+++ b/src/Directory.Packages.props
@@ -8,6 +8,7 @@
<PackageVersion Include="CommunityToolkit.Diagnostics" Version="$(CommunityToolkitVersion)" />
<PackageVersion Include="CommunityToolkit.Mvvm" Version="$(CommunityToolkitVersion)" />
<PackageVersion Include="coverlet.collector" Version="3.1.2" />
+ <PackageVersion Include="FluentAssertions" Version="6.11.0" />
<PackageVersion Include="FuzzySharp" Version="2.0.2" />
<PackageVersion Include="Microsoft.AspNetCore.Components.Web" Version="$(DotNetVersion)" />
<PackageVersion Include="Microsoft.AspNetCore.Components.WebView.Wpf" Version="7.0.86" />
diff --git a/src/app/dev/DevToys.Api/Core/ObservableCollectionExtensions.cs b/src/app/dev/DevToys.Api/Core/ObservableCollectionExtensions.cs
index 379b4d990..312913f0f 100644
--- a/src/app/dev/DevToys.Api/Core/ObservableCollectionExtensions.cs
+++ b/src/app/dev/DevToys.Api/Core/ObservableCollectionExtensions.cs
@@ -20,50 +20,4 @@ public static class ObservableCollectionExtensions
origin.Add(item);
}
}
-
- // TODO: Write unit tests
- /// <summary>
- /// Update the difference between the current items in the collection and the <paramref name="newItems"/>.
- /// </summary>
- public static void Update<T>(this ObservableCollection<T> origin, IEnumerable<T> newItems)
- {
- Guard.IsNotNull(origin);
- if (newItems is null)
- {
- return;
- }
-
- // First, remove the items that aren't part of the new list items.
- var oldToolsMenuItems = origin.ToList();
- for (int i = 0; i < oldToolsMenuItems.Count; i++)
- {
- T item = oldToolsMenuItems[i];
- if (!newItems.Contains(item))
- {
- origin.Remove(item);
- }
- }
-
- // Then:
- // 1. If an item from newItems already exist in the collection, but at a different position, move it to the desired index.
- // 2. If an item from newItems doesn't exist in the collection, insert it with respect of the position of older items in the collection.
- int insertionIndex = 0;
- foreach (T? item in newItems)
- {
- int indexOfItemInOldMenu = origin.IndexOf(item);
- if (indexOfItemInOldMenu > -1 && insertionIndex < origin.Count)
- {
- if (indexOfItemInOldMenu != insertionIndex)
- {
- origin.Move(indexOfItemInOldMenu, insertionIndex);
- }
- }
- else
- {
- origin.Insert(insertionIndex, item);
- }
-
- insertionIndex++;
- }
- }
}
diff --git a/src/app/dev/DevToys.Blazor/Components/NavBar/NavBar.razor.cs b/src/app/dev/DevToys.Blazor/Components/NavBar/NavBar.razor.cs
index b0d78b738..6b4772597 100644
--- a/src/app/dev/DevToys.Blazor/Components/NavBar/NavBar.razor.cs
+++ b/src/app/dev/DevToys.Blazor/Components/NavBar/NavBar.razor.cs
@@ -111,7 +111,7 @@ public partial class NavBar<TElement> : JSStyledComponentBase where TElement : c
private void OnSearchButtonClick()
{
- _sidebarState.ToggleSidebar();
+ _sidebarState.ForceExpand();
Task.Delay(200)
.ContinueWith(t =>
{
diff --git a/src/app/dev/DevToys.Blazor/Components/NavBar/NavBarState.cs b/src/app/dev/DevToys.Blazor/Components/NavBar/NavBarState.cs
index ae26bb942..89c3b5aad 100644
--- a/src/app/dev/DevToys.Blazor/Components/NavBar/NavBarState.cs
+++ b/src/app/dev/DevToys.Blazor/Components/NavBar/NavBarState.cs
@@ -1,6 +1,5 @@
namespace DevToys.Blazor.Components;
-// TODO: Write unit tests
internal sealed class NavBarState
{
// TODO: Save state in settings and restore it, along with window size and location.
diff --git a/src/app/dev/DevToys.Business/ViewModels/MainWindowViewModel.cs b/src/app/dev/DevToys.Business/ViewModels/MainWindowViewModel.cs
index 2e62f06c8..98010e98a 100644
--- a/src/app/dev/DevToys.Business/ViewModels/MainWindowViewModel.cs
+++ b/src/app/dev/DevToys.Business/ViewModels/MainWindowViewModel.cs
@@ -101,10 +101,7 @@ internal sealed partial class MainWindowViewModel : ObservableRecipient
OnPropertyChanged(nameof(IsSelectedMenuItemATool));
SelectedMenuItemChanged?.Invoke(this, EventArgs.Empty);
- if (guiToolViewItem is not null)
- {
- guiToolViewItem.RaiseGotSelected();
- }
+ guiToolViewItem?.RaiseGotSelected();
}
}
@@ -321,7 +318,7 @@ internal sealed partial class MainWindowViewModel : ObservableRecipient
}
}
- var firstItem = HeaderAndBodyToolViewItems[0];
+ INotifyPropertyChanged? firstItem = HeaderAndBodyToolViewItems[0];
Guard.IsNotNull(firstItem);
return firstItem;
}
@@ -330,10 +327,7 @@ internal sealed partial class MainWindowViewModel : ObservableRecipient
{
// Select the actual menu item in the navigation view. This will trigger the navigation.
SelectedMenuItem = GetBestMenuItemToSelect(message.Value);
- if (message.SmartDetectionInfo is not null)
- {
- message.SmartDetectionInfo.ToolInstance.PassSmartDetectedData(message.SmartDetectionInfo.DataTypeName, message.SmartDetectionInfo.ParsedData);
- }
+ message.SmartDetectionInfo?.ToolInstance.PassSmartDetectedData(message.SmartDetectionInfo.DataTypeName, message.SmartDetectionInfo.ParsedData);
}
[LoggerMessage(1, LogLevel.Warning, "Failed to perform smart detection.")]
diff --git a/src/app/dev/platforms/desktop/DevToys.Windows/MainWindow.xaml.cs b/src/app/dev/platforms/desktop/DevToys.Windows/MainWindow.xaml.cs
index 60d780934..1292e041b 100644
--- a/src/app/dev/platforms/desktop/DevToys.Windows/MainWindow.xaml.cs
+++ b/src/app/dev/platforms/desktop/DevToys.Windows/MainWindow.xaml.cs
@@ -9,6 +9,7 @@ using DevToys.Windows.Core;
using Microsoft.AspNetCore.Components.WebView;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
+using Microsoft.Web.WebView2.Core;
using PredefinedSettings = DevToys.Core.Settings.PredefinedSettings;
namespace DevToys.Windows;
@@ -74,6 +75,7 @@ public partial class MainWindow : MicaWindowWithOverlay
private void BlazorWebView_BlazorWebViewInitialized(object? sender, BlazorWebViewInitializedEventArgs e)
{
+ blazorWebView.WebView.CoreWebView2.Settings.IsStatusBarEnabled = false;
LogUiLoadTime((DateTime.Now - _uiLoadingTime).TotalMilliseconds);
}
diff --git a/src/app/tests/DevToys.UnitTests/Core/ExtensionOrdererTests.cs b/src/app/tests/DevToys.UnitTests/Api/Core/ExtensionOrdererTests.cs
similarity index 82%
rename from src/app/tests/DevToys.UnitTests/Core/ExtensionOrdererTests.cs
rename to src/app/tests/DevToys.UnitTests/Api/Core/ExtensionOrdererTests.cs
index 5a82fc0ff..87962e888 100644
--- a/src/app/tests/DevToys.UnitTests/Core/ExtensionOrdererTests.cs
+++ b/src/app/tests/DevToys.UnitTests/Api/Core/ExtensionOrdererTests.cs
@@ -1,4 +1,4 @@
-namespace DevToys.UnitTests.Core;
+namespace DevToys.UnitTests.Api.Core;
public class ExtensionOrdererTests
{
@@ -14,7 +14,7 @@ public class ExtensionOrdererTests
IEnumerable<Lazy<string, OrderableMetadata>> result = ExtensionOrderer.Order(extensions);
- Assert.Equivalent(extensions, result);
+ result.Should().BeEquivalentTo(extensions);
}
[Fact]
@@ -27,7 +27,8 @@ public class ExtensionOrdererTests
Create("Baz")
};
- Assert.Throws<ArgumentException>(() => ExtensionOrderer.Order(extensions));
+ Action act = () => ExtensionOrderer.Order(extensions);
+ act.Should().Throw<ArgumentException>();
}
[Fact]
@@ -38,7 +39,8 @@ public class ExtensionOrdererTests
Create("Foo").After("Foo"),
};
- Assert.Throws<ArgumentException>(() => ExtensionOrderer.Order(extensions));
+ Action act = () => ExtensionOrderer.Order(extensions);
+ act.Should().Throw<ArgumentException>();
}
[Fact]
@@ -49,7 +51,8 @@ public class ExtensionOrdererTests
Create("Foo").Before("Foo"),
};
- Assert.Throws<ArgumentException>(() => ExtensionOrderer.Order(extensions));
+ Action act = () => ExtensionOrderer.Order(extensions);
+ act.Should().Throw<ArgumentException>();
}
[Fact]
@@ -70,7 +73,7 @@ public class ExtensionOrdererTests
"Foo"
};
- Assert.Equivalent(result.Select(e => e.Metadata.InternalComponentName), expected);
+ result.Select(e => e.Metadata.InternalComponentName).Should().BeEquivalentTo(expected);
}
[Fact]
@@ -83,7 +86,8 @@ public class ExtensionOrdererTests
Create("Baz").After("Foo")
};
- Assert.Throws<ArgumentException>(() => ExtensionOrderer.Order(extensions));
+ Action act = () => ExtensionOrderer.Order(extensions);
+ act.Should().Throw<ArgumentException>();
}
[Fact]
@@ -104,7 +108,7 @@ public class ExtensionOrdererTests
"Foo"
};
- Assert.Equivalent(result.Select(e => e.Metadata.InternalComponentName), expected);
+ result.Select(e => e.Metadata.InternalComponentName).Should().BeEquivalentTo(expected);
}
[Fact]
@@ -125,7 +129,7 @@ public class ExtensionOrdererTests
"Foo"
};
- Assert.Equivalent(result.Select(e => e.Metadata.InternalComponentName), expected);
+ result.Select(e => e.Metadata.InternalComponentName).Should().BeEquivalentTo(expected);
}
#region Helper
diff --git a/src/app/tests/DevToys.UnitTests/Api/Core/ObservableHashSetTests.cs b/src/app/tests/DevToys.UnitTests/Api/Core/ObservableHashSetTests.cs
new file mode 100644
index 000000000..25ff6f8a9
--- /dev/null
+++ b/src/app/tests/DevToys.UnitTests/Api/Core/ObservableHashSetTests.cs
@@ -0,0 +1,524 @@
+using System.Collections.ObjectModel;
+using System.Collections.Specialized;
+using System.ComponentModel;
+
+namespace DevToys.UnitTests.Api.Core;
+
+public class ObservableHashSetTests
+{
+ private static readonly Random _random = new();
+
+ [Fact]
+ public void CanConstruct()
+ {
+ new ObservableHashSet<int>().Comparer.Should().BeSameAs(new HashSet<int>().Comparer);
+
+ new ObservableHashSet<object>(ReferenceEqualityComparer.Instance).Comparer.Should().BeSameAs(ReferenceEqualityComparer.Instance);
+
+ List<int> testData1 = CreateTestData();
+
+ var rh1 = new HashSet<int>(testData1);
+ var ohs1 = new ObservableHashSet<int>(testData1);
+ ohs1.OrderBy(i => i).Should().BeEquivalentTo(rh1.OrderBy(i => i));
+ ohs1.Comparer.Should().BeSameAs(rh1.Comparer);
+
+ IEnumerable<object> testData2 = CreateTestData().Cast<object>();
+
+ var rh2 = new HashSet<object>(testData2, ReferenceEqualityComparer.Instance);
+ var ohs2 = new ObservableHashSet<object>(testData2, ReferenceEqualityComparer.Instance);
+ ohs2.OrderBy(i => i).Should().BeEquivalentTo(rh2.OrderBy(i => i));
+ ohs2.Comparer.Should().BeSameAs(rh2.Comparer);
+ }
+
+ [Fact]
+ public void CanAdd()
+ {
+ var hashSet = new ObservableHashSet<string>();
+ int countChanging = 0;
+ int countChanged = 0;
+ int collectionChanged = 0;
+ int currentCount = 0;
+ int countChange = 1;
+ string[] adding = Array.Empty<string>();
+
+ hashSet.PropertyChanging += (s, a) => AssertCountChanging(hashSet, s, a, currentCount, ref countChanging);
+ hashSet.PropertyChanged += (s, a) => AssertCountChanged(hashSet, s, a, ref currentCount, countChange, ref countChanged);
+ hashSet.CollectionChanged += (s, a) =>
+ {
+ a.Action.Should().Be(NotifyCollectionChangedAction.Add);
+ a.OldItems.Should().BeNull();
+ a.NewItems.OfType<string>().Should().BeEquivalentTo(adding);
+ collectionChanged++;
+ };
+
+ adding = new[] { "Palmer" };
+ hashSet.Add("Palmer").Should().BeTrue();
+
+ countChanging.Should().Be(1);
+ countChanged.Should().Be(1);
+ collectionChanged.Should().Be(1);
+ hashSet.Should().BeEquivalentTo(new[] { "Palmer" });
+
+ adding = new[] { "Carmack" };
+ hashSet.Add("Carmack").Should().BeTrue();
+
+ countChanging.Should().Be(2);
+ countChanged.Should().Be(2);
+ collectionChanged.Should().Be(2);
+ hashSet.OrderBy(i => i).Should().BeEquivalentTo(new[] { "Carmack", "Palmer" });
+
+ hashSet.Add("Palmer").Should().BeFalse();
+
+ countChanging.Should().Be(2);
+ countChanged.Should().Be(2);
+ collectionChanged.Should().Be(2);
+ hashSet.OrderBy(i => i).Should().BeEquivalentTo(new[] { "Carmack", "Palmer" });
+ }
+
+ [Fact]
+ public void CanClear()
+ {
+ var testData = new HashSet<int>(CreateTestData());
+
+ var hashSet = new ObservableHashSet<int>(testData);
+ int countChanging = 0;
+ int countChanged = 0;
+ int collectionChanged = 0;
+ int currentCount = testData.Count;
+ int countChange = -testData.Count;
+
+ hashSet.PropertyChanging += (s, a) => AssertCountChanging(hashSet, s, a, currentCount, ref countChanging);
+ hashSet.PropertyChanged += (s, a) => AssertCountChanged(hashSet, s, a, ref currentCount, countChange, ref countChanged);
+ hashSet.CollectionChanged += (s, a) =>
+ {
+ a.Action.Should().Be(NotifyCollectionChangedAction.Replace);
+ a.OldItems.OfType<int>().OrderBy(i => i).Should().BeEquivalentTo(testData.OrderBy(i => i));
+ a.NewItems.Count.Should().Be(0);
+ collectionChanged++;
+ };
+
+ hashSet.Clear();
+
+ countChanging.Should().Be(testData.Count == 0 ? 0 : 1);
+ countChanged.Should().Be(testData.Count == 0 ? 0 : 1);
+ collectionChanged.Should().Be(testData.Count == 0 ? 0 : 1);
+ hashSet.Should().BeEmpty();
+
+ hashSet.Clear();
+
+ countChanging.Should().Be(testData.Count == 0 ? 0 : 1);
+ countChanged.Should().Be(testData.Count == 0 ? 0 : 1);
+ collectionChanged.Should().Be(testData.Count == 0 ? 0 : 1);
+ hashSet.Should().BeEmpty();
+ }
+
+ [Fact]
+ public void ContainsWorks()
+ {
+ List<int> testData = CreateTestData();
+ var hashSet = new ObservableHashSet<int>(testData);
+
+ foreach (int item in testData)
+ {
+ hashSet.Should().Contain(item);
+ }
+
+ foreach (int item in CreateTestData(1000, 10000).Except(testData))
+ {
+ hashSet.Should().NotContain(item);
+ }
+ }
+
+ [Fact]
+ public void CanCopyToArray()
+ {
+ List<int> testData = CreateTestData();
+ var orderedDistinct = testData.Distinct().OrderBy(i => i).ToList();
+
+ var hashSet = new ObservableHashSet<int>(testData);
+
+ hashSet.Count.Should().Be(orderedDistinct.Count);
+
+ int[] array = new int[hashSet.Count];
+ hashSet.CopyTo(array);
+
+ array.OrderBy(i => i).Should().BeEquivalentTo(orderedDistinct);
+
+ array = new int[hashSet.Count + 100];
+ hashSet.CopyTo(array, 100);
+
+ array.Skip(100).OrderBy(i => i).Should().BeEquivalentTo(orderedDistinct);
+
+ int toTake = Math.Min(10, hashSet.Count);
+ array = new int[100 + toTake];
+ hashSet.CopyTo(array, 100, toTake);
+
+ foreach (int value in array.Skip(100).Take(toTake))
+ {
+ hashSet.Should().Contain(value);
+ }
+ }
+
+ [Fact]
+ public void CanRemove()
+ {
+ var hashSet = new ObservableHashSet<string> { "Palmer", "Carmack" };
+ int countChanging = 0;
+ int countChanged = 0;
+ int collectionChanged = 0;
+ int currentCount = 2;
+ int countChange = -1;
+ string[] removing = Array.Empty<string>();
+
+ hashSet.PropertyChanging += (s, a) => AssertCountChanging(hashSet, s, a, currentCount, ref countChanging);
+ hashSet.PropertyChanged += (s, a) => AssertCountChanged(hashSet, s, a, ref currentCount, countChange, ref countChanged);
+ hashSet.CollectionChanged += (s, a) =>
+ {
+ a.Action.Should().Be(NotifyCollectionChangedAction.Remove);
+ a.OldItems.OfType<string>().Should().BeEquivalentTo(removing);
+ a.NewItems.Should().BeNull();
+ collectionChanged++;
+ };
+
+ removing = new[] { "Palmer" };
+ hashSet.Remove("Palmer").Should().BeTrue();
+
+ countChanging.Should().Be(1);
+ countChanged.Should().Be(1);
+ collectionChanged.Should().Be(1);
+ hashSet.Should().BeEquivalentTo(new[] { "Carmack" });
+
+ removing = new[] { "Carmack" };
+ hashSet.Remove("Carmack").Should().BeTrue();
+
+ countChanging.Should().Be(2);
+ countChanged.Should().Be(2);
+ collectionChanged.Should().Be(2);
+ hashSet.Should().BeEmpty();
+
+ hashSet.Remove("Palmer").Should().BeFalse();
+
+ countChanging.Should().Be(2);
+ countChanged.Should().Be(2);
+ collectionChanged.Should().Be(2);
+ hashSet.Should().BeEmpty();
+ }
+
+ [Fact]
+ public void NotReadOnly()
+ => Assert.False(new ObservableHashSet<Random>().IsReadOnly);
+
+ [Fact]
+ public void CanUnionWith()
+ {
+ var hashSet = new ObservableHashSet<string> { "Palmer", "Carmack" };
+ int countChanging = 0;
+ int countChanged = 0;
+ int collectionChanged = 0;
+ int currentCount = 2;
+ int countChange = 2;
+ string[] adding = new[] { "Brendan", "Nate" };
+
+ hashSet.PropertyChanging += (s, a) => AssertCountChanging(hashSet, s, a, currentCount, ref countChanging);
+ hashSet.PropertyChanged += (s, a) => AssertCountChanged(hashSet, s, a, ref currentCount, countChange, ref countChanged);
+ hashSet.CollectionChanged += (s, a) =>
+ {
+ a.Action.Should().Be(NotifyCollectionChangedAction.Replace);
+ a.OldItems.Count.Should().Be(0);
+ a.NewItems.OfType<string>().OrderBy(i => i).Should().BeEquivalentTo(adding);
+ collectionChanged++;
+ };
+
+ hashSet.UnionWith(new[] { "Carmack", "Nate", "Brendan" });
+
+ countChanging.Should().Be(1);
+ countChanged.Should().Be(1);
+ collectionChanged.Should().Be(1);
+ hashSet.OrderBy(i => i).Should().BeEquivalentTo(new[] { "Brendan", "Carmack", "Nate", "Palmer" });
+
+ hashSet.UnionWith(new[] { "Brendan" });
+
+ countChanging.Should().Be(1);
+ countChanged.Should().Be(1);
+ collectionChanged.Should().Be(1);
+ hashSet.OrderBy(i => i).Should().BeEquivalentTo(new[] { "Brendan", "Carmack", "Nate", "Palmer" });
+ }
+
+ [Fact]
+ public void CanIntersectWith()
+ {
+ var hashSet = new ObservableHashSet<string>
+ {
+ "Brendan",
+ "Carmack",
+ "Nate",
+ "Palmer"
+ };
+ int countChanging = 0;
+ int countChanged = 0;
+ int collectionChanged = 0;
+ int currentCount = 4;
+ int countChange = -2;
+ string[] removing = new[] { "Brendan", "Nate" };
+
+ hashSet.PropertyChanging += (s, a) => AssertCountChanging(hashSet, s, a, currentCount, ref countChanging);
+ hashSet.PropertyChanged += (s, a) => AssertCountChanged(hashSet, s, a, ref currentCount, countChange, ref countChanged);
+ hashSet.CollectionChanged += (s, a) =>
+ {
+ a.Action.Should().Be(NotifyCollectionChangedAction.Replace);
+ a.OldItems.OfType<string>().OrderBy(i => i).Should().BeEquivalentTo(removing);
+ a.NewItems.Count.Should().Be(0);
+ collectionChanged++;
+ };
+
+ hashSet.IntersectWith(new[] { "Carmack", "Palmer", "Abrash" });
+
+ countChanging.Should().Be(1);
+ countChanged.Should().Be(1);
+ collectionChanged.Should().Be(1);
+ hashSet.OrderBy(i => i).Should().BeEquivalentTo(new[] { "Carmack", "Palmer" });
+
+ hashSet.IntersectWith(new[] { "Carmack", "Palmer", "Abrash" });
+
+ countChanging.Should().Be(1);
+ countChanged.Should().Be(1);
+ collectionChanged.Should().Be(1);
+ hashSet.OrderBy(i => i).Should().BeEquivalentTo(new[] { "Carmack", "Palmer" });
+ }
+
+ [Fact]
+ public void CanExceptWith()
+ {
+ var hashSet = new ObservableHashSet<string>
+ {
+ "Brendan",
+ "Carmack",
+ "Nate",
+ "Palmer"
+ };
+ int countChanging = 0;
+ int countChanged = 0;
+ int collectionChanged = 0;
+ int currentCount = 4;
+ int countChange = -2;
+ string[] removing = new[] { "Carmack", "Palmer" };
+
+ hashSet.PropertyChanging += (s, a) => AssertCountChanging(hashSet, s, a, currentCount, ref countChanging);
+ hashSet.PropertyChanged += (s, a) => AssertCountChanged(hashSet, s, a, ref currentCount, countChange, ref countChanged);
+ hashSet.CollectionChanged += (s, a) =>
+ {
+ a.Action.Should().Be(NotifyCollectionChangedAction.Replace);
+ a.OldItems.OfType<string>().OrderBy(i => i).Should().BeEquivalentTo(removing);
+ a.NewItems.Count.Should().Be(0);
+ collectionChanged++;
+ };
+
+ hashSet.ExceptWith(new[] { "Carmack", "Palmer", "Abrash" });
+
+ countChanging.Should().Be(1);
+ countChanged.Should().Be(1);
+ collectionChanged.Should().Be(1);
+ hashSet.OrderBy(i => i).Should().BeEquivalentTo(new[] { "Brendan", "Nate" });
+
+ hashSet.ExceptWith(new[] { "Abrash", "Carmack", "Palmer" });
+
+ countChanging.Should().Be(1);
+ countChanged.Should().Be(1);
+ collectionChanged.Should().Be(1);
+ hashSet.OrderBy(i => i).Should().BeEquivalentTo(new[] { "Brendan", "Nate" });
+ }
+
+ [Fact]
+ public void CanSymmetricalExceptWith()
+ {
+ var hashSet = new ObservableHashSet<string>
+ {
+ "Brendan",
+ "Carmack",
+ "Nate",
+ "Palmer"
+ };
+ int countChanging = 0;
+ int countChanged = 0;
+ int collectionChanged = 0;
+ int currentCount = 4;
+ int countChange = -1;
+ string[] removing = new[] { "Carmack", "Palmer" };
+ string[] adding = new[] { "Abrash" };
+
+ hashSet.PropertyChanging += (s, a) => AssertCountChanging(hashSet, s, a, currentCount, ref countChanging);
+ hashSet.PropertyChanged += (s, a) => AssertCountChanged(hashSet, s, a, ref currentCount, countChange, ref countChanged);
+ hashSet.CollectionChanged += (s, a) =>
+ {
+ a.Action.Should().Be(NotifyCollectionChangedAction.Replace);
+ a.OldItems.OfType<string>().OrderBy(i => i).Should().BeEquivalentTo(removing);
+ a.NewItems.OfType<string>().OrderBy(i => i).Should().BeEquivalentTo(adding);
+ collectionChanged++;
+ };
+
+ hashSet.SymmetricExceptWith(new[] { "Carmack", "Palmer", "Abrash" });
+
+ countChanging.Should().Be(1);
+ countChanged.Should().Be(1);
+ collectionChanged.Should().Be(1);
+ hashSet.OrderBy(i => i).Should().BeEquivalentTo(new[] { "Abrash", "Brendan", "Nate" });
+
+ hashSet.SymmetricExceptWith(Array.Empty<string>());
+
+ countChanging.Should().Be(1);
+ countChanged.Should().Be(1);
+ collectionChanged.Should().Be(1);
+ hashSet.OrderBy(i => i).Should().BeEquivalentTo(new[] { "Abrash", "Brendan", "Nate" });
+ }
+
+ [Fact]
+ public void IsSubsetOfWorksLikeNormalHashSet()
+ {
+ List<int> bigData = CreateTestData();
+ List<int> smallData = CreateTestData(10);
+
+ new ObservableHashSet<int>(smallData).IsSubsetOf(bigData).Should().Be(new HashSet<int>(smallData).IsSubsetOf(bigData));
+ }
+
+ [Fact]
+ public void IsProperSubsetOfWorksLikeNormalHashSet()
+ {
+ List<int> bigData = CreateTestData();
+ List<int> smallData = CreateTestData(10);
+
+ new ObservableHashSet<int>(smallData).IsProperSubsetOf(bigData).Should().Be(new HashSet<int>(smallData).IsProperSubsetOf(bigData));
+ }
+
+ [Fact]
+ public void IsSupersetOfWorksLikeNormalHashSet()
+ {
+ List<int> bigData = CreateTestData();
+ List<int> smallData = CreateTestData(10);
+
+ new ObservableHashSet<int>(bigData).IsSupersetOf(smallData).Should().Be(new HashSet<int>(bigData).IsSupersetOf(smallData));
+ }
+
+ [Fact]
+ public void IsProperSupersetOfWorksLikeNormalHashSet()
+ {
+ List<int> bigData = CreateTestData();
+ List<int> smallData = CreateTestData(10);
+
+ new ObservableHashSet<int>(bigData).IsProperSupersetOf(smallData).Should().Be(new HashSet<int>(bigData).IsProperSupersetOf(smallData));
+ }
+
+ [Fact]
+ public void OverlapsWorksLikeNormalHashSet()
+ {
+ List<int> bigData = CreateTestData();
+ List<int> smallData = CreateTestData(10);
+
+ new ObservableHashSet<int>(bigData).Overlaps(smallData).Should().Be(new HashSet<int>(bigData).Overlaps(smallData));
+ }
+
+ [Fact]
+ public void SetEqualsWorksLikeNormalHashSet()
+ {
+ List<int> data1 = CreateTestData(5);
+ List<int> data2 = CreateTestData(5);
+
+ new ObservableHashSet<int>(data1).SetEquals(data2).Should().Be(new HashSet<int>(data1).SetEquals(data2));
+ }
+
+ [Fact]
+ public void TrimExcessDoesntThrow()
+ {
+ List<int> bigData = CreateTestData();
+ List<int> smallData = CreateTestData(10);
+
+ var hashSet = new ObservableHashSet<int>(bigData.Concat(smallData));
+ foreach (int item in bigData)
+ {
+ hashSet.Remove(item);
+ }
+
+ hashSet.Invoking(h => h.TrimExcess()).Should().NotThrow();
+ }
+
+ [Fact]
+ public void CanRemoveWithPredicate()
+ {
+ var hashSet = new ObservableHashSet<string>
+ {
+ "Brendan",
+ "Carmack",
+ "Nate",
+ "Palmer"
+ };
+ int countChanging = 0;
+ int countChanged = 0;
+ int collectionChanged = 0;
+ int currentCount = 4;
+ int countChange = -2;
+ string[] removing = new[] { "Carmack", "Palmer" };
+
+ hashSet.PropertyChanging += (s, a) => AssertCountChanging(hashSet, s, a, currentCount, ref countChanging);
+ hashSet.PropertyChanged += (s, a) => AssertCountChanged(hashSet, s, a, ref currentCount, countChange, ref countChanged);
+ hashSet.CollectionChanged += (s, a) =>
+ {
+ a.Action.Should().Be(NotifyCollectionChangedAction.Replace);
+ a.OldItems.OfType<string>().OrderBy(i => i).Should().BeEquivalentTo(removing);
+ a.NewItems.Count.Should().Be(0);
+ collectionChanged++;
+ };
+
+ hashSet.RemoveWhere(i => i.Contains('m')).Should().Be(2);
+
+ countChanging.Should().Be(1);
+ countChanged.Should().Be(1);
+ collectionChanged.Should().Be(1);
+ hashSet.OrderBy(i => i).Should().BeEquivalentTo(new[] { "Brendan", "Nate" });
+
+ hashSet.RemoveWhere(i => i.Contains('m')).Should().Be(0);
+
+ countChanging.Should().Be(1);
+ countChanged.Should().Be(1);
+ collectionChanged.Should().Be(1);
+ hashSet.OrderBy(i => i).Should().BeEquivalentTo(new[] { "Brendan", "Nate" });
+ }
+
+ private static void AssertCountChanging<T>(
+ ObservableHashSet<T> hashSet,
+ object sender,
+ PropertyChangingEventArgs eventArgs,
+ int expectedCount,
+ ref int changingCount)
+ {
+ sender.Should().Be(hashSet);
+ eventArgs.PropertyName.Should().Be("Count");
+ hashSet.Count.Should().Be(expectedCount);
+ changingCount++;
+ }
+
+ private static void AssertCountChanged<T>(
+ ObservableHashSet<T> hashSet,
+ object sender,
+ PropertyChangedEventArgs eventArgs,
+ ref int expectedCount,
+ int countDelta,
+ ref int changedCount)
+ {
+ sender.Should().Be(hashSet);
+ eventArgs.PropertyName.Should().Be("Count");
+ hashSet.Count.Should().Be(expectedCount + countDelta);
+ changedCount++;
+ expectedCount += countDelta;
+ }
+
+ private static List<int> CreateTestData(int minSize = 0, int maxLength = 1000)
+ {
+ int length = _random.Next(minSize, maxLength);
+ var data = new List<int>();
+ for (int i = 0; i < length; i++)
+ {
+ data.Add(_random.Next(int.MinValue, int.MaxValue));
+ }
+
+ return data;
+ }
+}
diff --git a/src/app/tests/DevToys.UnitTests/Core/Threading/AsyncLazyTests.cs b/src/app/tests/DevToys.UnitTests/Api/Core/Threading/AsyncLazyTests.cs
similarity index 75%
rename from src/app/tests/DevToys.UnitTests/Core/Threading/AsyncLazyTests.cs
rename to src/app/tests/DevToys.UnitTests/Api/Core/Threading/AsyncLazyTests.cs
index ff8bda666..45fb734b8 100644
--- a/src/app/tests/DevToys.UnitTests/Core/Threading/AsyncLazyTests.cs
+++ b/src/app/tests/DevToys.UnitTests/Api/Core/Threading/AsyncLazyTests.cs
@@ -1,6 +1,6 @@
using System.Threading.Tasks;
-namespace DevToys.UnitTests.Core.Threading;
+namespace DevToys.UnitTests.Api.Core.Threading;
public class AsyncLazyTests
{
@@ -15,7 +15,7 @@ public class AsyncLazyTests
});
TestClass actual = await lazy.GetValueAsync();
- Assert.Same(expected, actual);
+ actual.Should().BeSameAs(expected);
}
[Fact]
@@ -28,19 +28,20 @@ public class AsyncLazyTests
return new TestClass(5);
});
- Assert.False(lazy.IsValueCreated);
+ lazy.IsValueCreated.Should().BeFalse();
Task<TestClass> resultTask = lazy.GetValueAsync();
- Assert.False(lazy.IsValueCreated);
+ lazy.IsValueCreated.Should().BeFalse();
tcs.SetResult(true);
await resultTask;
- Assert.True(lazy.IsValueCreated);
- Assert.Equal(5, (await resultTask).Value);
+ lazy.IsValueCreated.Should().BeTrue();
+ (await resultTask).Value.Should().Be(5);
}
[Fact]
public void BadArguments()
{
- Assert.Throws<ArgumentNullException>(() => new AsyncLazy<object>(null));
+ Action act = () => new AsyncLazy<object>(null);
+ act.Should().Throw<ArgumentNullException>();
}
public class TestClass
@@ -55,9 +56,7 @@ public class AsyncLazyTests
public override bool Equals(object obj)
{
if (obj is TestClass other)
- {
return Value == other.Value;
- }
return false;
}
diff --git a/src/app/tests/DevToys.UnitTests/Api/Core/Threading/DisposableSemaphoreTests.cs b/src/app/tests/DevToys.UnitTests/Api/Core/Threading/DisposableSemaphoreTests.cs
new file mode 100644
index 000000000..bf8ee1eb6
--- /dev/null
+++ b/src/app/tests/DevToys.UnitTests/Api/Core/Threading/DisposableSemaphoreTests.cs
@@ -0,0 +1,47 @@
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace DevToys.UnitTests.Api.Core.Threading;
+
+public class DisposableSemaphoreTests
+{
+ [Fact]
+ public async Task DisposingTest()
+ {
+ var semaphore = new DisposableSemaphore(1);
+
+ using (await semaphore.WaitAsync(CancellationToken.None))
+ {
+ semaphore.IsBusy.Should().BeTrue();
+ }
+
+ semaphore.IsBusy.Should().BeFalse();
+ }
+
+ [Fact]
+ public async Task ThreadingTest()
+ {
+ var semaphore = new DisposableSemaphore(1);
+ int shared = 0;
+
+ async Task TestFunc(int val)
+ {
+ using (await semaphore.WaitAsync(CancellationToken.None))
+ {
+ shared = val;
+ }
+ }
+
+ var t1 = Task.Run(() => TestFunc(3));
+ await t1;
+ shared.Should().Be(3);
+
+ var t2 = Task.Run(() => TestFunc(5));
+ await t2;
+ shared.Should().Be(5);
+
+ var t3 = Task.Run(() => TestFunc(3));
+ await t3;
+ shared.Should().Be(3);
+ }
+}
diff --git a/src/app/tests/DevToys.UnitTests/Blazor/Components/NavBar/NavBarStateTests.cs b/src/app/tests/DevToys.UnitTests/Blazor/Components/NavBar/NavBarStateTests.cs
new file mode 100644
index 000000000..1710aaac7
--- /dev/null
+++ b/src/app/tests/DevToys.UnitTests/Blazor/Components/NavBar/NavBarStateTests.cs
@@ -0,0 +1,284 @@
+using System.ComponentModel;
+using DevToys.Blazor.Components;
+
+namespace DevToys.UnitTests.Blazor.Components.NavBar;
+
+public class NavBarStateTests
+{
+ private const int NavBarWidthSidebarHiddenThreshold = 640;
+ private const int NavBarWidthSidebarCollapseThreshold = 1008;
+ private readonly NavBarState _state = new();
+ private int _isHiddenChangedInvokeCount;
+
+ public NavBarStateTests()
+ {
+ _state.IsHiddenChanged += NavBarState_IsHiddenChanged;
+ }
+
+ [Fact]
+ [Description("By default, the nav bar is expanded.")]
+ public void Default()
+ {
+ _state.IsExpandedOverlay.Should().BeFalse();
+ _state.IsHidden.Should().BeFalse();
+ _state.IsCollapsed.Should().BeFalse();
+ _isHiddenChangedInvokeCount.Should().Be(0);
+ }
+
+ [Fact]
+ [Description("When the nav bar is hidden, making the window wider will turn the nav bar into collapsed mode.")]
+ public void FromHiddenToCollapsed()
+ {
+ _state.WidthUpdated(NavBarWidthSidebarHiddenThreshold - 1, NavBarWidthSidebarHiddenThreshold, NavBarWidthSidebarCollapseThreshold);
+ _isHiddenChangedInvokeCount.Should().Be(1);
+ _state.IsExpandedOverlay.Should().BeFalse();
+ _state.IsHidden.Should().BeTrue();
+ _state.IsCollapsed.Should().BeTrue();
+
+ _state.WidthUpdated(NavBarWidthSidebarHiddenThreshold, NavBarWidthSidebarHiddenThreshold, NavBarWidthSidebarCollapseThreshold);
+ _isHiddenChangedInvokeCount.Should().Be(2);
+ _state.IsExpandedOverlay.Should().BeFalse();
+ _state.IsHidden.Should().BeTrue();
+ _state.IsCollapsed.Should().BeTrue();
+
+ _state.WidthUpdated(NavBarWidthSidebarHiddenThreshold + 1, NavBarWidthSidebarHiddenThreshold, NavBarWidthSidebarCollapseThreshold);
+ _isHiddenChangedInvokeCount.Should().Be(3);
+ _state.IsExpandedOverlay.Should().BeFalse();
+ _state.IsHidden.Should().BeFalse();
+ _state.IsCollapsed.Should().BeTrue();
+ }
+
+ [Fact]
+ [Description("When the nav bar is collapsed, making the window smaller will turn the nav bar into hidden mode.")]
+ public void FromCollapsedToHidden()
+ {
+ _state.WidthUpdated(NavBarWidthSidebarHiddenThreshold + 1, NavBarWidthSidebarHiddenThreshold, NavBarWidthSidebarCollapseThreshold);
+ _isHiddenChangedInvokeCount.Should().Be(1);
+ _state.IsExpandedOverlay.Should().BeFalse();
+ _state.IsHidden.Should().BeFalse();
+ _state.IsCollapsed.Should().BeTrue();
+
+ _state.WidthUpdated(NavBarWidthSidebarHiddenThreshold, NavBarWidthSidebarHiddenThreshold, NavBarWidthSidebarCollapseThreshold);
+ _isHiddenChangedInvokeCount.Should().Be(2);
+ _state.IsExpandedOverlay.Should().BeFalse();
+ _state.IsHidden.Should().BeTrue();
+ _state.IsCollapsed.Should().BeTrue();
+
+ _state.WidthUpdated(NavBarWidthSidebarHiddenThreshold - 1, NavBarWidthSidebarHiddenThreshold, NavBarWidthSidebarCollapseThreshold);
+ _isHiddenChangedInvokeCount.Should().Be(3);
+ _state.IsExpandedOverlay.Should().BeFalse();
+ _state.IsHidden.Should().BeTrue();
+ _state.IsCollapsed.Should().BeTrue();
+ }
+
+ [Fact]
+ [Description("When the nav bar is collapsed, making the window wider will turn the nav bar into expanded mode.")]
+ public void FromCollapsedToExpanded()
+ {
+ _state.WidthUpdated(NavBarWidthSidebarCollapseThreshold - 1, NavBarWidthSidebarHiddenThreshold, NavBarWidthSidebarCollapseThreshold);
+ _isHiddenChangedInvokeCount.Should().Be(1);
+ _state.IsExpandedOverlay.Should().BeFalse();
+ _state.IsHidden.Should().BeFalse();
+ _state.IsCollapsed.Should().BeTrue();
+
+ _state.WidthUpdated(NavBarWidthSidebarCollapseThreshold, NavBarWidthSidebarHiddenThreshold, NavBarWidthSidebarCollapseThreshold);
+ _isHiddenChangedInvokeCount.Should().Be(2);
+ _state.IsExpandedOverlay.Should().BeFalse();
+ _state.IsHidden.Should().BeFalse();
+ _state.IsCollapsed.Should().BeTrue();
+
+ _state.WidthUpdated(NavBarWidthSidebarCollapseThreshold + 1, NavBarWidthSidebarHiddenThreshold, NavBarWidthSidebarCollapseThreshold);
+ _isHiddenChangedInvokeCount.Should().Be(3);
+ _state.IsExpandedOverlay.Should().BeFalse();
+ _state.IsHidden.Should().BeFalse();
+ _state.IsCollapsed.Should().BeFalse();
+ }
+
+ [Fact]
+ [Description("When the nav bar is expanded, making the window smaller will turn the nav bar into collapsed mode.")]
+ public void FromExpandedToCollapsed()
+ {
+ _state.WidthUpdated(NavBarWidthSidebarCollapseThreshold + 1, NavBarWidthSidebarHiddenThreshold, NavBarWidthSidebarCollapseThreshold);
+ _isHiddenChangedInvokeCount.Should().Be(1);
+ _state.IsExpandedOverlay.Should().BeFalse();
+ _state.IsHidden.Should().BeFalse();
+ _state.IsCollapsed.Should().BeFalse();
+
+ _state.WidthUpdated(NavBarWidthSidebarCollapseThreshold, NavBarWidthSidebarHiddenThreshold, NavBarWidthSidebarCollapseThreshold);
+ _isHiddenChangedInvokeCount.Should().Be(2);
+ _state.IsExpandedOverlay.Should().BeFalse();
+ _state.IsHidden.Should().BeFalse();
+ _state.IsCollapsed.Should().BeTrue();
+
+ _state.WidthUpdated(NavBarWidthSidebarCollapseThreshold - 1, NavBarWidthSidebarHiddenThreshold, NavBarWidthSidebarCollapseThreshold);
+ _isHiddenChangedInvokeCount.Should().Be(3);
+ _state.IsExpandedOverlay.Should().BeFalse();
+ _state.IsHidden.Should().BeFalse();
+ _state.IsCollapsed.Should().BeTrue();
+ }
+
+ [Fact]
+ [Description("When the nav bar is expanded, clicking the hamburger menu will collapse the nav bar. Clicking it again will expand it.")]
+ public void FromExpandedToCollapsedExplicitly()
+ {
+ _state.WidthUpdated(NavBarWidthSidebarCollapseThreshold + 1, NavBarWidthSidebarHiddenThreshold, NavBarWidthSidebarCollapseThreshold);
+ _isHiddenChangedInvokeCount.Should().Be(1);
+ _state.IsExpandedOverlay.Should().BeFalse();
+ _state.IsHidden.Should().BeFalse();
+ _state.IsCollapsed.Should().BeFalse();
+
+ _state.ToggleSidebar();
+ _isHiddenChangedInvokeCount.Should().Be(2);
+ _state.IsExpandedOverlay.Should().BeFalse();
+ _state.IsHidden.Should().BeFalse();
+ _state.IsCollapsed.Should().BeTrue();
+
+ _state.ToggleSidebar();
+ _isHiddenChangedInvokeCount.Should().Be(3);
+ _state.IsExpandedOverlay.Should().BeFalse();
+ _state.IsHidden.Should().BeFalse();
+ _state.IsCollapsed.Should().BeFalse();
+ }
+
+ [Fact]
+ [Description("When the nav bar is expanded, clicking the hamburger menu will collapse the nav bar. Making the window smaller or larger will keep the nav bar in hidden or collapsed mode")]
+ public void FromExpandedToCollapsedExplicitlyResizeKeepItCollapsed()
+ {
+ _state.WidthUpdated(NavBarWidthSidebarCollapseThreshold + 1, NavBarWidthSidebarHiddenThreshold, NavBarWidthSidebarCollapseThreshold);
+ _state.ToggleSidebar();
+ _isHiddenChangedInvokeCount.Should().Be(2);
+ _state.IsExpandedOverlay.Should().BeFalse();
+ _state.IsHidden.Should().BeFalse();
+ _state.IsCollapsed.Should().BeTrue();
+
+ _state.WidthUpdated(NavBarWidthSidebarCollapseThreshold - 1, NavBarWidthSidebarHiddenThreshold, NavBarWidthSidebarCollapseThreshold);
+ _state.WidthUpdated(NavBarWidthSidebarCollapseThreshold + 1, NavBarWidthSidebarHiddenThreshold, NavBarWidthSidebarCollapseThreshold);
+ _isHiddenChangedInvokeCount.Should().Be(4);
+ _state.IsExpandedOverlay.Should().BeFalse();
+ _state.IsHidden.Should().BeFalse();
+ _state.IsCollapsed.Should().BeTrue();
+ }
+
+ [Fact]
+ [Description("When the nav bar is collapsed, clicking the hamburger menu will open the expanded overlay experience.")]
+ public void FromCollapsedToExpandedOverlay()
+ {
+ _state.WidthUpdated(NavBarWidthSidebarCollapseThreshold - 1, NavBarWidthSidebarHiddenThreshold, NavBarWidthSidebarCollapseThreshold);
+ _isHiddenChangedInvokeCount.Should().Be(1);
+ _state.IsExpandedOverlay.Should().BeFalse();
+ _state.IsHidden.Should().BeFalse();
+ _state.IsCollapsed.Should().BeTrue();
+
+ _state.ToggleSidebar();
+ _isHiddenChangedInvokeCount.Should().Be(2);
+ _state.IsExpandedOverlay.Should().BeTrue();
+ _state.IsHidden.Should().BeFalse();
+ _state.IsCollapsed.Should().BeFalse();
+
+ _state.ToggleSidebar();
+ _isHiddenChangedInvokeCount.Should().Be(3);
+ _state.IsExpandedOverlay.Should().BeFalse();
+ _state.IsHidden.Should().BeFalse();
+ _state.IsCollapsed.Should().BeTrue();
+ }
+
+ [Fact]
+ [Description("When the nav bar has the expanded overlay opened, resizing the window will close the overlay and switch back to collapsed mode. Making the window larger will switch back to regular expanded mode.")]
+ public void ExpandedOverlayClosesBackToCollapsedWhenResizingWindow()
+ {
+ _state.WidthUpdated(NavBarWidthSidebarCollapseThreshold - 1, NavBarWidthSidebarHiddenThreshold, NavBarWidthSidebarCollapseThreshold);
+ _state.ToggleSidebar();
+ _isHiddenChangedInvokeCount.Should().Be(2);
+ _state.IsExpandedOverlay.Should().BeTrue();
+ _state.IsHidden.Should().BeFalse();
+ _state.IsCollapsed.Should().BeFalse();
+
+ _state.WidthUpdated(NavBarWidthSidebarCollapseThreshold - 2, NavBarWidthSidebarHiddenThreshold, NavBarWidthSidebarCollapseThreshold);
+ _isHiddenChangedInvokeCount.Should().Be(3);
+ _state.IsExpandedOverlay.Should().BeFalse();
+ _state.IsHidden.Should().BeFalse();
+ _state.IsCollapsed.Should().BeTrue();
+
+ _state.WidthUpdated(NavBarWidthSidebarCollapseThreshold + 1, NavBarWidthSidebarHiddenThreshold, NavBarWidthSidebarCollapseThreshold);
+ _isHiddenChangedInvokeCount.Should().Be(4);
+ _state.IsExpandedOverlay.Should().BeFalse();
+ _state.IsHidden.Should().BeFalse();
+ _state.IsCollapsed.Should().BeFalse();
+ }
+
+ [Fact]
+ [Description("When the nav bar has the expanded overlay opened, resizing the window will close the overlay and switch back to hidden mode. Making the window larger will switch back to regular collapsed mode.")]
+ public void ExpandedOverlayClosesBackToHiddenWhenResizingWindow()
+ {
+ _state.WidthUpdated(NavBarWidthSidebarHiddenThreshold - 1, NavBarWidthSidebarHiddenThreshold, NavBarWidthSidebarCollapseThreshold);
+ _state.ToggleSidebar();
+ _isHiddenChangedInvokeCount.Should().Be(2);
+ _state.IsExpandedOverlay.Should().BeTrue();
+ _state.IsHidden.Should().BeTrue();
+ _state.IsCollapsed.Should().BeFalse();
+
+ _state.WidthUpdated(NavBarWidthSidebarHiddenThreshold - 2, NavBarWidthSidebarHiddenThreshold, NavBarWidthSidebarCollapseThreshold);
+ _isHiddenChangedInvokeCount.Should().Be(3);
+ _state.IsExpandedOverlay.Should().BeFalse();
+ _state.IsHidden.Should().BeTrue();
+ _state.IsCollapsed.Should().BeTrue();
+
+ _state.WidthUpdated(NavBarWidthSidebarHiddenThreshold + 1, NavBarWidthSidebarHiddenThreshold, NavBarWidthSidebarCollapseThreshold);
+ _isHiddenChangedInvokeCount.Should().Be(4);
+ _state.IsExpandedOverlay.Should().BeFalse();
+ _state.IsHidden.Should().BeFalse();
+ _state.IsCollapsed.Should().BeTrue();
+ }
+
+ [Fact]
+ [Description("When the nav bar has the expanded overlay opened, clicking the hamburger menu again is equivalent to collapsing the nav bar. Making the window larger won't make it switch to expanded mode..")]
+ public void ExpandedOverlayForceClosedCauseNotToExpandWhenEnlargingWindow()
+ {
+ Default();
+ _state.WidthUpdated(NavBarWidthSidebarCollapseThreshold - 1, NavBarWidthSidebarHiddenThreshold, NavBarWidthSidebarCollapseThreshold);
+ _state.ToggleSidebar();
+ _isHiddenChangedInvokeCount.Should().Be(2);
+ _state.IsExpandedOverlay.Should().BeTrue();
+ _state.IsHidden.Should().BeFalse();
+ _state.IsCollapsed.Should().BeFalse();
+
+ _state.ToggleSidebar();
+ _state.IsExpandedOverlay.Should().BeFalse();
+ _state.IsHidden.Should().BeFalse();
+ _state.IsCollapsed.Should().BeTrue();
+
+ _state.WidthUpdated(NavBarWidthSidebarCollapseThreshold + 1, NavBarWidthSidebarHiddenThreshold, NavBarWidthSidebarCollapseThreshold);
+ _isHiddenChangedInvokeCount.Should().Be(4);
+ _state.IsExpandedOverlay.Should().BeFalse();
+ _state.IsHidden.Should().BeFalse();
+ _state.IsCollapsed.Should().BeTrue();
+ }
+
+ [Fact]
+ [Description("When the nav bar has the expanded overlay opened, clicking outside of the overlay area will close the overlay. Making the window larger will switch back to regular expanded mode.")]
+ public void ExpandedOverlayClickOutsideOfNavBar()
+ {
+ _state.WidthUpdated(NavBarWidthSidebarCollapseThreshold - 1, NavBarWidthSidebarHiddenThreshold, NavBarWidthSidebarCollapseThreshold);
+ _state.ToggleSidebar();
+ _isHiddenChangedInvokeCount.Should().Be(2);
+ _state.IsExpandedOverlay.Should().BeTrue();
+ _state.IsHidden.Should().BeFalse();
+ _state.IsCollapsed.Should().BeFalse();
+
+ _state.CloseExpandedOverlay();
+ _state.IsExpandedOverlay.Should().BeFalse();
+ _state.IsHidden.Should().BeFalse();
+ _state.IsCollapsed.Should().BeTrue();
+
+ _state.WidthUpdated(NavBarWidthSidebarCollapseThreshold + 1, NavBarWidthSidebarHiddenThreshold, NavBarWidthSidebarCollapseThreshold);
+ _isHiddenChangedInvokeCount.Should().Be(4);
+ _state.IsExpandedOverlay.Should().BeFalse();
+ _state.IsHidden.Should().BeFalse();
+ _state.IsCollapsed.Should().BeFalse();
+ }
+
+ private void NavBarState_IsHiddenChanged(object sender, EventArgs e)
+ {
+ _isHiddenChangedInvokeCount++;
+ }
+}
diff --git a/src/app/tests/DevToys.UnitTests/Blazor/Helpers/CssBuilderTests.cs b/src/app/tests/DevToys.UnitTests/Blazor/Helpers/CssBuilderTests.cs
new file mode 100644
index 000000000..6becdb0cb
--- /dev/null
+++ b/src/app/tests/DevToys.UnitTests/Blazor/Helpers/CssBuilderTests.cs
@@ -0,0 +1,166 @@
+using DevToys.Blazor.Core.Helpers;
+
+namespace DevToys.UnitTests.Blazor.Helpers;
+
+public class CssBuilderTests
+{
+ [Fact]
+ public void ShouldConstructWithDefaultValue()
+ {
+ //arrange
+ string ClassToRender = CssBuilder.Default("item-one").Build();
+
+ //assert
+ ClassToRender.Should().Be("item-one");
+ }
+
+ [Fact]
+ public void ShouldConstructWithEmpty()
+ {
+ //arrange
+ CssBuilder? ClassToRender = CssBuilder.Empty();
+ ClassToRender = string.IsNullOrEmpty(ClassToRender.ToString()) ? null : ClassToRender;
+
+ //assert
+ ClassToRender.Should().BeNull();
+ }
+
+ [Fact]
+ public void ShouldBuildConditionalCssClasses()
+ {
+ //arrange
+ bool hasTwo = false;
+ bool hasThree = true;
+ Func<bool> hasFive = () => false;
+
+ //act
+ string ClassToRender = new CssBuilder("item-one")
+ .AddClass("item-two", when: hasTwo)
+ .AddClass("item-three", when: hasThree)
+ .AddClass("item-four")
+ .AddClass("item-five", when: hasFive)
+ .Build();
+ //assert
+ ClassToRender.Should().Be("item-one item-three item-four");
+ }
+
+ [Fact]
+ public void ShouldBuildConditionalCssBuilderClasses()
+ {
+ //arrange
+ bool hasTwo = false;
+ bool hasThree = true;
+ Func<bool> hasFive = () => false;
+
+ //act
+ string ClassToRender = new CssBuilder("item-one")
+ .AddClass("item-two", when: hasTwo)
+ .AddClass(new CssBuilder("item-three")
+ .AddClass("item-foo", false)
+ .AddClass("item-sub-three"),
+ when: hasThree)
+ .AddClass("item-four")
+ .AddClass("item-five", when: hasFive)
+ .Build();
+ //assert
+ ClassToRender.Should().Be("item-one item-three item-sub-three item-four");
+ }
+
+ [Fact]
+ public void ShouldBuildEmptyClasses()
+ {
+ //arrange
+ bool shouldShow = false;
+
+ //act
+ string ClassToRender = new CssBuilder()
+ .AddClass("some-class", shouldShow)
+ .Build();
+ //assert
+ ClassToRender.Should().Be(string.Empty);
+ }
+
+ [Fact]
+ public void ShouldBuildClassesWithFunc()
+ {
+ {
+ //arrange
+ // Simulates Razor Components attribute splatting feature
+ IDictionary<string, object> attributes = new Dictionary<string, object> { { "class", "my-custom-class-1" } };
+
+ //act
+ string ClassToRender = new CssBuilder("item-one")
+ .AddClass(() => attributes["class"].ToString(), when: attributes.ContainsKey("class"))
+ .Build();
+ //assert
+ ClassToRender.Should().Be("item-one my-custom-class-1");
+ }
+ }
+
+ [Fact]
+ public void ShouldBuildClassesFromAttributes()
+ {
+ {
+ //arrange
+ // Simulates Razor Components attribute splatting feature
+ IDictionary<string, object> attributes = new Dictionary<string, object> { { "class", "my-custom-class-1" } };
+
+ //act
+ string ClassToRender = new CssBuilder("item-one")
+ .AddClassFromAttributes(attributes)
+ .Build();
+ //assert
+ ClassToRender.Should().Be("item-one my-custom-class-1");
+ }
+ }
+
+ [Fact]
+ public void ShouldNotThrowWhenNullFor_BuildClassesFromAttributes()
+ {
+ {
+ //arrange
+ // Simulates Razor Components attribute splatting feature
+ IDictionary<string, object> attributes = null;
+
+ //act
+ string ClassToRender = new CssBuilder("item-one")
+ .AddClassFromAttributes(attributes)
+ .Build();
+ //assert
+ ClassToRender.Should().Be("item-one");
+ }
+ }
+
+ [Fact]
+ public void ForceNullForWhitespace_BuildClassesFromAttributes()
+ {
+ {
+ //arrange
+ // Simulates Razor Components attribute splatting feature
+ IDictionary<string, object> attributes = null;
+
+ //act
+ CssBuilder? ClassToRender = new CssBuilder()
+ .AddClassFromAttributes(attributes);
+ ClassToRender = string.IsNullOrEmpty(ClassToRender.ToString()) ? null : ClassToRender;
+
+ //assert
+ ClassToRender.Should().BeNull();
+ }
+ }
+
+ [Fact]
+ public void ShouldNotThrowNoKeyExceptionWithDictionary()
+ {
+ //arrange
+ // Simulates Razor Components attribute splatting feature
+ IDictionary<string, object> attributes = new Dictionary<string, object> { { "foo", "bar" } };
+
+ //act
+ string ClassToRender = new CssBuilder("item-one")
+ .AddClass(() => attributes["string"].ToString(), when: attributes.ContainsKey("class"))
+ .Build();
+ //assert
+ ClassToRender.Should().Be("item-one");
+ }
+}
diff --git a/src/app/tests/DevToys.UnitTests/Helpers/StyleBuilderTests.cs b/src/app/tests/DevToys.UnitTests/Blazor/Helpers/StyleBuilderTests.cs
similarity index 70%
rename from src/app/tests/DevToys.UnitTests/Helpers/StyleBuilderTests.cs
rename to src/app/tests/DevToys.UnitTests/Blazor/Helpers/StyleBuilderTests.cs
index 4aa32dcd1..c8bbded7a 100644
--- a/src/app/tests/DevToys.UnitTests/Helpers/StyleBuilderTests.cs
+++ b/src/app/tests/DevToys.UnitTests/Blazor/Helpers/StyleBuilderTests.cs
@@ -1,11 +1,11 @@
using DevToys.Blazor.Core.Helpers;
-namespace DevToys.UnitTests.Helpers;
+namespace DevToys.UnitTests.Blazor.Helpers;
public class StyleBuilderTests
{
[Fact]
- public void ShouldBulidConditionalInlineStyles()
+ public void ShouldBuildConditionalInlineStyles()
{
//arrange
bool hasBorder = true;
@@ -23,13 +23,12 @@ public class StyleBuilderTests
.AddStyle("padding", "35px")
.Build();
//assert
- Assert.Equal("background-color:DodgerBlue;border-width:2px 20px 10px 4px;z-index:-1;padding:35px;", ClassToRender);
+ ClassToRender.Should().Be("background-color:DodgerBlue;border-width:2px 20px 10px 4px;z-index:-1;padding:35px;");
}
[Fact]
- public void ShouldBulidConditionalInlineStylesFromAttributes()
+ public void ShouldBuildConditionalInlineStylesFromAttributes()
{
-
//arrange
bool hasBorder = true;
bool isOnTop = false;
@@ -50,7 +49,7 @@ public class StyleBuilderTests
string ClassToRender = new StyleBuilder().AddStyleFromAttributes(attributes).Build();
//assert
- Assert.Equal("background-color:DodgerBlue;border-width:2px 20px 10px 4px;z-index:-1;padding:35px;", ClassToRender);
+ ClassToRender.Should().Be("background-color:DodgerBlue;border-width:2px 20px 10px 4px;z-index:-1;padding:35px;");
}
[Fact]
@@ -67,24 +66,22 @@ public class StyleBuilderTests
/// The CSS syntax allows for empty declarations, which means that you can add leading and trailing semicolons as you like. For instance, this is valid CSS
/// .foo { ;;;display:none;;;color:black;;; }
/// Trimming is possible, but is it worth the operations for a non-issue?
- Assert.Equal("background-color:DodgerBlue;;padding:35px;", StyleToRender);
- Assert.Equal("background-color:DodgerBlue;;padding:35px;;", StyleToRenderFromDefaultConstructor);
-
+ StyleToRender.Should().Be("background-color:DodgerBlue;;padding:35px;");
+ StyleToRenderFromDefaultConstructor.Should().Be("background-color:DodgerBlue;;padding:35px;;");
}
[Fact]
public void ShouldNotAddEmptyStyle()
{
- StyleBuilder StyleToRender = StyleBuilder.Empty().AddStyle("");
-
- Assert.Null(string.IsNullOrEmpty(StyleToRender.ToString()) ? null : StyleToRender.ToString());
+ StyleBuilder? StyleToRender = StyleBuilder.Empty().AddStyle("");
+ StyleToRender = string.IsNullOrEmpty(StyleToRender.ToString()) ? null : StyleToRender;
+ StyleToRender.Should().BeNull();
}
[Fact]
public void ShouldAddNestedStyles()
{
-
StyleBuilder Child = StyleBuilder.Empty()
.AddStyle("background-color", "DodgerBlue")
.AddStyle("padding", "35px");
@@ -98,7 +95,7 @@ public class StyleBuilderTests
/// The CSS syntax allows for empty declarations, which means that you can add leading and trailing semicolons as you like. For instance, this is valid CSS
/// .foo { ;;;display:none;;;color:black;;; }
/// Trimming is possible, but is it worth the operations for a non-issue?
- Assert.Equal("background-color:DodgerBlue;padding:35px;z-index:-1;", StyleToRender);
+ StyleToRender.Should().Be("background-color:DodgerBlue;padding:35px;z-index:-1;");
}
@@ -118,25 +115,22 @@ public class StyleBuilderTests
/// The CSS syntax allows for empty declarations, which means that you can add leading and trailing semicolons as you like. For instance, this is valid CSS
/// .foo { ;;;display:none;;;color:black;;; }
/// Trimming is possible, but is it worth the operations for a non-issue?
- Assert.Equal("text-decoration:underline line-through;z-index:-1;", StyleToRender);
-
+ StyleToRender.Should().Be("text-decoration:underline line-through;z-index:-1;");
}
[Fact]
public void ShouldBuildStyleWithFunc()
{
- {
- //arrange
- // Simulates Razor Components attribute splatting feature
- IReadOnlyDictionary<string, object> attributes = new Dictionary<string, object> { { "class", "my-custom-class-1" } };
-
- //act
- string StyleToRender = StyleBuilder.Empty()
- .AddStyle("background-color", () => attributes["style"].ToString(), when: attributes.ContainsKey("style"))
- .AddStyle("background-color", "black")
- .Build();
- //assert
- Assert.Equal("background-color:black;", StyleToRender);
- }
+ //arrange
+ // Simulates Razor Components attribute splatting feature
+ IReadOnlyDictionary<string, object> attributes = new Dictionary<string, object> { { "class", "my-custom-class-1" } };
+
+ //act
+ string StyleToRender = StyleBuilder.Empty()
+ .AddStyle("background-color", () => attributes["style"].ToString(), when: attributes.ContainsKey("style"))
+ .AddStyle("background-color", "black")
+ .Build();
+ //assert
+ StyleToRender.Should().Be("background-color:black;");
}
}
diff --git a/src/app/tests/DevToys.UnitTests/Core/Tools/GuiToolProviderTests.cs b/src/app/tests/DevToys.UnitTests/Core/Tools/GuiToolProviderTests.cs
index 3039fd729..03e06b10f 100644
--- a/src/app/tests/DevToys.UnitTests/Core/Tools/GuiToolProviderTests.cs
+++ b/src/app/tests/DevToys.UnitTests/Core/Tools/GuiToolProviderTests.cs
@@ -1,6 +1,5 @@
using System.Collections.ObjectModel;
using System.ComponentModel;
-using System.Threading.Tasks;
using DevToys.Core.Tools;
using DevToys.Core.Tools.ViewItems;
@@ -14,8 +13,8 @@ public class GuiToolProviderTests : MefBasedTest
GuiToolProvider guiToolProvider = MefProvider.Import<GuiToolProvider>();
GuiToolInstance toolInstance = guiToolProvider.AllTools.FirstOrDefault(t => t.InternalComponentName == "MockTool");
- Assert.Equal("Sample title", toolInstance.LongDisplayTitle);
- Assert.Equal("Sample title", toolInstance.ShortDisplayTitle);
+ toolInstance.LongDisplayTitle.Should().Be("Sample title");
+ toolInstance.ShortDisplayTitle.Should().Be("Sample title");
}
[Fact]
@@ -23,8 +22,8 @@ public class GuiToolProviderTests : MefBasedTest
{
GuiToolProvider guiToolProvider = MefProvider.Import<GuiToolProvider>();
- Assert.Equal("MockTool", guiToolProvider.AllTools[0].InternalComponentName);
- Assert.Equal("MockTool2", guiToolProvider.AllTools[1].InternalComponentName);
+ guiToolProvider.AllTools[0].InternalComponentName.Should().Be("MockTool");
+ guiToolProvider.AllTools[1].InternalComponentName.Should().Be("MockTool2");
}
[Fact]
@@ -33,19 +32,22 @@ public class GuiToolProviderTests : MefBasedTest
GuiToolProvider guiToolProvider = MefProvider.Import<GuiToolProvider>();
ReadOnlyObservableCollection<INotifyPropertyChanged> menuItems = guiToolProvider.HeaderAndBodyToolViewItems;
- Assert.Equal(3, menuItems.Count);
- Assert.IsType<GroupViewItem>(menuItems[0]);
- Assert.IsType<SeparatorViewItem>(menuItems[1]);
- Assert.IsType<GroupViewItem>(menuItems[2]);
-
- Assert.Equal("All tools", ((GroupViewItem)menuItems[0]).DisplayTitle);
- Assert.Null(((GroupViewItem)menuItems[0]).Children);
- Assert.Equal("Group title", ((GroupViewItem)menuItems[2]).DisplayTitle);
- Assert.Equal(4, ((GroupViewItem)menuItems[2]).Children.Count);
- Assert.Equal("MockTool", ((GroupViewItem)menuItems[2]).Children[0].ToolInstance.InternalComponentName);
- Assert.Equal("MockTool2", ((GroupViewItem)menuItems[2]).Children[1].ToolInstance.InternalComponentName);
- Assert.Equal("MockTool3-XMLFormatter", ((GroupViewItem)menuItems[2]).Children[2].ToolInstance.InternalComponentName);
- Assert.Equal("MockTool4-XMLValidator", ((GroupViewItem)menuItems[2]).Children[3].ToolInstance.InternalComponentName);
+
+ menuItems.Should().HaveCount(3);
+ menuItems[0].Should().BeOfType<GroupViewItem>();
+ menuItems[1].Should().BeOfType<SeparatorViewItem>();
+ menuItems[2].Should().BeOfType<GroupViewItem>();
+
+ ((GroupViewItem)menuItems[0]).DisplayTitle.Should().Be("All tools");
+ ((GroupViewItem)menuItems[0]).Children.Should().BeNull();
+
+ var item3 = ((GroupViewItem)menuItems[2]);
+ item3.DisplayTitle.Should().Be("Group title");
+ item3.Children.Should().HaveCount(4);
+ item3.Children[0].ToolInstance.InternalComponentName.Should().Be("MockTool");
+ item3.Children[1].ToolInstance.InternalComponentName.Should().Be("MockTool2");
+ item3.Children[2].ToolInstance.InternalComponentName.Should().Be("MockTool3-XMLFormatter");
+ item3.Children[3].ToolInstance.InternalComponentName.Should().Be("MockTool4-XMLValidator");
}
[Fact]
@@ -54,18 +56,18 @@ public class GuiToolProviderTests : MefBasedTest
GuiToolProvider guiToolProvider = MefProvider.Import<GuiToolProvider>();
ReadOnlyObservableCollection<INotifyPropertyChanged> menuItems = guiToolProvider.HeaderAndBodyToolViewItems;
- Assert.Equal(3, menuItems.Count);
- Assert.IsType<GroupViewItem>(menuItems[0]);
- Assert.IsType<SeparatorViewItem>(menuItems[1]);
- Assert.IsType<GroupViewItem>(menuItems[2]);
+ menuItems.Should().HaveCount(3);
+ menuItems[0].Should().BeOfType<GroupViewItem>();
+ menuItems[1].Should().BeOfType<SeparatorViewItem>();
+ menuItems[2].Should().BeOfType<GroupViewItem>();
guiToolProvider.SetMostRecentUsedTool(((GroupViewItem)menuItems[2]).Children[0].ToolInstance);
// Menu should be unchanged.
- Assert.Equal(3, menuItems.Count);
- Assert.IsType<GroupViewItem>(menuItems[0]);
- Assert.IsType<SeparatorViewItem>(menuItems[1]);
- Assert.IsType<GroupViewItem>(menuItems[2]);
+ menuItems.Should().HaveCount(3);
+ menuItems[0].Should().BeOfType<GroupViewItem>();
+ menuItems[1].Should().BeOfType<SeparatorViewItem>();
+ menuItems[2].Should().BeOfType<GroupViewItem>();
}
[Fact]
@@ -77,30 +79,30 @@ public class GuiToolProviderTests : MefBasedTest
GuiToolViewItem toolToSetAsFavorite = ((GroupViewItem)menuItems[2]).Children[0];
// At first, no favorites at all.
- Assert.Equal(3, menuItems.Count);
- Assert.IsType<GroupViewItem>(menuItems[0]);
- Assert.IsType<SeparatorViewItem>(menuItems[1]);
- Assert.IsType<GroupViewItem>(menuItems[2]);
+ menuItems.Should().HaveCount(3);
+ menuItems[0].Should().BeOfType<GroupViewItem>();
+ menuItems[1].Should().BeOfType<SeparatorViewItem>();
+ menuItems[2].Should().BeOfType<GroupViewItem>();
// Set item as favorite. The menu should then have "Favorites" group.
guiToolProvider.SetToolIsFavorite(toolToSetAsFavorite.ToolInstance, isFavorite: true);
- Assert.Equal(4, menuItems.Count);
- Assert.IsType<GroupViewItem>(menuItems[0]);
- Assert.IsType<SeparatorViewItem>(menuItems[1]);
- Assert.IsType<GroupViewItem>(menuItems[2]);
- Assert.IsType<GroupViewItem>(menuItems[3]);
- Assert.Equal("Favorites", ((GroupViewItem)menuItems[2]).DisplayTitle);
- Assert.Equal("Group title", ((GroupViewItem)menuItems[3]).DisplayTitle);
- Assert.Single(((GroupViewItem)menuItems[2]).Children);
- Assert.Equal(toolToSetAsFavorite.ToolInstance, ((GroupViewItem)menuItems[2]).Children[0].ToolInstance);
- Assert.NotEqual(toolToSetAsFavorite, ((GroupViewItem)menuItems[2]).Children[0]);
+ menuItems.Should().HaveCount(4);
+ menuItems[0].Should().BeOfType<GroupViewItem>();
+ menuItems[1].Should().BeOfType<SeparatorViewItem>();
+ menuItems[2].Should().BeOfType<GroupViewItem>();
+ menuItems[3].Should().BeOfType<GroupViewItem>();
+ ((GroupViewItem)menuItems[2]).DisplayTitle.Should().Be("Favorites");
+ ((GroupViewItem)menuItems[3]).DisplayTitle.Should().Be("Group title");
+ ((GroupViewItem)menuItems[2]).Children.Should().ContainSingle();
+ ((GroupViewItem)menuItems[2]).Children[0].ToolInstance.Should().Be(toolToSetAsFavorite.ToolInstance);
+ ((GroupViewItem)menuItems[2]).Children[0].Should().NotBe(toolToSetAsFavorite);
// Unfavorite the item. The menu should remove "Favorite" group because the group is now empty.
guiToolProvider.SetToolIsFavorite(toolToSetAsFavorite.ToolInstance, isFavorite: false);
- Assert.Equal(3, menuItems.Count);
- Assert.IsType<GroupViewItem>(menuItems[0]);
- Assert.IsType<SeparatorViewItem>(menuItems[1]);
- Assert.IsType<GroupViewItem>(menuItems[2]);
+ menuItems.Should().HaveCount(3);
+ menuItems[0].Should().BeOfType<GroupViewItem>();
+ menuItems[1].Should().BeOfType<SeparatorViewItem>();
+ menuItems[2].Should().BeOfType<GroupViewItem>();
}
[Theory]
@@ -109,16 +111,16 @@ public class GuiToolProviderTests : MefBasedTest
[InlineData("XML", "MockTool3-XMLFormatter", "MockTool4-XMLValidator")]
[InlineData("XML Validator", "MockTool4-XMLValidator")]
[InlineData("XML Valid", "MockTool4-XMLValidator")]
- public async Task ToolSearch(string searchQuery, params string[] toolNames)
+ public void ToolSearch(string searchQuery, params string[] toolNames)
{
GuiToolProvider guiToolProvider = MefProvider.Import<GuiToolProvider>();
var results = new ObservableCollection<GuiToolViewItem>();
guiToolProvider.SearchTools(searchQuery, results);
- Assert.Equal(toolNames.Length, results.Count);
+ results.Should().HaveSameCount(toolNames);
for (int i = 0; i < toolNames.Length; i++)
{
- Assert.Equal(toolNames[i], results[i].ToolInstance.InternalComponentName);
+ results[i].ToolInstance.InternalComponentName.Should().Be(toolNames[i]);
}
}
}
diff --git a/src/app/tests/DevToys.UnitTests/Core/Tools/SmartDetectionServiceTests.cs b/src/app/tests/DevToys.UnitTests/Core/Tools/SmartDetectionServiceTests.cs
index 8ceea9aec..3fcbbbe77 100644
--- a/src/app/tests/DevToys.UnitTests/Core/Tools/SmartDetectionServiceTests.cs
+++ b/src/app/tests/DevToys.UnitTests/Core/Tools/SmartDetectionServiceTests.cs
@@ -17,10 +17,10 @@ public class SmartDetectionServiceTests : MefBasedTest
SmartDetectionService smartDetectionService = MefProvider.Import<SmartDetectionService>();
IReadOnlyList<SmartDetectedTool> detectedTools = await smartDetectionService.DetectAsync(rawData, isStrict, CancellationToken.None);
- Assert.Equal(toolNames.Length, detectedTools.Count);
+ detectedTools.Should().HaveSameCount(toolNames);
for (int i = 0; i < toolNames.Length; i++)
{
- Assert.Equal(toolNames[i], detectedTools[i].ToolInstance.InternalComponentName);
+ detectedTools[i].ToolInstance.InternalComponentName.Should().Be(toolNames[i]);
}
}
}
diff --git a/src/app/tests/DevToys.UnitTests/DevToys.UnitTests.csproj b/src/app/tests/DevToys.UnitTests/DevToys.UnitTests.csproj
index f17b74e29..7406c8e10 100644
--- a/src/app/tests/DevToys.UnitTests/DevToys.UnitTests.csproj
+++ b/src/app/tests/DevToys.UnitTests/DevToys.UnitTests.csproj
@@ -1,4 +1,4 @@
-<Project Sdk="Microsoft.NET.Sdk">
+<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>$(NetCore)</TargetFrameworks>
@@ -6,6 +6,7 @@
</PropertyGroup>
<ItemGroup>
+ <PackageReference Include="FluentAssertions" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
diff --git a/src/app/tests/DevToys.UnitTests/Helpers/CssBuilderTests.cs b/src/app/tests/DevToys.UnitTests/Helpers/CssBuilderTests.cs
deleted file mode 100644
index 245f5fc69..000000000
--- a/src/app/tests/DevToys.UnitTests/Helpers/CssBuilderTests.cs
+++ /dev/null
@@ -1,260 +0,0 @@
-using DevToys.Blazor.Core.Helpers;
-using Xunit;
-
-namespace DevToys.UnitTests.Helpers;
-
-public class CssBuilderTests
-{
- [Fact]
- public void ShouldConstructWithDefaultValue()
- {
- //arrange
- string ClassToRender = CssBuilder.Default("item-one").Build();
-
- //assert
- Assert.Equal("item-one", ClassToRender);
- }
-
- [Fact]
- public void ShouldConstructWithEmpty()
- {
- //arrange
- string ClassToRender = string.IsNullOrEmpty(CssBuilder.Empty().ToString()) ? null : CssBuilder.Empty().ToString();
-
- //assert
- Assert.Null(ClassToRender);
- }
-
- [Fact]
- public void ShouldBuildConditionalCssClasses()
- {
- //arrange
- bool hasTwo = false;
- bool hasThree = true;
- Func<bool> hasFive = () => false;
-
- //act
- string ClassToRender = new CssBuilder("item-one")
- .AddClass("item-two", when: hasTwo)
- .AddClass("item-three", when: hasThree)
- .AddClass("item-four")
- .AddClass("item-five", when: hasFive)
- .Build();
- //assert
- Assert.Equal("item-one item-three item-four", ClassToRender);
- }
- [Fact]
- public void ShouldBuildConditionalCssBuilderClasses()
- {
- //arrange
- bool hasTwo = false;
- bool hasThree = true;
- Func<bool> hasFive = () => false;
-
- //act
- string ClassToRender = new CssBuilder("item-one")
- .AddClass("item-two", when: hasTwo)
- .AddClass(new CssBuilder("item-three")
- .AddClass("item-foo", false)
- .AddClass("item-sub-three"),
- when: hasThree)
- .AddClass("item-four")
- .AddClass("item-five", when: hasFive)
- .Build();
- //assert
- Assert.Equal("item-one item-three item-sub-three item-four", ClassToRender);
- }
- [Fact]
- public void ShouldBuildEmptyClasses()
- {
- //arrange
- bool shouldShow = false;
-
- //act
- string ClassToRender = new CssBuilder()
- .AddClass("some-class", shouldShow)
- .Build();
- //assert
- Assert.Equal(string.Empty, ClassToRender);
- }
-
- [Fact]
- public void ShouldBuildClassesWithFunc()
- {
- {
- //arrange
- // Simulates Razor Components attribute splatting feature
- IReadOnlyDictionary<string, object> attributes = new Dictionary<string, object> { { "class", "my-custom-class-1" } };
-
- //act
- string ClassToRender = new CssBuilder("item-one")
- .AddClass(() => attributes["class"].ToString(), when: attributes.ContainsKey("class"))
- .Build();
- //assert
- Assert.Equal("item-one my-custom-class-1", ClassToRender);
- }
- }
-
- [Fact]
- public void ShouldBuildClassesFromAttributes()
- {
- {
- //arrange
- // Simulates Razor Components attribute splatting feature
- IDictionary<string, object> attributes = new Dictionary<string, object> { { "class", "my-custom-class-1" } };
-
- //act
- string ClassToRender = new CssBuilder("item-one")
- .AddClassFromAttributes(attributes)
- .Build();
- //assert
- Assert.Equal("item-one my-custom-class-1", ClassToRender);
- }
- }
-
- [Fact]
- public void ShouldNotThrowWhenNullFor_BuildClassesFromAttributes()
- {
- {
- //arrange
- // Simulates Razor Components attribute splatting feature
- IDictionary<string, object> attributes = null;
-
- //act
- string ClassToRender = new CssBuilder("item-one")
- .AddClassFromAttributes(attributes)
- .Build();
- //assert
- Assert.Equal("item-one", ClassToRender);
- }
- }
-
- [Fact]
- public void ShouldNotThrowWhenNullForAttributeItem_BuildClassesFromAttributes()
- {
- {
- //arrange
- // Simulates Razor Components attribute splatting feature
- IDictionary<string, object> attributes = new Dictionary<string, object> { { "class", null } };
-
- //act
- string ClassToRender = new CssBuilder("item-one")
- .AddClassFromAttributes(attributes)
- .Build();
- //assert
- Assert.Equal("item-one", ClassToRender);
- }
- }
-
- [Fact]
- public void ForceNullForWhitespace_BuildClassesFromAttributes()
- {
- {
- //arrange
- // Simulates Razor Components attribute splatting feature
- IDictionary<string, object> attributes = null;
-
- //act
- string ClassToRender = string.IsNullOrEmpty(new CssBuilder().AddClassFromAttributes(attributes).ToString()) ? null : new CssBuilder().AddClassFromAttributes(attributes).ToString();
-
- //assert
- Assert.Null(ClassToRender);
- }
- }
-
- [Fact]
- public void ShouldNotThrowNoKeyExceptionWithDictionary()
- {
- {
- //arrange
- // Simulates Razor Components attribute splatting feature
- IDictionary<string, object> attributes = new Dictionary<string, object> { { "foo", "bar" } };
-
- //act
- string ClassToRender = new CssBuilder("item-one")
- .AddClass(() => attributes["string"].ToString(), when: attributes.ContainsKey("class"))
- .Build();
- //assert
- Assert.Equal("item-one", ClassToRender);
- }
- }
-
- [Fact]
- public void ShouldBuildConditionalCssClassesUsingPrefix()
- {
- //arrange
- bool hasTwo = false;
- bool hasThree = true;
- Func<bool> hasFive = () => false;
-
- //act
- string ClassToRender = new CssBuilder("default")
- .SetPrefix("item-")
- .AddClass("two", when: hasTwo)
- .AddClass("three", when: hasThree)
- .AddClass("four")
- .AddClass("five", when: hasFive)
- .Build();
- //assert
- Assert.Equal("default item-three item-four", ClassToRender);
- }
-
- [Fact]
- public void ShouldBuildConditionalCssClassesUsingMultiplePrefixes()
- {
- //arrange
- bool hasTwo = true;
- bool hasThree = true;
- Func<bool> hasFive = () => false;
-
- //act
- string ClassToRender = new CssBuilder("default")
- .AddClass("no-prefix-two", when: hasTwo)
- .SetPrefix("item-")
- .AddClass("three", when: hasThree)
- .SetPrefix("namespace-")
- .AddClass("four")
- .AddClass("five", when: hasFive)
- .Build();
- //assert
- Assert.Equal("default no-prefix-two item-three namespace-four", ClassToRender);
- }
-
- [Fact]
- public void ShouldBuildClassesFromAttributesWithPrefix()
- {
- {
- //arrange
- // Simulates Razor Components attribute splatting feature
- IDictionary<string, object> attributes = new Dictionary<string, object> { { "class", "my-custom-class-1" } };
-
- //act
- string ClassToRender = new CssBuilder("item-one")
- .SetPrefix("pre-")
- .AddClassFromAttributes(attributes)
- .Build();
- //assert
- Assert.Equal("item-one pre-my-custom-class-1", ClassToRender);
- }
- }
-
- [Fact]
- public void ShouldBuildClassesFromAttributesWithClearedPrefix()
- {
- {
- //arrange
- // Simulates Razor Components attribute splatting feature
- IDictionary<string, object> attributes = new Dictionary<string, object> { { "class", "my-custom-class-1" } };
-
- //act
- string ClassToRender = new CssBuilder("item-one")
- .SetPrefix("item-")
- .AddClass("two")
- .SetPrefix(String.Empty)
- .AddClassFromAttributes(attributes)
- .Build();
- //assert
- Assert.Equal("item-one item-two my-custom-class-1", ClassToRender);
- }
- }
-}
diff --git a/src/app/tests/DevToys.UnitTests/Usings.cs b/src/app/tests/DevToys.UnitTests/Usings.cs
index b77da866e..14a6519b1 100644
--- a/src/app/tests/DevToys.UnitTests/Usings.cs
+++ b/src/app/tests/DevToys.UnitTests/Usings.cs
@@ -2,4 +2,5 @@
global using System.Collections.Generic;
global using System.Linq;
global using DevToys.Api;
+global using FluentAssertions;
global using Xunit;
diff --git a/src/spellcheckerexclusion.dic b/src/spellcheckerexclusion.dic
index 4b03eb645..f4cc125d0 100644
--- a/src/spellcheckerexclusion.dic
+++ b/src/spellcheckerexclusion.dic
@@ -6,3 +6,4 @@ Fira
ntdll
dwmapi
hwnd
+Doesnt