Skip to content

Commit 2d51f09

Browse files
authored
housekeeping: add test coverage (#2)
1 parent 9f31f36 commit 2d51f09

7 files changed

+184
-56
lines changed

global.json

-7
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (c) 2019-2022 ReactiveUI Association Incorporated. All rights reserved.
2+
// ReactiveUI Association Incorporated licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for full license information.
4+
5+
using System;
6+
using System.Reactive.Disposables;
7+
using FluentAssertions;
8+
using Xunit;
9+
10+
namespace ReactiveMarbles.Extensions.Tests;
11+
12+
/// <summary>
13+
/// Tests disposable extensions.
14+
/// </summary>
15+
public class DisposableExtensionsTests
16+
{
17+
/// <summary>
18+
/// Tests DisposeWith returns a disposable.
19+
/// </summary>
20+
[Fact]
21+
public void GivenNull_WhenDisposeWith_ThenExceptionThrown()
22+
{
23+
// Given
24+
var sut = Disposable.Create(() => { });
25+
26+
// When
27+
var result = Record.Exception(() => sut.DisposeWith(null));
28+
29+
// Then
30+
result
31+
.Should()
32+
.BeOfType<ArgumentNullException>();
33+
}
34+
35+
/// <summary>
36+
/// Tests DisposeWith disposes the underlying disposable.
37+
/// </summary>
38+
[Fact]
39+
public void GivenDisposable_WhenDisposeWith_ThenDisposed()
40+
{
41+
// Given
42+
var sut = new CompositeDisposable();
43+
var compositeDisposable = new CompositeDisposable();
44+
sut.DisposeWith(compositeDisposable);
45+
46+
// When
47+
compositeDisposable.Dispose();
48+
49+
// Then
50+
sut.IsDisposed
51+
.Should()
52+
.BeTrue();
53+
}
54+
55+
/// <summary>
56+
/// Tests DisposeWith returns the original disposable.
57+
/// </summary>
58+
[Fact]
59+
public void GivenDisposable_WhenDisposeWith_ThenReturnsDisposable()
60+
{
61+
// Given, When
62+
var sut = new CompositeDisposable();
63+
var compositeDisposable = new CompositeDisposable();
64+
var result = sut.DisposeWith(compositeDisposable);
65+
66+
// Then
67+
sut.Should()
68+
.BeEquivalentTo(result);
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (c) 2019-2022 ReactiveUI Association Incorporated. All rights reserved.
2+
// ReactiveUI Association Incorporated licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for full license information.
4+
5+
using System;
6+
using System.Reactive;
7+
using System.Reactive.Linq;
8+
using FluentAssertions;
9+
using Xunit;
10+
11+
namespace ReactiveMarbles.Extensions.Tests;
12+
13+
/// <summary>
14+
/// Tests Reactive Extensions.
15+
/// </summary>
16+
public class ReactiveExtensionsTests
17+
{
18+
/// <summary>
19+
/// Tests the WhereIsNotNull extension.
20+
/// </summary>
21+
[Fact]
22+
public void GivenNull_WhenWhereIsNotNull_ThenNoNotification()
23+
{
24+
// Given, When
25+
bool? result = null;
26+
using var disposable = Observable.Return<bool?>(null).WhereIsNotNull().Subscribe(x => result = x);
27+
28+
// Then
29+
result
30+
.Should()
31+
.BeNull();
32+
}
33+
34+
/// <summary>
35+
/// Tests the WhereIsNotNull extension.
36+
/// </summary>
37+
[Fact]
38+
public void GivenValue_WhenWhereIsNotNull_ThenNotification()
39+
{
40+
// Given, When
41+
bool? result = null;
42+
using var disposable = Observable.Return<bool?>(false).WhereIsNotNull().Subscribe(x => result = x);
43+
44+
// Then
45+
result
46+
.Should()
47+
.BeFalse();
48+
}
49+
50+
/// <summary>
51+
/// Tests the AsSignal extension.
52+
/// </summary>
53+
[Fact]
54+
public void GivenObservable_WhenAsSignal_ThenNotifiesUnit()
55+
{
56+
// Given, When
57+
Unit? result = null;
58+
using var disposable = Observable.Return<bool?>(false).AsSignal().Subscribe(x => result = x);
59+
60+
// Then
61+
result
62+
.Should()
63+
.Be(Unit.Default);
64+
}
65+
}

src/ReactiveMarbles.Extensions.Tests/ReactiveMarbles.Extensions.Tests.csproj

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.1</TargetFramework>
5-
6-
<IsPackable>false</IsPackable>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<IsPackable>false</IsPackable>
76
</PropertyGroup>
87

98
<ItemGroup>
@@ -20,5 +19,9 @@
2019
<PrivateAssets>all</PrivateAssets>
2120
</PackageReference>
2221
</ItemGroup>
22+
23+
<ItemGroup>
24+
<ProjectReference Include="..\ReactiveMarbles.Extensions\ReactiveMarbles.Extensions.csproj" />
25+
</ItemGroup>
2326

2427
</Project>

src/ReactiveMarbles.Extensions/DisposableExtensions.cs

+18-19
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,29 @@
55
using System;
66
using System.Reactive.Disposables;
77

8-
namespace ReactiveMarbles.Extensions
8+
namespace ReactiveMarbles.Extensions;
9+
10+
/// <summary>
11+
/// Extension methods associated with the IDisposable interface.
12+
/// </summary>
13+
public static class DisposableExtensions
914
{
1015
/// <summary>
11-
/// Extension methods associated with the IDisposable interface.
16+
/// Ensures the provided disposable is disposed with the specified <see cref="CompositeDisposable"/>.
1217
/// </summary>
13-
public static class DisposableExtensions
18+
/// <typeparam name="T"> The type of the disposable. </typeparam>
19+
/// <param name="item"> The disposable we are going to want to be disposed by the CompositeDisposable. </param>
20+
/// <param name="compositeDisposable"> The <see cref="CompositeDisposable"/> to which <paramref name="item"/> will be added. </param>
21+
/// <returns> The disposable.</returns>
22+
public static T DisposeWith<T>(this T item, CompositeDisposable compositeDisposable)
23+
where T : IDisposable
1424
{
15-
/// <summary>
16-
/// Ensures the provided disposable is disposed with the specified <see cref="CompositeDisposable"/>.
17-
/// </summary>
18-
/// <typeparam name="T"> The type of the disposable. </typeparam>
19-
/// <param name="item"> The disposable we are going to want to be disposed by the CompositeDisposable. </param>
20-
/// <param name="compositeDisposable"> The <see cref="CompositeDisposable"/> to which <paramref name="item"/> will be added. </param>
21-
/// <returns> The disposable.</returns>
22-
public static T DisposeWith<T>(this T item, CompositeDisposable compositeDisposable)
23-
where T : IDisposable
25+
if (compositeDisposable is null)
2426
{
25-
if (compositeDisposable is null)
26-
{
27-
throw new ArgumentNullException(nameof(compositeDisposable));
28-
}
29-
30-
compositeDisposable.Add(item);
31-
return item;
27+
throw new ArgumentNullException(nameof(compositeDisposable));
3228
}
29+
30+
compositeDisposable.Add(item);
31+
return item;
3332
}
3433
}

src/ReactiveMarbles.Extensions/ReactiveExtensions.cs

+24-25
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,32 @@
66
using System.Reactive;
77
using System.Reactive.Linq;
88

9-
namespace ReactiveMarbles.Extensions
9+
namespace ReactiveMarbles.Extensions;
10+
11+
/// <summary>
12+
/// Extension methods for <see cref="System.Reactive"/>.
13+
/// </summary>
14+
public static class ReactiveExtensions
1015
{
1116
/// <summary>
12-
/// Extension methods for <see cref="System.Reactive"/>.
17+
/// Returns only values that are not null.
18+
/// Converts the nullability.
1319
/// </summary>
14-
public static class ReactiveExtensions
15-
{
16-
/// <summary>
17-
/// Returns only values that are not null.
18-
/// Converts the nullability.
19-
/// </summary>
20-
/// <typeparam name="T">The type of value emitted by the observable.</typeparam>
21-
/// <param name="observable">The observable that can contain nulls.</param>
22-
/// <returns>A non nullable version of the observable that only emits valid values.</returns>
23-
public static IObservable<T> WhereIsNotNull<T>(this IObservable<T> observable) =>
24-
observable
25-
.Where(x => x is not null);
20+
/// <typeparam name="T">The type of value emitted by the observable.</typeparam>
21+
/// <param name="observable">The observable that can contain nulls.</param>
22+
/// <returns>A non nullable version of the observable that only emits valid values.</returns>
23+
public static IObservable<T> WhereIsNotNull<T>(this IObservable<T> observable) =>
24+
observable
25+
.Where(x => x is not null);
2626

27-
/// <summary>
28-
/// Will convert an observable so that it's value is ignored and converted into just returning <see cref="Unit"/>.
29-
/// This allows us just to be notified when the observable signals.
30-
/// </summary>
31-
/// <typeparam name="T">The current type of the observable.</typeparam>
32-
/// <param name="observable">The observable to convert.</param>
33-
/// <returns>The converted observable.</returns>
34-
public static IObservable<Unit> AsSignal<T>(this IObservable<T> observable) =>
35-
observable
36-
.Select(_ => Unit.Default);
37-
}
27+
/// <summary>
28+
/// Change the source observable type to <see cref="Unit"/>.
29+
/// This allows us to be notified when the observable emits a value.
30+
/// </summary>
31+
/// <typeparam name="T">The current type of the observable.</typeparam>
32+
/// <param name="observable">The observable to convert.</param>
33+
/// <returns>The signal.</returns>
34+
public static IObservable<Unit> AsSignal<T>(this IObservable<T> observable) =>
35+
observable
36+
.Select(_ => Unit.Default);
3837
}

src/ReactiveMarbles.Extensions/ReactiveMarbles.Extensions.csproj

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.0;</TargetFrameworks>
5-
<LangVersion>preview</LangVersion>
4+
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
65
</PropertyGroup>
76

87
<ItemGroup>

0 commit comments

Comments
 (0)