Skip to content

Commit

Permalink
Update lang version and suppress warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
khanage committed Jan 21, 2025
1 parent a04d68d commit 513e795
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 81 deletions.
17 changes: 10 additions & 7 deletions Bearded.Monads.Tests/AsyncApplicativeTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
#pragma warning disable CS0618
using static Bearded.Monads.AsyncApplicative;

namespace Bearded.Monads.Tests
Expand Down Expand Up @@ -100,8 +97,14 @@ await Asynquence(Task.Delay(millisecondsPerCall).ContinueWith(_ => 1))

sw.Stop();

Assert.True(sw.ElapsedMilliseconds >= millisecondsPerCall, $"Took at least {millisecondsPerCall}ms");
Assert.True(sw.ElapsedMilliseconds <= millisecondsPerCall * 2, $"Took no longer than {millisecondsPerCall * 2}ms");
Assert.True(
sw.ElapsedMilliseconds >= millisecondsPerCall,
$"Took at least {millisecondsPerCall}ms"
);
Assert.True(
sw.ElapsedMilliseconds <= millisecondsPerCall * 2,
$"Took no longer than {millisecondsPerCall * 2}ms"
);
}

[Fact]
Expand Down
4 changes: 2 additions & 2 deletions Bearded.Monads.Tests/Bearded.Monads.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Bearded.Monads.Tests</RootNamespace>
<AssemblyName>Bearded.Monads.Tests</AssemblyName>
<LangVersion>latest</LangVersion>
Expand All @@ -19,4 +19,4 @@
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
</Project>
</Project>
85 changes: 51 additions & 34 deletions Bearded.Monads.Tests/OptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
using System.Threading.Tasks;
using Bearded.Monads;
using Xunit;
using Xunit.Sdk;

#pragma warning disable CS0618
namespace Bearded.Monads.Tests
{
public class OptionTests
Expand All @@ -26,7 +26,12 @@ public void None()
var option = Option<int>.None;

Assert.False(option.IsSome);
Assert.Throws<InvalidOperationException>(delegate { var _ = option.ForceValue(); });
Assert.Throws<InvalidOperationException>(
delegate
{
var _ = option.ForceValue();
}
);
}

[Fact]
Expand Down Expand Up @@ -330,32 +335,23 @@ public void TrueOperatorOnNone()
[Fact]
public void SelectManyBothSome()
{
var result =
from a in Option.Return(21)
from b in Option.Return(21)
select a + b;
var result = from a in Option.Return(21) from b in Option.Return(21) select a + b;

Assert.Equal(42, result.ForceValue());
}

[Fact]
public void SelectManyFirstNone()
{
var result =
from a in Option<int>.None
from b in Option.Return(21)
select a + b;
var result = from a in Option<int>.None from b in Option.Return(21) select a + b;

Assert.False(result.IsSome);
}

[Fact]
public void SelectManySecondNone()
{
var result =
from a in Option.Return(21)
from b in Option<int>.None
select a + b;
var result = from a in Option.Return(21) from b in Option<int>.None select a + b;

Assert.False(result.IsSome);
}
Expand Down Expand Up @@ -489,14 +485,17 @@ public void TryGetValueForMissingKey()

Assert.True(!result.IsSome);
}

[Fact]
public void TryGetValuesForPresentKey()
{
var key = "hello";
var expectedValues = new[] { 11, 42 };

var lookup = new[] { Tuple.Create("hello", 11), Tuple.Create("hello", 42) }
.ToLookup(t => t.Item1, t => t.Item2);
var lookup = new[] { Tuple.Create("hello", 11), Tuple.Create("hello", 42) }.ToLookup(
t => t.Item1,
t => t.Item2
);

var result = lookup.MaybeGetValues(key);

Expand All @@ -509,8 +508,11 @@ public void TryGetValuesForMissingKey()
{
var key = "hello";

var lookup = new[] { Tuple.Create("irrelevant", 11), Tuple.Create("irrelevant", 42) }
.ToLookup(t => t.Item1, t => t.Item2);
var lookup = new[]
{
Tuple.Create("irrelevant", 11),
Tuple.Create("irrelevant", 42),
}.ToLookup(t => t.Item1, t => t.Item2);

var result = lookup.MaybeGetValues(key);

Expand Down Expand Up @@ -542,7 +544,9 @@ public void AggregateWithOptions()
{
var list = new[] { 1.AsOption(), 2.AsOption(), 3.AsOption() };

var result = list.AggregateOrNone((total, current) => total.SelectMany(t => current.Map(c => t + c)));
var result = list.AggregateOrNone(
(total, current) => total.SelectMany(t => current.Map(c => t + c))
);

Assert.Equal(6, result.ForceValue());
}
Expand All @@ -552,7 +556,9 @@ public void AggregateWithOptionsAndNone()
{
var list = new[] { 1.AsOption(), Option<int>.None, 3.AsOption() };

var result = list.AggregateOrNone((total, current) => total.SelectMany(t => current.Map(c => t + c)));
var result = list.AggregateOrNone(
(total, current) => total.SelectMany(t => current.Map(c => t + c))
);

Assert.True(!result.IsSome);
}
Expand All @@ -576,12 +582,16 @@ public void AggregateWithMissingMembersAndSeed()

Assert.True(!result.IsSome);
}

[Fact]
public void AggregateWithOptionsAndSeed()
{
var list = new[] { 1.AsOption(), 2.AsOption(), 3.AsOption() };

var result = list.AggregateOrNone(10.AsOption(), (total, current) => total.SelectMany(t => current.Map(c => t + c)));
var result = list.AggregateOrNone(
10.AsOption(),
(total, current) => total.SelectMany(t => current.Map(c => t + c))
);

Assert.Equal(16, result.ForceValue());
}
Expand All @@ -591,10 +601,14 @@ public void AggregateWithOptionsAndNoneAndSeed()
{
var list = new[] { 1.AsOption(), Option<int>.None, 3.AsOption() };

var result = list.AggregateOrNone(10.AsOption(), (total, current) => total.SelectMany(t => current.Map(c => t + c)));
var result = list.AggregateOrNone(
10.AsOption(),
(total, current) => total.SelectMany(t => current.Map(c => t + c))
);

Assert.True(!result.IsSome);
}

[Fact]
public void ImplicitCastBool()
{
Expand Down Expand Up @@ -628,12 +642,18 @@ public void PipeOperatorShortCircuit()
{
var some = new object().AsOption();
var none = Option<object>.None;
Func<Option<object>> fail = () => { throw new Exception(); };
Func<Option<object>> fail = () =>
{
throw new Exception();
};

// Does not throw doesn't have
// an explicit operator
var result = some | fail;
Assert.Throws<Exception>(() => { var bad = none | fail; });
Assert.Throws<Exception>(() =>
{
var bad = none | fail;
});
}

[Fact]
Expand Down Expand Up @@ -686,8 +706,7 @@ public async Task MapAsyncWithSome()
{
var some = 0.AsOption();

var result = await some.MapAsync(
async x => await Task.FromResult(x + 1));
var result = await some.MapAsync(async x => await Task.FromResult(x + 1));

Assert.Equal(1.AsOption(), result);
}
Expand All @@ -697,8 +716,7 @@ public async Task MapAsyncWithNone()
{
var none = Option<int>.None;

var result = await none.MapAsync(
async x => await Task.FromResult(x + 1));
var result = await none.MapAsync(async x => await Task.FromResult(x + 1));

Assert.Equal(Option<int>.None, result);
}
Expand All @@ -708,8 +726,7 @@ public async Task MapAsyncWithNull()
{
Option<object> none = null;

var result = await none.MapAsync(
async x => await Task.FromResult(0));
var result = await none.MapAsync(async x => await Task.FromResult(0));

Assert.Equal(Option<int>.None, result);
}
Expand Down Expand Up @@ -770,17 +787,17 @@ public void Sequence_NotOk()
enum TestEnum
{
One = 1,
Two = 2
Two = 2,
}

[Fact]
public void EnumFromInt_Ok()
{
var knownIndex = 1;
var expected = TestEnum.One;

var actual = knownIndex.MaybeEnum<TestEnum>();

Assert.Equal(expected, actual);
}

Expand All @@ -791,7 +808,7 @@ public void EnumFromInt_NotOk()
var expected = Option<TestEnum>.None;

var actual = knownBadIndex.MaybeEnum<TestEnum>();

Assert.Equal(expected, actual);
}

Expand Down
Loading

0 comments on commit 513e795

Please sign in to comment.