github.com/pdfcpu/pdfcpu@v0.11.1/pkg/cli/test/keyword_test.go (about) 1 /* 2 Copyright 2020 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 "github.com/pdfcpu/pdfcpu/pkg/pdfcpu/types" 25 ) 26 27 func listKeywords(t *testing.T, msg, fileName string, want []string) []string { 28 t.Helper() 29 cmd := cli.ListKeywordsCommand(fileName, conf) 30 got, err := cli.Process(cmd) 31 if err != nil { 32 t.Fatalf("%s list keywords: %v\n", msg, err) 33 } 34 if len(got) != len(want) { 35 t.Fatalf("%s: list keywords %s: want %d got %d\n", msg, fileName, len(want), len(got)) 36 } 37 38 for _, v := range got { 39 if !types.MemberOf(v, want) { 40 t.Fatalf("%s: list keywords %s: want %v got %v\n", msg, fileName, want, got) 41 } 42 } 43 return got 44 } 45 46 func TestKeywordsCommand(t *testing.T) { 47 msg := "TestKeywordsCommand" 48 49 fileName := filepath.Join(outDir, "go.pdf") 50 if err := copyFile(t, filepath.Join(inDir, "go.pdf"), fileName); err != nil { 51 t.Fatalf("%s: copyFile: %v\n", msg, err) 52 } 53 54 // # of keywords must be 0 55 listKeywords(t, msg, fileName, nil) 56 57 keywords := []string{"keyword1", "keyword2"} 58 cmd := cli.AddKeywordsCommand(fileName, "", keywords, conf) 59 if _, err := cli.Process(cmd); err != nil { 60 t.Fatalf("%s add keywords: %v\n", msg, err) 61 } 62 63 listKeywords(t, msg, fileName, keywords) 64 65 cmd = cli.RemoveKeywordsCommand(fileName, "", []string{"keyword2"}, conf) 66 if _, err := cli.Process(cmd); err != nil { 67 t.Fatalf("%s remove 1 keyword: %v\n", msg, err) 68 } 69 70 listKeywords(t, msg, fileName, []string{"keyword1"}) 71 72 cmd = cli.RemoveKeywordsCommand(fileName, "", nil, conf) 73 if _, err := cli.Process(cmd); err != nil { 74 t.Fatalf("%s remove all keywords: %v\n", msg, err) 75 } 76 77 // # of keywords must be 0 78 listKeywords(t, msg, fileName, nil) 79 }