diff --git a/query_test.go b/query_test.go
index 173cdd3..41620a6 100644
--- a/query_test.go
+++ b/query_test.go
@@ -1,6 +1,7 @@
package jsonquery
import (
+ "strings"
"testing"
"github.com/antchfx/xpath"
@@ -102,3 +103,134 @@ func TestNavigator(t *testing.T) {
t.Fatalf("node type is not DocumentNode")
}
}
+
+func TestToXML(t *testing.T) {
+ s := `{
+ "name":"John",
+ "age":31,
+ "female":false
+ }`
+ doc, _ := Parse(strings.NewReader(s))
+ expected := `31falseJohn`
+ if got := doc.OutputXML(); got != expected {
+ t.Fatalf("expected %s, but got %s", expected, got)
+ }
+}
+
+func TestArrayToXML(t *testing.T) {
+ s := `[1,2,3,4]`
+ doc, _ := Parse(strings.NewReader(s))
+ expected := `<1>11><2>22><3>33><4>44>`
+ if got := doc.OutputXML(); got != expected {
+ t.Fatalf("expected %s, but got %s", expected, got)
+ }
+}
+
+func TestNestToArray(t *testing.T) {
+ s := `{
+ "address": {
+ "city": "Nara",
+ "postalCode": "630-0192",
+ "streetAddress": "naist street"
+ },
+ "age": 26,
+ "name": "John",
+ "phoneNumbers": [
+ {
+ "number": "0123-4567-8888",
+ "type": "iPhone"
+ },
+ {
+ "number": "0123-4567-8910",
+ "type": "home"
+ }
+ ]
+ }`
+ doc, _ := Parse(strings.NewReader(s))
+ expected := `Nara630-0192naist street26John0123-4567-8888iPhone0123-4567-8910home`
+ if got := doc.OutputXML(); got != expected {
+ t.Fatalf("expected \n%s, but got \n%s", expected, got)
+ }
+}
+
+func TestQuery(t *testing.T) {
+ doc, err := Parse(strings.NewReader(BooksExample))
+ if err != nil {
+ t.Fatal(err)
+ }
+ q := "/store/bicycle"
+ n := FindOne(doc, q)
+ if n == nil {
+ t.Fatal("should matched 1 but got nil")
+ }
+ q = "/store/bicycle/color"
+ n = FindOne(doc, q)
+ if n == nil {
+ t.Fatal("should matched 1 but got nil")
+ }
+ if n.Data != "color" {
+ t.Fatalf("expected data is color, but got %s", n.Data)
+ }
+}
+
+func TestQueryWhere(t *testing.T) {
+ doc, err := Parse(strings.NewReader(BooksExample))
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ // for number
+ q := "//*[price<=12.99]"
+ list := Find(doc, q)
+
+ if got, expected := len(list), 3; got != expected {
+ t.Fatalf("%s expected %d objects, but got %d", q, expected, got)
+ }
+
+ // for string
+ q = "//*/isbn[text()='0-553-21311-3']"
+ if n := FindOne(doc, q); n == nil {
+ t.Fatal("should matched 1 but got nil")
+ } else if n.Data != "isbn" {
+ t.Fatalf("should matched `isbm` but got %s", n.Data)
+ }
+}
+
+var BooksExample string = `{
+ "store": {
+ "book": [
+ {
+ "category": "reference",
+ "author": "Nigel Rees",
+ "title": "Sayings of the Century",
+ "price": 8.95
+ },
+ {
+ "category": "fiction",
+ "author": "Evelyn Waugh",
+ "title": "Sword of Honour",
+ "price": 12.99
+ },
+ {
+ "category": "fiction",
+ "author": "Herman Melville",
+ "title": "Moby Dick",
+ "isbn": "0-553-21311-3",
+ "price": 8.99
+ },
+ {
+ "category": "fiction",
+ "author": "J. R. R. Tolkien",
+ "title": "The Lord of the Rings",
+ "isbn": "0-395-19395-8",
+ "price": 22.99
+ }
+ ],
+ "bicycle": {
+ "color": "red",
+ "price": 19.95
+ }
+ },
+ "expensive": 10
+ }
+`