/*
 * Copyright (c) Microsoft Corporation.
 * Licensed under the MIT license.
 */

package astmodel

import (
	"bytes"
	"testing"

	. "github.com/onsi/gomega"

	"github.com/dave/dst"
	"github.com/dave/dst/decorator"
	"github.com/sebdah/goldie/v2"
)

// assertStmtsExpected asserts the given statement generates the expected code, comparing it to a golden file
func assertStmtsExpected(t *testing.T, stmts []dst.Stmt) {
	t.Helper()
	g := NewGomegaWithT(t)
	gold := goldie.New(t)

	if len(stmts) > 0 {
		stmt := stmts[0]
		stmt.Decorations().Before = dst.EmptyLine
		stmt.Decorations().After = dst.EmptyLine
		stmt.Decorations().Start = []string{
			"// Generated by " + t.Name(),
		}
	}

	block := &dst.BlockStmt{
		List: stmts,
	}

	fn := &dst.FuncDecl{
		Name: dst.NewIdent("TestCase"),
		Type: &dst.FuncType{},
		Body: block,
		Decs: dst.FuncDeclDecorations{
			NodeDecs: dst.NodeDecs{
				Before: dst.EmptyLine,
			},
		},
	}

	file := &dst.File{
		Decs: dst.FileDecorations{
			NodeDecs: dst.NodeDecs{
				After: dst.EmptyLine,
			},
		},
		Name:  dst.NewIdent("TestCase"),
		Decls: []dst.Decl{fn},
	}

	var buffer bytes.Buffer
	err := decorator.Fprint(&buffer, file)
	g.Expect(err).To(Succeed())

	gold.Assert(t, t.Name(), buffer.Bytes())
}
