github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/documentation/generator/description_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 TestCreateStepName(t *testing.T) {
    14  	tests := []struct {
    15  		name  string
    16  		input *config.StepData
    17  		want  string
    18  	}{
    19  		{
    20  			name: "simple step name section",
    21  			input: &config.StepData{
    22  				Metadata: config.StepMetadata{Name: "teststep", Description: "TestDescription"},
    23  			},
    24  			want: "# teststep\n\nTestDescription\n",
    25  		},
    26  	}
    27  	for _, testcase := range tests {
    28  		t.Run(testcase.name, func(t *testing.T) {
    29  			assert.Equal(t, testcase.want, createStepName(testcase.input))
    30  		})
    31  	}
    32  }
    33  
    34  func TestCreateDescriptionSection(t *testing.T) {
    35  	CustomLibrarySteps = []CustomLibrary{{
    36  		Name:        "TestLibrary",
    37  		BinaryName:  "myBinary",
    38  		LibraryName: "myLibrary",
    39  		Steps:       []string{"myCustomStep"},
    40  	}}
    41  
    42  	tests := []struct {
    43  		name  string
    44  		input *config.StepData
    45  		want  string
    46  	}{
    47  		{
    48  			name: "simple step description section",
    49  			input: &config.StepData{
    50  				Metadata: config.StepMetadata{Name: "teststep", LongDescription: "TestDescription"},
    51  			},
    52  			want: headlineDescription + "TestDescription" + "\n\n" +
    53  				headlineUsage + configRecommendation + "\n\n" +
    54  				"!!! tip \"\"" + "\n\n" +
    55  				headlineJenkinsPipeline + "        ```groovy\n        library('piper-lib-os')\n\n        teststep script: this\n        ```" + "\n\n" +
    56  				headlineCommandLine + "        ```sh\n        piper teststep\n        ```" + "\n\n",
    57  		},
    58  		{
    59  			name: "custom step description section",
    60  			input: &config.StepData{
    61  				Metadata: config.StepMetadata{Name: "myCustomStep", LongDescription: "TestDescription"},
    62  			},
    63  			want: headlineDescription + "TestDescription" + "\n\n" +
    64  				headlineUsage + configRecommendation + "\n\n" +
    65  				"!!! tip \"\"" + "\n\n" +
    66  				headlineJenkinsPipeline + "        ```groovy\n        library('myLibrary')\n\n        myCustomStep script: this\n        ```" + "\n\n" +
    67  				headlineCommandLine + "        ```sh\n        myBinary myCustomStep\n        ```" + "\n\n",
    68  		},
    69  	}
    70  	for _, testcase := range tests {
    71  		t.Run(testcase.name, func(t *testing.T) {
    72  			assert.Equal(t, testcase.want, createDescriptionSection(testcase.input))
    73  		})
    74  	}
    75  }