1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
|
package x2j
import (
"fmt"
"testing"
)
var doc1 = `
<doc>
<books>
<book seq="1">
<author>William H. Gaddis</author>
<title>The Recognitions</title>
<review>One of the great seminal American novels of the 20th century.</review>
</book>
<book seq="2">
<author>Austin Tappan Wright</author>
<title>Islandia</title>
<review>An example of earlier 20th century American utopian fiction.</review>
</book>
<book seq="3">
<author>John Hawkes</author>
<title>The Beetle Leg</title>
<review>A lyrical novel about the construction of Ft. Peck Dam in Montana.</review>
</book>
<book seq="4">
<author>
<first_name>T.E.</first_name>
<last_name>Porter</last_name>
</author>
<title>King's Day</title>
<review>A magical novella.</review>
</book>
</books>
</doc>
`
var doc2 = `
<doc>
<books>
<author>
<name>William H. Gaddis</name>
<book seq="1">
<title>The Recognitions</title>
<review>One of the great seminal American novels of the 20th century.</review>
</book>
<book>
<title>JR</title>
<review>Won the National Book Award</review>
</book>
</author>
<author>
<name>John Hawkes</name>
<books>
<book>
<title>The Beetle Leg</title>
</book>
<book>
<title>The Blood Oranges</title>
</book>
</books>
</author>
</books>
</doc>
`
var msg1 = `
<msg>
<pub>test</pub>
<text>This is a long cold winter</text>
</msg>`
var msg2 = `
<msgs>
<msg>
<pub>test</pub>
<text>This is a long cold winter</text>
</msg>
<msg>
<pub>test2</pub>
<text>I hope we have a cool summer, though</text>
</msg>
</msgs>`
func TestValuesAtKeyPath(t *testing.T) {
fmt.Println("\n============================ x2jat_test.go")
fmt.Println("\n=============== TestValuesAtKeyPath ...")
fmt.Println("\nValuesAtKeyPath ... doc1#author")
m, _ := DocToMap(doc1)
ss := PathsForKey(m,"author")
fmt.Println("ss:", ss)
for _,v := range ss {
vv := ValuesAtKeyPath(m,v,true)
fmt.Println("vv:", vv)
}
fmt.Println("\nValuesAtKeyPath ... doc1#first_name")
// m, _ := DocToMap(doc1)
ss = PathsForKey(m,"first_name")
fmt.Println("ss:", ss)
for _,v := range ss {
vv := ValuesAtKeyPath(m,v,true)
fmt.Println("vv:", vv)
}
fmt.Println("\nGetKeyPaths...doc2#book")
m, _ = DocToMap(doc2)
ss = PathsForKey(m,"book")
fmt.Println("ss:", ss)
for _,v := range ss {
vv := ValuesAtKeyPath(m,v,true)
fmt.Println("vv:", vv)
}
s := PathForKeyShortest(m,"book")
vv := ValuesAtKeyPath(m,s)
fmt.Println("vv,shortest_path:",vv)
fmt.Println("\nValuesAtKeyPath ... msg1#pub")
m, _ = DocToMap(msg1)
ss = PathsForKey(m,"pub")
fmt.Println("ss:", ss)
for _,v := range ss {
vv := ValuesAtKeyPath(m,v,true)
fmt.Println("vv:", vv)
}
fmt.Println("\nValuesAtKeyPath ... msg2#pub")
m, _ = DocToMap(msg2)
ss = PathsForKey(m,"pub")
fmt.Println("ss:", ss)
for _,v := range ss {
vv := ValuesAtKeyPath(m,v,true)
fmt.Println("vv:", vv)
}
}
func TestValuesAtTagPath(t *testing.T) {
fmt.Println("\n=============== TestValuesAtTagPath ...")
fmt.Println("\nValuesAtTagPath ... doc1#author")
m, _ := DocToMap(doc1)
ss := PathsForKey(m,"author")
fmt.Println("ss:", ss)
for _,v := range ss {
vv,_ := ValuesAtTagPath(doc1,v,true)
fmt.Println("vv:", vv)
}
fmt.Println("\nValuesAtTagPath ... doc1#first_name")
// m, _ := DocToMap(doc1)
ss = PathsForKey(m,"first_name")
fmt.Println("ss:", ss)
for _,v := range ss {
vv,_ := ValuesAtTagPath(doc1,v,true)
fmt.Println("vv:", vv)
}
fmt.Println("\nValuesAtTagPath...doc2#book")
m, _ = DocToMap(doc2)
ss = PathsForKey(m,"book")
fmt.Println("ss:", ss)
for _,v := range ss {
vv,_ := ValuesAtTagPath(doc2,v,true)
fmt.Println("vv:", vv)
}
s := PathForKeyShortest(m,"book")
vv,_ := ValuesAtTagPath(doc2,s)
fmt.Println("vv,shortest_path:",vv)
}
|