Fix minimal parsing testcase

This commit is contained in:
Gunnsteinn Hall 2018-08-03 11:01:54 +00:00
parent e254fd763c
commit 68a1dee9f2
2 changed files with 80 additions and 37 deletions

View File

@ -9,9 +9,8 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"encoding/hex" "encoding/hex"
//"fmt"
"io" "io"
//"os" "os"
"testing" "testing"
"github.com/unidoc/unidoc/common" "github.com/unidoc/unidoc/common"
@ -681,34 +680,21 @@ func TestObjectParse(t *testing.T) {
} }
} }
/* // TestMinimalPDFFile test basic parsing of a minimal pdf file.
var file1 = "../testfiles/minimal.pdf"
func TestMinimalPDFFile(t *testing.T) { func TestMinimalPDFFile(t *testing.T) {
file, err := os.Open(file1) file, err := os.Open("./testdata/minimal.pdf")
if err != nil { if err != nil {
t.Errorf("Unable to open minimal test file (%s)", err) t.Errorf("Unable to open minimal test file (%s)", err)
return return
} }
defer file.Close() defer file.Close()
reader, err := NewPdfReader(file) parser, err := NewParser(file)
if err != nil { if err != nil {
t.Errorf("Unable to read test file (%s)", err) t.Errorf("Unable to parse test file: %v", err)
return return
} }
numPages, err := reader.GetNumPages()
if err != nil {
t.Error("Unable to get number of pages")
}
fmt.Printf("Num pages: %d\n", numPages)
if numPages != 1 {
t.Error("Wrong number of pages")
}
parser := reader.parser
if len(parser.xrefs) != 4 { if len(parser.xrefs) != 4 {
t.Errorf("Wrong number of xrefs %d != 4", len(parser.xrefs)) t.Errorf("Wrong number of xrefs %d != 4", len(parser.xrefs))
} }
@ -745,54 +731,53 @@ func TestMinimalPDFFile(t *testing.T) {
if !ok { if !ok {
t.Error("Unable to find dictionary") t.Error("Unable to find dictionary")
} }
typename, ok := (*catalogDict)["Type"].(*PdfObjectName) typename, ok := catalogDict.Get("Type").(*PdfObjectName)
if !ok { if !ok {
t.Error("Unable to check type") t.Error("Unable to check type")
} }
if *typename != "Catalog" { if *typename != "Catalog" {
t.Error("Wrong type name (%s != Catalog)", *typename) t.Errorf("Wrong type name (%s != Catalog)", *typename)
} }
// Check Page object. // Check Page object.
pageObj, err := parser.LookupByNumber(3) pageObj, err := parser.LookupByNumber(3)
if err != nil { if err != nil {
t.Error("Unable to look up Page") t.Fatalf("Unable to look up Page")
} }
page, ok := pageObj.(*PdfIndirectObject) page, ok := pageObj.(*PdfIndirectObject)
if !ok { if !ok {
t.Error("Unable to look up Page") t.Fatalf("Unable to look up Page")
} }
pageDict, ok := page.PdfObject.(*PdfObjectDictionary) pageDict, ok := page.PdfObject.(*PdfObjectDictionary)
if !ok { if !ok {
t.Error("Unable to load Page dictionary") t.Fatalf("Unable to load Page dictionary")
} }
if len(*pageDict) != 4 { if len(pageDict.Keys()) != 4 {
t.Error("Page dict should have 4 objects (%d)", len(*pageDict)) t.Fatalf("Page dict should have 4 objects (%d)", len(pageDict.Keys()))
} }
resourcesDict, ok := (*pageDict)["Resources"].(*PdfObjectDictionary) resourcesDict, ok := pageDict.Get("Resources").(*PdfObjectDictionary)
if !ok { if !ok {
t.Error("Unable to load Resources dictionary") t.Fatalf("Unable to load Resources dictionary")
} }
if len(*resourcesDict) != 1 { if len(resourcesDict.Keys()) != 1 {
t.Error("Page Resources dict should have 1 member (%d)", len(*resourcesDict)) t.Fatalf("Page Resources dict should have 1 member (%d)", len(resourcesDict.Keys()))
} }
fontDict, ok := (*resourcesDict)["Font"].(*PdfObjectDictionary) fontDict, ok := resourcesDict.Get("Font").(*PdfObjectDictionary)
if !ok { if !ok {
t.Error("Unable to load font") t.Error("Unable to load font")
} }
f1Dict, ok := (*fontDict)["F1"].(*PdfObjectDictionary) f1Dict, ok := fontDict.Get("F1").(*PdfObjectDictionary)
if !ok { if !ok {
t.Error("Unable to load F1 dict") t.Error("Unable to load F1 dict")
} }
if len(*f1Dict) != 3 { if len(f1Dict.Keys()) != 3 {
t.Error("Invalid F1 dict length 3 != %d", len(*f1Dict)) t.Errorf("Invalid F1 dict length 3 != %d", len(f1Dict.Keys()))
} }
baseFont, ok := (*f1Dict)["BaseFont"].(*PdfObjectName) baseFont, ok := f1Dict.Get("BaseFont").(*PdfObjectName)
if !ok { if !ok {
t.Error("Unable to load base font") t.Error("Unable to load base font")
} }
if *baseFont != "Times-Roman" { if *baseFont != "Times-Roman" {
t.Error("Invalid base font (should be Times-Roman not %s)", *baseFont) t.Errorf("Invalid base font (should be Times-Roman not %s)", *baseFont)
} }
} }
*/

58
pdf/core/testdata/minimal.pdf vendored Normal file
View File

@ -0,0 +1,58 @@
%PDF-1.1
%¥±ë
1 0 obj
<< /Type /Catalog
/Pages 2 0 R
>>
endobj
2 0 obj
<< /Type /Pages
/Kids [3 0 R]
/Count 1
/MediaBox [0 0 300 144]
>>
endobj
3 0 obj
<< /Type /Page
/Parent 2 0 R
/Resources
<< /Font
<< /F1
<< /Type /Font
/Subtype /Type1
/BaseFont /Times-Roman
>>
>>
>>
/Contents 4 0 R
>>
endobj
4 0 obj
<< /Length 55 >>
stream
BT
/F1 18 Tf
0 0 Td
(Hello World) Tj
ET
endstream
endobj
xref
0 5
0000000000 65535 f
0000000018 00000 n
0000000077 00000 n
0000000178 00000 n
0000000457 00000 n
trailer
<< /Root 1 0 R
/Size 5
>>
startxref
565
%%EOF