| 1 |
// Copyright 2010 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.string.pathTest'); |
| 16 |
goog.setTestOnly('goog.string.pathTest'); |
| 17 |
|
| 18 |
goog.require('goog.string.path'); |
| 19 |
goog.require('goog.testing.jsunit'); |
| 20 |
|
| 21 |
// Some test data comes from Python's posixpath tests. |
| 22 |
// See http://svn.python.org/view/python/trunk/Lib/test/test_posixpath.py |
| 23 |
|
| 24 |
function testBasename() { |
| 25 |
assertEquals('bar', goog.string.path.baseName('/foo/bar')); |
| 26 |
assertEquals('', goog.string.path.baseName('/')); |
| 27 |
assertEquals('foo', goog.string.path.baseName('foo')); |
| 28 |
assertEquals('foo', goog.string.path.baseName('////foo')); |
| 29 |
assertEquals('bar', goog.string.path.baseName('//foo//bar')); |
| 30 |
} |
| 31 |
|
| 32 |
function testDirname() { |
| 33 |
assertEquals('/foo', goog.string.path.dirname('/foo/bar')); |
| 34 |
assertEquals('/', goog.string.path.dirname('/')); |
| 35 |
assertEquals('', goog.string.path.dirname('foo')); |
| 36 |
assertEquals('////', goog.string.path.dirname('////foo')); |
| 37 |
assertEquals('//foo', goog.string.path.dirname('//foo//bar')); |
| 38 |
} |
| 39 |
|
| 40 |
function testJoin() { |
| 41 |
assertEquals('/bar/baz', goog.string.path.join('/foo', 'bar', '/bar', 'baz')); |
| 42 |
assertEquals('/foo/bar/baz', goog.string.path.join('/foo', 'bar', 'baz')); |
| 43 |
assertEquals('/foo/bar/baz', goog.string.path.join('/foo/', 'bar', 'baz')); |
| 44 |
assertEquals('/foo/bar/baz/', goog.string.path.join('/foo/', 'bar/', 'baz/')); |
| 45 |
} |
| 46 |
|
| 47 |
function testNormalizePath() { |
| 48 |
assertEquals('.', goog.string.path.normalizePath('')); |
| 49 |
assertEquals('.', goog.string.path.normalizePath('./')); |
| 50 |
assertEquals('/', goog.string.path.normalizePath('/')); |
| 51 |
assertEquals('//', goog.string.path.normalizePath('//')); |
| 52 |
assertEquals('/', goog.string.path.normalizePath('///')); |
| 53 |
assertEquals('/foo/bar', goog.string.path.normalizePath('///foo/.//bar//')); |
| 54 |
assertEquals( |
| 55 |
'/foo/baz', |
| 56 |
goog.string.path.normalizePath('///foo/.//bar//.//..//.//baz')); |
| 57 |
assertEquals( |
| 58 |
'/foo/bar', goog.string.path.normalizePath('///..//./foo/.//bar')); |
| 59 |
assertEquals( |
| 60 |
'../../cat/dog', goog.string.path.normalizePath('../../cat/dog/')); |
| 61 |
assertEquals('../dog', goog.string.path.normalizePath('../cat/../dog/')); |
| 62 |
assertEquals('/cat/dog', goog.string.path.normalizePath('/../cat/dog/')); |
| 63 |
assertEquals('/dog', goog.string.path.normalizePath('/../cat/../dog')); |
| 64 |
assertEquals('/dog', goog.string.path.normalizePath('/../../../dog')); |
| 65 |
} |
| 66 |
|
| 67 |
function testSplit() { |
| 68 |
assertArrayEquals(['/foo', 'bar'], goog.string.path.split('/foo/bar')); |
| 69 |
assertArrayEquals(['/', ''], goog.string.path.split('/')); |
| 70 |
assertArrayEquals(['', 'foo'], goog.string.path.split('foo')); |
| 71 |
assertArrayEquals(['////', 'foo'], goog.string.path.split('////foo')); |
| 72 |
assertArrayEquals(['//foo', 'bar'], goog.string.path.split('//foo//bar')); |
| 73 |
} |
| 74 |
|
| 75 |
function testExtension() { |
| 76 |
assertEquals('jpg', goog.string.path.extension('././foo/bar/baz.jpg')); |
| 77 |
assertEquals('jpg', goog.string.path.extension('././foo bar/baz.jpg')); |
| 78 |
assertEquals('jpg', goog.string.path.extension('foo/bar/baz/blah blah.jpg')); |
| 79 |
assertEquals('', goog.string.path.extension('../../foo/bar/baz baz')); |
| 80 |
assertEquals('', goog.string.path.extension('../../foo bar/baz baz')); |
| 81 |
assertEquals('', goog.string.path.extension('foo/bar/.')); |
| 82 |
assertEquals('', goog.string.path.extension(' ')); |
| 83 |
assertEquals('', goog.string.path.extension('')); |
| 84 |
assertEquals('', goog.string.path.extension('/home/username/.bashrc')); |
| 85 |
|
| 86 |
// Tests cases taken from python os.path.splitext(). |
| 87 |
assertEquals('bar', goog.string.path.extension('foo.bar')); |
| 88 |
assertEquals('bar', goog.string.path.extension('foo.boo.bar')); |
| 89 |
assertEquals('bar', goog.string.path.extension('foo.boo.biff.bar')); |
| 90 |
assertEquals('rc', goog.string.path.extension('.csh.rc')); |
| 91 |
assertEquals('', goog.string.path.extension('nodots')); |
| 92 |
assertEquals('', goog.string.path.extension('.cshrc')); |
| 93 |
assertEquals('', goog.string.path.extension('...manydots')); |
| 94 |
assertEquals('ext', goog.string.path.extension('...manydots.ext')); |
| 95 |
assertEquals('', goog.string.path.extension('.')); |
| 96 |
assertEquals('', goog.string.path.extension('..')); |
| 97 |
assertEquals('', goog.string.path.extension('........')); |
| 98 |
assertEquals('', goog.string.path.extension('')); |
| 99 |
} |