|
|
|
@ -2,9 +2,8 @@
|
|
|
|
|
using System.Text.Json.Serialization; |
|
|
|
|
using CommunityToolkit.Mvvm.ComponentModel; |
|
|
|
|
using CommunityToolkit.Mvvm.Input; |
|
|
|
|
using Moq; |
|
|
|
|
using NSubstitute; |
|
|
|
|
using StabilityMatrix.Avalonia.Models; |
|
|
|
|
using StabilityMatrix.Avalonia.ViewModels; |
|
|
|
|
using StabilityMatrix.Avalonia.ViewModels.Base; |
|
|
|
|
|
|
|
|
|
#pragma warning disable CS0657 // Not a valid attribute location for this declaration |
|
|
|
@ -90,11 +89,7 @@ public class LoadableViewModelBaseTests
|
|
|
|
|
public void TestSaveStateToJsonObject_Observable() |
|
|
|
|
{ |
|
|
|
|
// Mvvm ObservableProperty should be serialized. |
|
|
|
|
var vm = new TestLoadableViewModelObservable |
|
|
|
|
{ |
|
|
|
|
Title = "abc", |
|
|
|
|
Id = 123, |
|
|
|
|
}; |
|
|
|
|
var vm = new TestLoadableViewModelObservable { Title = "abc", Id = 123, }; |
|
|
|
|
var state = vm.SaveStateToJsonObject(); |
|
|
|
|
|
|
|
|
|
// Title should be ignored since it has [JsonIgnore] |
|
|
|
@ -112,12 +107,9 @@ public class LoadableViewModelBaseTests
|
|
|
|
|
// SaveStateToJsonObject method. |
|
|
|
|
|
|
|
|
|
// Make a mock IJsonLoadableState |
|
|
|
|
var mockState = new Mock<IJsonLoadableState>(); |
|
|
|
|
var mockState = Substitute.For<IJsonLoadableState>(); |
|
|
|
|
|
|
|
|
|
var vm = new TestLoadableViewModelNestedInterface |
|
|
|
|
{ |
|
|
|
|
NestedState = mockState.Object |
|
|
|
|
}; |
|
|
|
|
var vm = new TestLoadableViewModelNestedInterface { NestedState = mockState }; |
|
|
|
|
|
|
|
|
|
// Serialize |
|
|
|
|
var state = vm.SaveStateToJsonObject(); |
|
|
|
@ -126,7 +118,7 @@ public class LoadableViewModelBaseTests
|
|
|
|
|
Assert.AreEqual(1, state.Count); |
|
|
|
|
|
|
|
|
|
// Check that SaveStateToJsonObject was called |
|
|
|
|
mockState.Verify(x => x.SaveStateToJsonObject(), Times.Once); |
|
|
|
|
mockState.Received().SaveStateToJsonObject(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
[TestMethod] |
|
|
|
@ -164,10 +156,7 @@ public class LoadableViewModelBaseTests
|
|
|
|
|
Ignored = 456, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
var vm = new TestLoadableViewModelNested |
|
|
|
|
{ |
|
|
|
|
NestedState = nested |
|
|
|
|
}; |
|
|
|
|
var vm = new TestLoadableViewModelNested { NestedState = nested }; |
|
|
|
|
|
|
|
|
|
var state = vm.SaveStateToJsonObject(); |
|
|
|
|
|
|
|
|
@ -178,7 +167,7 @@ public class LoadableViewModelBaseTests
|
|
|
|
|
// Check nested state was loaded |
|
|
|
|
Assert.IsNotNull(vm2.NestedState); |
|
|
|
|
|
|
|
|
|
var loadedNested = (TestLoadableViewModel) vm2.NestedState; |
|
|
|
|
var loadedNested = (TestLoadableViewModel)vm2.NestedState; |
|
|
|
|
Assert.AreEqual("abc", loadedNested.Included); |
|
|
|
|
Assert.AreEqual(123, loadedNested.Id); |
|
|
|
|
Assert.AreEqual(0, loadedNested.Ignored); |
|
|
|
@ -195,10 +184,7 @@ public class LoadableViewModelBaseTests
|
|
|
|
|
Ignored = 456, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
var vm = new TestLoadableViewModelNestedInterface |
|
|
|
|
{ |
|
|
|
|
NestedState = nested |
|
|
|
|
}; |
|
|
|
|
var vm = new TestLoadableViewModelNestedInterface { NestedState = nested }; |
|
|
|
|
|
|
|
|
|
var state = vm.SaveStateToJsonObject(); |
|
|
|
|
|
|
|
|
@ -212,7 +198,7 @@ public class LoadableViewModelBaseTests
|
|
|
|
|
// Check nested state was loaded |
|
|
|
|
Assert.IsNotNull(vm2.NestedState); |
|
|
|
|
|
|
|
|
|
var loadedNested = (TestLoadableViewModel) vm2.NestedState; |
|
|
|
|
var loadedNested = (TestLoadableViewModel)vm2.NestedState; |
|
|
|
|
Assert.AreEqual("abc", loadedNested.Included); |
|
|
|
|
Assert.AreEqual(123, loadedNested.Id); |
|
|
|
|
Assert.AreEqual(0, loadedNested.Ignored); |
|
|
|
@ -241,16 +227,16 @@ public class LoadableViewModelBaseTests
|
|
|
|
|
{ |
|
|
|
|
var vm = new TestLoadableViewModelReadOnlyLoadable |
|
|
|
|
{ |
|
|
|
|
ReadOnlyLoadable = |
|
|
|
|
{ |
|
|
|
|
Included = "abc-123" |
|
|
|
|
} |
|
|
|
|
ReadOnlyLoadable = { Included = "abc-123" } |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
var state = vm.SaveStateToJsonObject(); |
|
|
|
|
|
|
|
|
|
// Check readonly loadable property was serialized |
|
|
|
|
Assert.AreEqual(1, state.Count); |
|
|
|
|
Assert.AreEqual("abc-123", state["ReadOnlyLoadable"].Deserialize<TestLoadableViewModel>()!.Included); |
|
|
|
|
Assert.AreEqual( |
|
|
|
|
"abc-123", |
|
|
|
|
state["ReadOnlyLoadable"].Deserialize<TestLoadableViewModel>()!.Included |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|