Skip to content

Commit 4a96d58

Browse files
committed
Add object provider tests.
1 parent 1c6bcf9 commit 4a96d58

5 files changed

Lines changed: 196 additions & 5 deletions

File tree

tests/UnityMvvmToolkit.UnitTests/BindingContextMemberProviderTests.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,27 @@ public void GetBindingContextMembers_ShouldReturnBindingContextMembers()
3232
var decrementMemberHash =
3333
HashCodeHelper.GetMemberHashCode(bindingContextType, nameof(MyBindingContext.DecrementCommand));
3434

35+
var setValueMemberHash =
36+
HashCodeHelper.GetMemberHashCode(bindingContextType, nameof(MyBindingContext.SetValueCommand));
37+
3538
// Act
3639
membersProvider.GetBindingContextMembers(bindingContextType, members);
3740

3841
// Assert
39-
members.Count.Should().Be(5);
42+
members.Count.Should().Be(6);
4043

4144
members.Should().ContainKey(titleMemberHash);
4245
members.Should().ContainKey(countMemberHash);
4346
members.Should().ContainKey(descriptionMemberHash);
4447
members.Should().ContainKey(incrementMemberHash);
4548
members.Should().ContainKey(decrementMemberHash);
49+
members.Should().ContainKey(setValueMemberHash);
4650

4751
members[titleMemberHash].MemberType.Should().Be(MemberTypes.Property);
4852
members[countMemberHash].MemberType.Should().Be(MemberTypes.Field);
4953
members[descriptionMemberHash].MemberType.Should().Be(MemberTypes.Field);
5054
members[incrementMemberHash].MemberType.Should().Be(MemberTypes.Property);
5155
members[decrementMemberHash].MemberType.Should().Be(MemberTypes.Property);
56+
members[setValueMemberHash].MemberType.Should().Be(MemberTypes.Property);
5257
}
5358
}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
using FluentAssertions;
2+
using UnityMvvmToolkit.Core;
3+
using UnityMvvmToolkit.Core.Converters.ParameterValueConverters;
4+
using UnityMvvmToolkit.Core.Converters.PropertyValueConverters;
5+
using UnityMvvmToolkit.Core.Extensions;
6+
using UnityMvvmToolkit.Core.Interfaces;
7+
using UnityMvvmToolkit.UnitTests.TestBindingContext;
8+
9+
namespace UnityMvvmToolkit.UnitTests;
10+
11+
public class BindingContextObjectProviderTests
12+
{
13+
[Fact]
14+
public void RentProperty_ShouldReturnProperty_WhenDataIsValid()
15+
{
16+
// Arrange
17+
const int countValue = 69;
18+
const string titleValue = "TestTitle";
19+
20+
var objectProvider = new BindingContextObjectProvider(Array.Empty<IValueConverter>());
21+
var bindingContext = new MyBindingContext(titleValue)
22+
{
23+
Count = countValue,
24+
};
25+
26+
IProperty<int> countProperty;
27+
var countPropertyBindingData = nameof(MyBindingContext.Count).ToPropertyBindingData();
28+
29+
IReadOnlyProperty<string> titleProperty;
30+
var titlePropertyBindingData = nameof(MyBindingContext.Title).ToPropertyBindingData();
31+
32+
// Act
33+
countProperty = objectProvider.RentProperty<int>(bindingContext, countPropertyBindingData);
34+
titleProperty = objectProvider.RentReadOnlyProperty<string>(bindingContext, titlePropertyBindingData);
35+
36+
// Assert
37+
countProperty
38+
.Should().NotBeNull().And.BeAssignableTo<IProperty<int>>().And.BeAssignableTo<IReadOnlyProperty<int>>();
39+
40+
titleProperty
41+
.Should().NotBeNull().And.BeAssignableTo<IReadOnlyProperty<string>>();
42+
43+
countProperty.Value.Should().Be(countValue);
44+
titleProperty.Value.Should().Be(titleValue);
45+
}
46+
47+
[Fact]
48+
public void RentPropertyWithConverter_ShouldReturnProperty_WhenDataIsValid()
49+
{
50+
// Arrange
51+
const int countValue = 69;
52+
53+
var objectProvider = new BindingContextObjectProvider(new IValueConverter[]
54+
{
55+
new IntToStrConverter()
56+
});
57+
58+
var bindingContext = new MyBindingContext
59+
{
60+
Count = countValue,
61+
};
62+
63+
IProperty<string> countProperty;
64+
var countPropertyBindingData = nameof(MyBindingContext.Count).ToPropertyBindingData();
65+
66+
// Act
67+
countProperty = objectProvider.RentProperty<string>(bindingContext, countPropertyBindingData);
68+
69+
// Assert
70+
countProperty
71+
.Should()
72+
.NotBeNull()
73+
.And
74+
.BeAssignableTo<IProperty<string>>()
75+
.And
76+
.BeAssignableTo<IReadOnlyProperty<string>>();
77+
78+
countProperty.Value.Should().Be(countValue.ToString());
79+
}
80+
81+
[Fact]
82+
public void GetCommand_ShouldReturnCommand_WhenDataIsValid()
83+
{
84+
// Arrange
85+
var objectProvider = new BindingContextObjectProvider(Array.Empty<IValueConverter>());
86+
var bindingContext = new MyBindingContext();
87+
88+
ICommand incrementCommand;
89+
ICommand decrementCommand;
90+
91+
// Act
92+
incrementCommand =
93+
objectProvider.GetCommand<ICommand>(bindingContext, nameof(MyBindingContext.IncrementCommand));
94+
95+
decrementCommand =
96+
objectProvider.GetCommand<ICommand>(bindingContext, nameof(MyBindingContext.DecrementCommand));
97+
98+
// Assert
99+
incrementCommand.Should().NotBeNull().And.BeAssignableTo<IBaseCommand>();
100+
decrementCommand.Should().NotBeNull().And.BeAssignableTo<IBaseCommand>();
101+
}
102+
103+
[Fact]
104+
public void RentCommandWrapper_ShouldReturnCommand_WhenDataIsValid()
105+
{
106+
// Arrange
107+
var objectProvider = new BindingContextObjectProvider(new IValueConverter[]
108+
{
109+
new ParameterToIntConverter()
110+
});
111+
112+
var bindingContext = new MyBindingContext();
113+
114+
IBaseCommand setValueCommand;
115+
var setValueCommandBindingData = $"{nameof(MyBindingContext.SetValueCommand)}, 5".ToCommandBindingData(0);
116+
117+
// Act
118+
setValueCommand = objectProvider.RentCommandWrapper(bindingContext, setValueCommandBindingData);
119+
120+
// Assert
121+
setValueCommand.Should().NotBeNull();
122+
}
123+
124+
[Fact]
125+
public void RentPropertyWithConverter_ShouldThrow_WhenConverterIsNotSet()
126+
{
127+
// Arrange
128+
const int countValue = 69;
129+
130+
var objectProvider = new BindingContextObjectProvider(Array.Empty<IValueConverter>());
131+
var bindingContext = new MyBindingContext
132+
{
133+
Count = countValue,
134+
};
135+
136+
var countPropertyBindingData = nameof(MyBindingContext.Count).ToPropertyBindingData();
137+
138+
// Assert
139+
objectProvider
140+
.Invoking(objProvider => objProvider.RentProperty<string>(bindingContext, countPropertyBindingData))
141+
.Should()
142+
.Throw<NullReferenceException>()
143+
.WithMessage($"Property value converter from '{typeof(int)}' to '{typeof(string)}' not found.");
144+
}
145+
146+
[Fact]
147+
public void RentCommandWrapper_ShouldThrow_WhenConverterIsNotSet()
148+
{
149+
// Arrange
150+
var objectProvider = new BindingContextObjectProvider(Array.Empty<IValueConverter>());
151+
var bindingContext = new MyBindingContext();
152+
153+
var setValueCommandBindingData = $"{nameof(MyBindingContext.SetValueCommand)}, 5".ToCommandBindingData(0);
154+
155+
// Assert
156+
objectProvider
157+
.Invoking(objProvider => objProvider.RentCommandWrapper(bindingContext, setValueCommandBindingData))
158+
.Should()
159+
.Throw<NullReferenceException>()
160+
.WithMessage($"Parameter value converter to '{typeof(int)}' not found.");
161+
}
162+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using UnityMvvmToolkit.Core.Interfaces;
2+
3+
namespace UnityMvvmToolkit.UnitTests.Interfaces;
4+
5+
public interface IMyCommand : ICommand
6+
{
7+
}

tests/UnityMvvmToolkit.UnitTests/TestBindingContext/MyBindingContext.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using UnityMvvmToolkit.Core;
22
using UnityMvvmToolkit.Core.Interfaces;
3+
using UnityMvvmToolkit.UnitTests.Interfaces;
4+
using UnityMvvmToolkit.UnitTests.TestCommands;
35

46
namespace UnityMvvmToolkit.UnitTests.TestBindingContext;
57

@@ -8,12 +10,14 @@ public class MyBindingContext : IBindingContext
810
private readonly IProperty<int> _count = new ObservableProperty<int>();
911
private readonly IProperty<string> m_description = new ObservableProperty<string>();
1012

11-
public MyBindingContext()
13+
public MyBindingContext(string title = "Title")
1214
{
13-
Title = new ObservableProperty<string>("Title");
15+
Title = new ObservableProperty<string>(title);
1416

1517
IncrementCommand = new Command(() => Count++);
16-
DecrementCommand = new Command(() => Count--);
18+
DecrementCommand = new MyCommand(() => Count--);
19+
20+
SetValueCommand = new Command<int>(value => Count = value);
1721
}
1822

1923
public IReadOnlyProperty<string> Title { get; }
@@ -31,5 +35,7 @@ public string Description
3135
}
3236

3337
public ICommand IncrementCommand { get; }
34-
public ICommand DecrementCommand { get; }
38+
public IMyCommand DecrementCommand { get; }
39+
40+
public ICommand<int> SetValueCommand { get; }
3541
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using UnityMvvmToolkit.Core;
2+
using UnityMvvmToolkit.UnitTests.Interfaces;
3+
4+
namespace UnityMvvmToolkit.UnitTests.TestCommands;
5+
6+
public class MyCommand : Command, IMyCommand
7+
{
8+
public MyCommand(Action action, Func<bool>? canExecute = null) : base(action, canExecute)
9+
{
10+
}
11+
}

0 commit comments

Comments
 (0)