~bigbes/confluence-md-utilities

ref: v0.1.0 confluence-md-utilities/template/embed_test.go -rw-r--r-- 1.9 KiB
e0e81bc6 — Eugene Blikh chore: rename module to go.bigb.es/confluence-md-utilities a month ago
                                                                                
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
package template

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestEmbed_Basic(t *testing.T) {
	tmpl := `<root>
<!-- MD_CONTENT_START -->
<p>Old content</p>
<!-- MD_CONTENT_END -->
</root>`

	result, err := Embed(tmpl, "<h1>New</h1>", DefaultMarkerStart, DefaultMarkerEnd)
	require.NoError(t, err)
	assert.Contains(t, result, "<h1>New</h1>")
	assert.NotContains(t, result, "Old content")
	assert.Contains(t, result, DefaultMarkerStart)
	assert.Contains(t, result, DefaultMarkerEnd)
}

func TestEmbed_CustomMarkers(t *testing.T) {
	tmpl := `<root><!-- BEGIN --><p>old</p><!-- END --></root>`
	result, err := Embed(tmpl, "<h1>New</h1>", "<!-- BEGIN -->", "<!-- END -->")
	require.NoError(t, err)
	assert.Contains(t, result, "<h1>New</h1>")
}

func TestEmbed_MissingStartMarker(t *testing.T) {
	tmpl := `<root><!-- MD_CONTENT_END --></root>`
	_, err := Embed(tmpl, "content", DefaultMarkerStart, DefaultMarkerEnd)
	require.Error(t, err)
	assert.Contains(t, err.Error(), "start marker")
}

func TestEmbed_MissingEndMarker(t *testing.T) {
	tmpl := `<root><!-- MD_CONTENT_START --></root>`
	_, err := Embed(tmpl, "content", DefaultMarkerStart, DefaultMarkerEnd)
	require.Error(t, err)
	assert.Contains(t, err.Error(), "end marker")
}

func TestEmbed_EndBeforeStart(t *testing.T) {
	tmpl := `<!-- MD_CONTENT_END --> <!-- MD_CONTENT_START -->`
	_, err := Embed(tmpl, "content", DefaultMarkerStart, DefaultMarkerEnd)
	require.Error(t, err)
	assert.Contains(t, err.Error(), "before start")
}

func TestEmbed_PreserveSurroundingContent(t *testing.T) {
	tmpl := `<header>title</header>
<!-- MD_CONTENT_START -->
old
<!-- MD_CONTENT_END -->
<footer>end</footer>`

	result, err := Embed(tmpl, "<p>new</p>", DefaultMarkerStart, DefaultMarkerEnd)
	require.NoError(t, err)
	assert.Contains(t, result, "<header>title</header>")
	assert.Contains(t, result, "<footer>end</footer>")
}