@@ -498,90 +498,132 @@ pub pure fn normalize(components: &[~str]) -> ~[~str] {
498
498
move cs
499
499
}
500
500
501
- #[test]
502
- fn test_double_slash_collapsing()
503
- {
504
- let path = PosixPath(" tmp/") ;
505
- let path = path. push ( "/hmm" ) ;
506
- let path = path. normalize ( ) ;
507
- assert ~"tmp/hmm" == path. to_str ( ) ;
508
-
509
- let path = WindowsPath ( "tmp/" ) ;
510
- let path = path. push ( "/hmm" ) ;
511
- let path = path. normalize ( ) ;
512
- assert ~"tmp\\ hmm" == path. to_str ( ) ;
513
- }
514
-
515
- mod posix {
501
+ // Various windows helpers, and tests for the impl.
502
+ mod windows {
503
+ #[inline(always)]
504
+ pub pure fn is_sep(u: u8) -> bool {
505
+ u == '/' as u8 || u == '\\ ' as u8
506
+ }
516
507
517
- #[ cfg( test) ]
518
- fn mk ( s : & str ) -> PosixPath { PosixPath ( s) }
508
+ pub pure fn extract_unc_prefix(s: &str) -> Option<(~str,~str)> {
509
+ if (s.len() > 1 &&
510
+ s[0] == '\\ ' as u8 &&
511
+ s[1] == '\\ ' as u8) {
512
+ let mut i = 2;
513
+ while i < s.len() {
514
+ if s[i] == '\\ ' as u8 {
515
+ let pre = s.slice(2, i);
516
+ let rest = s.slice(i, s.len());
517
+ return Some((move pre, move rest));
518
+ }
519
+ i += 1;
520
+ }
521
+ }
522
+ None
523
+ }
519
524
520
- #[ cfg( test) ]
521
- fn t ( wp : & PosixPath , s : & str ) {
522
- let ss = wp. to_str ( ) ;
523
- let sss = str:: from_slice ( s) ;
524
- if ( ss != sss) {
525
- debug ! ( "got %s" , ss) ;
526
- debug ! ( "expected %s" , sss) ;
527
- assert ss == sss;
525
+ pub pure fn extract_drive_prefix(s: &str) -> Option<(~str,~str)> {
526
+ unsafe {
527
+ if (s.len() > 1 &&
528
+ libc::isalpha(s[0] as libc::c_int) != 0 &&
529
+ s[1] == ':' as u8) {
530
+ let rest = if s.len() == 2 {
531
+ ~" "
532
+ } else {
533
+ s. slice ( 2 , s. len ( ) )
534
+ } ;
535
+ return Some ( ( s. slice ( 0 , 1 ) , move rest) ) ;
536
+ }
537
+ None
528
538
}
529
539
}
540
+ }
541
+
542
+ #[ cfg ( tests) ]
543
+ mod tests {
544
+ #[ test]
545
+ fn test_double_slash_collapsing ( ) {
546
+ let path = PosixPath ( "tmp/" ) ;
547
+ let path = path. push ( "/hmm" ) ;
548
+ let path = path. normalize ( ) ;
549
+ assert ~"tmp/hmm" == path. to_str ( ) ;
550
+
551
+ let path = WindowsPath ( "tmp/" ) ;
552
+ let path = path. push ( "/hmm" ) ;
553
+ let path = path. normalize ( ) ;
554
+ assert ~"tmp\\ hmm" == path. to_str ( ) ;
555
+ }
530
556
531
557
#[ test]
532
558
fn test_filetype_foo_bar ( ) {
533
- let wp = mk ( "foo.bar" ) ;
559
+ let wp = PosixPath ( "foo.bar" ) ;
560
+ assert wp. filetype ( ) == Some ( ~". bar ") ;
561
+
562
+ let wp = WindowsPath ( "foo.bar" ) ;
534
563
assert wp. filetype ( ) == Some ( ~". bar ") ;
535
564
}
536
565
537
566
#[ test]
538
567
fn test_filetype_foo ( ) {
539
- let wp = mk ( "foo" ) ;
568
+ let wp = PosixPath ( "foo" ) ;
569
+ assert wp. filetype ( ) == None ;
570
+
571
+ let wp = WindowsPath ( "foo" ) ;
540
572
assert wp. filetype ( ) == None ;
541
573
}
542
574
543
575
#[ test]
544
576
fn test_posix_paths ( ) {
545
- t ( & ( mk ( "hi" ) ) , "hi" ) ;
546
- t ( & ( mk ( "/lib" ) ) , "/lib" ) ;
547
- t ( & ( mk ( "hi/there" ) ) , "hi/there" ) ;
548
- t ( & ( mk ( "hi/there.txt" ) ) , "hi/there.txt" ) ;
577
+ fn t ( wp : & PosixPath , s : & str ) {
578
+ let ss = wp. to_str ( ) ;
579
+ let sss = str:: from_slice ( s) ;
580
+ if ( ss != sss) {
581
+ debug ! ( "got %s" , ss) ;
582
+ debug ! ( "expected %s" , sss) ;
583
+ assert ss == sss;
584
+ }
585
+ }
586
+
587
+ t ( & ( PosixPath ( "hi" ) ) , "hi" ) ;
588
+ t ( & ( PosixPath ( "/lib" ) ) , "/lib" ) ;
589
+ t ( & ( PosixPath ( "hi/there" ) ) , "hi/there" ) ;
590
+ t ( & ( PosixPath ( "hi/there.txt" ) ) , "hi/there.txt" ) ;
549
591
550
- t ( & ( mk ( "hi/there.txt" ) ) , "hi/there.txt" ) ;
551
- t ( & ( mk ( "hi/there.txt" )
592
+ t ( & ( PosixPath ( "hi/there.txt" ) ) , "hi/there.txt" ) ;
593
+ t ( & ( PosixPath ( "hi/there.txt" )
552
594
. with_filetype ( "" ) ) , "hi/there" ) ;
553
595
554
- t ( & ( mk ( "/a/b/c/there.txt" )
596
+ t ( & ( PosixPath ( "/a/b/c/there.txt" )
555
597
. with_dirname ( "hi" ) ) , "hi/there.txt" ) ;
556
598
557
- t ( & ( mk ( "hi/there.txt" )
599
+ t ( & ( PosixPath ( "hi/there.txt" )
558
600
. with_dirname ( "." ) ) , "./there.txt" ) ;
559
601
560
- t ( & ( mk ( "a/b/c" )
602
+ t ( & ( PosixPath ( "a/b/c" )
561
603
. push ( ".." ) ) , "a/b/c/.." ) ;
562
604
563
- t ( & ( mk ( "there.txt" )
605
+ t ( & ( PosixPath ( "there.txt" )
564
606
. with_filetype ( "o" ) ) , "there.o" ) ;
565
607
566
- t ( & ( mk ( "hi/there.txt" )
608
+ t ( & ( PosixPath ( "hi/there.txt" )
567
609
. with_filetype ( "o" ) ) , "hi/there.o" ) ;
568
610
569
- t ( & ( mk ( "hi/there.txt" )
611
+ t ( & ( PosixPath ( "hi/there.txt" )
570
612
. with_filetype ( "o" )
571
613
. with_dirname ( "/usr/lib" ) ) ,
572
614
"/usr/lib/there.o" ) ;
573
615
574
- t ( & ( mk ( "hi/there.txt" )
616
+ t ( & ( PosixPath ( "hi/there.txt" )
575
617
. with_filetype ( "o" )
576
618
. with_dirname ( "/usr/lib/" ) ) ,
577
619
"/usr/lib/there.o" ) ;
578
620
579
- t ( & ( mk ( "hi/there.txt" )
621
+ t ( & ( PosixPath ( "hi/there.txt" )
580
622
. with_filetype ( "o" )
581
623
. with_dirname ( "/usr//lib//" ) ) ,
582
624
"/usr/lib/there.o" ) ;
583
625
584
- t ( & ( mk ( "/usr/bin/rust" )
626
+ t ( & ( PosixPath ( "/usr/bin/rust" )
585
627
. push_many ( [ ~"lib", ~"thingy. so "] )
586
628
. with_filestem ( "librustc" ) ) ,
587
629
"/usr/bin/rust/lib/librustc.so" ) ;
@@ -590,86 +632,55 @@ mod posix {
590
632
591
633
#[ test]
592
634
fn test_normalize ( ) {
593
- t ( & ( mk ( "hi/there.txt" )
635
+ fn t ( wp : & PosixPath , s : & str ) {
636
+ let ss = wp. to_str ( ) ;
637
+ let sss = str:: from_slice ( s) ;
638
+ if ( ss != sss) {
639
+ debug ! ( "got %s" , ss) ;
640
+ debug ! ( "expected %s" , sss) ;
641
+ assert ss == sss;
642
+ }
643
+ }
644
+
645
+ t ( & ( PosixPath ( "hi/there.txt" )
594
646
. with_dirname ( "." ) . normalize ( ) ) , "there.txt" ) ;
595
647
596
- t ( & ( mk ( "a/b/../c/././/../foo.txt/" ) . normalize ( ) ) ,
648
+ t ( & ( PosixPath ( "a/b/../c/././/../foo.txt/" ) . normalize ( ) ) ,
597
649
"a/foo.txt" ) ;
598
650
599
- t ( & ( mk ( "a/b/c" )
651
+ t ( & ( PosixPath ( "a/b/c" )
600
652
. push ( ".." ) . normalize ( ) ) , "a/b" ) ;
601
653
}
602
- }
603
-
604
- // Various windows helpers, and tests for the impl.
605
- mod windows {
606
-
607
- #[ inline( always) ]
608
- pub pure fn is_sep ( u : u8 ) -> bool {
609
- u == '/' as u8 || u == '\\' as u8
610
- }
611
-
612
- pub pure fn extract_unc_prefix ( s : & str ) -> Option < ( ~str , ~str ) > {
613
- if ( s. len ( ) > 1 &&
614
- s[ 0 ] == '\\' as u8 &&
615
- s[ 1 ] == '\\' as u8 ) {
616
- let mut i = 2 ;
617
- while i < s. len ( ) {
618
- if s[ i] == '\\' as u8 {
619
- let pre = s. slice ( 2 , i) ;
620
- let rest = s. slice ( i, s. len ( ) ) ;
621
- return Some ( ( move pre, move rest) ) ;
622
- }
623
- i += 1 ;
624
- }
625
- }
626
- None
627
- }
628
-
629
- pub pure fn extract_drive_prefix ( s : & str ) -> Option < ( ~str , ~str ) > {
630
- unsafe {
631
- if ( s. len ( ) > 1 &&
632
- libc:: isalpha ( s[ 0 ] as libc:: c_int ) != 0 &&
633
- s[ 1 ] == ':' as u8 ) {
634
- let rest = if s. len ( ) == 2 {
635
- ~""
636
- } else {
637
- s. slice ( 2 , s. len ( ) )
638
- } ;
639
- return Some ( ( s. slice ( 0 , 1 ) , move rest) ) ;
640
- }
641
- None
642
- }
643
- }
644
654
645
655
#[ test]
646
656
fn test_extract_unc_prefixes ( ) {
647
- assert extract_unc_prefix ( "\\ \\ " ) . is_none ( ) ;
648
- assert extract_unc_prefix ( "\\ \\ hi" ) . is_none ( ) ;
649
- assert extract_unc_prefix ( "\\ \\ hi\\ " ) == Some ( ( ~"hi", ~"\\ ") ) ;
650
- assert extract_unc_prefix ( "\\ \\ hi\\ there" ) ==
657
+ assert windows:: extract_unc_prefix ( "\\ \\ " ) . is_none ( ) ;
658
+ assert windows:: extract_unc_prefix ( "\\ \\ hi" ) . is_none ( ) ;
659
+ assert windows:: extract_unc_prefix ( "\\ \\ hi\\ " ) ==
660
+ Some ( ( ~"hi", ~"\\ ") ) ;
661
+ assert windows:: extract_unc_prefix ( "\\ \\ hi\\ there" ) ==
651
662
Some ( ( ~"hi", ~"\\ there") ) ;
652
- assert extract_unc_prefix ( "\\ \\ hi\\ there\\ friends.txt" ) ==
663
+ assert windows :: extract_unc_prefix ( "\\ \\ hi\\ there\\ friends.txt" ) ==
653
664
Some ( ( ~"hi", ~"\\ there\\ friends. txt ") ) ;
654
665
}
655
666
656
667
#[ test]
657
668
fn test_extract_drive_prefixes ( ) {
658
- assert extract_drive_prefix ( "c" ) . is_none ( ) ;
659
- assert extract_drive_prefix ( "c:" ) == Some ( ( ~"c", ~" ") ) ;
660
- assert extract_drive_prefix ( "d:" ) == Some ( ( ~"d", ~"") ) ;
661
- assert extract_drive_prefix ( "z:" ) == Some ( ( ~"z", ~"") ) ;
662
- assert extract_drive_prefix ( "c:\\ hi" ) == Some ( ( ~"c", ~"\\ hi" ) ) ;
663
- assert extract_drive_prefix ( "d:hi" ) == Some ( ( ~"d", ~"hi") ) ;
664
- assert extract_drive_prefix ( "c:hi\\ there.txt" ) ==
669
+ assert windows:: extract_drive_prefix ( "c" ) . is_none ( ) ;
670
+ assert windows:: extract_drive_prefix ( "c:" ) == Some ( ( ~"c", ~" ") ) ;
671
+ assert windows:: extract_drive_prefix ( "d:" ) == Some ( ( ~"d", ~"") ) ;
672
+ assert windows:: extract_drive_prefix ( "z:" ) == Some ( ( ~"z", ~"") ) ;
673
+ assert windows:: extract_drive_prefix ( "c:\\ hi" ) ==
674
+ Some ( ( ~"c", ~"\\ hi" ) ) ;
675
+ assert windows:: extract_drive_prefix ( "d:hi" ) == Some ( ( ~"d", ~"hi") ) ;
676
+ assert windows:: extract_drive_prefix ( "c:hi\\ there.txt" ) ==
665
677
Some ( ( ~"c", ~"hi\\ there.txt" ) ) ;
666
- assert extract_drive_prefix ( "c:\\ hi\\ there.txt" ) ==
678
+ assert windows :: extract_drive_prefix ( "c:\\ hi\\ there.txt" ) ==
667
679
Some ( ( ~"c", ~"\\ hi\\ there.txt" ) ) ;
668
680
}
669
681
670
682
#[ test]
671
683
fn test_windows_paths ( ) {
672
- fn mk ( s : & str ) -> WindowsPath { WindowsPath ( s) }
673
684
fn t ( wp : & WindowsPath , s : & str ) {
674
685
let ss = wp. to_str ( ) ;
675
686
let sss = str:: from_slice ( s) ;
@@ -680,51 +691,34 @@ mod windows {
680
691
}
681
692
}
682
693
683
- t ( & ( mk ( "hi" ) ) , "hi" ) ;
684
- t ( & ( mk ( "hi/there" ) ) , "hi\\ there" ) ;
685
- t ( & ( mk ( "hi/there.txt" ) ) , "hi\\ there.txt" ) ;
694
+ t ( & ( WindowsPath ( "hi" ) ) , "hi" ) ;
695
+ t ( & ( WindowsPath ( "hi/there" ) ) , "hi\\ there" ) ;
696
+ t ( & ( WindowsPath ( "hi/there.txt" ) ) , "hi\\ there.txt" ) ;
686
697
687
- t ( & ( mk ( "there.txt" )
698
+ t ( & ( WindowsPath ( "there.txt" )
688
699
. with_filetype ( "o" ) ) , "there.o" ) ;
689
700
690
- t ( & ( mk ( "hi/there.txt" )
701
+ t ( & ( WindowsPath ( "hi/there.txt" )
691
702
. with_filetype ( "o" ) ) , "hi\\ there.o" ) ;
692
703
693
- t ( & ( mk ( "hi/there.txt" )
704
+ t ( & ( WindowsPath ( "hi/there.txt" )
694
705
. with_filetype ( "o" )
695
706
. with_dirname ( "c:\\ program files A" ) ) ,
696
707
"c:\\ program files A\\ there.o" ) ;
697
708
698
- t ( & ( mk ( "hi/there.txt" )
709
+ t ( & ( WindowsPath ( "hi/there.txt" )
699
710
. with_filetype ( "o" )
700
711
. with_dirname ( "c:\\ program files B\\ " ) ) ,
701
712
"c:\\ program files B\\ there.o" ) ;
702
713
703
- t ( & ( mk ( "hi/there.txt" )
714
+ t ( & ( WindowsPath ( "hi/there.txt" )
704
715
. with_filetype ( "o" )
705
716
. with_dirname ( "c:\\ program files C\\ /" ) ) ,
706
717
"c:\\ program files C\\ there.o" ) ;
707
718
708
- t ( & ( mk ( "c:\\ program files (x86)\\ rust" )
719
+ t ( & ( WindowsPath ( "c:\\ program files (x86)\\ rust" )
709
720
. push_many ( [ ~"lib", ~"thingy. dll "] )
710
721
. with_filename ( "librustc.dll" ) ) ,
711
722
"c:\\ program files (x86)\\ rust\\ lib\\ librustc.dll" ) ;
712
-
713
- }
714
-
715
- #[ cfg( test) ]
716
- fn mk ( s : & str ) -> PosixPath { PosixPath ( s) }
717
-
718
- #[ test]
719
- fn test_filetype_foo_bar ( ) {
720
- let wp = mk ( "foo.bar" ) ;
721
- assert wp. filetype ( ) == Some ( ~". bar ") ;
722
723
}
723
-
724
- #[ test]
725
- fn test_filetype_foo ( ) {
726
- let wp = mk ( "foo" ) ;
727
- assert wp. filetype ( ) == None ;
728
- }
729
-
730
724
}
0 commit comments