File: escape_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 (55 lines) | stat: -rw-r--r-- 1,365 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
package converter

import (
	"testing"
)

func TestEscapeContent(t *testing.T) {
	conv := NewConverter()
	conv.Register.EscapedChar('>')

	input := []byte{'a', '>'}

	output := conv.escapeContent(input)
	if len(output) != 3 {
		t.Error("expected different length")
	}
	// Since '>' is a character used for quotes in markdown,
	// there should be a marker before it.
	if output[0] != 'a' || output[1] != placeholderByte || output[2] != '>' {
		t.Error("expected different characters")
	}
}

func TestUnEscapeContent(t *testing.T) {
	conv := NewConverter()
	conv.Register.UnEscaper(func(chars []byte, index int) int {
		if chars[index] != '>' {
			return -1
		}

		// A bit too simplistic for demonstration purposes.
		// Normally here would be content to check if the escaping is necessary...
		return 1
	}, PriorityStandard)

	input := []byte{placeholderByte, 'a', 'b'}
	output := conv.unEscapeContent(input)
	if len(output) != 2 {
		t.Error("expected different length")
	}
	// No escaping needed
	if output[0] != 'a' || output[1] != 'b' {
		t.Error("expected different characters")
	}

	input = []byte{placeholderByte, '>', 'a'}
	output = conv.unEscapeContent(input)
	if len(output) != 3 {
		t.Error("expected different length")
	}
	// Escaping needed
	if output[0] != '\\' || output[1] != '>' || output[2] != 'a' {
		t.Error("expected different characters")
	}
}