File: strikethrough_test.go

package info (click to toggle)
golang-github-johanneskaufmann-html-to-markdown 2.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,080 kB
  • sloc: makefile: 3
file content (101 lines) | stat: -rw-r--r-- 2,293 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package strikethrough_test

import (
	"bytes"
	"testing"

	"github.com/JohannesKaufmann/html-to-markdown/v2/converter"
	"github.com/JohannesKaufmann/html-to-markdown/v2/internal/tester"
	"github.com/JohannesKaufmann/html-to-markdown/v2/plugin/base"
	"github.com/JohannesKaufmann/html-to-markdown/v2/plugin/commonmark"
	"github.com/JohannesKaufmann/html-to-markdown/v2/plugin/strikethrough"
)

func TestNewStrikethroughPlugin(t *testing.T) {
	runs := []struct {
		desc     string
		input    string
		expected string
	}{
		{
			desc:     "simple",
			input:    `<p><s>Text</s></p>`,
			expected: `~~Text~~`,
		},
		{
			desc:     "with spaces inside",
			input:    `<p><s>  Text  </s></p>`,
			expected: `~~Text~~`,
		},
		{
			desc:     "with tilde characters inside",
			input:    `<p><s>~~A~~B~~</s></p>`,
			expected: `~~\~\~A\~\~B\~\~~~`,
		},
		{
			desc:     "nested",
			input:    `<p><s>A <s>B</s> C</s></p>`,
			expected: `~~A B C~~`,
		},
		{
			desc:     "adjacent",
			input:    `<p><s>A</s><s>B</s> <s>C</s></p>`,
			expected: `~~AB~~ ~~C~~`,
		},
	}
	for _, run := range runs {
		t.Run(run.desc, func(t *testing.T) {
			conv := converter.NewConverter(
				converter.WithPlugins(
					base.NewBasePlugin(),
					strikethrough.NewStrikethroughPlugin(),
				),
			)

			out, err := conv.ConvertString(run.input)
			if err != nil {
				t.Error(err)
			}
			if out != run.expected {
				t.Errorf("expected %q but got %q", run.expected, out)
			}
		})
	}
}
func TestWithDelimiter(t *testing.T) {
	conv := converter.NewConverter(
		converter.WithPlugins(
			base.NewBasePlugin(),
			strikethrough.NewStrikethroughPlugin(
				strikethrough.WithDelimiter("=="),
			),
		),
	)

	input := `<p><s>Text</s></p>`
	expected := `==Text==`

	out, err := conv.ConvertString(input)
	if err != nil {
		t.Error(err)
	}
	if out != expected {
		t.Errorf("expected %q but got %q", expected, out)
	}
}

func TestGoldenFiles(t *testing.T) {
	goldenFileConvert := func(htmlInput []byte) ([]byte, error) {
		conv := converter.NewConverter(
			converter.WithPlugins(
				base.NewBasePlugin(),
				commonmark.NewCommonmarkPlugin(),
				strikethrough.NewStrikethroughPlugin(),
			),
		)

		return conv.ConvertReader(bytes.NewReader(htmlInput))
	}

	tester.GoldenFiles(t, goldenFileConvert, goldenFileConvert)
}