diff options
author | Eric Maupin <me@ermau.com> | 2018-02-14 03:33:33 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-14 03:33:33 +0300 |
commit | 09d1cc8ccbc8f5d7d679994129222fb39381e37a (patch) | |
tree | 8290fc14d45d8f178e6eb3a781fad4ef6a3389be /Xamarin.PropertyEditing.Tests | |
parent | 69869bdc5939b2d09431591189ff7ae27cc752bc (diff) | |
parent | 3d6818c4b629b6c85b71f66efc0c0ad823098019 (diff) |
Merge pull request #179 from xamarin/ermau-numerics
[Core] Handle numerics more generically
Diffstat (limited to 'Xamarin.PropertyEditing.Tests')
8 files changed, 231 insertions, 26 deletions
diff --git a/Xamarin.PropertyEditing.Tests/BytePropertyViewModelTests.cs b/Xamarin.PropertyEditing.Tests/BytePropertyViewModelTests.cs index 0635a47..c8a3f26 100644 --- a/Xamarin.PropertyEditing.Tests/BytePropertyViewModelTests.cs +++ b/Xamarin.PropertyEditing.Tests/BytePropertyViewModelTests.cs @@ -7,7 +7,7 @@ namespace Xamarin.PropertyEditing.Tests { [TestFixture] internal class BytePropertyViewModelTests - : ConstrainedPropertyViewModelTests<byte, BytePropertyViewModel> + : NumericViewModelTests<byte> { protected override Tuple<byte, byte> MaxMin => new Tuple<byte, byte> (byte.MaxValue, byte.MinValue); @@ -42,9 +42,9 @@ namespace Xamarin.PropertyEditing.Tests return value; } - protected override BytePropertyViewModel GetViewModel (TargetPlatform platform, IPropertyInfo property, IEnumerable<IObjectEditor> editors) + protected override NumericPropertyViewModel<byte> GetViewModel (TargetPlatform platform, IPropertyInfo property, IEnumerable<IObjectEditor> editors) { - return new BytePropertyViewModel (platform, property, editors); + return new NumericPropertyViewModel<byte> (platform, property, editors); } } } diff --git a/Xamarin.PropertyEditing.Tests/ConstrainedPropertyViewModelTests.cs b/Xamarin.PropertyEditing.Tests/ConstrainedPropertyViewModelTests.cs index c2a97a0..015830a 100644 --- a/Xamarin.PropertyEditing.Tests/ConstrainedPropertyViewModelTests.cs +++ b/Xamarin.PropertyEditing.Tests/ConstrainedPropertyViewModelTests.cs @@ -31,10 +31,6 @@ namespace Xamarin.PropertyEditing.Tests vm.Value = value; Assume.That (vm.Value, Is.EqualTo (value)); - - Assume.That (vm.RaiseValue, Is.Not.Null); - Assert.That (vm.RaiseValue.CanExecute (null), Is.True, "RaiseValue can not execute"); - Assert.That (vm.LowerValue.CanExecute (null), Is.True, "LowerValue can not execute"); } [Test] @@ -55,10 +51,6 @@ namespace Xamarin.PropertyEditing.Tests vm.Value = value; Assert.That (vm.Value, Is.EqualTo (min)); - - Assume.That (vm.RaiseValue, Is.Not.Null); - Assert.That (vm.RaiseValue.CanExecute (null), Is.True, "Should be able to RaiseValue"); - Assert.That (vm.LowerValue.CanExecute (null), Is.False, "Should not be able to LowerValue"); } [Test] @@ -79,10 +71,6 @@ namespace Xamarin.PropertyEditing.Tests vm.Value = value; Assert.That (vm.Value, Is.EqualTo (max)); - - Assume.That (vm.RaiseValue, Is.Not.Null); - Assert.That (vm.RaiseValue.CanExecute (null), Is.False, "Should not be able to RaiseValue"); - Assert.That (vm.LowerValue.CanExecute (null), Is.True, "Should be able to LowerValue"); } [Test] @@ -121,7 +109,7 @@ namespace Xamarin.PropertyEditing.Tests protected abstract T GetConstrainedRandomValueAboveBounds (Random rand, out T max, out T min); protected abstract T GetConstrainedRandomValueBelowBounds (Random rand, out T max, out T min); - protected ConstrainedPropertyViewModel<T> GetViewModel (IPropertyInfo property, IObjectEditor editor) + protected TViewModel GetViewModel (IPropertyInfo property, IObjectEditor editor) { return GetViewModel (property, new[] { editor }); } diff --git a/Xamarin.PropertyEditing.Tests/FloatingPropertyViewModelTests.cs b/Xamarin.PropertyEditing.Tests/FloatingPropertyViewModelTests.cs index 94d6459..c45e5d3 100644 --- a/Xamarin.PropertyEditing.Tests/FloatingPropertyViewModelTests.cs +++ b/Xamarin.PropertyEditing.Tests/FloatingPropertyViewModelTests.cs @@ -7,16 +7,16 @@ namespace Xamarin.PropertyEditing.Tests { [TestFixture] internal class FloatingPropertyViewModelTests - : ConstrainedPropertyViewModelTests<double, FloatingPropertyViewModel> + : NumericViewModelTests<double> { protected override double GetRandomTestValue (Random rand) { return rand.NextDouble (); } - protected override FloatingPropertyViewModel GetViewModel (TargetPlatform platform, IPropertyInfo property, IEnumerable<IObjectEditor> editors) + protected override NumericPropertyViewModel<double> GetViewModel (TargetPlatform platform, IPropertyInfo property, IEnumerable<IObjectEditor> editors) { - return new FloatingPropertyViewModel (platform, property, editors); + return new NumericPropertyViewModel<double> (platform, property, editors); } protected override Tuple<double, double> MaxMin => new Tuple<double, double> (Double.MaxValue, Double.MinValue); diff --git a/Xamarin.PropertyEditing.Tests/IntegerPropertyViewModelTests.cs b/Xamarin.PropertyEditing.Tests/IntegerPropertyViewModelTests.cs index 88aedf5..e8eae00 100644 --- a/Xamarin.PropertyEditing.Tests/IntegerPropertyViewModelTests.cs +++ b/Xamarin.PropertyEditing.Tests/IntegerPropertyViewModelTests.cs @@ -7,7 +7,7 @@ namespace Xamarin.PropertyEditing.Tests { [TestFixture] internal class IntegerPropertyViewModelTests - : ConstrainedPropertyViewModelTests<long, IntegerPropertyViewModel> + : NumericViewModelTests<long> { protected override Tuple<long, long> MaxMin => new Tuple<long, long> (Int64.MaxValue, Int64.MinValue); @@ -42,9 +42,9 @@ namespace Xamarin.PropertyEditing.Tests return value; } - protected override IntegerPropertyViewModel GetViewModel (TargetPlatform platform, IPropertyInfo property, IEnumerable<IObjectEditor> editors) + protected override NumericPropertyViewModel<long> GetViewModel (TargetPlatform platform, IPropertyInfo property, IEnumerable<IObjectEditor> editors) { - return new IntegerPropertyViewModel (platform, property, editors); + return new NumericPropertyViewModel<long> (platform, property, editors); } } } diff --git a/Xamarin.PropertyEditing.Tests/MockControls/MockSampleControl.cs b/Xamarin.PropertyEditing.Tests/MockControls/MockSampleControl.cs index 22f491f..19704bf 100644 --- a/Xamarin.PropertyEditing.Tests/MockControls/MockSampleControl.cs +++ b/Xamarin.PropertyEditing.Tests/MockControls/MockSampleControl.cs @@ -8,8 +8,8 @@ namespace Xamarin.PropertyEditing.Tests.MockControls public MockSampleControl() { AddProperty<bool> ("Boolean", ReadWrite); - AddProperty<long> ("Integer", ReadWrite); - AddProperty<double> ("FloatingPoint", ReadWrite); + AddProperty<int> ("Integer", ReadWrite); + AddProperty<float> ("FloatingPoint", ReadWrite); AddProperty<string> ("String", ReadWrite); AddProperty<Enumeration> ("Enumeration", ReadWrite); AddProperty<Flags> ("Flags", ReadWrite, canWrite: true, flag: true); @@ -19,8 +19,8 @@ namespace Xamarin.PropertyEditing.Tests.MockControls // AddProperty<CommonThickness> ("Thickness", ReadWrite); // Lacking support on the mac at this point in time. AddReadOnlyProperty<bool> ("ReadOnlyBoolean", ReadOnly); - AddReadOnlyProperty<long> ("ReadOnlyInteger", ReadOnly); - AddReadOnlyProperty<double> ("ReadOnlyFloatingPoint", ReadOnly); + AddReadOnlyProperty<int> ("ReadOnlyInteger", ReadOnly); + AddReadOnlyProperty<float> ("ReadOnlyFloatingPoint", ReadOnly); AddReadOnlyProperty<string> ("ReadOnlyString", ReadOnly); AddReadOnlyProperty<Enumeration> ("ReadOnlyEnumeration", ReadOnly); AddProperty<Flags> ("ReadOnlyFlags", ReadOnly, canWrite: false, flag: true); diff --git a/Xamarin.PropertyEditing.Tests/NumericTests.cs b/Xamarin.PropertyEditing.Tests/NumericTests.cs new file mode 100644 index 0000000..1b2054b --- /dev/null +++ b/Xamarin.PropertyEditing.Tests/NumericTests.cs @@ -0,0 +1,125 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using NUnit.Framework; + +namespace Xamarin.PropertyEditing.Tests +{ + [TestFixture] + internal class NumericTests + { + [Test] + public void SByte () + { + sbyte v = 0; + v = Numeric<sbyte>.Increment (v); + Assert.That (v, Is.EqualTo (1)); + v = Numeric<sbyte>.Decrement (v); + Assert.That (v, Is.EqualTo (0)); + v = Numeric<sbyte>.Decrement (v); + Assert.That (v, Is.EqualTo (-1)); + } + + [Test] + public void Byte () + { + byte v = 0; + v = Numeric<byte>.Increment (v); + Assert.That (v, Is.EqualTo (1)); + v = Numeric<byte>.Decrement (v); + Assert.That (v, Is.EqualTo (0)); + } + + [Test] + public void Int16 () + { + short v = 0; + v = Numeric<short>.Increment (v); + Assert.That (v, Is.EqualTo (1)); + v = Numeric<short>.Decrement (v); + Assert.That (v, Is.EqualTo (0)); + v = Numeric<short>.Decrement (v); + Assert.That (v, Is.EqualTo (-1)); + } + + [Test] + public void UIn16 () + { + ushort v = 0; + v = Numeric<ushort>.Increment (v); + Assert.That (v, Is.EqualTo (1)); + v = Numeric<ushort>.Decrement (v); + Assert.That (v, Is.EqualTo (0)); + } + + [Test] + public void Int32 () + { + int v = 0; + v = Numeric<int>.Increment (v); + Assert.That (v, Is.EqualTo (1)); + v = Numeric<int>.Decrement (v); + Assert.That (v, Is.EqualTo (0)); + v = Numeric<int>.Decrement (v); + Assert.That (v, Is.EqualTo (-1)); + } + + [Test] + public void UIn32 () + { + uint v = 0; + v = Numeric<uint>.Increment (v); + Assert.That (v, Is.EqualTo (1)); + v = Numeric<uint>.Decrement (v); + Assert.That (v, Is.EqualTo (0)); + } + + [Test] + public void Int64() + { + long v = 0; + v = Numeric<long>.Increment (v); + Assert.That (v, Is.EqualTo (1)); + v = Numeric<long>.Decrement (v); + Assert.That (v, Is.EqualTo (0)); + v = Numeric<long>.Decrement (v); + Assert.That (v, Is.EqualTo (-1)); + } + + [Test] + public void UIn64 () + { + ulong v = 0; + v = Numeric<ulong>.Increment (v); + Assert.That (v, Is.EqualTo (1)); + v = Numeric<ulong>.Decrement (v); + Assert.That (v, Is.EqualTo (0)); + } + + [Test] + public void Single() + { + float v = 0; + v = Numeric<float>.Increment (v); + Assert.That (v, Is.EqualTo (1)); + v = Numeric<float>.Decrement (v); + Assert.That (v, Is.EqualTo (0)); + v = Numeric<float>.Decrement (v); + Assert.That (v, Is.EqualTo (-1)); + } + + [Test] + public void Double() + { + double v = 0; + v = Numeric<double>.Increment (v); + Assert.That (v, Is.EqualTo (1)); + v = Numeric<double>.Decrement (v); + Assert.That (v, Is.EqualTo (0)); + v = Numeric<double>.Decrement (v); + Assert.That (v, Is.EqualTo (-1)); + } + } +} diff --git a/Xamarin.PropertyEditing.Tests/NumericViewModelTests.cs b/Xamarin.PropertyEditing.Tests/NumericViewModelTests.cs new file mode 100644 index 0000000..35575b8 --- /dev/null +++ b/Xamarin.PropertyEditing.Tests/NumericViewModelTests.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Moq; +using NUnit.Framework; +using Xamarin.PropertyEditing.ViewModels; + +namespace Xamarin.PropertyEditing.Tests +{ + internal abstract class NumericViewModelTests<T> + : ConstrainedPropertyViewModelTests<T, NumericPropertyViewModel<T>> + where T : struct, IComparable<T> + { + [Test] + public void SelfConstrainedValuesCommands () + { + T max, min; + T value = GetConstrainedRandomValue (Random, out max, out min); + Assume.That (max, Is.GreaterThan (min)); + Assume.That (value, Is.LessThan (max)); + Assume.That (value, Is.GreaterThan (min)); + + var mockProperty = new Mock<IPropertyInfo> (); + var constrainedMock = mockProperty.As<ISelfConstrainedPropertyInfo<T>> (); + constrainedMock.SetupGet (pi => pi.MaxValue).Returns (max); + constrainedMock.SetupGet (pi => pi.MinValue).Returns (min); + + var vm = GetViewModel (mockProperty.Object, new MockObjectEditor (mockProperty.Object)); + Assume.That (vm.MaximumValue, Is.EqualTo (max)); + Assume.That (vm.MinimumValue, Is.EqualTo (min)); + + vm.Value = value; + Assume.That (vm.Value, Is.EqualTo (value)); + + Assume.That (vm.RaiseValue, Is.Not.Null); + Assert.That (vm.RaiseValue.CanExecute (null), Is.True, "RaiseValue can not execute"); + Assert.That (vm.LowerValue.CanExecute (null), Is.True, "LowerValue can not execute"); + } + + [Test] + public void SelfConstrainedBelowCommands () + { + T max, min; + T value = GetConstrainedRandomValueBelowBounds (Random, out max, out min); + Assume.That (max, Is.GreaterThan (min)); + Assume.That (value, Is.LessThan (max)); + Assume.That (value, Is.LessThan (min)); + + var mockProperty = new Mock<IPropertyInfo> (); + var constrainedMock = mockProperty.As<ISelfConstrainedPropertyInfo<T>> (); + constrainedMock.SetupGet (pi => pi.MaxValue).Returns (max); + constrainedMock.SetupGet (pi => pi.MinValue).Returns (min); + + var vm = GetViewModel (mockProperty.Object, new MockObjectEditor (mockProperty.Object)); + + vm.Value = value; + Assume.That (vm.Value, Is.EqualTo (min)); + + Assume.That (vm.RaiseValue, Is.Not.Null); + Assert.That (vm.RaiseValue.CanExecute (null), Is.True, "Should be able to RaiseValue"); + Assert.That (vm.LowerValue.CanExecute (null), Is.False, "Should not be able to LowerValue"); + } + + [Test] + public void SelfConstrainedAboveCommands () + { + T max, min; + T value = GetConstrainedRandomValueAboveBounds (Random, out max, out min); + Assume.That (max, Is.GreaterThan (min)); + Assume.That (value, Is.GreaterThan (max)); + Assume.That (value, Is.GreaterThan (min)); + + var mockProperty = new Mock<IPropertyInfo> (); + var constrainedMock = mockProperty.As<ISelfConstrainedPropertyInfo<T>> (); + constrainedMock.SetupGet (pi => pi.MaxValue).Returns (max); + constrainedMock.SetupGet (pi => pi.MinValue).Returns (min); + + var vm = GetViewModel (mockProperty.Object, new MockObjectEditor (mockProperty.Object)); + + vm.Value = value; + Assume.That (vm.Value, Is.EqualTo (max)); + + Assume.That (vm.RaiseValue, Is.Not.Null); + Assert.That (vm.RaiseValue.CanExecute (null), Is.False, "Should not be able to RaiseValue"); + Assert.That (vm.LowerValue.CanExecute (null), Is.True, "Should be able to LowerValue"); + } + } +} diff --git a/Xamarin.PropertyEditing.Tests/Xamarin.PropertyEditing.Tests.csproj b/Xamarin.PropertyEditing.Tests/Xamarin.PropertyEditing.Tests.csproj index a15a236..d35a070 100644 --- a/Xamarin.PropertyEditing.Tests/Xamarin.PropertyEditing.Tests.csproj +++ b/Xamarin.PropertyEditing.Tests/Xamarin.PropertyEditing.Tests.csproj @@ -68,6 +68,8 @@ <Compile Include="BrushPropertyViewModelTests.cs" /> <Compile Include="BytePropertyViewModelTests.cs" /> <Compile Include="MockControls\MockResourceProvider.cs" /> + <Compile Include="NumericTests.cs" /> + <Compile Include="NumericViewModelTests.cs" /> <Compile Include="ResourceSelectorViewModelTests.cs" /> <Compile Include="SimpleCollectionViewTests.cs" /> <Compile Include="CommonColorTests.cs" /> |