mirror of
https://github.com/hanzoai/o11y-foundry.git
synced 2026-10-30 18:26:45 +00:00
## Summary
- `Template` now carries its `Format` end-to-end. Each format declares
its `Material` constructor inline (`FormatYAML = Format{s: "yaml", new:
...}`), so adding a format is one `var` entry — no switch in `Render`,
no per-callsite dispatch.
- New `Template.Render(data, path) (Material, error)` replaces the
`Execute` → `bytes.Buffer` → `NewXMaterial` trio duplicated across
`ecsterraformcasting`, `kuberneteskustomizecasting`, `systemdcasting`,
and `infrastructure/terraform/generator`. Net −165/+259, but most of the
+s are the new test file.
- Errors flow through `internal/errors` (`TypeInvalidInput` /
`TypeInternal` / `TypeUnsupported`) using the project's `failed to
create <X> from %q: <reason>` wording, matching the recent `Material`
and `Address` refactors.
- Drop `Get` prefix on accessors: `Name()`, `Path()`, `Format()`.
- `internal/domain/template_test.go` covers `Render` across every format
plus the unsupported-format and invalid-output paths.
89 lines
2.1 KiB
Go
89 lines
2.1 KiB
Go
package domain
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestTemplateRender(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
contents []byte
|
|
format Format
|
|
path string
|
|
data any
|
|
pass bool
|
|
expectedFmt []byte
|
|
}{
|
|
{
|
|
name: "YAML_Valid",
|
|
contents: []byte("greeting: hello {{ .Name }}\n"),
|
|
format: FormatYAML,
|
|
path: "out.yaml",
|
|
data: map[string]string{"Name": "world"},
|
|
pass: true,
|
|
expectedFmt: []byte("greeting: hello world\n"),
|
|
},
|
|
{
|
|
name: "JSON_Valid",
|
|
contents: []byte(`{"greeting":"hello {{ .Name }}"}`),
|
|
format: FormatJSON,
|
|
path: "out.json",
|
|
data: map[string]string{"Name": "world"},
|
|
pass: true,
|
|
expectedFmt: []byte(`{"greeting":"hello world"}`),
|
|
},
|
|
{
|
|
name: "INI_Valid",
|
|
contents: []byte("[greeting]\nname = {{ .Name }}\n"),
|
|
format: FormatINI,
|
|
path: "out.ini",
|
|
data: map[string]string{"Name": "world"},
|
|
pass: true,
|
|
expectedFmt: []byte("[greeting]\nname=world\n"),
|
|
},
|
|
{
|
|
name: "Text_Valid",
|
|
contents: []byte("hello {{ .Name }}"),
|
|
format: FormatText,
|
|
path: "out.txt",
|
|
data: map[string]string{"Name": "world"},
|
|
pass: true,
|
|
expectedFmt: []byte("hello world"),
|
|
},
|
|
{
|
|
name: "UnsupportedFormat_Invalid",
|
|
contents: []byte("hello"),
|
|
format: Format{s: "bogus"},
|
|
path: "out",
|
|
data: nil,
|
|
pass: false,
|
|
},
|
|
{
|
|
name: "JSON_InvalidOutput",
|
|
contents: []byte(`{"greeting": hello {{ .Name }}}`),
|
|
format: FormatJSON,
|
|
path: "out.json",
|
|
data: map[string]string{"Name": "world"},
|
|
pass: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
tmpl := MustNewTemplate(tt.name, tt.contents, tt.format)
|
|
|
|
material, err := tmpl.Render(tt.data, tt.path)
|
|
if !tt.pass {
|
|
assert.Error(t, err)
|
|
return
|
|
}
|
|
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, tt.path, material.Path())
|
|
assert.Equal(t, tt.expectedFmt, material.FmtContents())
|
|
})
|
|
}
|
|
}
|