Some test utils moved to testutils package.

This commit is contained in:
Gunnsteinn Hall 2018-07-17 23:53:28 +00:00
parent 0435dff983
commit 499b0ca888
4 changed files with 101 additions and 89 deletions

View File

@ -0,0 +1,89 @@
/*
* This file is subject to the terms and conditions defined in
* file 'LICENSE.md', which is part of this source code package.
*/
// Package testutils provides test methods that are not intended to be exported.
package testutils
import (
"github.com/unidoc/unidoc/common"
"github.com/unidoc/unidoc/pdf/core"
)
func CompareDictionariesDeep(d1, d2 *core.PdfObjectDictionary) bool {
if len(d1.Keys()) != len(d2.Keys()) {
common.Log.Debug("Dict entries mismatch (%d != %d)", len(d1.Keys()), len(d2.Keys()))
common.Log.Debug("Was '%s' vs '%s'", d1.DefaultWriteString(), d2.DefaultWriteString())
return false
}
for _, k := range d1.Keys() {
if k == "Parent" {
continue
}
v1 := core.TraceToDirectObject(d1.Get(k))
v2 := core.TraceToDirectObject(d2.Get(k))
if v1 == nil {
common.Log.Debug("v1 is nil")
return false
}
if v2 == nil {
common.Log.Debug("v2 is nil")
return false
}
switch t1 := v1.(type) {
case *core.PdfObjectDictionary:
t2, ok := v2.(*core.PdfObjectDictionary)
if !ok {
common.Log.Debug("Type mismatch %T vs %T", v1, v2)
return false
}
if !CompareDictionariesDeep(t1, t2) {
return false
}
continue
case *core.PdfObjectArray:
t2, ok := v2.(*core.PdfObjectArray)
if !ok {
common.Log.Debug("v2 not an array")
return false
}
if t1.Len() != t2.Len() {
common.Log.Debug("array length mismatch (%d != %d)", t1.Len(), t2.Len())
return false
}
for i := 0; i < t1.Len(); i++ {
v1 := core.TraceToDirectObject(t1.Get(i))
v2 := core.TraceToDirectObject(t2.Get(i))
if d1, isD1 := v1.(*core.PdfObjectDictionary); isD1 {
d2, isD2 := v2.(*core.PdfObjectDictionary)
if !isD2 {
return false
}
if !CompareDictionariesDeep(d1, d2) {
return false
}
} else {
if v1.DefaultWriteString() != v2.DefaultWriteString() {
common.Log.Debug("Mismatch '%s' != '%s'", v1.DefaultWriteString(), v2.DefaultWriteString())
return false
}
}
}
continue
}
if v1.String() != v2.String() {
common.Log.Debug("key=%s Mismatch! '%s' != '%s'", k, v1.String(), v2.String())
common.Log.Debug("For '%T' - '%T'", v1, v2)
common.Log.Debug("For '%+v' - '%+v'", v1, v2)
return false
}
}
return true
}

View File

@ -151,7 +151,7 @@ type PdfField struct {
container *core.PdfIndirectObject // Dictionary information stored inside an indirect object.
isTerminal *bool // If set: indicates whether is a terminal field (if null, may not be determined yet).
Parent *PdfField
parent *PdfField
Annotations []*PdfAnnotationWidget
Kids []*PdfField
@ -180,7 +180,7 @@ func (f *PdfField) FullName() (string, error) {
noscanMap := map[*PdfField]bool{}
noscanMap[f] = true
parent := f.Parent
parent := f.parent
for parent != nil {
if _, has := noscanMap[parent]; has {
return fn.String(), errors.New("Recursive traversal")
@ -193,7 +193,7 @@ func (f *PdfField) FullName() (string, error) {
}
noscanMap[parent] = true
parent = parent.Parent
parent = parent.parent
}
for i := len(parts) - 1; i >= 0; i-- {
@ -249,8 +249,8 @@ func (f *PdfField) ToPdfObject() core.PdfObject {
d := container.PdfObject.(*core.PdfObjectDictionary)
d.SetIfNotNil("FT", f.FT)
if f.Parent != nil {
d.Set("Parent", f.Parent.GetContainingPdfObject())
if f.parent != nil {
d.Set("Parent", f.parent.GetContainingPdfObject())
}
if f.Kids != nil {
@ -502,7 +502,7 @@ func (f *PdfField) inherit(eval func(*PdfField) bool) (bool, error) {
}
nodeMap[node] = true
node = node.Parent
node = node.parent
}
return found, nil
@ -589,7 +589,7 @@ func (r *PdfReader) newPdfFieldFromIndirectObject(container *core.PdfIndirectObj
// Additional actions dictionary (Optional)
field.AA = d.Get("AA")
// Type specific types.
// Load type specific fields.
if field.FT != nil {
switch *field.FT {
case "Tx":
@ -635,7 +635,7 @@ func (r *PdfReader) newPdfFieldFromIndirectObject(container *core.PdfIndirectObj
// Set ourself?
if parent != nil {
field.Parent = parent
field.parent = parent
}
field.Annotations = []*PdfAnnotationWidget{}

View File

@ -8,87 +8,10 @@ package model
import (
"testing"
"github.com/unidoc/unidoc/common"
"github.com/unidoc/unidoc/pdf/core"
"github.com/unidoc/unidoc/pdf/internal/testutils"
)
func compareDictionariesDeep(d1, d2 *core.PdfObjectDictionary) bool {
if len(d1.Keys()) != len(d2.Keys()) {
common.Log.Debug("Dict entries mismatch (%d != %d)", len(d1.Keys()), len(d2.Keys()))
common.Log.Debug("Was '%s' vs '%s'", d1.DefaultWriteString(), d2.DefaultWriteString())
return false
}
for _, k := range d1.Keys() {
if k == "Parent" {
continue
}
v1 := core.TraceToDirectObject(d1.Get(k))
v2 := core.TraceToDirectObject(d2.Get(k))
if v1 == nil {
common.Log.Debug("v1 is nil")
return false
}
if v2 == nil {
common.Log.Debug("v2 is nil")
return false
}
switch t1 := v1.(type) {
case *core.PdfObjectDictionary:
t2, ok := v2.(*core.PdfObjectDictionary)
if !ok {
common.Log.Debug("Type mismatch %T vs %T", v1, v2)
return false
}
if !compareDictionariesDeep(t1, t2) {
return false
}
continue
case *core.PdfObjectArray:
t2, ok := v2.(*core.PdfObjectArray)
if !ok {
common.Log.Debug("v2 not an array")
return false
}
if t1.Len() != t2.Len() {
common.Log.Debug("array length mismatch (%d != %d)", t1.Len(), t2.Len())
return false
}
for i := 0; i < t1.Len(); i++ {
v1 := core.TraceToDirectObject(t1.Get(i))
v2 := core.TraceToDirectObject(t2.Get(i))
if d1, isD1 := v1.(*core.PdfObjectDictionary); isD1 {
d2, isD2 := v2.(*core.PdfObjectDictionary)
if !isD2 {
return false
}
if !compareDictionariesDeep(d1, d2) {
return false
}
} else {
if v1.DefaultWriteString() != v2.DefaultWriteString() {
common.Log.Debug("Mismatch '%s' != '%s'", v1.DefaultWriteString(), v2.DefaultWriteString())
return false
}
}
}
continue
}
if v1.String() != v2.String() {
common.Log.Debug("key=%s Mismatch! '%s' != '%s'", k, v1.String(), v2.String())
common.Log.Debug("For '%T' - '%T'", v1, v2)
common.Log.Debug("For '%+v' - '%+v'", v1, v2)
return false
}
}
return true
}
// Test loading of a basic checkbox field with a merged-in annotation.
func TestCheckboxField1(t *testing.T) {
rawText := `
@ -228,7 +151,7 @@ endobj
t.Fatalf("Unable to load expected dict")
}
if !compareDictionariesDeep(expDict, fieldDict) {
if !testutils.CompareDictionariesDeep(expDict, fieldDict) {
t.Fatalf("Mismatch in expected and actual field dictionaries (deep)")
}
}

View File

@ -26,9 +26,9 @@ func NewReaderForText(txt string) *PdfReader {
return r
}
// ParseIndObjSeries loads a series of indirect objects until it runs into an error.
// ParseIndObjSeries loads a series of indirect objects until it runs into an error or EOF.
// Fully loads the objects and traverses resolving references to *PdfIndirectObjects.
// For use in testing.
// For use in testing where a series of indirect objects can be defined sequentially.
func (r *PdfReader) ParseIndObjSeries() error {
for {
obj, err := r.parser.ParseIndirectObject()