package printer_test import ( "bytes" "testing" "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/php5" "github.com/z7zmey/php-parser/printer" ) func parsePhp5(src string) node.Node { php5parser := php5.NewParser([]byte(src), "5.6") php5parser.WithFreeFloating() php5parser.Parse() return php5parser.GetRootNode() } func printPhp5(n node.Node) string { o := bytes.NewBufferString("") p := printer.NewPrinter(o) p.Print(n) return o.String() } // test node func TestParseAndPrintPhp5Root(t *testing.T) { src := `
Hello
>= $b ; ` actual := printPhp5(parsePhp5(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } // test binary func TestParseAndPrintPhp5Binary(t *testing.T) { src := `= $b ; $a > $b ; $a === $b ; $a and $b ; $a or $b ; $a xor $b ; $a - $b ; $a % $b ; $a * $b ; $a != $b ; $a <> $b ; $a !== $b ; $a + $b ; $a ** $b ; $a << $b ; $a >> $b ; $a <= $b ; $a < $b ; ` actual := printPhp5(parsePhp5(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } // test cast func TestParseAndPrintPhp5Cast(t *testing.T) { src := ` $world , ] ; ` actual := printPhp5(parsePhp5(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } func TestParseAndPrintPhp5Array(t *testing.T) { src := ` 2 ) ; ` actual := printPhp5(parsePhp5(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } func TestParseAndPrintPhp5BitwiseNot(t *testing.T) { src := ` bar ( $arg ) ;` actual := printPhp5(parsePhp5(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } func TestParseAndPrintPhp5New(t *testing.T) { src := ` b ;` actual := printPhp5(parsePhp5(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } func TestParseAndPrintPhp5Reference(t *testing.T) { src := ` & $c ] ; $a = function ( ) use ( & $b ) { // do nothing } ; foreach ( $a as & $b ) { // do nothing }` actual := printPhp5(parsePhp5(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } func TestParseAndPrintPhp5Require(t *testing.T) { src := ` & $b , // one $c , /* two */ ] ; $a = [0, 1, 2] ;` actual := printPhp5(parsePhp5(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } func TestParseAndPrintPhp5StaticCall(t *testing.T) { src := ` $v ;` actual := printPhp5(parsePhp5(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } // test stmt func TestParseAndPrintPhp5AltIf(t *testing.T) { src := ` & $v ) : echo $v ; endforeach ;` actual := printPhp5(parsePhp5(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } func TestParseAndPrintPhp5AltSwitch(t *testing.T) { src := ` & $v ) { ; }` actual := printPhp5(parsePhp5(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } func TestParseAndPrintPhp5Function(t *testing.T) { src := `testtest call ( ) ; $a -> { $b . 'b' } ; $a -> $b ( ) -> { $c . 'c' } ; ` actual := printPhp5(parsePhp5(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } func TestParseAndPrintPhp5ComplexString1(t *testing.T) { src := `bar" ; ` actual := printPhp5(parsePhp5(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } func TestParseAndPrintPhp5ComplexString2(t *testing.T) { src := ` bar }" ; "test ${ $foo -> bar ( ) }" ; "test ${ $a . '' }" ; ` actual := printPhp5(parsePhp5(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } func TestParseAndPrintPhp5ComplexString3(t *testing.T) { src := ` bar }" ; "test ${$foo -> bar ( ) }" ; "test ${$a . '' }" ; ` actual := printPhp5(parsePhp5(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } func TestParseAndPrintPhp5ComplexString4(t *testing.T) { src := ` bar }" ; "test {$foo -> bar ( ) }" ; ` actual := printPhp5(parsePhp5(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } }