Skip to content

Commit

Permalink
Fix build errors and test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
rdghm1234 committed Feb 14, 2024
1 parent 450de16 commit cc1e2ae
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public record FluxAsyncCounterState {

public bool IsLoading { get; init; } = false;

public ImmutableArray<int> Histories { get; init; } = ImmutableArray.Create<int>();
public ImmutableArray<int> Histories { get; init; } = [];
}

public record FluxAsyncCounterCommand : Command {
Expand Down
3 changes: 2 additions & 1 deletion src/Memento.Blazor/StateChangedObserver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Components;
using Memento.Blazor.Internal;
using Microsoft.AspNetCore.Components;
using System.Collections.Concurrent;

namespace Memento.Blazor;
Expand Down
12 changes: 10 additions & 2 deletions src/Memento.Core/AbstractStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ public IDisposable Subscribe(IObserver<IStateChangedEventArgs<TState, TMessage>>
if (e is IStateChangedEventArgs<TState, TMessage> e2) {
observer.OnNext(e2);
}
else {
observer.OnNext(new StateChangedEventArgs<TState, TMessage>() {
LastState = State,
Message = e.Message,
State = State,
Sender = this,
});
}
}));
}

Expand Down Expand Up @@ -154,8 +162,8 @@ internal void ComputedAndApplyState(TState state, TMessage? command) {

[MethodImpl(MethodImplOptions.AggressiveInlining)]
(TState?, StateChangedEventArgs<TState, TMessage>?) ComputeNewState() {
var previous = state;
var postState = OnBeforeDispatch(previous, command);
var previous = State;
var postState = OnBeforeDispatch(state, command);

if (MiddlewareHandler.Invoke(postState, command) is TState s) {
var newState = OnAfterDispatch(s, command);
Expand Down
4 changes: 2 additions & 2 deletions src/Memento.ReduxDevTool/Internals/LiftedHistoryContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public void SetStatesToStore(HistoryState? history) {

public async Task CommitAsync() {
var history = CurrentHistory;
_histories = ImmutableArray.Create(history.AsInitial());
_histories = [history.AsInitial()];

await SyncWithPlugin();
_currentCursorId = 0;
Expand All @@ -176,7 +176,7 @@ public async Task CommitAsync() {

public async Task RollbackAsync() {
var history = _histories.First();
_histories = ImmutableArray.Create(history.AsInitial());
_histories = [history.AsInitial()];

await SyncWithPlugin();
_currentCursorId = 0;
Expand Down
16 changes: 8 additions & 8 deletions test/Memento.Test/Core/FluxStoreTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public async Task Command_CouldBeSubscribeCorrectly() {
Assert.NotEqual(e.State, lastState);
Assert.Equal(e.LastState, lastState);
lastState = e.State;
commands.Add(e.Command);
commands.Add(e.Message);
});

await store.CountUpAsync();
Expand Down Expand Up @@ -107,14 +107,14 @@ public async Task Force_ReplaceState() {
await store.CountUpAsync();

Assert.True(commands is [
{ Command: FluxAsyncCounterCommands.BeginLoading },
{ Command: FluxAsyncCounterCommands.Increment },
{ Command: FluxAsyncCounterCommands.EndLoading },
{ Command: FluxAsyncCounterCommands.ModifyCount(1234) },
{ Message: FluxAsyncCounterCommands.BeginLoading },
{ Message: FluxAsyncCounterCommands.Increment },
{ Message: FluxAsyncCounterCommands.EndLoading },
{ Message: FluxAsyncCounterCommands.ModifyCount(1234) },
{ State: FluxAsyncCounterState { Count: 5678 }, StateChangeType: StateChangeType.ForceReplaced },
{ Command: FluxAsyncCounterCommands.BeginLoading },
{ Command: FluxAsyncCounterCommands.Increment },
{ Command: FluxAsyncCounterCommands.EndLoading },
{ Message: FluxAsyncCounterCommands.BeginLoading },
{ Message: FluxAsyncCounterCommands.Increment },
{ Message: FluxAsyncCounterCommands.EndLoading },
]);
}

Expand Down
2 changes: 1 addition & 1 deletion test/Memento.Test/Core/Mock/AsyncCounterStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Memento.Test.Core.Mock;
// Define state to manage in store
public record AsyncCounterState {
public int Count { get; init; } = 0;
public ImmutableArray<int> History { get; init; } = ImmutableArray.Create<int>();
public ImmutableArray<int> History { get; init; } = [];
public bool IsLoading { get; init; } = false;
}

Expand Down
4 changes: 2 additions & 2 deletions test/Memento.Test/Core/Mock/FluxAsyncCounterStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Memento.Test.Core.Mock;
// Define state to manage in store
public record FluxAsyncCounterState {
public int Count { get; init; } = 0;
public ImmutableArray<int> History { get; init; } = ImmutableArray.Create<int>();
public ImmutableArray<int> History { get; init; } = [];
public bool IsLoading { get; init; } = false;
}

Expand Down Expand Up @@ -45,7 +45,7 @@ static FluxAsyncCounterState Reducer(FluxAsyncCounterState state, FluxAsyncCount
Count = payload.Value,
History = state.History.Add(payload.Value),
},
_ => throw new CommandNotHandledException(command),
_ => throw new CommandNotHandledException<FluxAsyncCounterCommands>(command),
};

static FluxAsyncCounterState HandleIncrement(FluxAsyncCounterState state) {
Expand Down
4 changes: 2 additions & 2 deletions test/Memento.Test/Core/Mock/MockMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class MockMiddlewareHandler : MiddlewareHandler {

public override RootState? HandleProviderDispatch(
RootState? state,
IStateChangedEventArgs<object, Command> e,
IStateChangedEventArgs<object, object> e,
NextProviderMiddlewareCallback next
) {
ProviderDispatchCalledCount++;
Expand All @@ -25,7 +25,7 @@ NextProviderMiddlewareCallback next

public override object? HandleStoreDispatch(
object? state,
Command command,
object? command,
NextStoreMiddlewareCallback next
) {
HandleStoreDispatchCalledCount++;
Expand Down
2 changes: 1 addition & 1 deletion test/Memento.Test/Core/Mock/RedoUndoTodoStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Memento.Sample.Blazor.Stores;

public record RedoUndoTodoState {
public ImmutableArray<Todo> Todos { get; init; } = ImmutableArray.Create<Todo>();
public ImmutableArray<Todo> Todos { get; init; } = [];

public bool IsLoading { get; init; }
}
Expand Down
7 changes: 3 additions & 4 deletions test/Memento.Test/Core/ProviderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ public async Task Test() {

await mementoProvider.InitializeAsync();

await Assert.ThrowsAnyAsync<InvalidOperationException>(async () => {
await mementoProvider.InitializeAsync();
});
// Ensure root state is correct
await mementoProvider.InitializeAsync();

store.CountUp();
store.CountUp();
Expand All @@ -43,7 +42,7 @@ await Assert.ThrowsAnyAsync<InvalidOperationException>(async () => {
var expected = JsonSerializer.Serialize(new {
AsyncCounterStore = new AsyncCounterState {
Count = 5,
History = ImmutableArray.Create(1, 2, 3, 4, 5),
History = [1, 2, 3, 4, 5],
}
});
var actual = JsonSerializer.Serialize(root.AsDictionary());
Expand Down
70 changes: 36 additions & 34 deletions test/Memento.Test/Core/StoreTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using FluentAssertions;
using Memento.Core;
using Memento.Test.Core.Mock;
using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities;
using System.Collections.Concurrent;
using System.Reflection;

Expand Down Expand Up @@ -55,74 +56,76 @@ public async Task Ensure_CanChangeStateAsync(int count) {
public async Task Command_CouldBeSubscribeCorrectly() {
var store = new AsyncCounterStore();

var commands = new List<Command?>();
var messages = new List<object?>();
var lastState = store.State;
using var subscription = store.Subscribe(e => {
Assert.Equal(e.Sender, store);
Assert.NotEqual(e.State, lastState);
Assert.Equal(e.LastState, lastState);
lastState = e.State;
commands.Add(e.Command);
messages.Add(e.Message);
});

await store.CountUpAsync();
store.SetCount(1234);
await store.CountUpAsync();

Assert.True(commands is [
Command.StateHasChanged,
Command.StateHasChanged,
Command.StateHasChanged,
Command.StateHasChanged,
Command.StateHasChanged,
Command.StateHasChanged,
Command.StateHasChanged,
Assert.True(messages is [
null,
null,
null,
null,
null,
null,
null,
]);
}

[Fact]
public async Task Force_ReplaceState() {
var store = new AsyncCounterStore();

var commands = new List<IStateChangedEventArgs<object, Command>>();
var events = new List<IStateChangedEventArgs<object, object>>();

var lastState = store.State;
using var subscription = store.Subscribe(e => {
Assert.Equal(e.Sender, store);
Assert.NotEqual(e.State, lastState);
Assert.Equal(e.LastState, lastState);
lastState = e.State;
commands.Add(e);
events.Add(e);
});

await store.CountUpAsync();
store.SetCount(1234);
if (store is IStore<object, Command> iStore) {
if (store is IStore<object, object> iStore) {
iStore.SetStateForce(store.State with {
Count = 5678
});
}

await store.CountUpAsync();

Assert.True(commands is [
{ Command: Command.StateHasChanged },
{ Command: Command.StateHasChanged },
{ Command: Command.StateHasChanged },
{ Command: Command.StateHasChanged },
{ Command: null, State: AsyncCounterState { Count: 5678 }, StateChangeType: StateChangeType.ForceReplaced },
{ Command: Command.StateHasChanged },
{ Command: Command.StateHasChanged },
{ Command: Command.StateHasChanged },
Assert.True(events is [
{ Message: null },
{ Message: null },
{ Message: null },
{ Message: null },
{ Message: null, State: AsyncCounterState { Count: 5678 }, StateChangeType: StateChangeType.ForceReplaced },
{ Message: null },
{ Message: null },
{ Message: null },
]);
}

[Fact]
public void Ensure_StateHasChangedInvoked() {
var store = new AsyncCounterStore();
var commands = new List<IStateChangedEventArgs<object, Command>>();
var commands = new List<IStateChangedEventArgs<object, object>>();
var lastState = store.State;
using var subscription = store.Subscribe(e => commands.Add(e));
using var subscription = store.Subscribe(e => {
commands.Add(e);
});

store.StateHasChanged();
store.StateHasChanged();
Expand All @@ -144,16 +147,15 @@ public void Ensure_StateHasChangedInvoked() {
[Fact]
public void Ensure_Observable() {
var store = new AsyncCounterStore();
var observers = (store.GetType()
.BaseType
?.BaseType
?.BaseType
?.GetField(
"_observers",
BindingFlags.NonPublic | BindingFlags.Instance
)
?.GetValue(store) as IDictionary<Guid, IObserver<IStateChangedEventArgs<AsyncCounterState, Command.StateHasChanged<AsyncCounterState, string>>>>
) ?? throw new Exception("_observers is not found.");
var observers = (
store.GetType()
.BaseType
?.BaseType
?.BaseType
?.BaseType
?.GetField("_observers", BindingFlags.NonPublic | BindingFlags.Instance)
?.GetValue(store) as ConcurrentDictionary<Guid, IObserver<IStateChangedEventArgs<string>>>
) ?? throw new Exception("_observers is not found.");

var disposables = new BlockingCollection<IDisposable>();
Parallel.For(0, 10000, i => {
Expand Down

0 comments on commit cc1e2ae

Please sign in to comment.