package printer_test import ( "bytes" "os" "testing" "github.com/z7zmey/php-parser/node" "github.com/z7zmey/php-parser/node/name" "github.com/z7zmey/php-parser/node/stmt" "github.com/z7zmey/php-parser/php7" "github.com/z7zmey/php-parser/printer" ) func ExamplePrinter() { src := `Hello >= $b ; ` actual := print(parse(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } // test binary func TestParseAndPrintBinary(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 ; $a <=> $b ; ` actual := print(parse(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } // test cast func TestParseAndPrintCast(t *testing.T) { src := ` $world , ... $unpack ] ; ` actual := print(parse(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } func TestParseAndPrintArray(t *testing.T) { src := ` 2 ) ; ` actual := print(parse(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } func TestParseAndPrintBitwiseNot(t *testing.T) { src := ` $c ; ` actual := print(parse(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } func TestParseAndPrintConstFetch(t *testing.T) { src := ` bar ( $arg , ) ;` actual := print(parse(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } func TestParseAndPrintNew(t *testing.T) { src := ` b ;` actual := print(parse(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } func TestParseAndPrintReference(t *testing.T) { src := ` & $c ] ; $a = function ( ) use ( & $b ) { // do nothing } ; foreach ( $a as & $b ) { // do nothing }` actual := print(parse(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } func TestParseAndPrintRequire(t *testing.T) { src := ` & $b , // one $c , /* two */ ] ; $a = [0, 1, 2] ;` actual := print(parse(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } func TestParseAndPrintShortList(t *testing.T) { src := ` $v ; yield from $a ;` actual := print(parse(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } // test stmt func TestParseAndPrintAltIf(t *testing.T) { src := ` & $v ) : echo $v ; endforeach ;` actual := print(parse(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } func TestParseAndPrintAltSwitch(t *testing.T) { src := ` & $v ) { ; }` actual := print(parse(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } func TestParseAndPrintFunction(t *testing.T) { src := `testtest call ( ) ; $a -> { $b . 'b' } ; ` actual := print(parse(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } func TestParseAndPrintComplexString1(t *testing.T) { src := `bar" ; ` actual := print(parse(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } func TestParseAndPrintComplexString2(t *testing.T) { src := ` bar }" ; "test ${ $foo -> bar ( ) }" ; "test ${ $a . '' }" ; ` actual := print(parse(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } func TestParseAndPrintComplexString3(t *testing.T) { src := ` bar }" ; "test ${$foo -> bar ( ) }" ; "test ${$a . '' }" ; ` actual := print(parse(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } } func TestParseAndPrintComplexString4(t *testing.T) { src := ` bar }" ; "test {$foo -> bar ( ) }" ; ` actual := print(parse(src)) if src != actual { t.Errorf("\nexpected: %s\ngot: %s\n", src, actual) } }