2021-07-30 17:53:27 +00:00
|
|
|
package tester
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
2023-03-31 14:50:08 +00:00
|
|
|
"github.com/laytan/php-parser/pkg/conf"
|
|
|
|
"github.com/laytan/php-parser/pkg/parser"
|
|
|
|
"github.com/laytan/php-parser/pkg/version"
|
|
|
|
"github.com/laytan/php-parser/pkg/visitor/dumper"
|
2021-07-30 17:53:27 +00:00
|
|
|
"gotest.tools/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ParserDumpTestSuite struct {
|
|
|
|
t *testing.T
|
|
|
|
|
|
|
|
Code string
|
|
|
|
Expected string
|
|
|
|
|
|
|
|
Version version.Version
|
|
|
|
|
|
|
|
actualDump *bytes.Buffer
|
|
|
|
dumper *dumper.Dumper
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewParserDumpTestSuite(t *testing.T) *ParserDumpTestSuite {
|
|
|
|
actualDump := bytes.NewBuffer(nil)
|
|
|
|
return &ParserDumpTestSuite{
|
|
|
|
t: t,
|
|
|
|
Version: version.Version{
|
|
|
|
Major: 7,
|
|
|
|
Minor: 4,
|
|
|
|
},
|
|
|
|
actualDump: actualDump,
|
|
|
|
dumper: dumper.NewDumper(actualDump),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ParserDumpTestSuite) WithTokens() {
|
|
|
|
p.dumper = p.dumper.WithTokens()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ParserDumpTestSuite) WithPositions() {
|
|
|
|
p.dumper = p.dumper.WithPositions()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ParserDumpTestSuite) UsePHP8() {
|
|
|
|
p.Version = version.Version{Major: 8, Minor: 0}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ParserDumpTestSuite) Run() {
|
2023-03-25 14:29:51 +00:00
|
|
|
p.t.Helper()
|
2023-03-25 14:02:34 +00:00
|
|
|
|
2021-07-30 17:53:27 +00:00
|
|
|
config := conf.Config{
|
|
|
|
Version: &p.Version,
|
|
|
|
}
|
|
|
|
|
|
|
|
actual, err := parser.Parse([]byte(p.Code), config)
|
|
|
|
if err != nil {
|
|
|
|
p.t.Fatalf("Error parse: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.dumper.Dump(actual)
|
|
|
|
|
|
|
|
assert.DeepEqual(p.t, p.Expected+"\n", p.actualDump.String())
|
|
|
|
}
|