2017-08-28 20:56:18 -05:00
|
|
|
// Copyright 2017 Baliance. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by the terms of the Affero GNU General
|
|
|
|
// Public License version 3.0 as published by the Free Software Foundation and
|
|
|
|
// appearing in the file LICENSE included in the packaging of this file. A
|
|
|
|
// commercial license can be purchased by contacting sales@baliance.com.
|
|
|
|
|
|
|
|
package document_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
2018-01-24 18:59:09 -06:00
|
|
|
"baliance.com/gooxml/common"
|
2017-08-28 20:56:18 -05:00
|
|
|
"baliance.com/gooxml/document"
|
|
|
|
"baliance.com/gooxml/testhelper"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSimpleDoc(t *testing.T) {
|
|
|
|
doc := document.New()
|
|
|
|
para := doc.AddParagraph()
|
|
|
|
run := para.AddRun()
|
|
|
|
run.AddText("foo")
|
|
|
|
got := bytes.Buffer{}
|
|
|
|
if err := doc.Validate(); err != nil {
|
|
|
|
t.Errorf("created an invalid document: %s", err)
|
|
|
|
}
|
|
|
|
doc.Save(&got)
|
|
|
|
testhelper.CompareGoldenZip(t, "simple-1.docx", got.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOpen(t *testing.T) {
|
|
|
|
wb, err := document.Open("testdata/simple-1.docx")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error opening document: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
got := bytes.Buffer{}
|
|
|
|
if err := wb.Validate(); err != nil {
|
|
|
|
t.Errorf("created an invalid document: %s", err)
|
|
|
|
}
|
|
|
|
wb.Save(&got)
|
2017-08-29 17:27:02 -05:00
|
|
|
testhelper.CompareZip(t, "simple-1.docx", got.Bytes(), true)
|
2017-08-28 20:56:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestOpenHeaderFooter(t *testing.T) {
|
|
|
|
wb, err := document.Open("testdata/header-footer-multiple.docx")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error opening document: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
got := bytes.Buffer{}
|
|
|
|
if err := wb.Validate(); err != nil {
|
|
|
|
t.Errorf("created an invalid document: %s", err)
|
|
|
|
}
|
|
|
|
wb.Save(&got)
|
2017-08-29 16:55:03 -05:00
|
|
|
testhelper.CompareGoldenZip(t, "header-footer-multiple.docx", got.Bytes())
|
2017-08-28 20:56:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddParagraph(t *testing.T) {
|
|
|
|
doc := document.New()
|
|
|
|
if len(doc.Paragraphs()) != 0 {
|
|
|
|
t.Errorf("expected 0 paragraphs, got %d", len(doc.Paragraphs()))
|
|
|
|
}
|
|
|
|
doc.AddParagraph()
|
|
|
|
if len(doc.Paragraphs()) != 1 {
|
|
|
|
t.Errorf("expected 1 paragraphs, got %d", len(doc.Paragraphs()))
|
|
|
|
}
|
|
|
|
doc.AddParagraph()
|
|
|
|
if len(doc.Paragraphs()) != 2 {
|
|
|
|
t.Errorf("expected 2 paragraphs, got %d", len(doc.Paragraphs()))
|
|
|
|
}
|
|
|
|
}
|
2017-08-29 17:27:02 -05:00
|
|
|
|
|
|
|
func TestOpenWord2016(t *testing.T) {
|
|
|
|
doc, err := document.Open("../testdata/Office2016/Word-Windows.docx")
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error opening Windows Word 2016 document: %s", err)
|
|
|
|
}
|
|
|
|
got := bytes.Buffer{}
|
|
|
|
if err := doc.Save(&got); err != nil {
|
|
|
|
t.Errorf("error saving W216 file: %s", err)
|
|
|
|
}
|
|
|
|
testhelper.CompareGoldenZipFilesOnly(t, "../../testdata/Office2016/Word-Windows.docx", got.Bytes())
|
|
|
|
}
|
2017-09-28 18:12:22 -05:00
|
|
|
|
|
|
|
func TestInsertParagraph(t *testing.T) {
|
|
|
|
doc := document.New()
|
|
|
|
if len(doc.Paragraphs()) != 0 {
|
|
|
|
t.Errorf("expected 0 paragraphs, got %d", len(doc.Paragraphs()))
|
|
|
|
}
|
|
|
|
p := doc.AddParagraph()
|
|
|
|
before := doc.InsertParagraphBefore(p)
|
|
|
|
after := doc.InsertParagraphAfter(p)
|
|
|
|
if len(doc.Paragraphs()) != 3 {
|
|
|
|
t.Errorf("expected 3 paragraphs, got %d", len(doc.Paragraphs()))
|
|
|
|
}
|
|
|
|
if doc.Paragraphs()[0].X() != before.X() {
|
|
|
|
t.Error("InsertParagraphBefore failed")
|
|
|
|
}
|
|
|
|
if doc.Paragraphs()[2].X() != after.X() {
|
|
|
|
t.Error("InsertParagraphAfter failed")
|
|
|
|
}
|
|
|
|
}
|
2017-09-28 19:01:03 -05:00
|
|
|
|
2018-05-25 02:15:07 +08:00
|
|
|
func TestInsertTable(t *testing.T) {
|
|
|
|
doc := document.New()
|
|
|
|
if len(doc.Paragraphs()) != 0 {
|
|
|
|
t.Errorf("expected 0 paragraphs, got %d", len(doc.Paragraphs()))
|
|
|
|
}
|
2018-05-25 03:18:38 +08:00
|
|
|
p1 := doc.AddParagraph()
|
|
|
|
p2 := doc.AddParagraph()
|
|
|
|
beforeP1 := doc.InsertTableBefore(p1)
|
|
|
|
afterP1 := doc.InsertTableAfter(p1)
|
|
|
|
beforeP2 := doc.InsertTableBefore(p2)
|
|
|
|
afterP2 := doc.InsertTableAfter(p2)
|
|
|
|
if len(doc.Tables()) != 4 {
|
|
|
|
t.Errorf("expected 4 tables, got %d", len(doc.Tables()))
|
|
|
|
}
|
|
|
|
if doc.Tables()[0].X() != beforeP1.X() {
|
|
|
|
t.Error("InsertTableBefore 1st paragraph failed")
|
|
|
|
}
|
|
|
|
if doc.Tables()[1].X() != afterP1.X() {
|
|
|
|
t.Error("InsertTableAfter 1st paragraph failed")
|
|
|
|
}
|
|
|
|
if doc.Tables()[2].X() != beforeP2.X() {
|
|
|
|
t.Error("InsertTableBefore 2nd paragraph failed")
|
|
|
|
}
|
|
|
|
if doc.Tables()[3].X() != afterP2.X() {
|
|
|
|
t.Error("InsertTableAfter 2nd paragraph failed")
|
2018-05-25 02:15:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-28 19:01:03 -05:00
|
|
|
func TestInsertRun(t *testing.T) {
|
|
|
|
doc := document.New()
|
|
|
|
if len(doc.Paragraphs()) != 0 {
|
|
|
|
t.Errorf("expected 0 paragraphs, got %d", len(doc.Paragraphs()))
|
|
|
|
}
|
|
|
|
p := doc.AddParagraph()
|
|
|
|
middle := p.AddRun()
|
|
|
|
before := p.InsertRunBefore(middle)
|
|
|
|
after := p.InsertRunAfter(middle)
|
|
|
|
middle.AddText("middle")
|
|
|
|
before.AddText("before")
|
|
|
|
after.AddText("after")
|
|
|
|
if len(p.Runs()) != 3 {
|
|
|
|
t.Errorf("expected 3 runs, got %d", len(p.Runs()))
|
|
|
|
}
|
|
|
|
if p.Runs()[0].X() != before.X() {
|
|
|
|
t.Error("InsertParagraphBefore failed")
|
|
|
|
}
|
|
|
|
if p.Runs()[2].X() != after.X() {
|
|
|
|
t.Error("InsertParagraphAfter failed")
|
|
|
|
}
|
|
|
|
|
2017-10-10 17:57:44 -04:00
|
|
|
p.RemoveRun(after)
|
|
|
|
|
2017-09-28 19:01:03 -05:00
|
|
|
if len(p.Runs()) != 2 {
|
|
|
|
t.Errorf("expected 2 runs, got %d", len(p.Runs()))
|
|
|
|
}
|
|
|
|
if p.Runs()[0].X() != before.X() {
|
|
|
|
t.Error("InsertParagraphBefore failed")
|
|
|
|
}
|
2017-10-10 17:57:44 -04:00
|
|
|
p.RemoveRun(before)
|
|
|
|
|
2017-09-28 19:01:03 -05:00
|
|
|
if len(p.Runs()) != 1 {
|
|
|
|
t.Errorf("expected 1 runs, got %d", len(p.Runs()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Runs()[0].X() != middle.X() {
|
|
|
|
t.Errorf("remove failed")
|
|
|
|
}
|
|
|
|
}
|
2017-12-03 11:20:17 -06:00
|
|
|
|
|
|
|
func TestInsertBookmarks(t *testing.T) {
|
|
|
|
doc := document.New()
|
|
|
|
if len(doc.Bookmarks()) != 0 {
|
|
|
|
t.Errorf("expected 0 bookmarks, got %d", len(doc.Bookmarks()))
|
|
|
|
}
|
|
|
|
|
|
|
|
p := doc.AddParagraph()
|
|
|
|
p.AddBookmark("bookmark1")
|
|
|
|
p.AddBookmark("bookmark2")
|
|
|
|
|
|
|
|
if len(doc.Bookmarks()) != 2 {
|
|
|
|
t.Errorf("expected 2 bookmarks, got %d", len(doc.Bookmarks()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDuplicateBookmarks(t *testing.T) {
|
|
|
|
doc := document.New()
|
|
|
|
if len(doc.Bookmarks()) != 0 {
|
|
|
|
t.Errorf("expected 0 bookmarks, got %d", len(doc.Bookmarks()))
|
|
|
|
}
|
|
|
|
|
|
|
|
p := doc.AddParagraph()
|
|
|
|
p.AddBookmark("bookmark1")
|
|
|
|
p.AddBookmark("bookmark1")
|
|
|
|
|
|
|
|
if len(doc.Bookmarks()) != 2 {
|
|
|
|
t.Errorf("expected 2 bookmarks, got %d", len(doc.Bookmarks()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := doc.Validate(); err == nil {
|
|
|
|
t.Errorf("expected error due to duplicate bookmark names")
|
|
|
|
}
|
|
|
|
}
|
2018-01-24 18:59:09 -06:00
|
|
|
|
2018-07-17 00:35:15 +03:00
|
|
|
func TestHeaderAndFooterImages(t *testing.T) {
|
2018-01-24 18:59:09 -06:00
|
|
|
doc := document.New()
|
|
|
|
img1, err := common.ImageFromFile("testdata/gopher.png")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create image: %s", err)
|
|
|
|
}
|
|
|
|
img2, err := common.ImageFromFile("testdata/gophercolor.png")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create image: %s", err)
|
|
|
|
}
|
2019-03-29 17:26:52 +01:00
|
|
|
png3x3 := []byte{
|
|
|
|
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
|
|
|
|
0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
|
|
|
|
0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x03,
|
|
|
|
0x08, 0x02, 0x00, 0x00, 0x00, 0xd9, 0x4a, 0x22,
|
|
|
|
0xe8, 0x00, 0x00, 0x00, 0x1e, 0x49, 0x44, 0x41,
|
|
|
|
0x54, 0x08, 0xd7, 0x63, 0xf8, 0xc5, 0x1e, 0xf8,
|
|
|
|
0x9d, 0xfd, 0xd7, 0x34, 0xf6, 0x5f, 0x0c, 0x10,
|
|
|
|
0x8a, 0x9d, 0xf7, 0x17, 0x03, 0x84, 0x62, 0xf7,
|
|
|
|
0xf9, 0x05, 0x00, 0xd2, 0x6f, 0x0d, 0x71, 0x26,
|
|
|
|
0x33, 0x2f, 0xe1, 0x00, 0x00, 0x00, 0x00, 0x49,
|
|
|
|
0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82,
|
|
|
|
}
|
|
|
|
img3, err := common.ImageFromBytes(png3x3)
|
2018-01-24 18:59:09 -06:00
|
|
|
if err != nil {
|
2019-03-29 17:26:52 +01:00
|
|
|
t.Fatalf("unable to create image: %s", err)
|
2018-01-24 18:59:09 -06:00
|
|
|
}
|
2019-03-29 17:26:52 +01:00
|
|
|
|
|
|
|
dir1, err := doc.AddImage(img1)
|
2018-01-24 18:59:09 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add image to doc: %s", err)
|
|
|
|
}
|
|
|
|
if dir1.RelID() != "rId4" {
|
2018-07-17 00:35:15 +03:00
|
|
|
t.Errorf("expected rId4 != %s", dir1.RelID())
|
2018-01-24 18:59:09 -06:00
|
|
|
}
|
2019-03-29 17:26:52 +01:00
|
|
|
|
|
|
|
dir2, err := doc.AddImage(img2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add image to doc: %s", err)
|
|
|
|
}
|
2018-01-24 18:59:09 -06:00
|
|
|
if dir2.RelID() != "rId5" {
|
2018-07-17 00:35:15 +03:00
|
|
|
t.Errorf("expected rId5 != %s", dir2.RelID())
|
2018-01-24 18:59:09 -06:00
|
|
|
}
|
|
|
|
|
2019-03-29 17:26:52 +01:00
|
|
|
dir3, err := doc.AddImage(img3)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add image to doc: %s", err)
|
|
|
|
}
|
|
|
|
if dir3.RelID() != "rId6" {
|
|
|
|
t.Errorf("expected rId6 != %s", dir3.RelID())
|
|
|
|
}
|
|
|
|
|
2018-01-24 18:59:09 -06:00
|
|
|
hdr := doc.AddHeader()
|
2018-07-17 00:35:15 +03:00
|
|
|
ftr := doc.AddFooter()
|
2019-03-29 17:26:52 +01:00
|
|
|
|
2018-01-24 18:59:09 -06:00
|
|
|
hir1, err := hdr.AddImage(img1)
|
2019-03-29 17:26:52 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add image to header: %s", err)
|
|
|
|
}
|
2018-01-24 18:59:09 -06:00
|
|
|
if hir1.RelID() != "rId1" {
|
2018-07-17 00:35:15 +03:00
|
|
|
t.Errorf("expected rId1 != %s", hir1.RelID())
|
2018-01-24 18:59:09 -06:00
|
|
|
}
|
2019-03-29 17:26:52 +01:00
|
|
|
|
|
|
|
hir2, err := hdr.AddImage(img2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add image to header: %s", err)
|
|
|
|
}
|
2018-01-24 18:59:09 -06:00
|
|
|
if hir2.RelID() != "rId2" {
|
2018-07-17 00:35:15 +03:00
|
|
|
t.Errorf("expected rId2 != %s", hir2.RelID())
|
|
|
|
}
|
2019-03-29 17:26:52 +01:00
|
|
|
|
|
|
|
hir3, err := hdr.AddImage(img3)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add image to header: %s", err)
|
|
|
|
}
|
|
|
|
if hir3.RelID() != "rId3" {
|
|
|
|
t.Errorf("expected rId3 != %s", hir3.RelID())
|
|
|
|
}
|
|
|
|
|
|
|
|
fir1, err := ftr.AddImage(img1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add image to footer: %s", err)
|
|
|
|
}
|
2018-07-17 00:35:15 +03:00
|
|
|
if fir1.RelID() != "rId1" {
|
2019-03-29 17:26:52 +01:00
|
|
|
t.Errorf("expected rId1 != %s", fir1.RelID())
|
|
|
|
}
|
|
|
|
|
|
|
|
fir2, err := ftr.AddImage(img2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add image to footer: %s", err)
|
2018-07-17 00:35:15 +03:00
|
|
|
}
|
|
|
|
if fir2.RelID() != "rId2" {
|
2019-03-29 17:26:52 +01:00
|
|
|
t.Errorf("expected rId2 != %s", fir2.RelID())
|
|
|
|
}
|
|
|
|
|
|
|
|
fir3, err := ftr.AddImage(img3)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to add image to footer: %s", err)
|
|
|
|
}
|
|
|
|
if fir3.RelID() != "rId3" {
|
|
|
|
t.Errorf("expected rId3 != %s", fir3.RelID())
|
2018-01-24 18:59:09 -06:00
|
|
|
}
|
|
|
|
}
|
2018-09-14 14:29:17 -05:00
|
|
|
|
|
|
|
func TestIssue198(t *testing.T) {
|
|
|
|
// this tests the image fixes performed as part of issue 198
|
|
|
|
// where we were breaking jpg images
|
|
|
|
fn := "issue198.docx"
|
|
|
|
doc, err := document.Open("testdata/" + fn)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("error reading %s: %s", fn, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
got := bytes.Buffer{}
|
|
|
|
doc.Save(&got)
|
|
|
|
testhelper.CompareGoldenZip(t, fn+".golden", got.Bytes())
|
|
|
|
}
|