Browse Source

[Test] Added test.json and a test for it

This feature allows someone to just feed in a json file to get a node.
Steve Thielemann 1 year ago
parent
commit
814f756724
2 changed files with 84 additions and 0 deletions
  1. 64 0
      node_test.go
  2. 20 0
      test.json

+ 64 - 0
node_test.go

@@ -314,3 +314,67 @@ func TestNewNode2(t *testing.T) {
 		t.Logf("Node should be nil! %s", pay)
 	}
 }
+
+func TestNewNode3(t *testing.T) {
+	n, err := node.NewNodeFromFile("test.json")
+	if err != nil {
+		t.Fail()
+		t.Logf("Node.NewNodeFromFile(\"test.json\"): %v", err)
+	}
+	if n == nil {
+		if !t.Failed() {
+			t.Fail()
+		}
+		t.Logf("Node expected, got nil.")
+	}
+	if n.Name() != "test" {
+		if !t.Failed() {
+			t.Fail()
+		}
+		t.Logf("Node name should be \"test\", got \"%s\"", n.Name())
+	}
+	if n.Len() != 4 {
+		if !t.Failed() {
+			t.Fail()
+		}
+		t.Logf("Node should have 4 children, got %d", n.Len())
+	}
+	k1 := n.KidByName("kid1")
+	if k1 == nil || k1.Name() != "kid1" {
+		if !t.Failed() {
+			t.Fail()
+		}
+		if k1 != nil {
+			t.Logf("Kid1] KidByName, Expected 'kid1', got '%s'", k1.Name())
+		} else {
+			t.Logf("Kid1] KidByName, Expected 'kid1', got nil!")
+		}
+	}
+	if k1 != nil {
+		if k1.Data().(float64) != 1.0 {
+			if !t.Failed() {
+				t.Fail()
+			}
+			t.Logf("Kid1] Expected 1, got %#v", k1.Data())
+		}
+	}
+	k3 := n.KidByName("kid3")
+	if k3 == nil || k3.Name() != "kid3" {
+		if !t.Failed() {
+			t.Fail()
+		}
+		if k1 != nil {
+			t.Logf("Kid3] KidByName, Expected 'kid3', got '%s'", k1.Name())
+		} else {
+			t.Logf("Kid3] KidByName, Expected 'kid3', got nil!")
+		}
+	}
+	if k3 != nil {
+		if k3.Len() != 2 {
+			if !t.Failed() {
+				t.Fail()
+			}
+			t.Logf("Kid3] Expected 2 children, got %d", k3.Len())
+		}
+	}
+}

+ 20 - 0
test.json

@@ -0,0 +1,20 @@
+{
+    "test": {
+        "kid1": 1,
+        "kid2": [
+            1,
+            2
+        ],
+        "kid3": [
+            {
+                "name": "Taco",
+                "price": 3.75
+            },
+            {
+                "name": "Soda",
+                "price": 1.99
+            }
+        ],
+        "kid4": 4.95
+    }
+}