github.com/nextlinux/gosbom@v0.81.1-0.20230627115839-1ff50c281391/gosbom/formats/common/cyclonedxhelpers/author_test.go (about)

     1  package cyclonedxhelpers
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/nextlinux/gosbom/gosbom/pkg"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func Test_encodeAuthor(t *testing.T) {
    11  	tests := []struct {
    12  		name     string
    13  		input    pkg.Package
    14  		expected string
    15  	}{
    16  		{
    17  			// note: since this is an optional field, no value is preferred over NONE or NOASSERTION
    18  			name:     "no metadata",
    19  			input:    pkg.Package{},
    20  			expected: "",
    21  		},
    22  		{
    23  			name: "from gem",
    24  			input: pkg.Package{
    25  				Metadata: pkg.GemMetadata{
    26  					Authors: []string{
    27  						"auth1",
    28  						"auth2",
    29  					},
    30  				},
    31  			},
    32  			expected: "auth1,auth2",
    33  		},
    34  		{
    35  			name: "from npm",
    36  			input: pkg.Package{
    37  				Metadata: pkg.NpmPackageJSONMetadata{
    38  					Author: "auth",
    39  				},
    40  			},
    41  			expected: "auth",
    42  		},
    43  		{
    44  			name: "from python - just name",
    45  			input: pkg.Package{
    46  				Metadata: pkg.PythonPackageMetadata{
    47  					Author: "auth",
    48  				},
    49  			},
    50  			expected: "auth",
    51  		},
    52  		{
    53  			name: "from python - just email",
    54  			input: pkg.Package{
    55  				Metadata: pkg.PythonPackageMetadata{
    56  					AuthorEmail: "auth@auth.gov",
    57  				},
    58  			},
    59  			expected: "auth@auth.gov",
    60  		},
    61  		{
    62  			name: "from python - both name and email",
    63  			input: pkg.Package{
    64  				Metadata: pkg.PythonPackageMetadata{
    65  					Author:      "auth",
    66  					AuthorEmail: "auth@auth.gov",
    67  				},
    68  			},
    69  			expected: "auth <auth@auth.gov>",
    70  		},
    71  		{
    72  			// note: since this is an optional field, no value is preferred over NONE or NOASSERTION
    73  			name: "empty",
    74  			input: pkg.Package{
    75  				Metadata: pkg.NpmPackageJSONMetadata{
    76  					Author: "",
    77  				},
    78  			},
    79  			expected: "",
    80  		},
    81  	}
    82  	for _, test := range tests {
    83  		t.Run(test.name, func(t *testing.T) {
    84  			assert.Equal(t, test.expected, encodeAuthor(test.input))
    85  		})
    86  	}
    87  }