Exemple #1
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

}
Exemple #2
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

}
Exemple #3
0
func TestCrazyMove(t *testing.T) {
	input := `
<html>
<body>
<div id="foo" name="foo1"> 
<div id="bar" name="bar1"></div>
<div id="foo" name="foo2"></div>
</div>
<div id="bar" name="bar2"></div>
</body>
</html>`
	doc, err := Parse([]byte(input), DefaultEncodingBytes, nil, DefaultParseOption, DefaultEncodingBytes)

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

	foos, err := doc.Search("//div[@id='foo']")
	if err != nil {
		t.Error("search has error:", err)
		return
	}
	for _, foo := range foos {
		bars, _ := foo.Search("//div[@id='bar']")
		for _, bar := range bars {
			bar.AddChild(foo)
		}
	}

	doc.Free()
	help.CheckXmlMemoryLeaks(t)
}
Exemple #4
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)
}
Exemple #5
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)
}
Exemple #6
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()
}
Exemple #7
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()
}
Exemple #8
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")
	}

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

	doc.Free()
	help.CheckXmlMemoryLeaks(t)
}
Exemple #9
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()
}