github.com/pdfcpu/pdfcpu@v0.11.1/pkg/api/test/property_test.go (about) 1 /* 2 Copyright 2020 The pdf Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package test 18 19 import ( 20 "os" 21 "path/filepath" 22 "testing" 23 24 "github.com/pdfcpu/pdfcpu/pkg/api" 25 "github.com/pdfcpu/pdfcpu/pkg/pdfcpu" 26 "github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model" 27 ) 28 29 func listPropertiesFile(t *testing.T, fileName string, conf *model.Configuration) ([]string, error) { 30 t.Helper() 31 32 msg := "listProperties" 33 34 f, err := os.Open(fileName) 35 if err != nil { 36 t.Fatalf("%s open: %v\n", msg, err) 37 } 38 defer f.Close() 39 40 if conf == nil { 41 conf = model.NewDefaultConfiguration() 42 } else { 43 conf.ValidationMode = model.ValidationRelaxed 44 } 45 conf.Cmd = model.LISTPROPERTIES 46 47 ctx, err := api.ReadValidateAndOptimize(f, conf) 48 if err != nil { 49 t.Fatalf("%s ReadValidateAndOptimize: %v\n", msg, err) 50 } 51 52 return pdfcpu.PropertiesList(ctx) 53 } 54 55 func listProperties(t *testing.T, msg, fileName string, want []string) []string { 56 t.Helper() 57 58 got, err := listPropertiesFile(t, fileName, nil) 59 if err != nil { 60 t.Fatalf("%s list properties: %v\n", msg, err) 61 } 62 63 // # of keywords must be want 64 if len(got) != len(want) { 65 t.Fatalf("%s: list properties %s: want %d got %d\n", msg, fileName, len(want), len(got)) 66 } 67 for i, v := range got { 68 if v != want[i] { 69 t.Fatalf("%s: list properties %s: want %v got %v\n", msg, fileName, want, got) 70 } 71 } 72 return got 73 } 74 75 func TestProperties(t *testing.T) { 76 msg := "TestProperties" 77 78 fileName := filepath.Join(outDir, "go.pdf") 79 if err := copyFile(t, filepath.Join(inDir, "go.pdf"), fileName); err != nil { 80 t.Fatalf("%s: copyFile: %v\n", msg, err) 81 } 82 83 // # of properties must be 0 84 listProperties(t, msg, fileName, nil) 85 86 properties := map[string]string{"name1": "value1", "nameÖ": "valueö", "cjkv": "你好"} 87 if err := api.AddPropertiesFile(fileName, "", properties, nil); err != nil { 88 t.Fatalf("%s add properties: %v\n", msg, err) 89 } 90 91 listProperties(t, msg, fileName, []string{"cjkv = 你好", "name1 = value1", "nameÖ = valueö"}) 92 93 if err := api.RemovePropertiesFile(fileName, "", []string{"nameÖ"}, nil); err != nil { 94 t.Fatalf("%s remove 1 property: %v\n", msg, err) 95 } 96 97 listProperties(t, msg, fileName, []string{"cjkv = 你好", "name1 = value1"}) 98 99 if err := api.RemovePropertiesFile(fileName, "", nil, nil); err != nil { 100 t.Fatalf("%s remove all properties: %v\n", msg, err) 101 } 102 103 // # of properties must be 0 104 listProperties(t, msg, fileName, nil) 105 }