github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/documentation/generator/metadata_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package generator
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/SAP/jenkins-library/pkg/config"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func Test_adjustDefaultValues(t *testing.T) {
    14  	tests := []struct {
    15  		want  interface{}
    16  		name  string
    17  		input *config.StepData
    18  	}{
    19  		{want: false, name: "boolean", input: &config.StepData{Spec: config.StepSpec{Inputs: config.StepInputs{Parameters: []config.StepParameters{
    20  			{Name: "param", Type: "bool", Mandatory: true},
    21  		}}}}},
    22  		{want: nil, name: "integer", input: &config.StepData{Spec: config.StepSpec{Inputs: config.StepInputs{Parameters: []config.StepParameters{
    23  			{Name: "param", Type: "int", Mandatory: true},
    24  		}}}}},
    25  		{want: nil, name: "string", input: &config.StepData{Spec: config.StepSpec{Inputs: config.StepInputs{Parameters: []config.StepParameters{
    26  			{Name: "param", Type: "string", Mandatory: true},
    27  		}}}}},
    28  		{want: nil, name: "string array", input: &config.StepData{Spec: config.StepSpec{Inputs: config.StepInputs{Parameters: []config.StepParameters{
    29  			{Name: "param", Type: "[]string", Mandatory: true},
    30  		}}}}},
    31  	}
    32  	for _, tt := range tests {
    33  		t.Run(tt.name, func(t *testing.T) {
    34  			// test
    35  			adjustDefaultValues(tt.input)
    36  			// assert
    37  			assert.Equal(t, tt.want, tt.input.Spec.Inputs.Parameters[0].Default)
    38  		})
    39  	}
    40  }
    41  
    42  func Test_adjustMandatoryFlags(t *testing.T) {
    43  	tests := []struct {
    44  		want  bool
    45  		name  string
    46  		input *config.StepData
    47  	}{
    48  		{want: false, name: "boolean with empty default", input: &config.StepData{Spec: config.StepSpec{Inputs: config.StepInputs{Parameters: []config.StepParameters{
    49  			{Name: "param", Type: "bool", Mandatory: true, Default: false},
    50  		}}}}},
    51  		{want: false, name: "boolean with default", input: &config.StepData{Spec: config.StepSpec{Inputs: config.StepInputs{Parameters: []config.StepParameters{
    52  			{Name: "param", Type: "bool", Mandatory: true, Default: true},
    53  		}}}}},
    54  		{want: true, name: "string with default not set", input: &config.StepData{Spec: config.StepSpec{Inputs: config.StepInputs{Parameters: []config.StepParameters{
    55  			{Name: "param", Type: "string", Mandatory: true},
    56  		}}}}},
    57  		{want: true, name: "string with empty default", input: &config.StepData{Spec: config.StepSpec{Inputs: config.StepInputs{Parameters: []config.StepParameters{
    58  			{Name: "param", Type: "string", Mandatory: true, Default: ""},
    59  		}}}}},
    60  		{want: false, name: "string with default", input: &config.StepData{Spec: config.StepSpec{Inputs: config.StepInputs{Parameters: []config.StepParameters{
    61  			{Name: "param", Type: "string", Mandatory: true, Default: "Oktober"},
    62  		}}}}},
    63  		{want: true, name: "string array with default not set", input: &config.StepData{Spec: config.StepSpec{Inputs: config.StepInputs{Parameters: []config.StepParameters{
    64  			{Name: "param", Type: "[]string", Mandatory: true},
    65  		}}}}},
    66  		{want: true, name: "string array with empty default", input: &config.StepData{Spec: config.StepSpec{Inputs: config.StepInputs{Parameters: []config.StepParameters{
    67  			{Name: "param", Type: "[]string", Mandatory: true, Default: []string{}},
    68  		}}}}},
    69  		{want: false, name: "string array with default", input: &config.StepData{Spec: config.StepSpec{Inputs: config.StepInputs{Parameters: []config.StepParameters{
    70  			{Name: "param", Type: "[]string", Mandatory: true, Default: []string{"Oktober"}},
    71  		}}}}},
    72  	}
    73  	for _, tt := range tests {
    74  		t.Run(tt.name, func(t *testing.T) {
    75  			// test
    76  			adjustMandatoryFlags(tt.input)
    77  			// assert
    78  			assert.Equal(t, tt.want, tt.input.Spec.Inputs.Parameters[0].Mandatory)
    79  		})
    80  	}
    81  }
    82  
    83  func Test_interfaceArrayLength(t *testing.T) {
    84  	type args struct {
    85  		i interface{}
    86  	}
    87  	tests := []struct {
    88  		name string
    89  		args args
    90  		want int
    91  	}{
    92  		{
    93  			name: "empty type",
    94  			args: args{},
    95  			want: -1,
    96  		},
    97  		{
    98  			name: "string type",
    99  			args: args{"string"},
   100  			want: -1,
   101  		},
   102  		{
   103  			name: "empty array type",
   104  			args: args{[]interface{}{}},
   105  			want: 0,
   106  		},
   107  		{
   108  			name: "string array type",
   109  			args: args{[]interface{}{"string1", "string1"}},
   110  			want: 2,
   111  		},
   112  		{
   113  			name: "string array type",
   114  			args: args{[]string{"string1", "string1"}},
   115  			want: 2,
   116  		},
   117  	}
   118  	for _, tt := range tests {
   119  		t.Run(tt.name, func(t *testing.T) {
   120  			if got := interfaceArrayLength(tt.args.i); got != tt.want {
   121  				t.Errorf("interfaceArrayLength() = %v, want %v", got, tt.want)
   122  			}
   123  		})
   124  	}
   125  }