package services import ( "strings" "testing" ) // La vista previa ejecuta la plantilla de verdad: es lo que hace que un // {{range}} se vea como va a salir y que un error de sintaxis aparezca mientras // se edita, y no recién al generar el documento. func TestRenderizarPlantillaEjemplo(t *testing.T) { html := `

{{.Cliente.Nombre}} — {{.Cliente.Documento}}

{{.Alcance}}

{{range .Items}}{{end}}
{{.Descripcion}}{{.Cantidad}}

Total: {{.Total}} — {{.Fecha}} — {{.EmpresaNombre}}

` out, err := RenderizarPlantillaEjemplo("cotizacion", html) if err != nil { t.Fatalf("RenderizarPlantillaEjemplo: %v", err) } for _, quiero := range []string{"Acme S.A.S.", "900.123.456-7", "Diseño de interfaz", "Integración con pasarela", "U-SITE"} { if !strings.Contains(out, quiero) { t.Errorf("falta %q en:\n%s", quiero, out) } } if strings.Contains(out, "{{") { t.Errorf("quedaron variables sin resolver:\n%s", out) } } func TestRenderizarPlantillaEjemploAvisaDeErrores(t *testing.T) { if _, err := RenderizarPlantillaEjemplo("cotizacion", "{{range .Items}}sin fin"); err == nil { t.Error("un {{range}} sin {{end}} debería dar error") } // Una variable que no existe no rompe: se renderiza como "", igual // que en la generación real. Eso se ve en la vista previa, que es el punto — // hacerla más estricta que producción rechazaría plantillas que sí andan. out, err := RenderizarPlantillaEjemplo("cotizacion", "Hola {{.NoExiste}}") if err != nil { t.Fatalf("no debería fallar: %v", err) } if !strings.Contains(out, "no value") { t.Errorf("una variable inexistente tendría que notarse en la vista previa: %q", out) } } // Cada tipo agrega sus propios campos; si esto se desincroniza de los // generadores, la vista previa muestra una cosa y el PDF sale con otra. func TestDatosDeEjemploPorTipo(t *testing.T) { casos := map[string][]string{ "contrato": {"Servicio", "Periodicidad"}, "acta": {"Proyecto", "Entregables"}, "cuenta_cobro": {"Concepto", "Numero"}, } for tipo, campos := range casos { datos := DatosDeEjemploPlantilla(tipo) for _, campo := range campos { if _, ok := datos[campo]; !ok { t.Errorf("faltan datos de ejemplo de %q para el tipo %q", campo, tipo) } } for _, comun := range []string{"Cliente", "Fecha", "EmpresaNombre", "Total"} { if _, ok := datos[comun]; !ok { t.Errorf("falta el campo común %q en el tipo %q", comun, tipo) } } } }