...

Source file src/github.com/redhat-developer/odo/pkg/component/describe/describe_test.go

Documentation: github.com/redhat-developer/odo/pkg/component/describe

     1  package describe
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/google/go-cmp/cmp"
     8  
     9  	fcontext "github.com/redhat-developer/odo/pkg/odo/commonflags/context"
    10  )
    11  
    12  type testType struct {
    13  	value    string
    14  	platform string
    15  }
    16  
    17  var _ platformDependent = testType{}
    18  
    19  func (c testType) GetPlatform() string {
    20  	return c.platform
    21  }
    22  
    23  func Test_filterByPlatform(t *testing.T) {
    24  	type args struct {
    25  		ctx           context.Context
    26  		isFeatEnabled bool
    27  	}
    28  	type testCase struct {
    29  		name       string
    30  		args       args
    31  		wantResult []testType
    32  	}
    33  	allValues := []testType{
    34  		{value: "value without platform"},
    35  		{value: "value11 (cluster)", platform: "cluster"},
    36  		{value: "value12 (cluster)", platform: "cluster"},
    37  		{value: "value21 (podman)", platform: "podman"},
    38  		{value: "value22 (podman)", platform: "podman"},
    39  	}
    40  	tests := []testCase{
    41  		{
    42  			name: "feature disabled",
    43  			args: args{
    44  				ctx:           context.Background(),
    45  				isFeatEnabled: false,
    46  			},
    47  			wantResult: nil,
    48  		},
    49  		{
    50  			name: "feature enabled and platform unset in context",
    51  			args: args{
    52  				ctx:           context.Background(),
    53  				isFeatEnabled: true,
    54  			},
    55  			wantResult: allValues,
    56  		},
    57  		{
    58  			name: "feature enabled and platform set to cluster in context",
    59  			args: args{
    60  				ctx:           fcontext.WithPlatform(context.Background(), "cluster"),
    61  				isFeatEnabled: true,
    62  			},
    63  			wantResult: []testType{
    64  				{"value without platform", ""},
    65  				{"value11 (cluster)", "cluster"},
    66  				{"value12 (cluster)", "cluster"},
    67  			},
    68  		},
    69  		{
    70  			name: "feature enabled and platform set to podman in context",
    71  			args: args{
    72  				ctx:           fcontext.WithPlatform(context.Background(), "podman"),
    73  				isFeatEnabled: true,
    74  			},
    75  			wantResult: []testType{
    76  				{"value21 (podman)", "podman"},
    77  				{"value22 (podman)", "podman"},
    78  			},
    79  		},
    80  	}
    81  	for _, tt := range tests {
    82  		t.Run(tt.name, func(t *testing.T) {
    83  			gotResult := filterByPlatform(tt.args.ctx, tt.args.isFeatEnabled, allValues)
    84  			if diff := cmp.Diff(tt.wantResult, gotResult, cmp.AllowUnexported(testType{})); diff != "" {
    85  				t.Errorf("filterByPlatform() mismatch (-want +got):\n%s", diff)
    86  			}
    87  		})
    88  	}
    89  }
    90  

View as plain text