github.com/pdfcpu/pdfcpu@v0.11.1/pkg/cli/test/images_test.go (about) 1 /* 2 Copyright 2024 The pdfcpu 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 "path/filepath" 21 "testing" 22 23 "github.com/pdfcpu/pdfcpu/pkg/cli" 24 ) 25 26 func testUpdateImages(t *testing.T, msg string, inFile, imgFile, outFile string, objNrOrPageNr int, id string) { 27 t.Helper() 28 29 cmd := cli.UpdateImagesCommand(inFile, imgFile, outFile, objNrOrPageNr, id, conf) 30 if _, err := cli.Process(cmd); err != nil { 31 t.Fatalf("%s %s: %v\n", msg, inFile, err) 32 } 33 34 if err := validateFile(t, outFile, conf); err != nil { 35 t.Fatalf("%s: %v\n", msg, err) 36 } 37 } 38 39 func TestUpdateImages(t *testing.T) { 40 inDir := filepath.Join(samplesDir, "images") 41 42 for _, tt := range []struct { 43 msg string 44 inFile string 45 imgFile string 46 outFile string 47 objNrOrPageNr int 48 id string 49 }{ 50 {"TestUpdateByObjNr", 51 "test.pdf", 52 "test_1_Im1.png", 53 "ImageUpdatedByObjNr.pdf", 54 8, 55 ""}, 56 57 {"TestUpdateByPageNrAndId", 58 "test.pdf", 59 "test_1_Im1.png", 60 "imageUpdatedByPageNrAndIdPage1.pdf", 61 1, 62 "Im1"}, 63 64 {"TestUpdateByPageNrAndId", 65 "test.pdf", 66 "test_1_Im1.png", 67 "imageUpdatedByPageNrAndIdPage2.pdf", 68 2, 69 "Im1"}, 70 71 {"TestUpdateByImageFileName", 72 "test.pdf", 73 "test_1_Im1.png", 74 "imageUpdatedByFileName.pdf", 75 0, 76 ""}, 77 78 {"TestUpdateByPageNrAndId", 79 "test.pdf", 80 "any.png", 81 "imageUpdatedByPageNrAndIdAny.pdf", 82 1, 83 "Im1"}, 84 85 {"TestUpdateByObjNrPNG", 86 "test.pdf", 87 "any.png", 88 "imageUpdatedByObjNrPNG.pdf", 89 8, 90 ""}, 91 92 {"TestUpdateByObjNrJPG", 93 "test.pdf", 94 "any.jpg", 95 "imageUpdatedByObjNrJPG.pdf", 96 8, 97 ""}, 98 99 {"TestUpdateByObjNrTIFF", 100 "test.pdf", 101 "any.tiff", 102 "imageUpdatedByObjNrTIFF.pdf", 103 8, 104 ""}, 105 106 {"TestUpdateByObjNrWEBP", 107 "test.pdf", 108 "any.webp", 109 "imageUpdatedByObjNrWEBP.pdf", 110 8, 111 ""}, 112 {"TestUpdateByObjNrPNGGray", 113 "test.pdf", 114 "any_gray.png", 115 "imageUpdatedByObjNrPNGGray.pdf", 116 8, 117 ""}, 118 } { 119 testUpdateImages(t, tt.msg, 120 filepath.Join(inDir, tt.inFile), 121 filepath.Join(inDir, tt.imgFile), 122 filepath.Join(outDir, tt.outFile), 123 tt.objNrOrPageNr, 124 tt.id) 125 } 126 }