Skip to content

A Swiss Army knife for developers.

Added unit tests

Etienne Baudouxb557f2302bc2
diff --git a/src/app/dev/DevToys.Blazor/Helpers/CssBuilder.cs b/src/app/dev/DevToys.Blazor/Helpers/CssBuilder.cs
index 3506c6b85..16826dcb7 100644
--- a/src/app/dev/DevToys.Blazor/Helpers/CssBuilder.cs
+++ b/src/app/dev/DevToys.Blazor/Helpers/CssBuilder.cs
@@ -1,7 +1,5 @@
namespace DevToys.Blazor.Core.Helpers;
-// TODO: Add unit tests from https://github.com/EdCharbeneau/BlazorComponentUtilities/blob/master/BlazorComponentUtilities.Tests/CssBuilderTests.cs
-
public struct CssBuilder
{
private string? _stringBuffer;
diff --git a/src/app/tests/DevToys.UnitTests/Helpers/CssBuilderTests.cs b/src/app/tests/DevToys.UnitTests/Helpers/CssBuilderTests.cs
new file mode 100644
index 000000000..245f5fc69
--- /dev/null
+++ b/src/app/tests/DevToys.UnitTests/Helpers/CssBuilderTests.cs
@@ -0,0 +1,260 @@
+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/Helpers/StyleBuilderTests.cs b/src/app/tests/DevToys.UnitTests/Helpers/StyleBuilderTests.cs
new file mode 100644
index 000000000..4aa32dcd1
--- /dev/null
+++ b/src/app/tests/DevToys.UnitTests/Helpers/StyleBuilderTests.cs
@@ -0,0 +1,142 @@
+using DevToys.Blazor.Core.Helpers;
+
+namespace DevToys.UnitTests.Helpers;
+
+public class StyleBuilderTests
+{
+ [Fact]
+ public void ShouldBulidConditionalInlineStyles()
+ {
+ //arrange
+ bool hasBorder = true;
+ bool isOnTop = false;
+ int top = 2;
+ int bottom = 10;
+ int left = 4;
+ int right = 20;
+
+ //act
+ string ClassToRender = new StyleBuilder("background-color", "DodgerBlue")
+ .AddStyle("border-width", $"{top}px {right}px {bottom}px {left}px", when: hasBorder)
+ .AddStyle("z-index", "999", when: isOnTop)
+ .AddStyle("z-index", "-1", when: !isOnTop)
+ .AddStyle("padding", "35px")
+ .Build();
+ //assert
+ Assert.Equal("background-color:DodgerBlue;border-width:2px 20px 10px 4px;z-index:-1;padding:35px;", ClassToRender);
+ }
+
+ [Fact]
+ public void ShouldBulidConditionalInlineStylesFromAttributes()
+ {
+
+ //arrange
+ bool hasBorder = true;
+ bool isOnTop = false;
+ int top = 2;
+ int bottom = 10;
+ int left = 4;
+ int right = 20;
+
+ //act
+ string StyleToRender = new StyleBuilder("background-color", "DodgerBlue")
+ .AddStyle("border-width", $"{top}px {right}px {bottom}px {left}px", when: hasBorder)
+ .AddStyle("z-index", "999", when: isOnTop)
+ .AddStyle("z-index", "-1", when: !isOnTop)
+ .AddStyle("padding", "35px")
+ .Build();
+
+ IReadOnlyDictionary<string, object> attributes = new Dictionary<string, object> { { "style", StyleToRender } };
+
+ 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);
+ }
+
+ [Fact]
+ public void ShouldAddExistingStyle()
+ {
+ string StyleToRender = StyleBuilder.Empty()
+ .AddStyle("background-color:DodgerBlue;")
+ .AddStyle("padding", "35px")
+ .Build();
+
+ string StyleToRenderFromDefaultConstructor = StyleBuilder.Default(StyleToRender).Build();
+
+ /// Double ;; is valid HTML.
+ /// 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);
+
+ }
+
+ [Fact]
+ public void ShouldNotAddEmptyStyle()
+ {
+ StyleBuilder StyleToRender = StyleBuilder.Empty().AddStyle("");
+
+ Assert.Null(string.IsNullOrEmpty(StyleToRender.ToString()) ? null : StyleToRender.ToString());
+
+ }
+
+ [Fact]
+ public void ShouldAddNestedStyles()
+ {
+
+ StyleBuilder Child = StyleBuilder.Empty()
+ .AddStyle("background-color", "DodgerBlue")
+ .AddStyle("padding", "35px");
+
+ string StyleToRender = StyleBuilder.Empty()
+ .AddStyle(Child)
+ .AddStyle("z-index", "-1")
+ .Build();
+
+ /// Double ;; is valid HTML.
+ /// 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);
+
+ }
+
+ [Fact]
+ public void ShouldAddComplexStyles()
+ {
+ string StyleToRender = StyleBuilder.Empty()
+ .AddStyle("text-decoration", v => v
+ .AddValue("underline", true)
+ .AddValue("overline", false)
+ .AddValue("line-through", true),
+ when: true)
+ .AddStyle("z-index", "-1")
+ .Build();
+
+ /// Double ;; is valid HTML.
+ /// 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);
+
+ }
+
+ [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);
+ }
+ }
+}