code.gitea.io/gitea@v1.19.3/modules/markup/markdown/renderconfig_test.go (about)

     1  // Copyright 2022 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package markdown
     5  
     6  import (
     7  	"strings"
     8  	"testing"
     9  
    10  	"gopkg.in/yaml.v3"
    11  )
    12  
    13  func TestRenderConfig_UnmarshalYAML(t *testing.T) {
    14  	tests := []struct {
    15  		name     string
    16  		expected *RenderConfig
    17  		args     string
    18  	}{
    19  		{
    20  			"empty", &RenderConfig{
    21  				Meta: "table",
    22  				Icon: "table",
    23  				Lang: "",
    24  			}, "",
    25  		},
    26  		{
    27  			"lang", &RenderConfig{
    28  				Meta: "table",
    29  				Icon: "table",
    30  				Lang: "test",
    31  			}, "lang: test",
    32  		},
    33  		{
    34  			"metatable", &RenderConfig{
    35  				Meta: "table",
    36  				Icon: "table",
    37  				Lang: "",
    38  			}, "gitea: table",
    39  		},
    40  		{
    41  			"metanone", &RenderConfig{
    42  				Meta: "none",
    43  				Icon: "table",
    44  				Lang: "",
    45  			}, "gitea: none",
    46  		},
    47  		{
    48  			"metadetails", &RenderConfig{
    49  				Meta: "details",
    50  				Icon: "table",
    51  				Lang: "",
    52  			}, "gitea: details",
    53  		},
    54  		{
    55  			"metawrong", &RenderConfig{
    56  				Meta: "details",
    57  				Icon: "table",
    58  				Lang: "",
    59  			}, "gitea: wrong",
    60  		},
    61  		{
    62  			"toc", &RenderConfig{
    63  				TOC:  true,
    64  				Meta: "table",
    65  				Icon: "table",
    66  				Lang: "",
    67  			}, "include_toc: true",
    68  		},
    69  		{
    70  			"tocfalse", &RenderConfig{
    71  				TOC:  false,
    72  				Meta: "table",
    73  				Icon: "table",
    74  				Lang: "",
    75  			}, "include_toc: false",
    76  		},
    77  		{
    78  			"toclang", &RenderConfig{
    79  				Meta: "table",
    80  				Icon: "table",
    81  				TOC:  true,
    82  				Lang: "testlang",
    83  			}, `
    84  				include_toc: true
    85  				lang: testlang
    86  				`,
    87  		},
    88  		{
    89  			"complexlang", &RenderConfig{
    90  				Meta: "table",
    91  				Icon: "table",
    92  				Lang: "testlang",
    93  			}, `
    94  				gitea:
    95  					lang: testlang
    96  				`,
    97  		},
    98  		{
    99  			"complexlang2", &RenderConfig{
   100  				Meta: "table",
   101  				Icon: "table",
   102  				Lang: "testlang",
   103  			}, `
   104  	lang: notright
   105  	gitea:
   106  		lang: testlang
   107  `,
   108  		},
   109  		{
   110  			"complexlang", &RenderConfig{
   111  				Meta: "table",
   112  				Icon: "table",
   113  				Lang: "testlang",
   114  			}, `
   115  	gitea:
   116  		lang: testlang
   117  `,
   118  		},
   119  		{
   120  			"complex2", &RenderConfig{
   121  				Lang: "two",
   122  				Meta: "table",
   123  				TOC:  true,
   124  				Icon: "smiley",
   125  			}, `
   126  	lang: one
   127  	include_toc: true
   128  	gitea:
   129  		details_icon: smiley
   130  		meta: table
   131  		include_toc: true
   132  		lang: two
   133  `,
   134  		},
   135  	}
   136  	for _, tt := range tests {
   137  		t.Run(tt.name, func(t *testing.T) {
   138  			got := &RenderConfig{
   139  				Meta: "table",
   140  				Icon: "table",
   141  				Lang: "",
   142  			}
   143  			if err := yaml.Unmarshal([]byte(strings.ReplaceAll(tt.args, "\t", "    ")), got); err != nil {
   144  				t.Errorf("RenderConfig.UnmarshalYAML() error = %v\n%q", err, tt.args)
   145  				return
   146  			}
   147  
   148  			if got.Meta != tt.expected.Meta {
   149  				t.Errorf("Meta Expected %s Got %s", tt.expected.Meta, got.Meta)
   150  			}
   151  			if got.Icon != tt.expected.Icon {
   152  				t.Errorf("Icon Expected %s Got %s", tt.expected.Icon, got.Icon)
   153  			}
   154  			if got.Lang != tt.expected.Lang {
   155  				t.Errorf("Lang Expected %s Got %s", tt.expected.Lang, got.Lang)
   156  			}
   157  			if got.TOC != tt.expected.TOC {
   158  				t.Errorf("TOC Expected %t Got %t", tt.expected.TOC, got.TOC)
   159  			}
   160  		})
   161  	}
   162  }