|
| 1 | +<?php |
| 2 | + |
| 3 | +require_once __DIR__ . '/../../wp-includes/parser/class-wp-parser-node.php'; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | + |
| 7 | +class WP_Parser_Node_Tests extends TestCase { |
| 8 | + public function testEmptyChildren(): void { |
| 9 | + $node = new WP_Parser_Node( 1, 'root' ); |
| 10 | + |
| 11 | + $this->assertFalse( $node->has_child() ); |
| 12 | + $this->assertFalse( $node->has_child_node() ); |
| 13 | + $this->assertFalse( $node->has_child_token() ); |
| 14 | + |
| 15 | + $this->assertNull( $node->get_child() ); |
| 16 | + $this->assertNull( $node->get_child_node() ); |
| 17 | + $this->assertNull( $node->get_child_node( 'root' ) ); |
| 18 | + $this->assertNull( $node->get_child_token() ); |
| 19 | + $this->assertNull( $node->get_child_token( 1 ) ); |
| 20 | + |
| 21 | + $this->assertNull( $node->get_descendant_node() ); |
| 22 | + $this->assertNull( $node->get_descendant_token() ); |
| 23 | + |
| 24 | + $this->assertEmpty( $node->get_children() ); |
| 25 | + $this->assertEmpty( $node->get_child_nodes() ); |
| 26 | + $this->assertEmpty( $node->get_child_nodes( 'root' ) ); |
| 27 | + $this->assertEmpty( $node->get_child_tokens() ); |
| 28 | + $this->assertEmpty( $node->get_child_tokens( 1 ) ); |
| 29 | + |
| 30 | + $this->assertEmpty( $node->get_descendants() ); |
| 31 | + $this->assertEmpty( $node->get_descendant_nodes() ); |
| 32 | + $this->assertEmpty( $node->get_descendant_nodes( 'root' ) ); |
| 33 | + $this->assertEmpty( $node->get_descendant_tokens() ); |
| 34 | + $this->assertEmpty( $node->get_descendant_tokens( 1 ) ); |
| 35 | + } |
| 36 | + |
| 37 | + public function testNodeTree(): void { |
| 38 | + // Prepare nodes and tokens. |
| 39 | + $root = new WP_Parser_Node( 1, 'root' ); |
| 40 | + $n_keyword = new WP_Parser_Node( 2, 'keyword' ); |
| 41 | + $n_expr_a = new WP_Parser_Node( 3, 'expr' ); |
| 42 | + $n_expr_b = new WP_Parser_Node( 3, 'expr' ); |
| 43 | + $n_expr_c = new WP_Parser_Node( 3, 'expr' ); |
| 44 | + $t_select = new WP_Parser_Token( 100, 'SELECT' ); |
| 45 | + $t_comma = new WP_Parser_Token( 200, ',' ); |
| 46 | + $t_plus = new WP_Parser_Token( 300, '+' ); |
| 47 | + $t_one = new WP_Parser_Token( 400, '1' ); |
| 48 | + $t_two_a = new WP_Parser_Token( 400, '2' ); |
| 49 | + $t_two_b = new WP_Parser_Token( 400, '2' ); |
| 50 | + $t_eof = new WP_Parser_Token( 500, '' ); |
| 51 | + |
| 52 | + // Prepare a tree. |
| 53 | + // |
| 54 | + // A simplified testing tree for an input like "SELECT 1 + 2, 2". |
| 55 | + // |
| 56 | + // root |
| 57 | + // |- keyword |
| 58 | + // | |- "SELECT" |
| 59 | + // |- expr [a] |
| 60 | + // | |- "1" |
| 61 | + // | |- "+" |
| 62 | + // | |- expr [c] |
| 63 | + // | | |- "2" [b] |
| 64 | + // |- "," |
| 65 | + // |- expr [b] |
| 66 | + // | |- "2" [a] |
| 67 | + // |- EOF |
| 68 | + $root->append_child( $n_keyword ); |
| 69 | + $root->append_child( $n_expr_a ); |
| 70 | + $root->append_child( $t_comma ); |
| 71 | + $root->append_child( $n_expr_b ); |
| 72 | + $root->append_child( $t_eof ); |
| 73 | + |
| 74 | + $n_keyword->append_child( $t_select ); |
| 75 | + $n_expr_a->append_child( $t_one ); |
| 76 | + $n_expr_a->append_child( $t_plus ); |
| 77 | + $n_expr_a->append_child( $n_expr_c ); |
| 78 | + $n_expr_b->append_child( $t_two_a ); |
| 79 | + $n_expr_c->append_child( $t_two_b ); |
| 80 | + |
| 81 | + // Test "has" methods. |
| 82 | + $this->assertTrue( $root->has_child() ); |
| 83 | + $this->assertTrue( $root->has_child_node() ); |
| 84 | + $this->assertTrue( $root->has_child_token() ); |
| 85 | + |
| 86 | + // Test single child methods. |
| 87 | + $this->assertSame( $n_keyword, $root->get_child() ); |
| 88 | + $this->assertSame( $n_keyword, $root->get_child_node() ); |
| 89 | + $this->assertSame( $n_keyword, $root->get_child_node( 'keyword' ) ); |
| 90 | + $this->assertSame( $n_expr_a, $root->get_child_node( 'expr' ) ); |
| 91 | + $this->assertSame( $t_comma, $root->get_child_token() ); |
| 92 | + $this->assertSame( $t_comma, $root->get_child_token( 200 ) ); |
| 93 | + $this->assertNull( $root->get_child_token( 100 ) ); |
| 94 | + |
| 95 | + // Test multiple children methods. |
| 96 | + $this->assertSame( array( $n_keyword, $n_expr_a, $t_comma, $n_expr_b, $t_eof ), $root->get_children() ); |
| 97 | + $this->assertSame( array( $n_keyword, $n_expr_a, $n_expr_b ), $root->get_child_nodes() ); |
| 98 | + $this->assertSame( array( $n_expr_a, $n_expr_b ), $root->get_child_nodes( 'expr' ) ); |
| 99 | + $this->assertSame( array(), $root->get_child_nodes( 'root' ) ); |
| 100 | + $this->assertSame( array( $t_comma, $t_eof ), $root->get_child_tokens() ); |
| 101 | + $this->assertSame( array( $t_comma ), $root->get_child_tokens( 200 ) ); |
| 102 | + $this->assertSame( array(), $root->get_child_tokens( 100 ) ); |
| 103 | + |
| 104 | + // Test single descendant methods. |
| 105 | + // @TODO: Consider breadth-first search vs depth-first search. |
| 106 | + $this->assertSame( $n_keyword, $root->get_descendant_node() ); |
| 107 | + $this->assertSame( $n_expr_a, $root->get_descendant_node( 'expr' ) ); |
| 108 | + $this->assertSame( null, $root->get_descendant_node( 'root' ) ); |
| 109 | + $this->assertSame( $t_comma, $root->get_descendant_token() ); |
| 110 | + $this->assertSame( $t_one, $root->get_descendant_token( 400 ) ); |
| 111 | + $this->assertSame( null, $root->get_descendant_token( 123 ) ); |
| 112 | + |
| 113 | + // Test multiple descendant methods. |
| 114 | + // @TODO: Consider breadth-first search vs depth-first search. |
| 115 | + $this->assertSame( |
| 116 | + array( $n_keyword, $n_expr_a, $t_comma, $n_expr_b, $t_eof, $t_select, $t_one, $t_plus, $n_expr_c, $t_two_a, $t_two_b ), |
| 117 | + $root->get_descendants() |
| 118 | + ); |
| 119 | + $this->assertSame( |
| 120 | + array( $n_keyword, $n_expr_a, $n_expr_b, $n_expr_c ), |
| 121 | + $root->get_descendant_nodes() |
| 122 | + ); |
| 123 | + $this->assertSame( |
| 124 | + array( $n_expr_a, $n_expr_b, $n_expr_c ), |
| 125 | + $root->get_descendant_nodes( 'expr' ) |
| 126 | + ); |
| 127 | + $this->assertSame( |
| 128 | + array(), |
| 129 | + $root->get_descendant_nodes( 'root' ) |
| 130 | + ); |
| 131 | + $this->assertSame( |
| 132 | + array( $t_comma, $t_eof, $t_select, $t_one, $t_plus, $t_two_a, $t_two_b ), |
| 133 | + $root->get_descendant_tokens() |
| 134 | + ); |
| 135 | + $this->assertSame( |
| 136 | + array( $t_one, $t_two_a, $t_two_b ), |
| 137 | + $root->get_descendant_tokens( 400 ) |
| 138 | + ); |
| 139 | + $this->assertSame( |
| 140 | + array(), |
| 141 | + $root->get_descendant_tokens( 123 ) |
| 142 | + ); |
| 143 | + } |
| 144 | +} |
0 commit comments