Example #1
0
func (s *BasicSuite) TestRootText(c *C) {
	node, err := xmlpath.Parse(bytes.NewBuffer(trivialXml))
	c.Assert(err, IsNil)
	path := xmlpath.MustCompile("/")
	result, ok := path.String(node)
	c.Assert(ok, Equals, true)
	c.Assert(result, Equals, "abcdefg")
}
Example #2
0
func (s *BasicSuite) TestLibraryTable(c *C) {
	node, err := xmlpath.Parse(bytes.NewBuffer(libraryXml))
	c.Assert(err, IsNil)
	for _, test := range libraryTable {
		cmt := Commentf("xml path: %s", test.path)
		path, err := xmlpath.Compile(test.path)
		if want, ok := test.result.(cerror); ok {
			c.Assert(err, ErrorMatches, string(want), cmt)
			c.Assert(path, IsNil, cmt)
			continue
		}
		c.Assert(err, IsNil)
		switch want := test.result.(type) {
		case string:
			got, ok := path.String(node)
			c.Assert(ok, Equals, true, cmt)
			c.Assert(got, Equals, want, cmt)
			c.Assert(path.Exists(node), Equals, true, cmt)
			iter := path.Iter(node)
			iter.Next()
			node := iter.Node()
			c.Assert(node.String(), Equals, want, cmt)
			c.Assert(string(node.Bytes()), Equals, want, cmt)
		case []string:
			var alls []string
			var allb []string
			iter := path.Iter(node)
			for iter.Next() {
				alls = append(alls, iter.Node().String())
				allb = append(allb, string(iter.Node().Bytes()))
			}
			c.Assert(alls, DeepEquals, want, cmt)
			c.Assert(allb, DeepEquals, want, cmt)
			s, sok := path.String(node)
			b, bok := path.Bytes(node)
			if len(want) == 0 {
				c.Assert(sok, Equals, false, cmt)
				c.Assert(bok, Equals, false, cmt)
				c.Assert(s, Equals, "")
				c.Assert(b, IsNil)
			} else {
				c.Assert(sok, Equals, true, cmt)
				c.Assert(bok, Equals, true, cmt)
				c.Assert(s, Equals, alls[0], cmt)
				c.Assert(string(b), Equals, alls[0], cmt)
				c.Assert(path.Exists(node), Equals, true, cmt)
			}
		case exists:
			wantb := bool(want)
			ok := path.Exists(node)
			c.Assert(ok, Equals, wantb, cmt)
			_, ok = path.String(node)
			c.Assert(ok, Equals, wantb, cmt)
		}
	}
}
Example #3
0
func (s *BasicSuite) BenchmarkSimplePathExists(c *C) {
	node, err := xmlpath.Parse(bytes.NewBuffer(instancesXml))
	c.Assert(err, IsNil)
	path := xmlpath.MustCompile("/DescribeInstancesResponse/reservationSet/item/instancesSet/item/instanceType")
	var exists bool
	c.ResetTimer()
	for i := 0; i < c.N; i++ {
		exists = path.Exists(node)
	}
	c.StopTimer()
	c.Assert(exists, Equals, true)
}
Example #4
0
func (s *BasicSuite) BenchmarkSimplePathString(c *C) {
	node, err := xmlpath.Parse(bytes.NewBuffer(instancesXml))
	c.Assert(err, IsNil)
	path := xmlpath.MustCompile("/DescribeInstancesResponse/reservationSet/item/instancesSet/item/instanceType")
	var str string
	c.ResetTimer()
	for i := 0; i < c.N; i++ {
		str, _ = path.String(node)
	}
	c.StopTimer()
	c.Assert(str, Equals, "m1.small")
}
Example #5
0
func getConfigValueFromXpath(path, xpath string) (string, error) {
	file, err := os.Open(path + "/config.pvs")
	if err != nil {
		return "", err
	}
	xpathComp := xmlpath.MustCompile(xpath)
	root, err := xmlpath.Parse(file)
	if err != nil {
		return "", err
	}
	value, _ := xpathComp.String(root)
	return value, nil
}
Example #6
0
func (s *BasicSuite) BenchmarkParse(c *C) {
	for i := 0; i < c.N; i++ {
		_, err := xmlpath.Parse(bytes.NewBuffer(instancesXml))
		c.Assert(err, IsNil)
	}
}