github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/doc/generate_test.go (about)

     1  // Copyright 2021 iLogtail 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 doc
    16  
    17  import (
    18  	"os"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  )
    23  
    24  type TestDoc struct {
    25  	Field1 int               `json:"field_1" comment:"field one"`
    26  	Field2 string            `json:"field_2" comment:"field two"`
    27  	Field3 int64             `json:"field_3" mapstructure:"field_33" comment:"field three"`
    28  	Field4 []string          `json:"field_4" comment:"field four"`
    29  	Field5 map[string]string `json:"field_5" comment:"field five"`
    30  	//nolint:unused,structcheck
    31  	ignoreField string
    32  }
    33  
    34  func (t TestDoc) Description() string {
    35  	return "this is a test doc demo"
    36  }
    37  
    38  func Test_extractDocConfig(t *testing.T) {
    39  
    40  	config := extractDocConfig(new(TestDoc))
    41  	assert.Equal(t, []*FieldConfig{
    42  		{
    43  			Name:    "field_1",
    44  			Type:    "int",
    45  			Comment: "field one",
    46  			Default: "0",
    47  		},
    48  		{
    49  			Name:    "field_2",
    50  			Type:    "string",
    51  			Comment: "field two",
    52  			Default: "\"\"",
    53  		},
    54  		{
    55  			Name:    "field_33",
    56  			Type:    "int64",
    57  			Comment: "field three",
    58  			Default: "0",
    59  		},
    60  		{
    61  			Name:    "field_4",
    62  			Type:    "[]string",
    63  			Comment: "field four",
    64  			Default: "null",
    65  		},
    66  		{
    67  			Name:    "field_5",
    68  			Type:    "map[string]string",
    69  			Comment: "field five",
    70  			Default: "null",
    71  		},
    72  	}, config)
    73  
    74  }
    75  
    76  func Test_generatePluginDoc(t *testing.T) {
    77  	str := `# test-plugin
    78  ## Description
    79  this is a test doc demo
    80  ## Config
    81  |  field   |   type   |   description   | default value   |
    82  | ---- | ---- | ---- | ---- |
    83  |field_1|int|field one|1|
    84  |field_2|string|field two|"filed 2"|
    85  |field_33|int64|field three|3|
    86  |field_4|[]string|field four|["field","4"]|
    87  |field_5|map[string]string|field five|{"k":"v"}|`
    88  	t2 := new(TestDoc)
    89  	t2.Field1 = 1
    90  	t2.Field2 = "filed 2"
    91  	t2.Field3 = 3
    92  	t2.Field4 = []string{"field", "4"}
    93  	t2.Field5 = map[string]string{
    94  		"k": "v",
    95  	}
    96  	generatePluginDoc("test-plugin.md", "test-plugin", t2)
    97  	bytes, _ := os.ReadFile("test-plugin.md")
    98  	assert.Equal(t, str, string(bytes))
    99  	_ = os.Remove("test-plugin.md")
   100  }