Skip to content

Commit 128f2ca

Browse files
committed
Add unit test for handling resources by ViewModel.
1 parent 866f88f commit 128f2ca

File tree

2 files changed

+502
-363
lines changed

2 files changed

+502
-363
lines changed
Lines changed: 86 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,103 @@
1-
using CarinaStudio.Collections;
2-
using CarinaStudio.Configuration;
1+
using CarinaStudio.Configuration;
32
using Microsoft.Extensions.Logging;
43
using System;
5-
using System.Collections.Generic;
6-
using System.Linq;
74
using System.Threading.Tasks;
85

9-
namespace CarinaStudio.ViewModels
6+
namespace CarinaStudio.ViewModels;
7+
8+
/// <summary>
9+
/// Test implementation of <see cref="ViewModel"/>.
10+
/// </summary>
11+
class TestViewModel : ViewModel
1012
{
11-
/// <summary>
12-
/// Test implementation of <see cref="ViewModel"/>.
13-
/// </summary>
14-
class TestViewModel : ViewModel
13+
// Properties.
14+
public static readonly ObservableProperty<int> TestInt32Property = ObservableProperty.Register<TestViewModel, int>(nameof(TestInt32));
15+
public static readonly ObservableProperty<int> TestRangeInt32Property = ObservableProperty.Register<TestViewModel, int>(nameof(TestRangeInt32), 1, (_, value) => Math.Max(Math.Min(MaxTestRangeInt32, value), MinTestRangeInt32), (value) => value != InvalidTestRangeInt32);
16+
17+
18+
// Constants.
19+
public const int InvalidTestRangeInt32 = 0;
20+
public const int MaxTestRangeInt32 = 100;
21+
public const int MinTestRangeInt32 = -100;
22+
23+
24+
// Fields.
25+
public SettingChangedEventArgs? LatestSettingChangedEventArgs;
26+
public SettingChangingEventArgs? LatestSettingChangingEventArgs;
27+
readonly Random random = new();
28+
29+
30+
// Constructor.
31+
public TestViewModel(TestApplication app) : base(app)
32+
{ }
33+
34+
35+
// Add resource.
36+
public new void AddResource(IDisposable resource) =>
37+
base.AddResource(resource);
38+
39+
40+
// Add resource.
41+
public new void AddResource(IAsyncDisposable resource) =>
42+
base.AddResource(resource);
43+
44+
45+
// Add resource.
46+
public new void AddResource(Action setup, Action dispose) =>
47+
base.AddResource(setup, dispose);
48+
49+
50+
// Add resource.
51+
public new void AddResource(Func<Task> setupAsync, Func<Task> disposeAsync) =>
52+
base.AddResource(setupAsync, disposeAsync);
53+
54+
55+
// Add resources.
56+
public new void AddResources(params IDisposable[] resources) =>
57+
base.AddResources(resources);
58+
59+
60+
// Add resources.
61+
public new void AddResources(params IAsyncDisposable[] resources) =>
62+
base.AddResources(resources);
63+
64+
65+
// Setting changed.
66+
protected override void OnSettingChanged(SettingChangedEventArgs e)
1567
{
16-
// Properties.
17-
public static readonly ObservableProperty<int> TestInt32Property = ObservableProperty.Register<TestViewModel, int>(nameof(TestInt32));
18-
public static readonly ObservableProperty<int> TestRangeInt32Property = ObservableProperty.Register<TestViewModel, int>(nameof(TestRangeInt32), 1, (o, value) => Math.Max(Math.Min(MaxTestRangeInt32, value), MinTestRangeInt32), (value) => value != InvalidTestRangeInt32);
19-
20-
21-
// Constants.
22-
public const int InvalidTestRangeInt32 = 0;
23-
public const int MaxTestRangeInt32 = 100;
24-
public const int MinTestRangeInt32 = -100;
25-
26-
27-
// Fields.
28-
public SettingChangedEventArgs? LatestSettingChangedEventArgs;
29-
public SettingChangingEventArgs? LatestSettingChangingEventArgs;
30-
readonly Random random = new Random();
31-
32-
33-
// Constructor.
34-
public TestViewModel(TestApplication app) : base(app)
35-
{ }
36-
37-
38-
// Setting changed.
39-
protected override void OnSettingChanged(SettingChangedEventArgs e)
40-
{
41-
base.OnSettingChanged(e);
42-
this.LatestSettingChangedEventArgs = e;
43-
}
68+
base.OnSettingChanged(e);
69+
this.LatestSettingChangedEventArgs = e;
70+
}
4471

4572

46-
// Setting changing.
47-
protected override void OnSettingChanging(SettingChangingEventArgs e)
48-
{
49-
base.OnSettingChanging(e);
50-
this.LatestSettingChangingEventArgs = e;
51-
}
73+
// Setting changing.
74+
protected override void OnSettingChanging(SettingChangingEventArgs e)
75+
{
76+
base.OnSettingChanging(e);
77+
this.LatestSettingChangingEventArgs = e;
78+
}
5279

5380

54-
// Perform necessary task.
55-
public async Task PerformNecessaryTaskAsync() => await this.WaitForNecessaryTaskAsync(Task.Delay(this.random.Next(50, 3000)));
81+
// Perform necessary task.
82+
public async Task PerformNecessaryTaskAsync() => await this.WaitForNecessaryTaskAsync(Task.Delay(this.random.Next(50, 3000)));
5683

5784

58-
// Print log.
59-
public void PrintLog(LogLevel level, EventId eventId, string message) => this.Logger.Log(level, eventId, message);
85+
// Print log.
86+
public void PrintLog(LogLevel level, EventId eventId, string message) => this.Logger.Log(level, eventId, message);
6087

6188

62-
// Test property (Int32).
63-
public int TestInt32
64-
{
65-
get => this.GetValue(TestInt32Property);
66-
set => this.SetValue(TestInt32Property, value);
67-
}
89+
// Test property (Int32).
90+
public int TestInt32
91+
{
92+
get => this.GetValue(TestInt32Property);
93+
set => this.SetValue(TestInt32Property, value);
94+
}
6895

6996

70-
// Test property (Int32).
71-
public int TestRangeInt32
72-
{
73-
get => this.GetValue(TestRangeInt32Property);
74-
set => this.SetValue(TestRangeInt32Property, value);
75-
}
97+
// Test property (Int32).
98+
public int TestRangeInt32
99+
{
100+
get => this.GetValue(TestRangeInt32Property);
101+
set => this.SetValue(TestRangeInt32Property, value);
76102
}
77-
}
103+
}

0 commit comments

Comments
 (0)