Skip to content

Commit 14d7bb3

Browse files
committed
housekeeping: add unit tests
1 parent 77fc15a commit 14d7bb3

9 files changed

+518
-45
lines changed

build.cake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ var packageWhitelist = new[]
88
MakeAbsolute(File("./src/ReactiveUI.Validation/ReactiveUI.Validation.csproj")),
99
};
1010

11-
var packageTestWhitelist = new FilePath[]
11+
var packageTestWhitelist = new[]
1212
{
13+
MakeAbsolute(File("./src/ReactiveUI.Validation.Tests/ReactiveUI.Validation.Tests.csproj")),
1314
};
1415

1516
BuildParameters.SetParameters(context: Context,

src/ApiGeneratorGlobalSuppressions.cs

Lines changed: 171 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reactive.Linq;
5+
using System.Reactive.Subjects;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using ReactiveUI.Validation.Components;
9+
using Xunit;
10+
11+
namespace ReactiveUI.Validation.Tests
12+
{
13+
public class ModelObservableTests
14+
{
15+
[Fact]
16+
public void InitialValidStateIsCorrect()
17+
{
18+
var model = new TestViewModel() {Name = "name", Name2 = "name2"};
19+
20+
Subject<bool> validState = new Subject<bool>();
21+
22+
var v = new ModelObservableValidation<TestViewModel>(model,(m) => validState.StartWith(true),(m,s) => "broken") ;
23+
24+
Assert.True(v.IsValid);
25+
}
26+
27+
[Fact]
28+
public void ObservableToInvalid()
29+
{
30+
var model = new TestViewModel() { Name = "name", Name2 = "name2" };
31+
32+
ReplaySubject<bool> validState = new ReplaySubject<bool>(1);
33+
34+
var v = new ModelObservableValidation<TestViewModel>(model, (m) => validState, (m,s) => "broken");
35+
36+
validState.OnNext(false);
37+
validState.OnNext(true);
38+
validState.OnNext(false);
39+
40+
Assert.False(v.IsValid);
41+
Assert.Equal("broken",v.Text.ToSingleLine());
42+
}
43+
}
44+
}
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using ReactiveUI.Validation.Comparators;
7+
using ReactiveUI.Validation.Components;
8+
using ReactiveUI.Validation.States;
9+
using Xunit;
10+
11+
namespace ReactiveUI.Validation.Tests
12+
{
13+
public class PropertyValidationTests
14+
{
15+
/// <summary>
16+
/// Default state is true.
17+
/// </summary>
18+
[Fact]
19+
public void ValidModelDefaultState()
20+
{
21+
var model = CreateDefaultValidModel();
22+
23+
var validation = new BasePropertyValidation<TestViewModel, string>(model,
24+
vm => vm.Name,
25+
(n) => !string.IsNullOrEmpty(n),"broken");
26+
27+
Assert.True(validation.IsValid);
28+
Assert.True(string.IsNullOrEmpty(validation.Text.ToSingleLine()));
29+
}
30+
31+
[Fact]
32+
public void StateTransitionsWhenValidityChangesTest()
33+
{
34+
var model = new TestViewModel();
35+
36+
var testValue = "test";
37+
38+
var validation = new BasePropertyValidation<TestViewModel, string>(model,
39+
vm => vm.Name,
40+
(n) => n != null && n.Length >= testValue.Length, "broken");
41+
42+
bool? lastVal = null;
43+
44+
var obs = validation.ValidationStatusChange.Subscribe(v => lastVal = v.IsValid);
45+
46+
Assert.False(validation.IsValid);
47+
Assert.False(lastVal);
48+
Assert.True(lastVal.HasValue);
49+
50+
model.Name = testValue+"-"+testValue;
51+
52+
Assert.True(validation.IsValid);
53+
Assert.True(lastVal);
54+
}
55+
56+
[Fact]
57+
public void PropertyContentsProvidedToMessageTest()
58+
{
59+
var model = new TestViewModel();
60+
61+
var testValue = "bongo";
62+
63+
var validation = new BasePropertyValidation<TestViewModel, string>(model,
64+
vm => vm.Name,
65+
(n) => n != null && n.Length > testValue.Length, (v) => $"The value '{v}' is incorrect");
66+
67+
model.Name = testValue;
68+
69+
var i = validation.IsValid;
70+
71+
Assert.Equal("The value 'bongo' is incorrect", validation.Text.ToSingleLine());
72+
}
73+
74+
75+
76+
/// <summary>
77+
/// Verify that validation message updates are correctly propogated.
78+
/// </summary>
79+
[Fact]
80+
public void MessageUpdatedWhenPropertyChanged()
81+
{
82+
var model = new TestViewModel();
83+
84+
var testRoot = "bon";
85+
var testValue = testRoot+"go";
86+
87+
88+
var validation = new BasePropertyValidation<TestViewModel, string>(model,
89+
vm => vm.Name,
90+
(n) => n != null && n.Length > testValue.Length, (v) => $"The value '{v}' is incorrect");
91+
92+
model.Name = testValue;
93+
94+
var i = validation.IsValid;
95+
96+
List<ValidationState> changes = new List<ValidationState>();
97+
98+
validation.ValidationStatusChange.Subscribe(v => changes.Add(v));
99+
100+
Assert.Equal("The value 'bongo' is incorrect", validation.Text.ToSingleLine());
101+
Assert.Equal(1,changes.Count);
102+
Assert.Equal(new ValidationState(false, "The value 'bongo' is incorrect", validation), changes[0], new ValidationStateComparer());
103+
104+
model.Name = testRoot;
105+
106+
Assert.Equal("The value 'bon' is incorrect", validation.Text.ToSingleLine());
107+
Assert.Equal(2, changes.Count);
108+
Assert.Equal(new ValidationState(false, "The value 'bon' is incorrect",validation),changes[1],new ValidationStateComparer() );
109+
}
110+
111+
[Fact]
112+
public void DualStateMessageTest()
113+
{
114+
var testRoot = "bon";
115+
var testValue = testRoot + "go";
116+
117+
var model = new TestViewModel() {Name = testValue};
118+
119+
120+
var validation = new BasePropertyValidation<TestViewModel, string>(model,
121+
vm => vm.Name,
122+
(n) => n != null && n.Length > testRoot.Length, (p,v) => v ? "cool" : $"The value '{p}' is incorrect");
123+
124+
Assert.Equal("cool",validation.Text.ToSingleLine());
125+
126+
model.Name = testRoot;
127+
128+
Assert.Equal("The value 'bon' is incorrect", validation.Text.ToSingleLine());
129+
130+
}
131+
132+
133+
private TestViewModel CreateDefaultValidModel()
134+
{
135+
return new TestViewModel() {Name = "name"};
136+
137+
}
138+
}
139+
140+
public class TestViewModel : ReactiveObject
141+
{
142+
private string _name;
143+
144+
public string Name
145+
{
146+
get { return _name; }
147+
set { this.RaiseAndSetIfChanged(ref _name, value); }
148+
}
149+
150+
private string _name2;
151+
152+
public string Name2
153+
{
154+
get { return _name2; }
155+
set { this.RaiseAndSetIfChanged(ref _name2, value); }
156+
}
157+
158+
public List<string> GetIt { get; set; } = new List<string>();
159+
160+
public string Go()
161+
{
162+
return "here";
163+
}
164+
}
165+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="MSBuild.Sdk.Extras">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>netcoreapp2.1</TargetFrameworks>
5+
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">$(TargetFrameworks);net472</TargetFrameworks>
6+
<NoWarn>$(NoWarn);1591;CA1707;SA1633</NoWarn>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\ReactiveUI.Validation\ReactiveUI.Validation.csproj" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Update="xunit.runner.json">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
17+
</ItemGroup>
18+
</Project>
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using ReactiveUI.Validation.Components;
7+
using ReactiveUI.Validation.Contexts;
8+
using Xunit;
9+
10+
namespace ReactiveUI.Validation.Tests
11+
{
12+
public class ValidationContextTests
13+
{
14+
[Fact]
15+
public void EmptyValidationContextIsValid()
16+
{
17+
var vc = new ValidationContext();
18+
19+
Assert.True(vc.IsValid);
20+
Assert.Equal(0,vc.Text.Count);
21+
}
22+
23+
[Fact]
24+
public void CanAddValidationComponentsTest()
25+
{
26+
var vc = new ValidationContext();
27+
28+
var validName = "valid";
29+
var invalidName = String.Empty;
30+
31+
var vm = new TestViewModel() {Name = "valid"};
32+
33+
var v1 = new BasePropertyValidation<TestViewModel, string>(vm, v => v.Name,
34+
(s) => !string.IsNullOrEmpty(s), s => $"{s} isn't valid");
35+
36+
vc.Add(v1);
37+
38+
Assert.True(vc.IsValid);
39+
40+
vm.Name = invalidName;
41+
42+
Assert.False(v1.IsValid);
43+
Assert.False(vc.IsValid);
44+
45+
Assert.Equal(1,vc.Text.Count);
46+
}
47+
48+
[Fact]
49+
public void TwoValidationComponentsCorrectlyResultInContextTest()
50+
{
51+
var vc = new ValidationContext();
52+
53+
var validName = "valid";
54+
var invalidName = String.Empty;
55+
56+
var vm = new TestViewModel() { Name = validName,Name2=validName };
57+
58+
var v1 = new BasePropertyValidation<TestViewModel, string>(vm, v => v.Name,
59+
(s) => !string.IsNullOrEmpty(s), s => $"Name {s} isn't valid");
60+
61+
var v2 = new BasePropertyValidation<TestViewModel, string>(vm, v => v.Name2,
62+
(s) => !string.IsNullOrEmpty(s), s => $"Name 2 {s} isn't valid");
63+
64+
vc.Add(v1);
65+
vc.Add(v2);
66+
67+
Assert.True(vc.IsValid);
68+
Assert.Equal(0, vc.Text.Count);
69+
70+
vm.Name = invalidName;
71+
72+
Assert.False(vc.IsValid);
73+
74+
Assert.Equal(1, vc.Text.Count);
75+
Assert.Equal("Name " + invalidName + " isn't valid", vc.Text[0]);
76+
77+
78+
79+
vm.Name2 = invalidName;
80+
Assert.False(vc.IsValid);
81+
Assert.Equal(2, vc.Text.Count);
82+
Assert.Equal("Name "+invalidName+" isn't valid",vc.Text[0]);
83+
Assert.Equal("Name 2 " + invalidName + " isn't valid", vc.Text[1]);
84+
85+
vm.Name = validName;
86+
vm.Name2 = validName;
87+
88+
Assert.True(vc.IsValid);
89+
Assert.Equal(0,vc.Text.Count);
90+
}
91+
}
92+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"shadowCopy": false
3+
}
Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1-
2-
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReactiveUI.Validation", "ReactiveUI.Validation.csproj", "{B62AABD0-22A4-470D-B6EB-F6B3EAE668DE}"
5-
EndProject
6-
Global
7-
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8-
Debug|Any CPU = Debug|Any CPU
9-
Release|Any CPU = Release|Any CPU
10-
EndGlobalSection
11-
GlobalSection(ProjectConfigurationPlatforms) = postSolution
12-
{B62AABD0-22A4-470D-B6EB-F6B3EAE668DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
13-
{B62AABD0-22A4-470D-B6EB-F6B3EAE668DE}.Debug|Any CPU.Build.0 = Debug|Any CPU
14-
{B62AABD0-22A4-470D-B6EB-F6B3EAE668DE}.Release|Any CPU.ActiveCfg = Release|Any CPU
15-
{B62AABD0-22A4-470D-B6EB-F6B3EAE668DE}.Release|Any CPU.Build.0 = Release|Any CPU
16-
EndGlobalSection
17-
EndGlobal
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReactiveUI.Validation", "ReactiveUI.Validation\ReactiveUI.Validation.csproj", "{B62AABD0-22A4-470D-B6EB-F6B3EAE668DE}"
5+
EndProject
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReactiveUI.Validation.Tests", "ReactiveUI.Validation.Tests\ReactiveUI.Validation.Tests.csproj", "{892D51D9-7A3F-4E66-A871-082A63D9BE05}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{B62AABD0-22A4-470D-B6EB-F6B3EAE668DE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{B62AABD0-22A4-470D-B6EB-F6B3EAE668DE}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{B62AABD0-22A4-470D-B6EB-F6B3EAE668DE}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{B62AABD0-22A4-470D-B6EB-F6B3EAE668DE}.Release|Any CPU.Build.0 = Release|Any CPU
18+
{892D51D9-7A3F-4E66-A871-082A63D9BE05}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19+
{892D51D9-7A3F-4E66-A871-082A63D9BE05}.Debug|Any CPU.Build.0 = Debug|Any CPU
20+
{892D51D9-7A3F-4E66-A871-082A63D9BE05}.Release|Any CPU.ActiveCfg = Release|Any CPU
21+
{892D51D9-7A3F-4E66-A871-082A63D9BE05}.Release|Any CPU.Build.0 = Release|Any CPU
22+
EndGlobalSection
23+
EndGlobal

0 commit comments

Comments
 (0)