Dreamine.FullKit.Tests 1.0.0.0
Dreamine.FullKit.Tests 기능을 검증하는 자동화 테스트 프로젝트입니다.
로딩중...
검색중...
일치하는것 없음
ViewModelAndRelayCommandTests.cs
이 파일의 문서화 페이지로 가기
1using System.ComponentModel;
2using System.Threading;
3using Dreamine.MVVM.ViewModels;
4
6
16{
25 [Fact]
27 {
28 var executed = false;
29 var raised = false;
30 var command = new RelayCommand(() => executed = true, () => true);
31 command.CanExecuteChanged += (_, _) => raised = true;
32
33 command.Execute(null);
34 command.RaiseCanExecuteChanged();
35
36 Assert.True(executed);
37 Assert.True(command.CanExecute(null));
38 Assert.True(raised);
39 }
40
49 [Fact]
51 {
52 var command = new RelayCommand<int>(_ => { }, value => value > 0);
53
54 // CanExecute returns false for a bad parameter type — Execute silently no-ops.
55 Assert.False(command.CanExecute("bad"));
56 var executed = false;
57 command = new RelayCommand<int>(_ => executed = true, value => value > 0);
58 command.Execute("bad");
59 Assert.False(executed);
60 }
61
70 [Fact]
72 {
73 var viewModel = new TestViewModel();
74 var propertyNames = new List<string?>();
75 viewModel.PropertyChanged += (_, args) => propertyNames.Add(args.PropertyName);
76
77 viewModel.Name = "one";
78 viewModel.Name = "one";
79 viewModel.Name = "two";
80
81 Assert.Equal(new[] { "Name", "Name" }, propertyNames);
82 }
83
100 [Fact]
102 {
103 var concurrentCount = 0;
104 var maxConcurrent = 0;
105 var command = new AsyncRelayCommand(async () =>
106 {
107 var c = Interlocked.Increment(ref concurrentCount);
108 Interlocked.Exchange(ref maxConcurrent, Math.Max(Volatile.Read(ref maxConcurrent), c));
109 await Task.Delay(30);
110 Interlocked.Decrement(ref concurrentCount);
111 });
112
113 var tasks = Enumerable.Range(0, 10).Select(_ => Task.Run(() => command.Execute(null)));
114 await Task.WhenAll(tasks);
115 await Task.Delay(100); // let the one accepted execution finish
116
117 Assert.Equal(1, maxConcurrent);
118 }
119
128 [Fact]
130 {
131 var ex = new InvalidOperationException("test");
132 var command = new AsyncRelayCommand(() => Task.FromException(ex));
133
134 command.Execute(null);
135 Thread.Sleep(50);
136
137 Assert.Same(ex, command.LastException);
138 }
139
148 private sealed class TestViewModel : ViewModelBase
149 {
158 private string _name = string.Empty;
159
168 public string Name
169 {
170 get => _name;
171 set => SetProperty(ref _name, value);
172 }
173 }
174}