Example #1
0
func TestParseXml(t *testing.T) {
	input := "<foo></foo>"
	expected := `<?xml version="1.0" encoding="utf-8"?>
<foo/>
`
	doc, err := ParseXml([]byte(input))
	if err != nil {
		t.Error("Parsing has error:", err)
		return
	}

	if doc.String() != expected {
		t.Error("the output of the xml doc does not match the expected")
	}

	expected = `<?xml version="1.0" encoding="utf-8"?>
<foo>
  <bar/>
</foo>
`
	doc.Root().AddChild("<bar/>")
	if doc.String() != expected {
		t.Error("the output of the xml doc does not match the expected")
	}
	doc.Free()
	help.CheckXmlMemoryLeaks(t)
}
Example #2
0
func TestParseHtml(t *testing.T) {
	input := "<html><body><div><h1></div>"
	expected := `<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><div><h1></h1></div></body></html>
`
	doc, err := ParseHtml([]byte(input))
	if err != nil {
		t.Error("Parsing has error:", err)
		return
	}
	if doc.String() != expected {
		t.Error("the output of the html doc does not match the expected")
	}

	expected = `<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body><div><h1></h1></div></body>
</html>
`
	doc.Root().FirstChild().AddPreviousSibling("<head></head>")

	if doc.String() != expected {
		println(doc.String())
		t.Error("the output of the html doc does not match the expected")
	}
	doc.Free()
	help.CheckXmlMemoryLeaks(t)
}
Example #3
0
func RunDocumentParseTest(t *testing.T, name string) (error *string) {

	var errorMessage string
	offset := "\t"

	defer help.CheckXmlMemoryLeaks(t)

	input, output, dataError := getTestData(name)

	if len(dataError) > 0 {
		errorMessage += dataError
	}

	doc, err := Parse(input, DefaultEncodingBytes, nil, DefaultParseOption, DefaultEncodingBytes)

	if err != nil {
		errorMessage = fmt.Sprintf("parsing error:%v\n", err)
	}

	if doc.String() != string(output) {
		formattedOutput := offset + strings.Join(strings.Split("["+doc.String()+"]", "\n"), "\n"+offset)
		formattedExpectedOutput := offset + strings.Join(strings.Split("["+string(output)+"]", "\n"), "\n"+offset)
		errorMessage = fmt.Sprintf("%v-- Got --\n%v\n%v-- Expected --\n%v\n", offset, formattedOutput, offset, formattedExpectedOutput)
		testOutput := filepath.Join(name, "test_output.txt")
		ioutil.WriteFile(testOutput, []byte(doc.String()), os.FileMode(0666))
		errorMessage += fmt.Sprintf("%v Output test output to: %v\n", offset, testOutput)
	}
	doc.Free()

	if len(errorMessage) > 0 {
		return &errorMessage
	}
	return nil

}
Example #4
0
func RunParseDocumentWithBufferTest(t *testing.T, name string) (error *string) {
	var errorMessage string
	offset := "\t"

	defer help.CheckXmlMemoryLeaks(t)

	input, output, dataError := getTestData(name)

	if len(dataError) > 0 {
		errorMessage += dataError
	}

	buffer := make([]byte, 500000)

	doc, err := Parse(input, DefaultEncodingBytes, nil, DefaultParseOption, DefaultEncodingBytes)

	if err != nil {
		errorMessage = fmt.Sprintf("parsing error:%v\n", err)
	}

	if string(doc.ToBuffer(buffer)) != string(output) {
		formattedOutput := offset + strings.Join(strings.Split("["+doc.String()+"]", "\n"), "\n"+offset)
		formattedExpectedOutput := offset + strings.Join(strings.Split("["+string(output)+"]", "\n"), "\n"+offset)
		errorMessage = fmt.Sprintf("%v-- Got --\n%v\n%v-- Expected --\n%v\n", offset, formattedOutput, offset, formattedExpectedOutput)
	}
	doc.Free()

	if len(errorMessage) > 0 {
		return &errorMessage
	}
	return nil

}
Example #5
0
func TestInnerScript2(t *testing.T) {
	defer help.CheckXmlMemoryLeaks(t)
	script := `<script>try {
var productNAPage = "",
suppressReviews = "false";
var bvtoken = MACYS.util.Cookie.get("BazaarVoiceToken","GCs");
//bvtoken=bvtoken.substring(0,bvtoken.length-1);
$BV.configure("global", {
userToken: bvtoken,
productId: '531726',
submissionUI: 'LIGHTBOX',
submissionContainerUrl: window.location.href,
allowSamePageSubmission: true,
doLogin: function(callback, success_url) {
MACYS.util.Cookie.set("FORWARDPAGE_KEY",success_url);
window.location = 'https://www.macys.com/signin/index.ognc?fromPage=pdpReviews';
},
doShowContent: function(app, dc, sub, sr) {
if (suppressReviews !== 'true' && app == "PRR") {
MACYS.pdp.showReviewsTab();
} else if (productNAPage !== 'true' && app == "QA") {
MACYS.pdp.showQATab();
}
}
});
if (suppressReviews !== 'true') {
$BV.ui('rr', 'show_reviews', {
});
}
$BV.ui("qa", "show_questions", {
subjectType: 'product'
});
} catch ( e ) { }</script>`

	doc, err := Parse([]byte("<html><body><div><h1></div>"), DefaultEncodingBytes, nil, DefaultParseOption, DefaultEncodingBytes)

	if err != nil {
		t.Error("Parsing has error:", err)
		return
	}

	h1 := doc.Root().FirstChild().FirstChild().FirstChild()
	h1.SetInnerHtml(script)
	if h1.String() != "<h1>"+script+"</h1>" {
		t.Error("script does not match")
	}
	doc.Free()
}
Example #6
0
func TestInnerScript(t *testing.T) {
	defer help.CheckXmlMemoryLeaks(t)

	doc, err := Parse([]byte("<html><body><div><h1></div>"), DefaultEncodingBytes, nil, DefaultParseOption, DefaultEncodingBytes)

	if err != nil {
		t.Error("Parsing has error:", err)
		return
	}

	h1 := doc.Root().FirstChild().FirstChild().FirstChild()
	h1.SetInnerHtml("<script>if (suppressReviews !== 'true' && app == 'PRR') { ok = true; }</script>")
	if h1.String() != "<h1><script>if (suppressReviews !== 'true' && app == 'PRR') { ok = true; }</script></h1>" {
		t.Error("script does not match")
	}
	doc.Free()
}
Example #7
0
func TestParseDocumentFragmentText(t *testing.T) {
	doc, err := Parse(nil, []byte("iso-8859-1"), nil, DefaultParseOption, []byte("iso-8859-1"))
	if err != nil {
		println(err.Error())
	}
	docFragment, err := doc.ParseFragment([]byte("ok\r\n"), nil, DefaultParseOption)
	if err != nil {
		t.Error(err.Error())
		return
	}
	if len(docFragment.Children()) != 1 || docFragment.Children()[0].String() != "ok\r\n" {
		println(docFragment.String())
		t.Error("the children from the fragment text do not match")
	}
	doc.Free()
	help.CheckXmlMemoryLeaks(t)
}
Example #8
0
func TestParseDocumentFragment(t *testing.T) {
	doc, err := Parse(nil, DefaultEncodingBytes, nil, DefaultParseOption, DefaultEncodingBytes)
	if err != nil {
		println(err.Error())
	}
	docFragment, err := doc.ParseFragment([]byte("<div><h1>"), nil, DefaultParseOption)
	if err != nil {
		t.Error(err.Error())
		return
	}
	if len(docFragment.Children()) != 1 || docFragment.Children()[0].String() != "<div><h1></h1></div>" {
		t.Error("the of children from the fragment do not match")
	}

	doc.Free()
	help.CheckXmlMemoryLeaks(t)

}
Example #9
0
func TestParseDocument_CP1252(t *testing.T) {
	input, err := ioutil.ReadFile("./tests/document/encoding/input.html")
	if err != nil {
		t.Error("err:", err.Error())
		return
	}
	doc, err := Parse(input, []byte("windows-1252"), nil, DefaultParseOption, DefaultEncodingBytes)
	if err != nil {
		t.Error("err:", err.Error())
		return
	}
	out := doc.String()
	if index := bytes.IndexByte([]byte(out), byte(146)); index >= 0 {
		t.Error("the output is not properly encoded")
	}
	doc.Free()
	help.CheckXmlMemoryLeaks(t)
}
Example #10
0
func TestEmptyDocument(t *testing.T) {
	expected :=
		`<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">

`
	doc, err := Parse(nil, DefaultEncodingBytes, nil, DefaultParseOption, DefaultEncodingBytes)

	if err != nil {
		t.Error("Parsing has error:", err)
		return
	}

	if doc.String() != expected {
		println(doc.String())
		t.Error("the output of the html doc does not match the empty xml")
	}
	doc.Free()
	help.CheckXmlMemoryLeaks(t)
}
Example #11
0
func RunTest(t *testing.T, suite string, name string, specificLogic func(t *testing.T, doc *XmlDocument), extraAssertions ...func(doc *XmlDocument) (string, string, string)) {
	defer help.CheckXmlMemoryLeaks(t)

	//println("Initiating test:" + suite + ":" + name)

	input, output, error := getTestData(filepath.Join("tests", suite, name))

	if len(error) > 0 {
		t.Errorf("Error gathering test data for %v:\n%v\n", name, error)
		t.FailNow()
	}

	expected := string(output)

	//println("Got raw input/output")

	doc, err := parseInput(input)

	if err != nil {
		t.Error(err.Error())
	}

	//println("parsed input")

	if specificLogic != nil {
		specificLogic(t, doc)
	}
	if doc.String() != expected {
		badOutput(doc.String(), expected)
		t.Error("the output of the xml doc does not match")
	}
	for _, extraAssertion := range extraAssertions {
		actual, expected, message := extraAssertion(doc)

		if actual != expected {
			badOutput(actual, expected)
			t.Error(message)
		}
	}

	doc.Free()
}
Example #12
0
func TestSetValue(t *testing.T) {
	defer help.CheckXmlMemoryLeaks(t)
	doc, err := Parse([]byte("<foo id=\"a\" myname=\"ff\"><bar class=\"shine\"/></foo>"), DefaultEncodingBytes, nil, DefaultParseOption, DefaultEncodingBytes)
	if err != nil {
		t.Error("Parsing has error:", err)
		return
	}
	root := doc.Root()
	attributes := root.Attributes()
	if len(attributes) != 2 || attributes["myname"].String() != "ff" {
		fmt.Printf("%v, %q\n", attributes, attributes["myname"].String())
		t.Error("root's attributes do not match")
	}
	child := root.FirstChild()
	childAttributes := child.Attributes()
	if len(childAttributes) != 1 || childAttributes["class"].String() != "shine" {
		t.Error("child's attributes do not match")
	}
	attributes["myname"].SetValue("new")
	expected :=
		`<foo id="a" myname="new">
  <bar class="shine"/>
</foo>`
	if root.String() != expected {
		println("got:\n", root.String())
		println("expected:\n", expected)
		t.Error("root's new attr do not match")
	}
	attributes["id"].Remove()
	expected =
		`<foo myname="new">
  <bar class="shine"/>
</foo>`

	if root.String() != expected {
		println("got:\n", root.String())
		println("expected:\n", expected)
		t.Error("root's remove attr do not match")
	}
	doc.Free()
}
Example #13
0
func TestSearchDocumentFragment(t *testing.T) {
	doc, err := Parse([]byte("<div class='cool'></div>"), DefaultEncodingBytes, nil, DefaultParseOption, DefaultEncodingBytes)
	if err != nil {
		println(err.Error())
	}
	docFragment, err := doc.ParseFragment([]byte("<div class='cool'><h1>"), nil, DefaultParseOption)
	if err != nil {
		t.Error(err.Error())
		return
	}
	if len(docFragment.Children()) != 1 || docFragment.Children()[0].String() != "<div class=\"cool\"><h1></h1></div>" {
		t.Error("the of children from the fragment do not match")
	}

	nodes, err := docFragment.Search(".//*")
	if err != nil {
		t.Error("fragment search has error")
		return
	}
	if len(nodes) != 2 {
		t.Error("the number of children from the fragment does not match")
	}
	nodes, err = docFragment.Search("//div[@class='cool']")

	if err != nil {
		t.Error("fragment search has error")
		return
	}

	if len(nodes) != 1 {
		println(len(nodes))
		for _, node := range nodes {
			println(node.String())
		}
		t.Error("the number of children from the fragment's document does not match")
	}

	doc.Free()
	help.CheckXmlMemoryLeaks(t)
}
Example #14
0
func TestParseDocument(t *testing.T) {
	expected :=
		`<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><div><h1></h1></div></body></html>
`
	expected_xml :=
		`<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
  <body>
    <div>
      <h1/>
    </div>
  </body>
</html>
`
	doc, err := Parse([]byte("<html><body><div><h1></div>"), DefaultEncodingBytes, nil, DefaultParseOption, DefaultEncodingBytes)

	if err != nil {
		t.Error("Parsing has error:", err)
		return
	}

	if doc.String() != expected {
		println("got:\n", doc.String())
		println("expected:\n", expected)
		t.Error("the output of the html doc does not match")
	}

	s, _ := doc.ToXml(nil, nil)
	if string(s) != expected_xml {
		println("got:\n", string(s))
		println("expected:\n", expected_xml)
		t.Error("the xml output of the html doc does not match")
	}

	doc.Free()
	help.CheckXmlMemoryLeaks(t)
}
Example #15
0
func TestParseDocumentFragment2(t *testing.T) {
	docStr := `<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body>
  </body>
</html>`
	doc, err := Parse([]byte(docStr), DefaultEncodingBytes, nil, DefaultParseOption, DefaultEncodingBytes)
	if err != nil {
		println(err.Error())
	}
	docFragment, err := doc.ParseFragment([]byte("<script>cool & fun</script>"), nil, DefaultParseOption)
	if err != nil {
		t.Error(err.Error())
		return
	}
	if len(docFragment.Children()) != 1 || docFragment.Children()[0].String() != "<script>cool & fun</script>" {
		t.Error("the of children from the fragment do not match")
	}

	doc.Free()
	help.CheckXmlMemoryLeaks(t)
}
Example #16
0
func TestParseDocumentFragmentBasic(t *testing.T) {
	defer help.CheckXmlMemoryLeaks(t)

	doc, err := Parse(nil, DefaultEncodingBytes, nil, DefaultParseOption, DefaultEncodingBytes)
	if err != nil {
		t.Error("parsing error:", err.Error())
		return
	}
	root := doc.Root()
	if root != nil {
		println("root:", root.String())
	}
	docFragment, err := doc.ParseFragment([]byte("hi"), nil, DefaultParseOption)
	if err != nil {
		t.Error(err.Error())
		doc.Free()
		return
	}
	if len(docFragment.Children()) != 1 {
		t.Error("the number of children from the fragment does not match")
	}
	doc.Free()
}
Example #17
0
func TestSearchDocumentFragmentWithEmptyDoc(t *testing.T) {
	defer help.CheckXmlMemoryLeaks(t)

	doc, err := Parse(nil, DefaultEncodingBytes, nil, DefaultParseOption, DefaultEncodingBytes)
	if err != nil {
		t.Error("parsing error:", err.Error())
		return
	}
	docFragment, err := doc.ParseFragment([]byte("<foo></foo><!-- comment here --><bar>fun</bar>"), nil, DefaultParseOption)
	if err != nil {
		t.Error(err.Error())
		doc.Free()
		return
	}
	nodes, err := docFragment.Search(".//*")
	if err != nil {
		t.Error("fragment search has error")
		doc.Free()
		return
	}
	if len(nodes) != 2 {
		t.Error("the number of children from the fragment does not match")
	}
	nodes, err = docFragment.Search("//*")

	if err != nil {
		t.Error("fragment search has error")
		doc.Free()
		return
	}

	if len(nodes) != 0 {
		t.Error("the number of children from the fragment's document does not match")
	}

	doc.Free()
}
Example #18
0
func TestSetEmptyAttribute(t *testing.T) {
	defer help.CheckXmlMemoryLeaks(t)
	doc, err := Parse([]byte("<foo id=\"a\" myname=\"ff\"><bar class=\"shine\"/></foo>"), DefaultEncodingBytes, nil, DefaultParseOption, DefaultEncodingBytes)
	if err != nil {
		t.Error("Parsing has error:", err)
		return
	}
	root := doc.Root()
	attributes := root.Attributes()
	if len(attributes) != 2 || attributes["myname"].String() != "ff" {
		fmt.Printf("%v, %q\n", attributes, attributes["myname"].String())
		t.Error("root's attributes do not match")
	}

	root.SetAttr("", "cool")
	expected :=
		`<foo id="a" myname="ff" ="cool">
  <bar class="shine"/>
</foo>`
	if root.String() != expected {
		println("got:\n", root.String())
		println("expected:\n", expected)
		t.Error("root's new attr do not match")
	}

	root.SetAttr("", "")
	expected =
		`<foo id="a" myname="ff" ="">
  <bar class="shine"/>
</foo>`
	if root.String() != expected {
		println("got:\n", root.String())
		println("expected:\n", expected)
		t.Error("root's new attr do not match")
	}
	doc.Free()
}
Example #19
0
func TestParseDocumentFragment(t *testing.T) {
	defer help.CheckXmlMemoryLeaks(t)

	doc, err := Parse(nil, DefaultEncodingBytes, nil, DefaultParseOption, DefaultEncodingBytes)
	if err != nil {
		t.Error("parsing error:", err.Error())
		return
	}
	docFragment, err := doc.ParseFragment([]byte("<foo></foo><!-- comment here --><bar>fun</bar>"), nil, DefaultParseOption)
	if err != nil {
		t.Error(err.Error())
		doc.Free()
		return
	}
	if docFragment.String() != "<foo/><!-- comment here --><bar>fun</bar>" {
		t.Error("fragment output is wrong\n")
		doc.Free()
		return
	}
	if len(docFragment.Children()) != 3 {
		t.Error("the number of children from the fragment does not match")
	}
	doc.Free()
}