Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Rust
提交
28745ce7
R
Rust
项目概览
int
/
Rust
大约 1 年 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rust
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
28745ce7
编写于
11月 17, 2012
作者:
E
Erick Tryzelaar
提交者:
Graydon Hoare
11月 20, 2012
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
libcore: Clean up the path tests
上级
ab5d8425
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
121 addition
and
127 deletion
+121
-127
src/libcore/path.rs
src/libcore/path.rs
+121
-127
未找到文件。
src/libcore/path.rs
浏览文件 @
28745ce7
...
...
@@ -498,90 +498,132 @@ impl WindowsPath : GenericPath {
move
cs
}
#[test]
fn
test_double_slash_collapsing
()
{
let
path
=
PosixPath
(
"tmp/"
);
let
path
=
path
.push
(
"/hmm"
);
let
path
=
path
.normalize
();
assert
~
"tmp/hmm"
==
path
.to_str
();
let
path
=
WindowsPath
(
"tmp/"
);
let
path
=
path
.push
(
"/hmm"
);
let
path
=
path
.normalize
();
assert
~
"tmp
\\
hmm"
==
path
.to_str
();
}
mod
posix
{
// Various windows helpers, and tests for the impl.
mod
windows
{
#[inline(always)]
pub
pure
fn
is_sep
(
u
:
u8
)
->
bool
{
u
==
'/'
as
u8
||
u
==
'\\'
as
u8
}
#[cfg(test)]
fn
mk
(
s
:
&
str
)
->
PosixPath
{
PosixPath
(
s
)
}
pub
pure
fn
extract_unc_prefix
(
s
:
&
str
)
->
Option
<
(
~
str
,
~
str
)
>
{
if
(
s
.len
()
>
1
&&
s
[
0
]
==
'\\'
as
u8
&&
s
[
1
]
==
'\\'
as
u8
)
{
let
mut
i
=
2
;
while
i
<
s
.len
()
{
if
s
[
i
]
==
'\\'
as
u8
{
let
pre
=
s
.slice
(
2
,
i
);
let
rest
=
s
.slice
(
i
,
s
.len
());
return
Some
((
move
pre
,
move
rest
));
}
i
+=
1
;
}
}
None
}
#[cfg(test)]
fn
t
(
wp
:
&
PosixPath
,
s
:
&
str
)
{
let
ss
=
wp
.to_str
();
let
sss
=
str
::
from_slice
(
s
);
if
(
ss
!=
sss
)
{
debug!
(
"got %s"
,
ss
);
debug!
(
"expected %s"
,
sss
);
assert
ss
==
sss
;
pub
pure
fn
extract_drive_prefix
(
s
:
&
str
)
->
Option
<
(
~
str
,
~
str
)
>
{
unsafe
{
if
(
s
.len
()
>
1
&&
libc
::
isalpha
(
s
[
0
]
as
libc
::
c_int
)
!=
0
&&
s
[
1
]
==
':'
as
u8
)
{
let
rest
=
if
s
.len
()
==
2
{
~
""
}
else
{
s
.slice
(
2
,
s
.len
())
};
return
Some
((
s
.slice
(
0
,
1
),
move
rest
));
}
None
}
}
}
#[cfg(tests)]
mod
tests
{
#[test]
fn
test_double_slash_collapsing
()
{
let
path
=
PosixPath
(
"tmp/"
);
let
path
=
path
.push
(
"/hmm"
);
let
path
=
path
.normalize
();
assert
~
"tmp/hmm"
==
path
.to_str
();
let
path
=
WindowsPath
(
"tmp/"
);
let
path
=
path
.push
(
"/hmm"
);
let
path
=
path
.normalize
();
assert
~
"tmp
\\
hmm"
==
path
.to_str
();
}
#[test]
fn
test_filetype_foo_bar
()
{
let
wp
=
mk
(
"foo.bar"
);
let
wp
=
PosixPath
(
"foo.bar"
);
assert
wp
.filetype
()
==
Some
(
~
".bar"
);
let
wp
=
WindowsPath
(
"foo.bar"
);
assert
wp
.filetype
()
==
Some
(
~
".bar"
);
}
#[test]
fn
test_filetype_foo
()
{
let
wp
=
mk
(
"foo"
);
let
wp
=
PosixPath
(
"foo"
);
assert
wp
.filetype
()
==
None
;
let
wp
=
WindowsPath
(
"foo"
);
assert
wp
.filetype
()
==
None
;
}
#[test]
fn
test_posix_paths
()
{
t
(
&
(
mk
(
"hi"
)),
"hi"
);
t
(
&
(
mk
(
"/lib"
)),
"/lib"
);
t
(
&
(
mk
(
"hi/there"
)),
"hi/there"
);
t
(
&
(
mk
(
"hi/there.txt"
)),
"hi/there.txt"
);
fn
t
(
wp
:
&
PosixPath
,
s
:
&
str
)
{
let
ss
=
wp
.to_str
();
let
sss
=
str
::
from_slice
(
s
);
if
(
ss
!=
sss
)
{
debug!
(
"got %s"
,
ss
);
debug!
(
"expected %s"
,
sss
);
assert
ss
==
sss
;
}
}
t
(
&
(
PosixPath
(
"hi"
)),
"hi"
);
t
(
&
(
PosixPath
(
"/lib"
)),
"/lib"
);
t
(
&
(
PosixPath
(
"hi/there"
)),
"hi/there"
);
t
(
&
(
PosixPath
(
"hi/there.txt"
)),
"hi/there.txt"
);
t
(
&
(
mk
(
"hi/there.txt"
)),
"hi/there.txt"
);
t
(
&
(
mk
(
"hi/there.txt"
)
t
(
&
(
PosixPath
(
"hi/there.txt"
)),
"hi/there.txt"
);
t
(
&
(
PosixPath
(
"hi/there.txt"
)
.with_filetype
(
""
)),
"hi/there"
);
t
(
&
(
mk
(
"/a/b/c/there.txt"
)
t
(
&
(
PosixPath
(
"/a/b/c/there.txt"
)
.with_dirname
(
"hi"
)),
"hi/there.txt"
);
t
(
&
(
mk
(
"hi/there.txt"
)
t
(
&
(
PosixPath
(
"hi/there.txt"
)
.with_dirname
(
"."
)),
"./there.txt"
);
t
(
&
(
mk
(
"a/b/c"
)
t
(
&
(
PosixPath
(
"a/b/c"
)
.push
(
".."
)),
"a/b/c/.."
);
t
(
&
(
mk
(
"there.txt"
)
t
(
&
(
PosixPath
(
"there.txt"
)
.with_filetype
(
"o"
)),
"there.o"
);
t
(
&
(
mk
(
"hi/there.txt"
)
t
(
&
(
PosixPath
(
"hi/there.txt"
)
.with_filetype
(
"o"
)),
"hi/there.o"
);
t
(
&
(
mk
(
"hi/there.txt"
)
t
(
&
(
PosixPath
(
"hi/there.txt"
)
.with_filetype
(
"o"
)
.with_dirname
(
"/usr/lib"
)),
"/usr/lib/there.o"
);
t
(
&
(
mk
(
"hi/there.txt"
)
t
(
&
(
PosixPath
(
"hi/there.txt"
)
.with_filetype
(
"o"
)
.with_dirname
(
"/usr/lib/"
)),
"/usr/lib/there.o"
);
t
(
&
(
mk
(
"hi/there.txt"
)
t
(
&
(
PosixPath
(
"hi/there.txt"
)
.with_filetype
(
"o"
)
.with_dirname
(
"/usr//lib//"
)),
"/usr/lib/there.o"
);
t
(
&
(
mk
(
"/usr/bin/rust"
)
t
(
&
(
PosixPath
(
"/usr/bin/rust"
)
.push_many
([
~
"lib"
,
~
"thingy.so"
])
.with_filestem
(
"librustc"
)),
"/usr/bin/rust/lib/librustc.so"
);
...
...
@@ -590,86 +632,55 @@ fn test_posix_paths() {
#[test]
fn
test_normalize
()
{
t
(
&
(
mk
(
"hi/there.txt"
)
fn
t
(
wp
:
&
PosixPath
,
s
:
&
str
)
{
let
ss
=
wp
.to_str
();
let
sss
=
str
::
from_slice
(
s
);
if
(
ss
!=
sss
)
{
debug!
(
"got %s"
,
ss
);
debug!
(
"expected %s"
,
sss
);
assert
ss
==
sss
;
}
}
t
(
&
(
PosixPath
(
"hi/there.txt"
)
.with_dirname
(
"."
)
.normalize
()),
"there.txt"
);
t
(
&
(
mk
(
"a/b/../c/././/../foo.txt/"
)
.normalize
()),
t
(
&
(
PosixPath
(
"a/b/../c/././/../foo.txt/"
)
.normalize
()),
"a/foo.txt"
);
t
(
&
(
mk
(
"a/b/c"
)
t
(
&
(
PosixPath
(
"a/b/c"
)
.push
(
".."
)
.normalize
()),
"a/b"
);
}
}
// Various windows helpers, and tests for the impl.
mod
windows
{
#[inline(always)]
pub
pure
fn
is_sep
(
u
:
u8
)
->
bool
{
u
==
'/'
as
u8
||
u
==
'\\'
as
u8
}
pub
pure
fn
extract_unc_prefix
(
s
:
&
str
)
->
Option
<
(
~
str
,
~
str
)
>
{
if
(
s
.len
()
>
1
&&
s
[
0
]
==
'\\'
as
u8
&&
s
[
1
]
==
'\\'
as
u8
)
{
let
mut
i
=
2
;
while
i
<
s
.len
()
{
if
s
[
i
]
==
'\\'
as
u8
{
let
pre
=
s
.slice
(
2
,
i
);
let
rest
=
s
.slice
(
i
,
s
.len
());
return
Some
((
move
pre
,
move
rest
));
}
i
+=
1
;
}
}
None
}
pub
pure
fn
extract_drive_prefix
(
s
:
&
str
)
->
Option
<
(
~
str
,
~
str
)
>
{
unsafe
{
if
(
s
.len
()
>
1
&&
libc
::
isalpha
(
s
[
0
]
as
libc
::
c_int
)
!=
0
&&
s
[
1
]
==
':'
as
u8
)
{
let
rest
=
if
s
.len
()
==
2
{
~
""
}
else
{
s
.slice
(
2
,
s
.len
())
};
return
Some
((
s
.slice
(
0
,
1
),
move
rest
));
}
None
}
}
#[test]
fn
test_extract_unc_prefixes
()
{
assert
extract_unc_prefix
(
"
\\\\
"
)
.is_none
();
assert
extract_unc_prefix
(
"
\\\\
hi"
)
.is_none
();
assert
extract_unc_prefix
(
"
\\\\
hi
\\
"
)
==
Some
((
~
"hi"
,
~
"
\\
"
));
assert
extract_unc_prefix
(
"
\\\\
hi
\\
there"
)
==
assert
windows
::
extract_unc_prefix
(
"
\\\\
"
)
.is_none
();
assert
windows
::
extract_unc_prefix
(
"
\\\\
hi"
)
.is_none
();
assert
windows
::
extract_unc_prefix
(
"
\\\\
hi
\\
"
)
==
Some
((
~
"hi"
,
~
"
\\
"
));
assert
windows
::
extract_unc_prefix
(
"
\\\\
hi
\\
there"
)
==
Some
((
~
"hi"
,
~
"
\\
there"
));
assert
extract_unc_prefix
(
"
\\\\
hi
\\
there
\\
friends.txt"
)
==
assert
windows
::
extract_unc_prefix
(
"
\\\\
hi
\\
there
\\
friends.txt"
)
==
Some
((
~
"hi"
,
~
"
\\
there
\\
friends.txt"
));
}
#[test]
fn
test_extract_drive_prefixes
()
{
assert
extract_drive_prefix
(
"c"
)
.is_none
();
assert
extract_drive_prefix
(
"c:"
)
==
Some
((
~
"c"
,
~
""
));
assert
extract_drive_prefix
(
"d:"
)
==
Some
((
~
"d"
,
~
""
));
assert
extract_drive_prefix
(
"z:"
)
==
Some
((
~
"z"
,
~
""
));
assert
extract_drive_prefix
(
"c:
\\
hi"
)
==
Some
((
~
"c"
,
~
"
\\
hi"
));
assert
extract_drive_prefix
(
"d:hi"
)
==
Some
((
~
"d"
,
~
"hi"
));
assert
extract_drive_prefix
(
"c:hi
\\
there.txt"
)
==
assert
windows
::
extract_drive_prefix
(
"c"
)
.is_none
();
assert
windows
::
extract_drive_prefix
(
"c:"
)
==
Some
((
~
"c"
,
~
""
));
assert
windows
::
extract_drive_prefix
(
"d:"
)
==
Some
((
~
"d"
,
~
""
));
assert
windows
::
extract_drive_prefix
(
"z:"
)
==
Some
((
~
"z"
,
~
""
));
assert
windows
::
extract_drive_prefix
(
"c:
\\
hi"
)
==
Some
((
~
"c"
,
~
"
\\
hi"
));
assert
windows
::
extract_drive_prefix
(
"d:hi"
)
==
Some
((
~
"d"
,
~
"hi"
));
assert
windows
::
extract_drive_prefix
(
"c:hi
\\
there.txt"
)
==
Some
((
~
"c"
,
~
"hi
\\
there.txt"
));
assert
extract_drive_prefix
(
"c:
\\
hi
\\
there.txt"
)
==
assert
windows
::
extract_drive_prefix
(
"c:
\\
hi
\\
there.txt"
)
==
Some
((
~
"c"
,
~
"
\\
hi
\\
there.txt"
));
}
#[test]
fn
test_windows_paths
()
{
fn
mk
(
s
:
&
str
)
->
WindowsPath
{
WindowsPath
(
s
)
}
fn
t
(
wp
:
&
WindowsPath
,
s
:
&
str
)
{
let
ss
=
wp
.to_str
();
let
sss
=
str
::
from_slice
(
s
);
...
...
@@ -680,51 +691,34 @@ fn t(wp: &WindowsPath, s: &str) {
}
}
t
(
&
(
mk
(
"hi"
)),
"hi"
);
t
(
&
(
mk
(
"hi/there"
)),
"hi
\\
there"
);
t
(
&
(
mk
(
"hi/there.txt"
)),
"hi
\\
there.txt"
);
t
(
&
(
WindowsPath
(
"hi"
)),
"hi"
);
t
(
&
(
WindowsPath
(
"hi/there"
)),
"hi
\\
there"
);
t
(
&
(
WindowsPath
(
"hi/there.txt"
)),
"hi
\\
there.txt"
);
t
(
&
(
mk
(
"there.txt"
)
t
(
&
(
WindowsPath
(
"there.txt"
)
.with_filetype
(
"o"
)),
"there.o"
);
t
(
&
(
mk
(
"hi/there.txt"
)
t
(
&
(
WindowsPath
(
"hi/there.txt"
)
.with_filetype
(
"o"
)),
"hi
\\
there.o"
);
t
(
&
(
mk
(
"hi/there.txt"
)
t
(
&
(
WindowsPath
(
"hi/there.txt"
)
.with_filetype
(
"o"
)
.with_dirname
(
"c:
\\
program files A"
)),
"c:
\\
program files A
\\
there.o"
);
t
(
&
(
mk
(
"hi/there.txt"
)
t
(
&
(
WindowsPath
(
"hi/there.txt"
)
.with_filetype
(
"o"
)
.with_dirname
(
"c:
\\
program files B
\\
"
)),
"c:
\\
program files B
\\
there.o"
);
t
(
&
(
mk
(
"hi/there.txt"
)
t
(
&
(
WindowsPath
(
"hi/there.txt"
)
.with_filetype
(
"o"
)
.with_dirname
(
"c:
\\
program files C
\\
/"
)),
"c:
\\
program files C
\\
there.o"
);
t
(
&
(
mk
(
"c:
\\
program files (x86)
\\
rust"
)
t
(
&
(
WindowsPath
(
"c:
\\
program files (x86)
\\
rust"
)
.push_many
([
~
"lib"
,
~
"thingy.dll"
])
.with_filename
(
"librustc.dll"
)),
"c:
\\
program files (x86)
\\
rust
\\
lib
\\
librustc.dll"
);
}
#[cfg(test)]
fn
mk
(
s
:
&
str
)
->
PosixPath
{
PosixPath
(
s
)
}
#[test]
fn
test_filetype_foo_bar
()
{
let
wp
=
mk
(
"foo.bar"
);
assert
wp
.filetype
()
==
Some
(
~
".bar"
);
}
#[test]
fn
test_filetype_foo
()
{
let
wp
=
mk
(
"foo"
);
assert
wp
.filetype
()
==
None
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录