2021-07-30 17:53:27 +00:00
|
|
|
package tester
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
2021-07-30 18:01:34 +00:00
|
|
|
"github.com/VKCOM/php-parser/pkg/conf"
|
|
|
|
"github.com/VKCOM/php-parser/pkg/parser"
|
|
|
|
"github.com/VKCOM/php-parser/pkg/version"
|
|
|
|
"github.com/VKCOM/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:02:34 +00:00
|
|
|
p.t.Helper()
|
|
|
|
|
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())
|
|
|
|
}
|