| | 26 | |
| | 27 | function test_extract_from_markers() { |
| | 28 | $filename = DIR_TESTDATA . '/formatting/markers.txt'; |
| | 29 | |
| | 30 | // Assert that section marked with marker that is prefix of another marker is extracted correctly. |
| | 31 | $test_foo = array( |
| | 32 | 'Foo line', |
| | 33 | ); |
| | 34 | $this->assertEquals( $test_foo, extract_from_markers( $filename, 'Foo' ) ); |
| | 35 | |
| | 36 | // Assert that section marked by marker with spaces is extracted correctly. |
| | 37 | $test_foo_bar = array( |
| | 38 | 'Foo Bar line', |
| | 39 | ); |
| | 40 | $this->assertEquals( $test_foo_bar, extract_from_markers( $filename, 'Foo Bar' ) ); |
| | 41 | |
| | 42 | // Assert that section with multiple lines (including empty lines) is extracted correctly. |
| | 43 | $test_wordpress = array( |
| | 44 | '', |
| | 45 | 'Multiline 2', |
| | 46 | 'Multiline 3', |
| | 47 | '', |
| | 48 | 'Multiline 5', |
| | 49 | '', |
| | 50 | ); |
| | 51 | $this->assertEquals( $test_wordpress, extract_from_markers( $filename, 'Multiline' ) ); |
| | 52 | |
| | 53 | // Assert that empty array is returned for empty section. |
| | 54 | $this->assertEquals( array(), extract_from_markers( $filename, 'Empty' ) ); |
| | 55 | |
| | 56 | // Assert that empty array is returned for non-existing section. |
| | 57 | $this->assertEquals( array(), extract_from_markers( $filename, 'Null' ) ); |
| | 58 | |
| | 59 | // Assert that empty array is returned if file does not exists. |
| | 60 | $this->assertEquals( array(), extract_from_markers( rand_str(), 'Foo' ) ); |
| | 61 | } |
| | 62 | |
| | 63 | /** |
| | 64 | * @ticket 26829 |
| | 65 | */ |
| | 66 | function test_split_by_markers() { |
| | 67 | // Test data |
| | 68 | $pre = array( |
| | 69 | '# BEGIN FooPre', |
| | 70 | 'Line before', |
| | 71 | '# END FooPre', |
| | 72 | ); |
| | 73 | $in = array( |
| | 74 | '# BEGIN Foo', |
| | 75 | 'Line in', |
| | 76 | '# END Foo', |
| | 77 | ); |
| | 78 | $post = array( |
| | 79 | '# BEGIN FooPost', |
| | 80 | 'Line after', |
| | 81 | '# END FooPost', |
| | 82 | ); |
| | 83 | |
| | 84 | $lines = array_merge( $pre, $in, $post ); |
| | 85 | |
| | 86 | // Assert correct results when splitting by FooPre |
| | 87 | $this->assertEquals( |
| | 88 | array( array(), array( 'Line before' ), array_merge( $in, $post ) ), |
| | 89 | split_by_markers( $lines, 'FooPre' ) |
| | 90 | ); |
| | 91 | // Assert correct results when splitting by Foo |
| | 92 | $this->assertEquals( |
| | 93 | array( $pre, array( 'Line in' ), $post ), |
| | 94 | split_by_markers( $lines, 'Foo' ) |
| | 95 | ); |
| | 96 | // Assert correct results when splitting by FooPost |
| | 97 | $this->assertEquals( |
| | 98 | array( array_merge( $pre, $in ), array( 'Line after' ), array() ), |
| | 99 | split_by_markers( $lines, 'FooPost' ) |
| | 100 | ); |
| | 101 | } |