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