github.com/mineiros-io/terradoc@v0.0.9-0.20220711062319-018bd4ae81f5/internal/renderers/markdown/markdown_test.go (about)

     1  package markdown_test
     2  
     3  import (
     4  	"bytes"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/google/go-cmp/cmp"
     9  	"github.com/madlambda/spells/assert"
    10  	"github.com/mineiros-io/terradoc/internal/entities"
    11  	"github.com/mineiros-io/terradoc/internal/renderers/markdown"
    12  	"github.com/mineiros-io/terradoc/internal/types"
    13  	"github.com/mineiros-io/terradoc/test"
    14  )
    15  
    16  func TestRender(t *testing.T) {
    17  	definition := entities.Doc{
    18  		Header: entities.Header{
    19  			Image:  "",
    20  			URL:    "",
    21  			Badges: []entities.Badge{},
    22  		},
    23  		Sections: []entities.Section{
    24  			{
    25  				Title:   "Section Title",
    26  				Content: "Section Content",
    27  				Level:   1,
    28  				TOC:     true,
    29  				SubSections: []entities.Section{
    30  					{
    31  						Level: 2,
    32  						Title: "SubSection 1",
    33  						SubSections: []entities.Section{
    34  							{
    35  								Level:   3,
    36  								Title:   "SubSection 1.1",
    37  								Content: "We can add infinite subsections!",
    38  							},
    39  						},
    40  						Variables: []entities.Variable{
    41  							{
    42  								Name: "simple_string",
    43  								Type: entities.Type{
    44  									TFType: types.TerraformString,
    45  								},
    46  								Description: "A simple string",
    47  							},
    48  						},
    49  					},
    50  					{
    51  						Level: 2,
    52  						Title: "SubSection 2",
    53  						Variables: []entities.Variable{
    54  							{
    55  								Name:    "test_objects",
    56  								Default: []byte("[]"),
    57  								Type: entities.Type{
    58  									TFType: types.TerraformList,
    59  									Nested: &entities.Type{
    60  										TFType: types.TerraformObject,
    61  										Label:  "test_object",
    62  									},
    63  								},
    64  								Attributes: []entities.Attribute{
    65  									{
    66  										Level:       1,
    67  										Name:        "name",
    68  										Description: "A string",
    69  										Type: entities.Type{
    70  											TFType: types.TerraformString,
    71  										},
    72  									},
    73  									{
    74  										Level:       1,
    75  										Name:        "something_complex",
    76  										Description: "Some other object",
    77  										Type: entities.Type{
    78  											TFType: types.TerraformObject,
    79  											Label:  "nested_object",
    80  										},
    81  										Attributes: []entities.Attribute{
    82  											{
    83  												Level:       2,
    84  												Name:        "nested_string",
    85  												Description: "a nested string",
    86  												Type: entities.Type{
    87  													TFType: types.TerraformString,
    88  												},
    89  											},
    90  										},
    91  									},
    92  								},
    93  							},
    94  						},
    95  					},
    96  				},
    97  			},
    98  		},
    99  		References: []entities.Reference{},
   100  	}
   101  
   102  	buf := new(bytes.Buffer)
   103  	err := markdown.Render(buf, definition)
   104  	assert.NoError(t, err)
   105  
   106  	got := strings.TrimSpace(buf.String())
   107  
   108  	wantContent := test.ReadFixture(t, "markdown-structure.md")
   109  	want := string(bytes.TrimSpace(wantContent))
   110  
   111  	if diff := cmp.Diff(got, want); diff != "" {
   112  		t.Errorf("Expected golden file to match result (-want +got):\n%s", diff)
   113  	}
   114  }