-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.path.php
110 lines (95 loc) · 3.51 KB
/
test.path.php
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/*
* This file is part of the Ariadne Component Library.
*
* (c) Muze <info@muze.nl>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
require_once( __DIR__ . DIRECTORY_SEPARATOR . 'bootstrap.php' );
class TestPath extends UnitTestCase {
function testMapReduce() {
$path = '/a/b/c/';
$result = \arc\path::map( $path, function( $entry ) {
return strtoupper($entry);
});
$this->assertTrue( $result === '/A/B/C/' );
$result = \arc\path::reduce( $path, function( &$result, $entry ) {
return $result.$entry;
});
$this->assertTrue( $result === 'abc' );
$result = \arc\path::map( '/', function( $entry ) {
return 'a';
});
$this->assertTrue( $result === '/' );
$result = \arc\path::map( 'frop', function($entry) {
return 'a';
});
$this->assertTrue( $result === '/a/' );
}
function testSearch() {
$path = '/a/b/c/';
$count = 0;
$result = \arc\path::search( $path, function( $parent ) use ( &$count ) {
$count++;
if ( $parent == '/a/' ) {
return true;
}
});
$this->assertTrue( $result );
$this->assertTrue( $count == 2 );
$count = 0;
$result = \arc\path::search( $path, function( $parent ) use ( &$count ) {
$count++;
if ( $parent == '/a/' ) {
return true;
}
}, false ); // reverse order
$this->assertTrue( $result );
$this->assertTrue( $count == 3 );
}
function testCollapse() {
$this->assertTrue( \arc\path::collapse('/') === '/' );
$this->assertTrue( \arc\path::collapse('/test/') === '/test/' );
$this->assertTrue( \arc\path::collapse('/test//') === '/test/' );
$this->assertTrue( \arc\path::collapse('/test/../') === '/' );
$this->assertTrue( \arc\path::collapse('test') === '/test/' );
$this->assertTrue( \arc\path::collapse( '../', '/test/') === '/' );
$this->assertTrue( \arc\path::collapse( '..', '/test/foo/') === '/test/' );
$this->assertTrue( \arc\path::collapse( '/..//../', '/test/') === '/' );
$this->assertTrue( \arc\path::collapse( '', '/test/') === '/test/' );
}
function testParents() {
$parents = \arc\path::parents('/test/');
$this->assertTrue( $parents == array('/','/test/'));
$parents = \arc\path::parents('/test/foo/','/test/');
$this->assertTrue( $parents == array( '/test/', '/test/foo/'));
$parents = \arc\path::parents('/test/','/tost/');
$this->assertTrue( $parents == array( '/tost/') );
}
function testParent() {
$this->assertTrue( \arc\path::parent('/') == null );
$this->assertTrue( \arc\path::parent('/test/') == '/');
$this->assertTrue( \arc\path::parent('/a/b/') == '/a/');
$this->assertTrue( \arc\path::parent('/a/b/', '/a/b/') == null );
$this->assertTrue( \arc\path::parent('/a/b/', '/a/') == '/a/' );
$this->assertTrue( \arc\path::parent('/a/b/', '/test/') == null );
}
function testClean() {
$this->assertTrue( \arc\path::clean('/a/b/') == '/a/b/' );
$this->assertTrue( \arc\path::clean(' ') == '/%20/' );
$this->assertTrue( \arc\path::clean('/#/') == '/%23/');
$this->assertTrue( \arc\path::clean('/an a/', function( $filename ) {
return str_replace( 'a','', $filename );
}) == '/n /');
}
function testIsChild() {
$this->assertTrue( \arc\path::isChild( '/a/b/', '/a/' ) );
$this->assertFalse( \arc\path::isChild( '/b/', '/a/' ) );
}
function testDiff() {
$this->assertTrue( \arc\path::diff( '/a/', '/a/b/' ) == 'b/' );
$this->assertTrue( \arc\path::diff( '/a/', '/b/' ) == '../b/' );
}
}