Check for null on LinkedListNode extension methods.
authorMichael Welch <michaelgwelch@gmail.com>
Thu, 16 Feb 2012 23:46:32 +0000 (17:46 -0600)
committerMichael Welch <michaelgwelch@gmail.com>
Thu, 16 Feb 2012 23:46:32 +0000 (17:46 -0600)
src.net/BrainmessCore/LinkedListNode.cs
src.net/BrainmessCoreTests/LinkedListNodeTests.cs

index fc64d7d..d9b9094 100644 (file)
@@ -15,6 +15,7 @@ namespace Welch.Brainmess
         /// </summary>
         public static LinkedListNode<T> MoveForward<T>(this LinkedListNode<T> node)
         {
+            if (node == null) throw new ArgumentNullException("node");
             if (node.List == null) throw new ArgumentException("The specified node is not linked into a list", "node");
             return node.Next ?? node.List.AddLast(default(T));
         }
@@ -26,6 +27,7 @@ namespace Welch.Brainmess
         /// </summary>
         public static LinkedListNode<T> MoveBackward<T>(this LinkedListNode<T> node)
         {
+            if (node==null) throw new ArgumentNullException("node");
             if (node.List == null) throw new ArgumentException("The specified node is not linked into a list", "node");
             return node.Previous ?? node.List.AddFirst(default(T));
             
index b461ac5..007c408 100644 (file)
@@ -19,7 +19,7 @@ namespace Welch.Brainmess
             try
             {
                 // Act
-                node = node.MoveForward();
+                node.MoveForward();
 
                 // Assert
                 Assert.Fail("Expected ArgumentException");
@@ -34,13 +34,13 @@ namespace Welch.Brainmess
         public void MoveForward_NextIsNotNull_ExpectTheNextIsReturned()
         {
             // Arrange
-            LinkedList<int> list = new LinkedList<int>(new int[] { 1, 3, 5, 7 });
+            LinkedList<int> list = new LinkedList<int>(new[] { 1, 3, 5, 7 });
             var before = list.Find(3);
             var after = list.Find(7);
 
             // Act
             var cell = before.MoveForward();
-            var expectedCellValue = 5;
+            const int expectedCellValue = 5;
 
             // Assert
             AssertMatchingCell(before, cell, expectedCellValue, after);
@@ -51,19 +51,52 @@ namespace Welch.Brainmess
         public void MoveForward_NextIsNull_ExpectNewDefaultNode()
         {
             // Arrange
-            LinkedList<int> list = new LinkedList<int>(new int[] { 1, 3, 4, 7 });
+            LinkedList<int> list = new LinkedList<int>(new[] { 1, 3, 4, 7 });
             var before = list.Last;
-            LinkedListNode<int> after = null;
 
             // Act
             var cell = before.MoveForward();
-            var expectedCellValue = 0; // default value of a new cell
+            const int expectedCellValue = 0; // default value of a new cell
 
             // Assert
-            AssertMatchingCell(before, cell, expectedCellValue, after);
+            AssertMatchingCell(before, cell, expectedCellValue, null);
         }
 
         [Test]
+        public void MoveForward_WithNullNode_ShouldThrow()
+        {
+            try
+            {
+                LinkedListNode<int> node = null;
+// ReSharper disable ConditionIsAlwaysTrueOrFalse
+                node.MoveForward();
+// ReSharper restore ConditionIsAlwaysTrueOrFalse
+                Assert.Fail("Expected ArgumentNullException");
+
+            } catch(ArgumentNullException)
+            {
+                
+            }
+        }
+
+        [Test]
+        public void MoveBackward_WithNullNode_ShouldThrow()
+        {
+            try
+            {
+                LinkedListNode<int> node = null;
+                // ReSharper disable ConditionIsAlwaysTrueOrFalse
+                node.MoveBackward();
+                // ReSharper restore ConditionIsAlwaysTrueOrFalse
+                Assert.Fail("Expected ArgumentNullException");
+
+            }
+            catch (ArgumentNullException)
+            {
+
+            }
+        }
+        [Test]
         public void MoveBackward_NodeIsNotLinkedToAList_ExpectArgumentException()
         {
             // Arrange
@@ -72,7 +105,7 @@ namespace Welch.Brainmess
             try
             {
                 // Act
-                node = node.MoveBackward();
+                node.MoveBackward();
 
                 // Assert
                 Assert.Fail("Expected ArgumentException");
@@ -93,7 +126,7 @@ namespace Welch.Brainmess
 
             // Act
             var cell = after.MoveBackward();
-            var expectedCellValue = 5;
+            const int expectedCellValue = 5;
 
             // Assert
             AssertMatchingCell(before, cell, expectedCellValue, after);
@@ -106,14 +139,13 @@ namespace Welch.Brainmess
             // Arrange
             LinkedList<int> list = new LinkedList<int>(new [] { 1, 3, 4, 7 });
             var after = list.First;
-            LinkedListNode<int> before = null;
 
             // Act
             var cell = after.MoveBackward();
-            var expectedCellValue = 0; // default value of a new cell
+            const int expectedCellValue = 0; // default value of a new cell
 
             // Assert
-            AssertMatchingCell(before, cell, expectedCellValue, after);
+            AssertMatchingCell(null, cell, expectedCellValue, after);
         }
 
         /// <summary>