Skip to content

Commit 8a6c17b

Browse files
committed
Add unit tests.
1 parent 5a8d437 commit 8a6c17b

48 files changed

Lines changed: 2018 additions & 254 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

tests/UnityMvvmToolkit.Test.Unit/.config/dotnet-tools.json renamed to src/UnityMvvmToolkit.Core/.config/dotnet-tools.json

File renamed without changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"stryker-config":
3+
{
4+
"test-projects": [
5+
"../../../tests/UnityMvvmToolkit.Test.Unit/UnityMvvmToolkit.Test.Unit.csproj",
6+
"../../../tests/UnityMvvmToolkit.Test.Integration/UnityMvvmToolkit.Test.Integration.csproj"
7+
]
8+
}
9+
}

src/UnityMvvmToolkit.Core/AssemblyInfo.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@
44
[assembly: InternalsVisibleTo("UnityMvvmToolkit.UITK")]
55
[assembly: InternalsVisibleTo("UnityMvvmToolkit.Common")]
66

7-
[assembly: InternalsVisibleTo("UnityMvvmToolkit.Test.Unit")]
7+
[assembly: InternalsVisibleTo("UnityMvvmToolkit.Test.Unit")]
8+
[assembly: InternalsVisibleTo("UnityMvvmToolkit.Test.Integration")]
9+
10+
// NSubstitute
11+
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]

src/UnityMvvmToolkit.Core/Extensions/StringExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ public static class StringExtensions
1717
};
1818

1919
[MethodImpl(MethodImplOptions.AggressiveInlining)]
20-
public static bool TryParse(this string span, out float result)
20+
public static bool TryParse(this string str, out float result)
2121
{
22-
return float.TryParse(span, NumberStyles.Any, CommaCulture, out result) ||
23-
float.TryParse(span, NumberStyles.Any, PointCulture, out result);
22+
return float.TryParse(str, NumberStyles.Any, CommaCulture, out result) ||
23+
float.TryParse(str, NumberStyles.Any, PointCulture, out result);
2424
}
2525

2626
public static CommandBindingData ToCommandBindingData(this string bindingString, int elementId)

src/UnityMvvmToolkit.Core/Internal/BindingContextMemberProvider.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ private static bool TryGetFieldHashCode(Type contextType, FieldInfo fieldInfo, o
6060

6161
if (fieldInfo.IsPublic)
6262
{
63-
return TryGetHashCode(contextType, fieldInfo.Name, fieldInfo.FieldType.GetInterfaces(), out hashCode);
63+
return TryGetHashCode(contextType, fieldInfo.Name, fieldInfo.FieldType, out hashCode);
6464
}
6565

6666
if (TryGetPropertyNameFromAttribute(fieldInfo, out var fieldName))
6767
{
68-
return TryGetHashCode(contextType, fieldName, fieldInfo.FieldType.GetInterfaces(), out hashCode);
68+
return TryGetHashCode(contextType, fieldName, fieldInfo.FieldType, out hashCode);
6969
}
7070

7171
fieldName = fieldInfo.Name;
@@ -88,7 +88,7 @@ private static bool TryGetFieldHashCode(Type contextType, FieldInfo fieldInfo, o
8888
throw new InvalidOperationException($"Field name '{fieldName}' is not supported.");
8989
}
9090

91-
return TryGetHashCode(contextType, fieldName, fieldInfo.FieldType.GetInterfaces(), out hashCode);
91+
return TryGetHashCode(contextType, fieldName, fieldInfo.FieldType, out hashCode);
9292
}
9393

9494
[MethodImpl(MethodImplOptions.AggressiveInlining)]
@@ -109,24 +109,23 @@ private static bool TryGetPropertyNameFromAttribute(MemberInfo fieldInfo, out st
109109
[MethodImpl(MethodImplOptions.AggressiveInlining)]
110110
private static bool TryGetPropertyHashCode(Type contextType, PropertyInfo propertyInfo, out int hashCode)
111111
{
112-
return TryGetHashCode(contextType, propertyInfo.Name, propertyInfo.PropertyType.GetInterfaces(),
113-
out hashCode);
112+
if (propertyInfo.GetMethod.IsPrivate)
113+
{
114+
hashCode = default;
115+
return false;
116+
}
117+
118+
return TryGetHashCode(contextType, propertyInfo.Name, propertyInfo.PropertyType, out hashCode);
114119
}
115120

116121
[MethodImpl(MethodImplOptions.AggressiveInlining)]
117-
private static bool TryGetHashCode(Type contextType, string memberName, Type[] memberInterfaces,
118-
out int hashCode)
122+
private static bool TryGetHashCode(Type contextType, string memberName, Type memberType, out int hashCode)
119123
{
120-
for (var i = 0; i < memberInterfaces.Length; i++)
124+
if (typeof(IBaseCommand).IsAssignableFrom(memberType) ||
125+
typeof(IBaseProperty).IsAssignableFrom(memberType))
121126
{
122-
var interfaceType = memberInterfaces[i];
123-
124-
if (interfaceType == typeof(IBaseCommand) ||
125-
interfaceType == typeof(IBaseProperty))
126-
{
127-
hashCode = HashCodeHelper.GetMemberHashCode(contextType, memberName);
128-
return true;
129-
}
127+
hashCode = HashCodeHelper.GetMemberHashCode(contextType, memberName);
128+
return true;
130129
}
131130

132131
hashCode = default;

src/UnityMvvmToolkit.Core/Internal/Helpers/HashCodeHelper.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static int GetPropertyConverterHashCode(IPropertyValueConverter converter
3030
var targetTypeHash = converter.TargetType.GetHashCode();
3131
var sourceTypeHash = converter.SourceType.GetHashCode();
3232

33-
return string.IsNullOrEmpty(converterName)
33+
return string.IsNullOrWhiteSpace(converterName)
3434
? CombineHashCode(targetTypeHash, sourceTypeHash)
3535
: CombineHashCode(converterName.GetHashCode(), targetTypeHash, sourceTypeHash);
3636
}
@@ -49,7 +49,7 @@ public static int GetParameterConverterHashCode(IParameterValueConverter convert
4949
{
5050
var targetTypeHash = converter.TargetType.GetHashCode();
5151

52-
return string.IsNullOrEmpty(converterName)
52+
return string.IsNullOrWhiteSpace(converterName)
5353
? targetTypeHash
5454
: CombineHashCode(converterName.GetHashCode(), targetTypeHash);
5555
}
@@ -63,7 +63,7 @@ public static int GetPropertyWrapperConverterId(IPropertyValueConverter converte
6363
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6464
public static int GetPropertyWrapperConverterId(Type targetType, Type sourceType, string converterName = null)
6565
{
66-
return string.IsNullOrEmpty(converterName)
66+
return string.IsNullOrWhiteSpace(converterName)
6767
? CombineHashCode(targetType.GetHashCode(), sourceType.GetHashCode())
6868
: CombineHashCode(converterName.GetHashCode(), targetType.GetHashCode(), sourceType.GetHashCode());
6969
}
@@ -77,15 +77,15 @@ public static int GetCommandWrapperConverterId(IParameterValueConverter converte
7777
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7878
public static int GetCommandWrapperConverterId(Type targetType, string converterName = null)
7979
{
80-
return string.IsNullOrEmpty(converterName)
80+
return string.IsNullOrWhiteSpace(converterName)
8181
? targetType.GetHashCode()
8282
: CombineHashCode(converterName.GetHashCode(), targetType.GetHashCode());
8383
}
8484

8585
[MethodImpl(MethodImplOptions.AggressiveInlining)]
8686
public static int GetCommandWrapperId(Type contextType, Type targetType, string converterName = null)
8787
{
88-
return string.IsNullOrEmpty(converterName)
88+
return string.IsNullOrWhiteSpace(converterName)
8989
? CombineHashCode(contextType.GetHashCode(), targetType.GetHashCode())
9090
: CombineHashCode(contextType.GetHashCode(), converterName.GetHashCode(), targetType.GetHashCode());
9191
}

src/UnityMvvmToolkit.Core/Internal/ObjectHandlers/ObjectWrapperHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public TProperty GetProperty<TProperty, TValueType>(IBindingContext context, Bin
8484
}
8585

8686
var args = new object[] { valueConverter };
87-
var wrapperType = typeof(PropertyWrapper<,>).MakeGenericType(targetType, sourceType);
87+
var wrapperType = typeof(PropertyWrapper<,>).MakeGenericType(sourceType, targetType);
8888

8989
return (TProperty) ObjectWrapperHelper.CreatePropertyWrapper(wrapperType, args, converterId,
9090
contextProperty);
@@ -219,7 +219,7 @@ private void CreatePropertyValueConverterInstances(int converterId, IPropertyVal
219219
var itemsQueue = new Queue<IObjectWrapper>();
220220

221221
var args = new object[] { converter };
222-
var wrapperType = typeof(PropertyWrapper<,>).MakeGenericType(converter.TargetType, converter.SourceType);
222+
var wrapperType = typeof(PropertyWrapper<,>).MakeGenericType(converter.SourceType, converter.TargetType);
223223

224224
for (var i = 0; i < capacity; i++)
225225
{

src/UnityMvvmToolkit.Core/Internal/ObjectHandlers/ValueConverterHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private void RegisterValueConverters(IValueConverter[] converters)
4343
RegisterValueConverter(convertersSpan[i]);
4444
}
4545

46-
if (_valueConvertersByHash.ContainsKey(typeof(ParameterToStrConverter).GetHashCode()) == false)
46+
if (TryGetValueConverterByType(typeof(ParameterToStrConverter), out _) == false)
4747
{
4848
RegisterValueConverter(new ParameterToStrConverter());
4949
}

src/UnityMvvmToolkit.Core/Internal/ObjectWrappers/PropertyWrapper.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,23 @@
66

77
namespace UnityMvvmToolkit.Core.Internal.ObjectWrappers
88
{
9-
internal sealed class PropertyWrapper<TValueType, TSourceType> : IProperty<TValueType>, IPropertyWrapper
9+
internal sealed class PropertyWrapper<TSourceType, TValueType> : IProperty<TValueType>, IPropertyWrapper
1010
{
1111
private readonly IPropertyValueConverter<TSourceType, TValueType> _valueConverter;
1212

13+
private int _converterId;
14+
1315
private TValueType _value;
1416
private TSourceType _sourceValue;
1517
private IProperty<TSourceType> _property;
1618

1719
public PropertyWrapper(IPropertyValueConverter<TSourceType, TValueType> valueConverter)
1820
{
21+
_converterId = -1;
1922
_valueConverter = valueConverter;
2023
}
2124

22-
public int ConverterId { get; private set; }
25+
public int ConverterId => _converterId;
2326

2427
public TValueType Value
2528
{
@@ -33,7 +36,13 @@ public TValueType Value
3336

3437
public IPropertyWrapper SetConverterId(int converterId)
3538
{
36-
ConverterId = converterId;
39+
if (_converterId != -1)
40+
{
41+
throw new InvalidOperationException("Can not change converter ID.");
42+
}
43+
44+
_converterId = converterId;
45+
3746
return this;
3847
}
3948

src/UnityMvvmToolkit.Core/Internal/Structs/LineSplitEnumerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public bool MoveNext()
4343
return true;
4444
}
4545

46-
Current = CreateNewLine(_index, _start, span.Slice(0, index));
46+
Current = CreateNewLine(_index, _start, span[..index]);
4747

4848
_index++;
4949
_start += index + 1;

0 commit comments

Comments
 (0)