summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core.UnitTests/CommandTests.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Xamarin.Forms.Core.UnitTests/CommandTests.cs')
-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");
+ }
}
}