Skip to content

Commit

Permalink
♻ refactor: make some method names more semantic (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
capdiem authored Feb 21, 2024
1 parent f0106ff commit 1bab669
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 67 deletions.
41 changes: 29 additions & 12 deletions BemIt.Tests/BlockTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,12 @@ public void Extend_element()
{
Assert.Equal("m-list-item-title__intro", _block.Extend("title").Element("intro").Build());
}

[Fact]
public void Extend_element_modifier()
{
Assert.Equal("m-list-item-title__intro m-list-item-title__intro--hoverable", _block.Extend("title").Element("intro").Modifier("hoverable").Build());
}

[Fact]
public void Block_modifier_empty_ctor()
public void Extend_element_modifier()
{
Assert.Equal("m-list-item", _block.Modifier().Build());
Assert.Equal("m-list-item-title__intro m-list-item-title__intro--hoverable",
_block.Extend("title").Element("intro").Modifier("hoverable").Build());
}

[Fact]
Expand Down Expand Up @@ -122,11 +117,13 @@ public void Block_modifier_with_two_arguments_and_custom_name()
{
var active = true;
var link = true;
Assert.Equal("m-list-item m-list-item--living m-list-item--clickable", _block.Modifier(active, link, "living", "clickable").Build());
Assert.Equal("m-list-item m-list-item--living m-list-item--clickable",
_block.Modifier(active, link, "living", "clickable").Build());

var modifier1 = false;
var modifier2 = false;
Assert.Equal("m-list-item", _block.Modifier(modifier1, modifier2, "custom-modifier1", "custom-modifier2").Build());
Assert.Equal("m-list-item",
_block.Modifier(modifier1, modifier2, "custom-modifier1", "custom-modifier2").Build());
}

[Fact]
Expand All @@ -135,7 +132,8 @@ public void Block_modifier_with_one_true_and_one_false_arguments()
var active = false;
var link = true;
Assert.Equal("m-list-item m-list-item--link", _block.Modifier(active, link).Build());
Assert.Equal("m-list-item m-list-item--clickable", _block.Modifier(active, link, "living", "clickable").Build());
Assert.Equal("m-list-item m-list-item--clickable",
_block.Modifier(active, link, "living", "clickable").Build());
}

[Fact]
Expand All @@ -145,4 +143,23 @@ public void Block_modifier_enum()
Assert.Equal("m-list-item m-list-item--density-comfortable", _block.Modifier(Density.Comfortable).Build());
Assert.Equal("m-list-item m-list-item--density-compact", _block.Modifier(Density.Compact).Build());
}
}

[Fact]
public void GenerateCssClasses()
{
var classes = _block.Modifier("active").And("link").GenerateCssClasses();
Assert.Collection(classes,
item => Assert.Equal("m-list-item", item),
item => Assert.Equal("m-list-item--active", item),
item => Assert.Equal("m-list-item--link", item));

var block2 = new Block("m-sheet");
var classes2 = classes.Concat(block2.Modifier("outlined").GenerateCssClasses());
Assert.Collection(classes2,
item => Assert.Equal("m-list-item", item),
item => Assert.Equal("m-list-item--active", item),
item => Assert.Equal("m-list-item--link", item),
item => Assert.Equal("m-sheet", item),
item => Assert.Equal("m-sheet--outlined", item));
}
}
6 changes: 0 additions & 6 deletions BemIt.Tests/ElementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ public void Element()
Assert.Equal("m-list-item__avatar", _block.Element("avatar").Build());
}

[Fact]
public void Block_modifier_empty_ctor()
{
Assert.Equal("m-list-item__avatar", _block.Element("avatar").Modifier().Build());
}

[Fact]
public void Element_modifier()
{
Expand Down
22 changes: 12 additions & 10 deletions BemIt.Tests/ModifierTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,35 @@ public class ModifierTests
[Fact]
public void Modifier_one_with()
{
Assert.Equal("m-list-item m-list-item--active", _block.Modifier().Add("active").Build());
Assert.Equal("m-list-item m-list-item--active", _block.Modifier("active").Build());
}

[Fact]
public void Modifier_two_withs()
{
Assert.Equal("m-list-item m-list-item--active m-list-item--link", _block.Modifier().Add("active").Add("link").Build());
Assert.Equal("m-list-item m-list-item--active m-list-item--link",
_block.Modifier("active").And("link").Build());
}

[Fact]
public void Modifier_three_withs()
{
Assert.Equal("m-list-item m-list-item--active m-list-item--link m-list-item--ripple",
_block.Modifier().Add("active").Add("link").Add("ripple").Build());
_block.Modifier("active").And("link").And("ripple").Build());
}

[Fact]
public void Modifier_with_enum_with_one_mod()
{
Assert.Equal("m-list-item m-list-item--density-comfortable m-list-item--link",
_block.Modifier().Add(Density.Comfortable).Add("link").Build());
_block.Modifier(Density.Comfortable).And("link").Build());
}

[Fact]
public void Modifier_with_enum_with_multi()
{
Assert.Equal("m-list-item m-list-item--density-comfortable m-list-item--link m-list-item--active",
_block.Modifier().Add(Density.Comfortable).Add(new Dictionary<string, bool>()
_block.Modifier(Density.Comfortable).And(new Dictionary<string, bool>()
{
{ "link", true },
{ "ripple", false },
Expand All @@ -50,12 +51,13 @@ public void Modifier_with_args_with_enum()
var ripple = true;

Assert.Equal("m-list-item m-list-item--active m-list-item--density-comfortable",
_block.Modifier().Add(active).Add(Density.Comfortable).Build());
_block.Modifier(active).And(Density.Comfortable).Build());

Assert.Equal("m-list-item m-list-item--active m-list-item--link m-list-item--density-comfortable",
_block.Modifier().Add(active, link).Add(Density.Comfortable).Build());
_block.Modifier(active, link).And(Density.Comfortable).Build());

Assert.Equal("m-list-item m-list-item--active m-list-item--link m-list-item--ripple m-list-item--density-comfortable",
_block.Modifier().Add(active, link, ripple).Add(Density.Comfortable).Build());
Assert.Equal(
"m-list-item m-list-item--active m-list-item--link m-list-item--ripple m-list-item--density-comfortable",
_block.Modifier(active, link, ripple).And(Density.Comfortable).Build());
}
}
}
26 changes: 13 additions & 13 deletions BemIt.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=3938f614_002D8c5b_002D4d0c_002D9317_002Dcc628d53109f/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="Block" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;TestAncestor&gt;&#xD;
&lt;TestId&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::xUnit::BemIt.Tests.UnitTest1.Block&lt;/TestId&gt;&#xD;
&lt;TestId&gt;xUnit::F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::BemIt.Tests.UnitTest1.Block&lt;/TestId&gt;&#xD;
&lt;/TestAncestor&gt;&#xD;
&lt;/SessionState&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=3a528811_002D1ad3_002D4b36_002Dbd6b_002D4761f88f961e/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="All tests from &amp;lt;BemIt.Tests&amp;gt; #2" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
Expand All @@ -10,24 +10,24 @@
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=5b198ea2_002D635b_002D46f0_002Dbea1_002D7227609c93d5/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="Block_modifier_with_true_condition #2" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;Or&gt;&#xD;
&lt;TestAncestor&gt;&#xD;
&lt;TestId&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::xUnit::BemIt.Tests.BlockTests.Block_modifier_with_true_condition&lt;/TestId&gt;&#xD;
&lt;TestId&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::xUnit::BemIt.Tests.ElementTests.Element_modifier&lt;/TestId&gt;&#xD;
&lt;TestId&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::xUnit::BemIt.Tests.BlockTests.ChildBlock&lt;/TestId&gt;&#xD;
&lt;TestId&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::xUnit::BemIt.Tests.ElementTests&lt;/TestId&gt;&#xD;
&lt;TestId&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::xUnit::BemIt.Tests.ModifierTests.Modifier_with_true_condition&lt;/TestId&gt;&#xD;
&lt;TestId&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::xUnit::BemIt.Tests.ModifierTests.Modifier_with_two_mods&lt;/TestId&gt;&#xD;
&lt;TestId&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::xUnit::BemIt.Tests.ModifierTests.Modifier_three_withs&lt;/TestId&gt;&#xD;
&lt;TestId&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::xUnit::BemIt.Tests.ModifierTests.Modifier_with_enum_with_one_mod&lt;/TestId&gt;&#xD;
&lt;TestId&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::xUnit::BemIt.Tests.ModifierTests.Modifier_with_enum_with_multi&lt;/TestId&gt;&#xD;
&lt;TestId&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::xUnit::BemIt.Tests.ModifierTests.Modifier_with_args_with_enum&lt;/TestId&gt;&#xD;
&lt;TestId&gt;xUnit::F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::BemIt.Tests.BlockTests.Block_modifier_with_true_condition&lt;/TestId&gt;&#xD;
&lt;TestId&gt;xUnit::F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::BemIt.Tests.ElementTests.Element_modifier&lt;/TestId&gt;&#xD;
&lt;TestId&gt;xUnit::F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::BemIt.Tests.BlockTests.ChildBlock&lt;/TestId&gt;&#xD;
&lt;TestId&gt;xUnit::F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::BemIt.Tests.ElementTests&lt;/TestId&gt;&#xD;
&lt;TestId&gt;xUnit::F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::BemIt.Tests.ModifierTests.Modifier_with_true_condition&lt;/TestId&gt;&#xD;
&lt;TestId&gt;xUnit::F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::BemIt.Tests.ModifierTests.Modifier_with_two_mods&lt;/TestId&gt;&#xD;
&lt;TestId&gt;xUnit::F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::BemIt.Tests.ModifierTests.Modifier_three_withs&lt;/TestId&gt;&#xD;
&lt;TestId&gt;xUnit::F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::BemIt.Tests.ModifierTests.Modifier_with_enum_with_one_mod&lt;/TestId&gt;&#xD;
&lt;TestId&gt;xUnit::F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::BemIt.Tests.ModifierTests.Modifier_with_enum_with_multi&lt;/TestId&gt;&#xD;
&lt;TestId&gt;xUnit::F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::BemIt.Tests.ModifierTests.Modifier_with_args_with_enum&lt;/TestId&gt;&#xD;
&lt;/TestAncestor&gt;&#xD;
&lt;ProjectFile&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1/f:BlockTests.cs&lt;/ProjectFile&gt;&#xD;
&lt;/Or&gt;&#xD;
&lt;/SessionState&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=608d3728_002Dfa3b_002D416d_002Dbccb_002D1f8d8412878b/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="Block_modifier_with_true_condition" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;TestAncestor&gt;&#xD;
&lt;TestId&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::xUnit::BemIt.Tests.BlockTests.Block_modifier_with_true_condition&lt;/TestId&gt;&#xD;
&lt;TestId&gt;F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::xUnit::BemIt.Tests.BlockTests.Block_modifier&lt;/TestId&gt;&#xD;
&lt;TestId&gt;xUnit::F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::BemIt.Tests.BlockTests.Block_modifier_with_true_condition&lt;/TestId&gt;&#xD;
&lt;TestId&gt;xUnit::F686E4F1-1AA0-4473-B7FB-F3654B7377E1::net6.0::BemIt.Tests.BlockTests.Block_modifier&lt;/TestId&gt;&#xD;
&lt;/TestAncestor&gt;&#xD;
&lt;/SessionState&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=638b456a_002D975f_002D4204_002D9ead_002Da67e5256e5ab/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="All tests from &amp;lt;BemIt.Tests&amp;gt;" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
Expand Down
2 changes: 1 addition & 1 deletion BemIt/Block.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ public Element Element(string element)
{
return new Element(Name, element);
}
}
}
5 changes: 0 additions & 5 deletions BemIt/Extensions/BlockOrElementExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ namespace BemIt;

public static class BlockOrElementExtensions
{
public static Modifier Modifier(this IBlockOrElement blockOrElement)
{
return new Modifier(blockOrElement.Name);
}

public static Modifier Modifier(this IBlockOrElement blockOrElement, string modifier)
{
return new Modifier(blockOrElement.Name, modifier);
Expand Down
40 changes: 20 additions & 20 deletions BemIt/Modifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public Modifier(string blockOrElement, IDictionary<string, bool> modifiers) : th
/// <param name="modifier"></param>
/// <param name="condition"></param>
/// <returns></returns>
public Modifier Add(string modifier, bool condition)
public Modifier And(string modifier, bool condition)
{
_modifiers.Add(modifier, condition);

Expand All @@ -69,9 +69,9 @@ public Modifier Add(string modifier, bool condition)
/// </summary>
/// <param name="modifier"></param>
/// <returns></returns>
public Modifier Add(string modifier)
public Modifier And(string modifier)
{
return Add(modifier, true);
return And(modifier, true);
}

/// <summary>
Expand All @@ -80,12 +80,12 @@ public Modifier Add(string modifier)
/// <param name="value">The value of enum</param>
/// <param name="apply">Applies the modifier if <see keywork="true"/></param>
/// <returns></returns>
public Modifier Add(Enum value, bool apply = true)
public Modifier And(Enum value, bool apply = true)
{
if (!apply) return this;

var modifier = FormatEnum(value);
return Add(modifier);
return And(modifier);
}

/// <summary>
Expand All @@ -95,20 +95,20 @@ public Modifier Add(Enum value, bool apply = true)
/// <param name="name"></param>
/// <param name="apply">Applies the modifier if <see keywork="true"/></param>
/// <returns></returns>
public Modifier Add(Enum value, string name, bool apply = true)
public Modifier And(Enum value, string name, bool apply = true)
{
if (!apply) return this;

var modifier = FormatEnum(value, name);
return Add(modifier);
return And(modifier);
}

/// <summary>
/// Adds a set of modifiers of type dictionary
/// </summary>
/// <param name="modifiers"></param>
/// <returns></returns>
public Modifier Add(IDictionary<string, bool> modifiers)
public Modifier And(IDictionary<string, bool> modifiers)
{
foreach (var (key, value) in modifiers)
{
Expand All @@ -124,9 +124,9 @@ public Modifier Add(IDictionary<string, bool> modifiers)
/// <param name="modifier"></param>
/// <param name="name"></param>
/// <returns></returns>
public Modifier Add(bool modifier, [CallerArgumentExpression("modifier")] string name = "")
public Modifier And(bool modifier, [CallerArgumentExpression("modifier")] string name = "")
{
return Add(name, modifier);
return And(name, modifier);
}

/// <summary>
Expand All @@ -137,13 +137,13 @@ public Modifier Add(bool modifier, [CallerArgumentExpression("modifier")] string
/// <param name="name1"></param>
/// <param name="name2"></param>
/// <returns></returns>
public Modifier Add(bool modifier1, bool modifier2,
public Modifier And(bool modifier1, bool modifier2,
[CallerArgumentExpression("modifier1")]
string name1 = "",
[CallerArgumentExpression("modifier2")]
string name2 = "")
{
return Add(new Dictionary<string, bool>()
return And(new Dictionary<string, bool>()
{
{ name1, modifier1 },
{ name2, modifier2 }
Expand All @@ -160,15 +160,15 @@ public Modifier Add(bool modifier1, bool modifier2,
/// <param name="name2"></param>
/// <param name="name3"></param>
/// <returns></returns>
public Modifier Add(bool modifier1, bool modifier2, bool modifier3,
public Modifier And(bool modifier1, bool modifier2, bool modifier3,
[CallerArgumentExpression("modifier1")]
string name1 = "",
[CallerArgumentExpression("modifier2")]
string name2 = "",
[CallerArgumentExpression("modifier3")]
string name3 = "")
{
return Add(new Dictionary<string, bool>()
return And(new Dictionary<string, bool>()
{
{ name1, modifier1 },
{ name2, modifier2 },
Expand All @@ -193,11 +193,11 @@ public Modifier AddOneOf(bool modifier1, bool modifier2,
{
if (modifier1)
{
Add(name1);
And(name1);
}
else if (modifier2)
{
Add(name2);
And(name2);
}

return this;
Expand Down Expand Up @@ -225,15 +225,15 @@ public Modifier AddOneOf(bool modifier1, bool modifier2, bool modifier3,
{
if (modifier1)
{
Add(name1);
And(name1);
}
else if (modifier2)
{
Add(name2);
And(name2);
}
else if (modifier3)
{
Add(name3);
And(name3);
}

return this;
Expand Down Expand Up @@ -281,7 +281,7 @@ public override IEnumerable<string> GenerateCssClasses()
/// <returns></returns>
public override string Build()
{
return string.Join(" ", GenerateCssClasses()).Trim();
return string.Join(" ", GenerateCssClasses());
}

// inherit
Expand Down

0 comments on commit 1bab669

Please sign in to comment.