github.com/anthonyme00/gomarkdoc@v1.0.0/format/plain_test.go (about) 1 package format_test 2 3 import ( 4 "fmt" 5 "path/filepath" 6 "testing" 7 8 "github.com/anthonyme00/gomarkdoc/format" 9 "github.com/anthonyme00/gomarkdoc/lang" 10 "github.com/matryer/is" 11 ) 12 13 func TestPlainMarkdown_Bold(t *testing.T) { 14 is := is.New(t) 15 16 var f format.PlainMarkdown 17 res, err := f.Bold("sample text") 18 is.NoErr(err) 19 is.Equal(res, "**sample text**") 20 } 21 22 func TestPlainMarkdown_CodeBlock(t *testing.T) { 23 is := is.New(t) 24 25 var f format.PlainMarkdown 26 res, err := f.CodeBlock("go", "Line 1\nLine 2") 27 is.NoErr(err) 28 is.Equal(res, "\tLine 1\n\tLine 2") 29 } 30 31 func TestPlainMarkdown_CodeBlock_noLanguage(t *testing.T) { 32 is := is.New(t) 33 34 var f format.PlainMarkdown 35 res, err := f.CodeBlock("", "Line 1\nLine 2") 36 is.NoErr(err) 37 is.Equal(res, "\tLine 1\n\tLine 2") 38 } 39 40 func TestPlainMarkdown_Header(t *testing.T) { 41 tests := []struct { 42 text string 43 level int 44 result string 45 }{ 46 {"header text", 1, "# header text"}, 47 {"level 2", 2, "## level 2"}, 48 {"level 3", 3, "### level 3"}, 49 {"level 4", 4, "#### level 4"}, 50 {"level 5", 5, "##### level 5"}, 51 {"level 6", 6, "###### level 6"}, 52 {"other level", 12, "###### other level"}, 53 {"with * escape", 2, "## with \\* escape"}, 54 } 55 56 for _, test := range tests { 57 t.Run(fmt.Sprintf("%s (level %d)", test.text, test.level), func(t *testing.T) { 58 is := is.New(t) 59 60 var f format.PlainMarkdown 61 res, err := f.Header(test.level, test.text) 62 is.NoErr(err) 63 is.Equal(res, test.result) 64 }) 65 } 66 } 67 68 func TestPlainMarkdown_Header_invalidLevel(t *testing.T) { 69 is := is.New(t) 70 71 var f format.PlainMarkdown 72 _, err := f.Header(-1, "invalid") 73 is.Equal(err.Error(), "format: header level cannot be less than 1") 74 } 75 76 func TestPlainMarkdown_RawHeader(t *testing.T) { 77 tests := []struct { 78 text string 79 level int 80 result string 81 }{ 82 {"header text", 1, "# header text"}, 83 {"with * escape", 2, "## with * escape"}, 84 } 85 86 for _, test := range tests { 87 t.Run(fmt.Sprintf("%s (level %d)", test.text, test.level), func(t *testing.T) { 88 is := is.New(t) 89 90 var f format.PlainMarkdown 91 res, err := f.RawHeader(test.level, test.text) 92 is.NoErr(err) 93 is.Equal(res, test.result) 94 }) 95 } 96 } 97 98 func TestPlainMarkdown_LocalHref(t *testing.T) { 99 is := is.New(t) 100 101 var f format.PlainMarkdown 102 res, err := f.LocalHref("Normal Header") 103 is.NoErr(err) 104 is.Equal(res, "") 105 } 106 107 func TestPlainMarkdown_CodeHref(t *testing.T) { 108 is := is.New(t) 109 110 wd, err := filepath.Abs(".") 111 is.NoErr(err) 112 locPath := filepath.Join(wd, "subdir", "file.go") 113 114 var f format.PlainMarkdown 115 res, err := f.CodeHref(lang.Location{ 116 Start: lang.Position{Line: 12, Col: 1}, 117 End: lang.Position{Line: 14, Col: 43}, 118 Filepath: locPath, 119 WorkDir: wd, 120 Repo: &lang.Repo{ 121 Remote: "https://dev.azure.com/org/project/_git/repo", 122 DefaultBranch: "master", 123 PathFromRoot: "/", 124 }, 125 }) 126 is.NoErr(err) 127 is.Equal(res, "") 128 } 129 130 func TestPlainMarkdown_CodeHref_noRepo(t *testing.T) { 131 is := is.New(t) 132 133 wd, err := filepath.Abs(".") 134 is.NoErr(err) 135 locPath := filepath.Join(wd, "subdir", "file.go") 136 137 var f format.PlainMarkdown 138 res, err := f.CodeHref(lang.Location{ 139 Start: lang.Position{Line: 12, Col: 1}, 140 End: lang.Position{Line: 14, Col: 43}, 141 Filepath: locPath, 142 WorkDir: wd, 143 Repo: nil, 144 }) 145 is.NoErr(err) 146 is.Equal(res, "") 147 } 148 149 func TestPlainMarkdown_Link(t *testing.T) { 150 is := is.New(t) 151 152 var f format.PlainMarkdown 153 res, err := f.Link("link text", "https://test.com/a/b/c") 154 is.NoErr(err) 155 is.Equal(res, "[link text](<https://test.com/a/b/c>)") 156 } 157 158 func TestPlainMarkdown_ListEntry(t *testing.T) { 159 is := is.New(t) 160 161 var f format.PlainMarkdown 162 res, err := f.ListEntry(0, "list entry text") 163 is.NoErr(err) 164 is.Equal(res, "- list entry text") 165 } 166 167 func TestPlainMarkdown_ListEntry_nested(t *testing.T) { 168 is := is.New(t) 169 170 var f format.PlainMarkdown 171 res, err := f.ListEntry(2, "nested text") 172 is.NoErr(err) 173 is.Equal(res, " - nested text") 174 } 175 176 func TestPlainMarkdown_ListEntry_empty(t *testing.T) { 177 is := is.New(t) 178 179 var f format.PlainMarkdown 180 res, err := f.ListEntry(0, "") 181 is.NoErr(err) 182 is.Equal(res, "") 183 }