path_test.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. goog.provide('goog.string.pathTest');
  15. goog.setTestOnly('goog.string.pathTest');
  16. goog.require('goog.string.path');
  17. goog.require('goog.testing.jsunit');
  18. // Some test data comes from Python's posixpath tests.
  19. // See http://svn.python.org/view/python/trunk/Lib/test/test_posixpath.py
  20. function testBasename() {
  21. assertEquals('bar', goog.string.path.baseName('/foo/bar'));
  22. assertEquals('', goog.string.path.baseName('/'));
  23. assertEquals('foo', goog.string.path.baseName('foo'));
  24. assertEquals('foo', goog.string.path.baseName('////foo'));
  25. assertEquals('bar', goog.string.path.baseName('//foo//bar'));
  26. }
  27. function testDirname() {
  28. assertEquals('/foo', goog.string.path.dirname('/foo/bar'));
  29. assertEquals('/', goog.string.path.dirname('/'));
  30. assertEquals('', goog.string.path.dirname('foo'));
  31. assertEquals('////', goog.string.path.dirname('////foo'));
  32. assertEquals('//foo', goog.string.path.dirname('//foo//bar'));
  33. }
  34. function testJoin() {
  35. assertEquals('/bar/baz', goog.string.path.join('/foo', 'bar', '/bar', 'baz'));
  36. assertEquals('/foo/bar/baz', goog.string.path.join('/foo', 'bar', 'baz'));
  37. assertEquals('/foo/bar/baz', goog.string.path.join('/foo/', 'bar', 'baz'));
  38. assertEquals('/foo/bar/baz/', goog.string.path.join('/foo/', 'bar/', 'baz/'));
  39. }
  40. function testNormalizePath() {
  41. assertEquals('.', goog.string.path.normalizePath(''));
  42. assertEquals('.', goog.string.path.normalizePath('./'));
  43. assertEquals('/', goog.string.path.normalizePath('/'));
  44. assertEquals('//', goog.string.path.normalizePath('//'));
  45. assertEquals('/', goog.string.path.normalizePath('///'));
  46. assertEquals('/foo/bar', goog.string.path.normalizePath('///foo/.//bar//'));
  47. assertEquals(
  48. '/foo/baz',
  49. goog.string.path.normalizePath('///foo/.//bar//.//..//.//baz'));
  50. assertEquals(
  51. '/foo/bar', goog.string.path.normalizePath('///..//./foo/.//bar'));
  52. assertEquals(
  53. '../../cat/dog', goog.string.path.normalizePath('../../cat/dog/'));
  54. assertEquals('../dog', goog.string.path.normalizePath('../cat/../dog/'));
  55. assertEquals('/cat/dog', goog.string.path.normalizePath('/../cat/dog/'));
  56. assertEquals('/dog', goog.string.path.normalizePath('/../cat/../dog'));
  57. assertEquals('/dog', goog.string.path.normalizePath('/../../../dog'));
  58. }
  59. function testSplit() {
  60. assertArrayEquals(['/foo', 'bar'], goog.string.path.split('/foo/bar'));
  61. assertArrayEquals(['/', ''], goog.string.path.split('/'));
  62. assertArrayEquals(['', 'foo'], goog.string.path.split('foo'));
  63. assertArrayEquals(['////', 'foo'], goog.string.path.split('////foo'));
  64. assertArrayEquals(['//foo', 'bar'], goog.string.path.split('//foo//bar'));
  65. }
  66. function testExtension() {
  67. assertEquals('jpg', goog.string.path.extension('././foo/bar/baz.jpg'));
  68. assertEquals('jpg', goog.string.path.extension('././foo bar/baz.jpg'));
  69. assertEquals('jpg', goog.string.path.extension('foo/bar/baz/blah blah.jpg'));
  70. assertEquals('', goog.string.path.extension('../../foo/bar/baz baz'));
  71. assertEquals('', goog.string.path.extension('../../foo bar/baz baz'));
  72. assertEquals('', goog.string.path.extension('foo/bar/.'));
  73. assertEquals('', goog.string.path.extension(' '));
  74. assertEquals('', goog.string.path.extension(''));
  75. assertEquals('', goog.string.path.extension('/home/username/.bashrc'));
  76. // Tests cases taken from python os.path.splitext().
  77. assertEquals('bar', goog.string.path.extension('foo.bar'));
  78. assertEquals('bar', goog.string.path.extension('foo.boo.bar'));
  79. assertEquals('bar', goog.string.path.extension('foo.boo.biff.bar'));
  80. assertEquals('rc', goog.string.path.extension('.csh.rc'));
  81. assertEquals('', goog.string.path.extension('nodots'));
  82. assertEquals('', goog.string.path.extension('.cshrc'));
  83. assertEquals('', goog.string.path.extension('...manydots'));
  84. assertEquals('ext', goog.string.path.extension('...manydots.ext'));
  85. assertEquals('', goog.string.path.extension('.'));
  86. assertEquals('', goog.string.path.extension('..'));
  87. assertEquals('', goog.string.path.extension('........'));
  88. assertEquals('', goog.string.path.extension(''));
  89. }