Skip to content

Commit

Permalink
Add some more unit tests for the generator
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Frittoli <andrea.frittoli@gmail.com>
  • Loading branch information
afrittoli committed May 17, 2024
1 parent 837d538 commit 0029e15
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions tools/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ SPDX-License-Identifier: Apache-2.0
package main

import (
"os"
"path/filepath"
"strings"
"testing"
"text/template"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
Expand Down Expand Up @@ -107,3 +110,93 @@ func TestDataFromSchema(t *testing.T) {
t.Errorf("args: diff(-want,+got):\n%s", d)
}
}

// TestExecuteTemplate_Success verifies that templating and code formatting
// are both applied
func TestExecuteTemplate_Success(t *testing.T) {
// Template code. The result will be compiled.
helloTemplate := `package main
import "fmt"
func main() {
fmt.Println("Hello, {{.Name}}")
}`
// Expected output is the formatted code
expectedOutput := `package main
import "fmt"
func main() {
fmt.Println("Hello, World")
}
`

// Create a temporary directory to store the generated files
tempDir, err := os.MkdirTemp("", "test-execute-template-*")
if err != nil {
t.Fatalf("error creating temporary directory: %s", err)
}
defer os.RemoveAll(tempDir)

// Create a template file
tmpTemplateFilename := "template.txt.tmpl"
templateFile := filepath.Join(tempDir, tmpTemplateFilename)
err = os.WriteFile(templateFile, []byte(helloTemplate), 0644)
if err != nil {
t.Fatalf("error creating template file: %s", err)
}

// Create a template set
allTemplates := template.New("all")
_, err = allTemplates.ParseFiles(templateFile)
if err != nil {
t.Fatalf("error parsing template file: %s", err)
}

// Execute the template
outputFileName := filepath.Join(tempDir, "output.txt")
err = executeTemplate(allTemplates, tmpTemplateFilename, outputFileName, struct{ Name string }{"World"})
if err != nil {
t.Fatalf("error executing template: %s", err)
}

// Check the output file contents
output, err := os.ReadFile(outputFileName)
if err != nil {
t.Fatalf("error reading output file: %s", err)
}
if string(output) != expectedOutput {
t.Errorf("unexpected output: got %q, want %q", output, expectedOutput)
}
}

// TestExecuteTemplate_Error tests the error case of executeTemplate()
func TestExecuteTemplate_Error(t *testing.T) {
// Create a temporary directory to store the generated files
tempDir, err := os.MkdirTemp("", "test-execute-template-*")
if err != nil {
t.Fatalf("error creating temporary directory: %s", err)
}
defer os.RemoveAll(tempDir)

// Create an template file, valid template but invalid go code
templateFile := filepath.Join(tempDir, "template.txt")
err = os.WriteFile(templateFile, []byte("Hello, {{.Name}}!"), 0644)
if err != nil {
t.Fatalf("error creating template file: %s", err)
}

// Create a template set
allTemplates := template.New("all")
_, err = allTemplates.ParseFiles(templateFile)
if err != nil {
t.Fatalf("error parsing template file: %s", err)
}

// Execute the template
outputFileName := filepath.Join(tempDir, "output.txt")
err = executeTemplate(allTemplates, "template.txt", outputFileName, struct{ Name string }{"World"})
if err == nil {
t.Fatal("expected error executing template, got nil")
}
}

0 comments on commit 0029e15

Please sign in to comment.