Skip to content

Commit

Permalink
Merge pull request #29 from ildoc/fix/drop_fa
Browse files Browse the repository at this point in the history
Replaced FluentAssertions with Shouldly
  • Loading branch information
ildoc authored Jan 15, 2025
2 parents 5e4b752 + a3099bd commit b42badf
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 96 deletions.
6 changes: 4 additions & 2 deletions src/Extensions/RandomExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;

namespace Extensions
{
Expand All @@ -10,9 +12,9 @@ public static class RandomExtensions
public static DateTime NextDatetime(this Random random, in DateTime from, in DateTime to) =>
from + new TimeSpan((long)(random.NextDouble() * (to - from).Ticks));

public static T OneOf<T>(this Random random, params T[] values)
public static T OneOf<T>(this Random random, IEnumerable<T> values)
{
return values[random.Next(values.Length)];
return values.ElementAt(random.Next(values.Count()));
}

public static string NextHexColor(this Random random)
Expand Down
9 changes: 5 additions & 4 deletions tests/Extensions.Tests/CharExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Xunit;
using Shouldly;
using Xunit;

namespace Extensions.Tests
{
Expand All @@ -10,7 +11,7 @@ public class CharExtensionsTests
[InlineData(' ', ' ')]
public void ShouldTransformToUppercase(char value, char expected)
{
Assert.Equal(expected, value.ToUpper());
value.ToUpper().ShouldBe(expected);
}

[Theory]
Expand All @@ -19,7 +20,7 @@ public void ShouldTransformToUppercase(char value, char expected)
[InlineData(' ', ' ')]
public void ShouldTransformToLowercase(char value, char expected)
{
Assert.Equal(expected, value.ToLower());
value.ToLower().ShouldBe(expected);
}

[Theory]
Expand All @@ -28,7 +29,7 @@ public void ShouldTransformToLowercase(char value, char expected)
[InlineData(' ', false)]
public void ShouldReturnIfIsAlphabet(char value, bool expected)
{
Assert.Equal(expected, value.IsAsciiAlphabetLetter());
value.IsAsciiAlphabetLetter().ShouldBe(expected);
}
}
}
10 changes: 5 additions & 5 deletions tests/Extensions.Tests/DateTimeExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using FluentAssertions;
using Shouldly;
using Xunit;

namespace Extensions.Tests
Expand All @@ -11,23 +11,23 @@ public void DayStartMinusOneMillisecondIsPreviousDay()
{
var date = DateTime.Now;

Assert.True(date.DayStart().AddMilliseconds(-1).Date == date.AddDays(-1).Date);
date.DayStart().AddMilliseconds(-1).Date.ShouldBe(date.AddDays(-1).Date);
}

[Fact]
public void DayEndPlusOneMillisecondIsNextDay()
{
var date = DateTime.Now;

Assert.True(date.DayEnd().AddMilliseconds(1).Date == date.AddDays(1).Date);
date.DayEnd().AddMilliseconds(1).Date.ShouldBe(date.AddDays(1).Date);
}

[Fact]
public void ShouldCalculateAgeOnSpecificDate()
{
var birthday = new DateTime(1990, 6, 22);

birthday.ToAgeAtDate(new DateTime(2022, 4, 12)).Should().Be(31);
birthday.ToAgeAtDate(new DateTime(2022, 4, 12)).ShouldBe(31);
}
}

Expand All @@ -43,7 +43,7 @@ public class ObjectExtensionsTests
[InlineData(-1.3, true)]
public void ShouldCheckIfNumber(object o, bool expected)
{
o.IsNumber().Should().Be(expected);
o.IsNumber().ShouldBe(expected);
}
}
}
24 changes: 12 additions & 12 deletions tests/Extensions.Tests/DictionaryExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;
using FluentAssertions;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using Shouldly;
using Xunit;

namespace Extensions.Tests
Expand All @@ -16,42 +16,42 @@ public void ShouldRunActionForEachItemWhileRemoving()

_dict.RemoveAllWithAction(x => x.Key >= 3, x => str += x.Value);

_dict.Should().HaveCount(2);
str.Should().Be("cccddd");
_dict.Count.ShouldBe(2);
str.ShouldBe("cccddd");
}

[Fact]
public void ShouldRemoveItemsWhere()
{
_dict.RemoveAll(x => x.Key >= 3);
_dict.Count.Should().Be(2);
_dict.Count.ShouldBe(2);
}

[Fact]
public void ShouldTryGetValueIfExists()
{
_dict.GetValue(2).Should().Be("bbb");
_dict.GetValue(2).ShouldBe("bbb");
}

[Fact]
public void ShouldThrowIfValueDontExists()
{
var message = Assert.Throws<KeyNotFoundException>(() => _dict.GetValue(5)).Message;
message.Should().Be("'5' not found in Dictionary");
var message = Should.Throw<KeyNotFoundException>(() => _dict.GetValue(5)).Message;
message.ShouldBe("'5' not found in Dictionary");
}

[Fact]
public void ShouldReturnDefaultIfValueDontExists()
{
_dict.GetValueOrDefault(4).Should().Be("ddd");
_dict.GetValueOrDefault(5).Should().Be(default);
_dict.GetValueOrDefault(4).ShouldBe("ddd");
_dict.GetValueOrDefault(5).ShouldBe(default);
}

[Fact]
public void ShouldSwapKeyValues()
{
var outputdict = new Dictionary<string, int> { { "aaa", 1 }, { "bbb", 2 }, { "ccc", 3 }, { "ddd", 4 } };
_dict.SwapKeyValue().Should().BeEquivalentTo(outputdict);
_dict.SwapKeyValue().ShouldBeEquivalentTo(outputdict);
}

[Fact]
Expand All @@ -60,7 +60,7 @@ public void ShouldRunActionForEachItem()
var str = "";
_dict.Each(x => str += x.Value);

str.Should().Be("aaabbbcccddd");
str.ShouldBe("aaabbbcccddd");
}

[Fact]
Expand All @@ -73,7 +73,7 @@ public void ShouldConvertToDictionary()
new KeyValuePair<int, string>(4,"ddd"),
};

list.ToDictionary().Should().BeEquivalentTo(_dict);
list.ToDictionary().ShouldBeEquivalentTo(_dict);
}
}
}
4 changes: 2 additions & 2 deletions tests/Extensions.Tests/EnumExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.ComponentModel;
using FluentAssertions;
using Shouldly;
using Xunit;

namespace Extensions.Tests
Expand All @@ -20,7 +20,7 @@ public enum TestEnum
[InlineData(TestEnum.ValueB, "ValueB")]
public void ShouldGetDescription(TestEnum testEnumValue, string expected)
{
testEnumValue.GetDescription().Should().Be(expected);
testEnumValue.GetDescription().ShouldBe(expected);
}
}
}
2 changes: 1 addition & 1 deletion tests/Extensions.Tests/Extensions.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="Shouldly" Version="4.2.1" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1">
<PrivateAssets>all</PrivateAssets>
Expand Down
36 changes: 19 additions & 17 deletions tests/Extensions.Tests/GenericExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
using FluentAssertions;
using Shouldly;
using Xunit;

namespace Extensions.Tests
Expand All @@ -12,7 +12,7 @@ public class GenericExtensionsTests
[InlineData(false, false)]
public void BoolShouldResolveAsBool(bool value, bool expected)
{
value.ToBool().Should().Be(expected);
value.ToBool().ShouldBe(expected);
}

[Theory]
Expand All @@ -22,7 +22,7 @@ public void BoolShouldResolveAsBool(bool value, bool expected)
[InlineData(-7, true)]
public void IntShouldResolveAsBool(int? value, bool expected)
{
value.ToBool().Should().Be(expected);
value.ToBool().ShouldBe(expected);
}

[Theory]
Expand All @@ -32,7 +32,7 @@ public void IntShouldResolveAsBool(int? value, bool expected)
[InlineData("asd", false)]
public void StringShouldResolveAsBool(string value, bool expected)
{
value.ToBool().Should().Be(expected);
value.ToBool().ShouldBe(expected);
}

[Theory]
Expand All @@ -42,7 +42,7 @@ public void StringShouldResolveAsBool(string value, bool expected)
[InlineData(-7f, true)]
public void FloatShouldResolveAsBool(float? value, bool expected)
{
value.ToBool().Should().Be(expected);
value.ToBool().ShouldBe(expected);
}

[Theory]
Expand All @@ -52,37 +52,37 @@ public void FloatShouldResolveAsBool(float? value, bool expected)
[InlineData(-7.0, true)]
public void DoubleShouldResolveAsBool(double? value, bool expected)
{
value.ToBool().Should().Be(expected);
value.ToBool().ShouldBe(expected);
}

[Fact]
public void ObjectShouldResolveAsTrue()
{
new { Id = 7 }.ToBool().Should().BeTrue();
new { Id = 7 }.ToBool().ShouldBeTrue();
}

[Fact]
public void NullShouldResolveAsFlase()
{
object value = null;

value.ToBool().Should().BeFalse();
value.ToBool().ShouldBeFalse();
}

[Theory]
[InlineData('b', "abcd", true)]
[InlineData('f', "abcd", false)]
public void ShouldCheckIfValueIsInString(char value, string fullString, bool expected)
{
value.IsIn(fullString).Should().Be(expected);
value.IsIn(fullString).ShouldBe(expected);
}

[Theory]
[InlineData('b', "abcd", false)]
[InlineData('f', "abcd", true)]
public void ShouldCheckIfValueIsNotInString(char value, string fullString, bool expected)
{
value.IsNotIn(fullString).Should().Be(expected);
value.IsNotIn(fullString).ShouldBe(expected);
}

[Fact]
Expand All @@ -91,8 +91,8 @@ public void ShouldCheckIfStringIsInStrings()
var value = "test";
var strings = new List<string> { "this", "is", "a", "test" };

value.IsIn(strings).Should().BeTrue();
value.IsNotIn(strings).Should().BeFalse();
value.IsIn(strings).ShouldBeTrue();
value.IsNotIn(strings).ShouldBeFalse();
}

[Fact]
Expand All @@ -101,8 +101,8 @@ public void ShouldCheckIfStringIsNotInStrings()
var value = "not";
var strings = new List<string> { "this", "is", "a", "test" };

value.IsIn(strings).Should().BeFalse();
value.IsNotIn(strings).Should().BeTrue();
value.IsIn(strings).ShouldBeFalse();
value.IsNotIn(strings).ShouldBeTrue();
}

[Fact]
Expand All @@ -117,7 +117,9 @@ public void ShouldCastFromAnonymousObject()

var casted = anon.AnonymousCastTo<TestClass>();

casted.Should().BeEquivalentTo(anon);
casted.Id.ShouldBe(anon.Id);
casted.Description.ShouldBe(anon.Description);
casted.TimeStamp.ShouldBe(anon.TimeStamp);
}

[Fact]
Expand All @@ -132,7 +134,7 @@ public void ShouldClone()

var b = a.Clone();

b.Should().BeEquivalentTo(a);
b.ShouldBeEquivalentTo(a);
}

[Theory]
Expand All @@ -143,7 +145,7 @@ public void ShouldClone()
[InlineData("asd", "asd", true)]
public void ShouldReturnEqual(object a, object b, bool expected)
{
a.IsEqualTo(b).Should().Be(expected);
a.IsEqualTo(b).ShouldBe(expected);
}

private class TestClass
Expand Down
8 changes: 4 additions & 4 deletions tests/Extensions.Tests/ICollectionExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Collections.Generic;
using FluentAssertions;
using Shouldly;
using Xunit;

namespace Extensions.Tests
Expand All @@ -15,8 +15,8 @@ public void AddIf()
list.AddIf(_ => false, "Buzz"); // Doesn't add "Buzz" value


list.Should().Contain("Fizz");
list.Should().NotContain("Buzz");
list.ShouldContain("Fizz");
list.ShouldNotContain("Buzz");
}

[Fact]
Expand All @@ -30,7 +30,7 @@ public void AddIfNotContains()
list.AddIfNotContains("FizzExisting"); // Doesn't add "FizzExisting" value, the Collection already contains it.

// Unit Test
list.Should().HaveCount(2);
list.Count.ShouldBe(2);
}
}
}
6 changes: 3 additions & 3 deletions tests/Extensions.Tests/IEnumerableExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Collections.Generic;
using FluentAssertions;
using Shouldly;
using Xunit;

namespace Extensions.Tests
Expand All @@ -10,14 +10,14 @@ public class IEnumerableExtensionsTests
public void ShouldJoinListString()
{
var list = new List<string> { "questa", "è", "una", "lista" };
list.Join(", ").Should().Be("questa, è, una, lista");
list.Join(", ").ShouldBe("questa, è, una, lista");
}

[Fact]
public void ShouldJoinListChar()
{
var list = new List<string> { "questa", "è", "una", "lista" };
list.Join(',').Should().Be("questa,è,una,lista");
list.Join(',').ShouldBe("questa,è,una,lista");
}
}
}
Loading

0 comments on commit b42badf

Please sign in to comment.