PageRenderTime 57ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/specs/util/path.js

https://gitlab.com/zouxc/seajs
JavaScript | 181 lines | 123 code | 31 blank | 27 comment | 1 complexity | cd9dc7a9ae43fae0239dcbcba08a110e MD5 | raw file
  1. define(function(require) {
  2. var test = require('../../test')
  3. var assert = test.assert
  4. assert(dirname('./a/b/c.js') === './a/b/', 'dirname')
  5. assert(dirname('a/b/c.js') === 'a/b/', 'dirname')
  6. assert(dirname('/a/b/c.js') === '/a/b/', 'dirname')
  7. assert(dirname('/d.js') === '/', 'dirname')
  8. assert(dirname('/') === '/', 'dirname')
  9. assert(dirname('/xxx') === '/', 'dirname')
  10. assert(dirname('http://cdn.com/js/file.js') === 'http://cdn.com/js/', 'dirname')
  11. assert(dirname('http://cdn.com/js/file.js?t=xxx') === 'http://cdn.com/js/', 'dirname')
  12. assert(dirname('http://cdn.com/js/file.js?t=xxx#zzz') === 'http://cdn.com/js/', 'dirname')
  13. assert(dirname('http://example.com/page/index.html#zzz?t=xxx') === 'http://example.com/page/', 'dirname')
  14. assert(dirname('http://example.com/arale/seajs/1.2.0/??sea.js,seajs-combo.js') === 'http://example.com/arale/seajs/1.2.0/', 'dirname')
  15. assert(dirname('http://cdn.com/??seajs/1.2.0/sea.js,jquery/1.7.2/jquery.js') === 'http://cdn.com/', 'dirname')
  16. assert(dirname('http://seajs.com/docs/#/abc') === 'http://seajs.com/docs/', 'dirname')
  17. //assert(dirname('') === null, 'dirname')
  18. //assert(realpath('http://test.com/./a//b/../c') === 'http://test.com/a/c', 'realpath')
  19. assert(realpath('http://test.com/a/b/c/d/e/f/g/h/../../../../../../i') === 'http://test.com/a/b/i', 'realpath')
  20. assert(realpath('https://test.com/a/b/../../c') === 'https://test.com/c', 'realpath')
  21. assert(realpath('file:///a//b/c') === 'file:///a/b/c', 'realpath')
  22. assert(realpath('http://a//b/c') === 'http://a/b/c', 'realpath')
  23. assert(realpath('http://a/b/c//../d') === 'http://a/b/d', 'realpath')
  24. assert(realpath('http://a///b/c') === 'http://a/b/c', 'realpath')
  25. assert(normalize('a/b/c') === 'a/b/c.js', 'normalize')
  26. assert(normalize('a/b/c.js') === 'a/b/c.js', 'normalize')
  27. assert(normalize('a/b/c.css') === 'a/b/c.css.js', 'normalize')
  28. assert(normalize('a/b/c.d') === 'a/b/c.d.js', 'normalize')
  29. assert(normalize('a/b/c.json#') === 'a/b/c.json', 'normalize')
  30. assert(normalize('a/b/c.json') === 'a/b/c.json.js', 'normalize')
  31. assert(normalize('c?t=20110525') === 'c?t=20110525', 'normalize')
  32. assert(normalize('c?t=20110525#') === 'c?t=20110525', 'normalize')
  33. //assert(normalize('a/b/') === 'a/b/', 'normalize')
  34. seajs.config({
  35. alias: {
  36. 'jquery-debug': 'jquery/1.8.0/jquery-debug',
  37. 'app': 'app/1.2/app',
  38. 'alias/a': 'path/to/biz/a.js',
  39. './alias/b': 'path/to/b.js',
  40. '/alias/c': 'c.js',
  41. 'http://test.com/router': 'router.js?t=20110525'
  42. }
  43. })
  44. assert(parseAlias('jquery-debug') === 'jquery/1.8.0/jquery-debug', 'parseAlias')
  45. assert(parseAlias('app') === 'app/1.2/app', 'parseAlias')
  46. assert(parseAlias('alias/a') === 'path/to/biz/a.js', 'parseAlias')
  47. assert(parseAlias('./alias/b') === 'path/to/b.js', 'parseAlias')
  48. assert(parseAlias('/alias/c') === 'c.js', 'parseAlias')
  49. assert(parseAlias('http://test.com/router') === 'router.js?t=20110525', 'parseAlias')
  50. seajs.config({
  51. paths: {
  52. 'xx-path': 'http://xx.com/path/to/',
  53. 'xx/path': 'http://xx.com/path/to/',
  54. 'http:': 'WRONG'
  55. }
  56. })
  57. assert(parsePaths('xx-path/a') === 'http://xx.com/path/to//a', 'parsePaths')
  58. assert(parsePaths('/xx-path/a') === '/xx-path/a', 'parsePaths')
  59. assert(parsePaths('xx/path/a') === 'xx/path/a', 'parsePaths')
  60. assert(parsePaths('http://xx/path/xx-path/') === 'http://xx/path/xx-path/', 'parsePaths')
  61. assert(parsePaths('/') === '/', 'parsePaths')
  62. assert(parsePaths('.') === '.', 'parsePaths')
  63. seajs.config({
  64. vars: {
  65. 'locale': 'zh-cn',
  66. 'biz': 'path/to/biz',
  67. 'xx': './xx',
  68. 'zz': 'zz.js',
  69. 'a/b': 'path/to/a/b.js',
  70. 'c': '{path}/to/c.js'
  71. }
  72. })
  73. assert(parseVars('./i18n/{locale}.js') === './i18n/zh-cn.js', 'parseVars')
  74. assert(parseVars('{biz}/js/x') === 'path/to/biz/js/x', 'parseVars')
  75. assert(parseVars('/js/{xx}/c.js') === '/js/./xx/c.js', 'parseVars')
  76. assert(parseVars('/js/{xx}/{zz}') === '/js/./xx/zz.js', 'parseVars')
  77. assert(parseVars('{a/b}') === 'path/to/a/b.js', 'parseVars')
  78. assert(parseVars('{not-existed}') === '{not-existed}', 'parseVars')
  79. assert(parseVars('{c}') === '{path}/to/c.js', 'parseVars')
  80. var CWR = cwd.match(/^.*?\/\/.*?\//)[0]
  81. assert(addBase('http://a.com/b.js') === 'http://a.com/b.js', 'addBase')
  82. assert(addBase('./a.js', 'http://test.com/path/b.js') === 'http://test.com/path/a.js', 'addBase')
  83. assert(addBase('/b.js', 'http://test.com/path/to/c.js') === CWR + 'b.js', 'addBase')
  84. assert(addBase('c', 'http://test.com/path/to/c.js') === cwd + 'c', 'addBase')
  85. seajs.config({
  86. map: [
  87. ['aa.js', 'aa-debug.js'],
  88. [/^jquery\/.*/, 'http://localhost/jquery.js'],
  89. [/^(.*)\/js\/(.*)$/, function(m, m1, m2) {
  90. return m1 + '/script/' + m2
  91. }],
  92. function(uri) {
  93. if (uri.indexOf('/function/') > -1) {
  94. return 'http://test.com/path/to/function.js'
  95. }
  96. },
  97. ['cc.js', './cc.js']
  98. ]
  99. })
  100. assert(parseMap('path/to/aa.js') === 'path/to/aa-debug.js', 'parseMap')
  101. assert(parseMap('jquery/2.0.0/jquery') === 'http://localhost/jquery.js', 'parseMap')
  102. assert(parseMap('jquery/2.0.0/jquery-debug') === 'http://localhost/jquery.js', 'parseMap')
  103. assert(parseMap('path/to/js/a') === 'path/to/script/a', 'parseMap')
  104. assert(parseMap('path/to/function/b') === 'http://test.com/path/to/function.js', 'parseMap')
  105. assert(parseMap('cc.js') === './cc.js', 'parseMap')
  106. assert(id2Uri('path/to/a') === cwd + 'path/to/a.js', 'id2Uri')
  107. assert(id2Uri('path/to/a.js') === cwd + 'path/to/a.js', 'id2Uri')
  108. assert(id2Uri('path/to/a.js#') === cwd + 'path/to/a.js', 'id2Uri')
  109. assert(id2Uri('path/to/z.js?t=1234') === cwd + 'path/to/z.js?t=1234', 'id2Uri')
  110. assert(id2Uri('path/to/z?t=1234') === cwd + 'path/to/z?t=1234', 'id2Uri')
  111. assert(id2Uri('./b', 'http://test.com/path/to/x.js') === 'http://test.com/path/to/b.js', 'id2Uri')
  112. assert(id2Uri('/c', 'http://test.com/path/x.js') === CWR + 'c.js', 'id2Uri')
  113. //assert(id2Uri('/root/', 'file:///Users/lifesinger/tests/specs/util/test.html') === CWR + 'root/', 'id2Uri')
  114. assert(id2Uri('http://test.com/x.js') === 'http://test.com/x.js', 'id2Uri')
  115. assert(id2Uri('http://test.com/x.js#') === 'http://test.com/x.js', 'id2Uri')
  116. assert(id2Uri('./z.js', 'http://test.com/x.js') === 'http://test.com/z.js', 'id2Uri')
  117. assert(id2Uri('') === '', 'id2Uri')
  118. assert(id2Uri() === '', 'id2Uri')
  119. assert(id2Uri('http://XXX.com.cn/min/index.php?g=commonCss.css') === 'http://XXX.com.cn/min/index.php?g=commonCss.css', 'id2Uri')
  120. assert(id2Uri('./front/jquery.x.queue.js#') === cwd + 'front/jquery.x.queue.js', 'id2Uri')
  121. assert(id2Uri('//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js') === location.protocol + '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js', 'id2Uri')
  122. var _cwd = cwd
  123. seajs.config({ cwd: '/User/lifesinger/path/to/root/' })
  124. assert(id2Uri('/C/path/to/a') === '/C/path/to/a.js', 'id2Uri ' + id2Uri('/C/path/to/a'))
  125. seajs.config({ cwd: _cwd })
  126. /*
  127. assert(isAbsolute('http://test.com/') === true, 'isAbsolute')
  128. assert(isAbsolute('https://test.com/') === true, 'isAbsolute')
  129. assert(isAbsolute('file:///c/') === true, 'isAbsolute')
  130. assert(isRelative('./') === true, 'isRelative')
  131. assert(isRelative('../') === true, 'isRelative')
  132. assert(isRoot('/') === true, 'isRoot')
  133. assert(isRoot('//') === true, 'isRoot')
  134. assert(isRoot('/a') === true, 'isRoot')
  135. */
  136. /*
  137. assert(isTopLevel('xxx') === true, 'isTopLevel')
  138. assert(isTopLevel('./xxx') === false, 'isTopLevel')
  139. assert(isTopLevel('../xxx') === false, 'isTopLevel')
  140. assert(isTopLevel('/xxx') === false, 'isTopLevel')
  141. assert(isTopLevel('xxx:/zzz') === false, 'isTopLevel')
  142. assert(isTopLevel('$') === true, 'isTopLevel')
  143. assert(isTopLevel('_') === true, 'isTopLevel')
  144. assert(isTopLevel('$abc') === true, 'isTopLevel')
  145. assert(isTopLevel('_abc') === true, 'isTopLevel')
  146. */
  147. test.next()
  148. });