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>")
}