...

Source file src/github.com/clbanning/mxj/v2/x2j-wrapper/x2jat_test.go

Documentation: github.com/clbanning/mxj/v2/x2j-wrapper

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

View as plain text