gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/fspath/builder_test.go (about)

     1  // Copyright 2019 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package fspath
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  func TestBuilder(t *testing.T) {
    22  	type testCase struct {
    23  		pcs   []string // path components in reverse order
    24  		after string
    25  		want  string
    26  	}
    27  	tests := []testCase{
    28  		{
    29  			// Empty case.
    30  		},
    31  		{
    32  			pcs:  []string{"foo"},
    33  			want: "foo",
    34  		},
    35  		{
    36  			pcs:  []string{"foo", "bar", "baz"},
    37  			want: "baz/bar/foo",
    38  		},
    39  		{
    40  			pcs:   []string{"foo", "bar"},
    41  			after: " (deleted)",
    42  			want:  "bar/foo (deleted)",
    43  		},
    44  	}
    45  
    46  	for _, test := range tests {
    47  		t.Run(test.want, func(t *testing.T) {
    48  			var b Builder
    49  			for _, pc := range test.pcs {
    50  				b.PrependComponent(pc)
    51  			}
    52  			b.AppendString(test.after)
    53  			if got := b.String(); got != test.want {
    54  				t.Errorf("got %q, wanted %q", got, test.want)
    55  			}
    56  		})
    57  	}
    58  }