summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core.UnitTests
diff options
context:
space:
mode:
authorE.Z. Hart <hartez@users.noreply.github.com>2016-12-01 14:15:17 -0700
committerStephane Delcroix <stephane@delcroix.org>2016-12-01 22:15:17 +0100
commit46c25a2edbf257153d7bb33d2ac3997a3718e3ee (patch)
tree92209a25a152c11af33684c1e1933514ac4c407a /Xamarin.Forms.Core.UnitTests
parent3d85653f270854b4aab82e45e6e58afb760feb85 (diff)
downloadxamarin-forms-46c25a2edbf257153d7bb33d2ac3997a3718e3ee.tar.gz
xamarin-forms-46c25a2edbf257153d7bb33d2ac3997a3718e3ee.tar.bz2
xamarin-forms-46c25a2edbf257153d7bb33d2ac3997a3718e3ee.zip
Don't run Command CanExecute on incorrect inherited binding context type (#572)
* Allow Command CanExecute to recover when run on inherited bindingcontext * Make exception handler more generic * Checking types in Command delegates to avoid exception in the first place * Adding type chekc to other Command constructor * Use nameof for ArgumentNullExceptions * Add unit tests for null parameters, handle value types and Nullable<T>
Diffstat (limited to 'Xamarin.Forms.Core.UnitTests')
-rw-r--r--Xamarin.Forms.Core.UnitTests/CommandTests.cs100
1 files changed, 99 insertions, 1 deletions
diff --git a/Xamarin.Forms.Core.UnitTests/CommandTests.cs b/Xamarin.Forms.Core.UnitTests/CommandTests.cs
index 1182fe40..c653b976 100644
--- a/Xamarin.Forms.Core.UnitTests/CommandTests.cs
+++ b/Xamarin.Forms.Core.UnitTests/CommandTests.cs
@@ -1,7 +1,6 @@
using System;
using NUnit.Framework;
-
namespace Xamarin.Forms.Core.UnitTests
{
[TestFixture]
@@ -151,5 +150,104 @@ namespace Xamarin.Forms.Core.UnitTests
Assert.AreEqual (expected, cmd.CanExecute ("Foo"));
Assert.AreEqual ("Foo", result);
}
+
+ class FakeParentContext
+ {
+ }
+
+ // ReSharper disable once ClassNeverInstantiated.Local
+ class FakeChildContext
+ {
+ }
+
+ [Test]
+ public void CanExecuteReturnsFalseIfParameterIsWrongReferenceType()
+ {
+ var command = new Command<FakeChildContext>(context => { }, context => true);
+
+ Assert.IsFalse(command.CanExecute(new FakeParentContext()), "the parameter is of the wrong type");
+ }
+
+ [Test]
+ public void CanExecuteReturnsFalseIfParameterIsWrongValueType()
+ {
+ var command = new Command<int>(context => { }, context => true);
+
+ Assert.IsFalse(command.CanExecute(10.5), "the parameter is of the wrong type");
+ }
+
+ [Test]
+ public void CanExecuteUsesParameterIfReferenceTypeAndSetToNull()
+ {
+ var command = new Command<FakeChildContext>(context => { }, context => true);
+
+ Assert.IsTrue(command.CanExecute(null), "null is a valid value for a reference type");
+ }
+
+ [Test]
+ public void CanExecuteUsesParameterIfNullableAndSetToNull()
+ {
+ var command = new Command<int?>(context => { }, context => true);
+
+ Assert.IsTrue(command.CanExecute(null), "null is a valid value for a Nullable<int> type");
+ }
+
+ [Test]
+ public void CanExecuteIgnoresParameterIfValueTypeAndSetToNull()
+ {
+ var command = new Command<int>(context => { }, context => true);
+
+ Assert.IsFalse(command.CanExecute(null), "null is not a valid valid for int");
+ }
+
+ [Test]
+ public void ExecuteDoesNotRunIfParameterIsWrongReferenceType()
+ {
+ int executions = 0;
+ var command = new Command<FakeChildContext>(context => executions += 1);
+
+ Assert.DoesNotThrow(() => command.Execute(new FakeParentContext()), "the command should not execute, so no exception should be thrown");
+ Assert.IsTrue(executions == 0, "the command should not have executed");
+ }
+
+ [Test]
+ public void ExecuteDoesNotRunIfParameterIsWrongValueType()
+ {
+ int executions = 0;
+ var command = new Command<int>(context => executions += 1);
+
+ Assert.DoesNotThrow(() => command.Execute(10.5), "the command should not execute, so no exception should be thrown");
+ Assert.IsTrue(executions == 0, "the command should not have executed");
+ }
+
+ [Test]
+ public void ExecuteRunsIfReferenceTypeAndSetToNull()
+ {
+ int executions = 0;
+ var command = new Command<FakeChildContext>(context => executions += 1);
+
+ Assert.DoesNotThrow(() => command.Execute(null), "null is a valid value for a reference type");
+ Assert.IsTrue(executions == 1, "the command should have executed");
+ }
+
+ [Test]
+ public void ExecuteRunsIfNullableAndSetToNull()
+ {
+ int executions = 0;
+ var command = new Command<int?>(context => executions += 1);
+
+ Assert.DoesNotThrow(() => command.Execute(null), "null is a valid value for a Nullable<int> type");
+ Assert.IsTrue(executions == 1, "the command should have executed");
+ }
+
+ [Test]
+ public void ExecuteDoesNotRunIfValueTypeAndSetToNull()
+ {
+ int executions = 0;
+ var command = new Command<int>(context => executions += 1);
+
+ Assert.DoesNotThrow(() => command.Execute(null), "null is not a valid value for int");
+ Assert.IsTrue(executions == 0, "the command should not have executed");
+ }
}
}