Browse Source

Added AnalyzeJSON_EmptyJObject

Piotr Czajkowski 2 years ago
parent
commit
5d63d5675b
2 changed files with 22 additions and 4 deletions
  1. 16 2
      analyzeJSON/AnalyzeJSON.cs
  2. 6 2
      analyzeJSONTests/AnalyzeJSONUnitTests.cs

+ 16 - 2
analyzeJSON/AnalyzeJSON.cs

@@ -6,6 +6,8 @@ using Newtonsoft.Json.Linq;
 
 namespace analyzeJSON
 {
+    public record Status(bool Success = false, string Message = "");
+
     public class AnalyzeJSON
     {
         private readonly JObject json;
@@ -20,6 +22,14 @@ namespace analyzeJSON
             json = JsonConvert.DeserializeObject<JObject>(jsonString);
         }
 
+        public AnalyzeJSON(JObject jObject)
+        {
+            if (jObject == null)
+                throw new ArgumentNullException("jObject");
+
+            json = jObject;
+        }
+
         public static string GetNameFromPath(string tokenPath)
         {
             if (string.IsNullOrWhiteSpace(tokenPath))
@@ -38,12 +48,16 @@ namespace analyzeJSON
             }
         }
 
-        public void Traverse(Action<JToken> action)
+        public Status Traverse(Action<JToken> action)
         {
             if (!json.HasValues)
-                return;
+                return new(false, "JSON is empty!");
+
+            if (action == null)
+                return new(false, "Action can't be null!");
 
             Traverse(json.Children(), action);
+            return new(true, string.Empty);
         }
     }
 }

+ 6 - 2
analyzeJSONTests/AnalyzeJSONUnitTests.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using analyzeJSON;
+using Newtonsoft.Json.Linq;
 using Xunit;
 
 namespace analyzeJSONTests
@@ -10,9 +11,12 @@ namespace analyzeJSONTests
     public class AnalyzeJSONUnitTests
     {
         [Fact]
-        public void AnalyzeJSON_NullPath()
+        public void AnalyzeJSON_EmptyJObject()
         {
-            Assert.Throws<ArgumentNullException>(() => new AnalyzeJSON(null));
+            var test = new AnalyzeJSON(new JObject());
+            var result = test.Traverse((token) => Console.WriteLine(token));
+            Assert.False(result.Success);
+            Assert.NotEmpty(result.Message);
         }
 
         [Fact]