-
Notifications
You must be signed in to change notification settings - Fork 45
/
ppi_element_replace.t
executable file
·39 lines (32 loc) · 1.33 KB
/
ppi_element_replace.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/perl
# Unit testing for PPI::Element
use lib 't/lib';
use PPI::Test::pragmas;
use PPI::Document ();
use Test::More;
__REPLACE: {
my $Document = PPI::Document->new( \"print 'Hello World';" );
isa_ok( $Document, 'PPI::Document' );
my $string = $Document->find_first('Token::Quote');
isa_ok( $string, 'PPI::Token::Quote' );
is( $string->content, "'Hello World'", 'Got expected token' );
my $foo = PPI::Token::Quote::Single->new("'foo'");
isa_ok( $foo, 'PPI::Token::Quote::Single' );
is( $foo->content, "'foo'", 'Created Quote token' );
$string->replace( $foo );
is( $Document->serialize, "print 'foo';", 'replace works' );
}
__REPLACE_PARENT: {
my $Document = PPI::Document->new( \"print 'Hello World';" );
isa_ok( $Document, 'PPI::Document' );
my $statement = $Document->find_first('Statement');
isa_ok( $statement, 'PPI::Statement' );
is( $statement->content, "print 'Hello World';", 'Got expected token' );
my $doc = PPI::Document->new(\'for my $var ( @vars ) { say "foo" }');
my $foo = $doc->find('PPI::Statement::Compound');
isa_ok( $foo->[0], 'PPI::Statement::Compound');
is( $foo->[0]->content, q~for my $var ( @vars ) { say "foo" }~, 'for loop');
ok( $statement->parent->replace_child( $statement, $foo->[0] ) );
is( $Document->serialize, 'for my $var ( @vars ) { say "foo" }', 'replace works' );
}
done_testing();