...

Source file src/go.einride.tech/aip/resourcename/sprint_test.go

Documentation: go.einride.tech/aip/resourcename

     1  package resourcename
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gotest.tools/v3/assert"
     7  )
     8  
     9  func TestSprint(t *testing.T) {
    10  	t.Parallel()
    11  	for _, tt := range []struct {
    12  		name      string
    13  		pattern   string
    14  		variables []string
    15  		expected  string
    16  	}{
    17  		{
    18  			name:     "no variables",
    19  			pattern:  "singleton",
    20  			expected: "singleton",
    21  		},
    22  
    23  		{
    24  			name:      "too many variables",
    25  			pattern:   "singleton",
    26  			variables: []string{"foo"},
    27  			expected:  "singleton",
    28  		},
    29  
    30  		{
    31  			name:      "single variable",
    32  			pattern:   "publishers/{publisher}",
    33  			variables: []string{"foo"},
    34  			expected:  "publishers/foo",
    35  		},
    36  
    37  		{
    38  			name:      "two variables",
    39  			pattern:   "publishers/{publisher}/books/{book}",
    40  			variables: []string{"foo", "bar"},
    41  			expected:  "publishers/foo/books/bar",
    42  		},
    43  
    44  		{
    45  			name:      "singleton two variables",
    46  			pattern:   "publishers/{publisher}/books/{book}/settings",
    47  			variables: []string{"foo", "bar"},
    48  			expected:  "publishers/foo/books/bar/settings",
    49  		},
    50  
    51  		{
    52  			name:      "empty variable",
    53  			pattern:   "publishers/{publisher}/books/{book}",
    54  			variables: []string{"foo", ""},
    55  			expected:  "publishers/foo/books/",
    56  		},
    57  
    58  		{
    59  			name:      "too few variables",
    60  			pattern:   "publishers/{publisher}/books/{book}/settings",
    61  			variables: []string{"foo"},
    62  			expected:  "publishers/foo/books//settings",
    63  		},
    64  	} {
    65  		tt := tt
    66  		t.Run(tt.name, func(t *testing.T) {
    67  			t.Parallel()
    68  			assert.Equal(t, tt.expected, Sprint(tt.pattern, tt.variables...))
    69  		})
    70  	}
    71  }
    72  
    73  //nolint:gochecknoglobals
    74  var benchmarkSprintSink string
    75  
    76  func BenchmarkSprint(b *testing.B) {
    77  	for i := 0; i < b.N; i++ {
    78  		benchmarkSprintSink = Sprint("publishers/{publisher}/books/{book}", "foo", "bar")
    79  	}
    80  }
    81  

View as plain text