@@ -335,6 +335,128 @@ pub mod ct {
335
335
336
336
Parsed::new(t, i + 1)
337
337
}
338
+
339
+ #[cfg(test)]
340
+ fn die(s: &str) -> ! { fail s.to_owned() }
341
+
342
+ #[test]
343
+ fn test_parse_count() {
344
+ fn test(s: &str, count: Count, next: uint) -> bool {
345
+ parse_count(s, 0, s.len()) == Parsed::new(count, next)
346
+ }
347
+
348
+ assert test(" ", CountImplied , 0 ) ;
349
+ assert test( "*" , CountIsNextParam , 1 ) ;
350
+ assert test( "*1" , CountIsNextParam , 1 ) ;
351
+ assert test( "*1$" , CountIsParam ( 1 ) , 3 ) ;
352
+ assert test( "123" , CountIs ( 123 ) , 3 ) ;
353
+ }
354
+
355
+ #[ test]
356
+ fn test_parse_flags ( ) {
357
+ fn pack ( fs : & [ Flag ] ) -> uint {
358
+ fs. foldl ( 0 , |& p, & f| p | ( 1 << f as uint ) )
359
+ }
360
+
361
+ fn test ( s : & str , flags : & [ Flag ] , next : uint ) {
362
+ let f = parse_flags ( s, 0 , s. len ( ) ) ;
363
+ assert pack( f. val ) == pack ( flags) ;
364
+ assert f. next == next;
365
+ }
366
+
367
+ test ( "" , [ ] , 0 ) ;
368
+ test ( "!#-+ 0" , [ ] , 0 ) ;
369
+ test ( "#-+" , [ FlagAlternate , FlagLeftJustify , FlagSignAlways ] , 3 ) ;
370
+ test ( " 0" , [ FlagSpaceForSign , FlagLeftZeroPad ] , 2 ) ;
371
+ }
372
+
373
+ #[ test]
374
+ fn test_parse_fmt_string ( ) {
375
+ assert parse_fmt_string ( "foo %s bar" , die) == ~[
376
+ PieceString ( ~"foo "),
377
+ PieceConv(Conv {param: None, flags: ~[], width: CountImplied,
378
+ precision: CountImplied, ty: TyStr}),
379
+ PieceString(~" bar") ] ;
380
+
381
+ assert parse_fmt_string ( "%s" , die) == ~[
382
+ PieceConv ( Conv { param : None , flags : ~[ ] , width : CountImplied ,
383
+ precision : CountImplied , ty : TyStr } ) ] ;
384
+
385
+ assert parse_fmt_string ( "%%%%" , die) == ~[
386
+ PieceString ( ~"%"), PieceString(~" %")];
387
+ }
388
+
389
+ #[test]
390
+ fn test_parse_parameter() {
391
+ fn test(s: &str, param: Option<uint>, next: uint) -> bool {
392
+ parse_parameter(s, 0, s.len()) == Parsed::new(param, next)
393
+ }
394
+
395
+ assert test(" ", None , 0 ) ;
396
+ assert test( "foo" , None , 0 ) ;
397
+ assert test( "123" , None , 0 ) ;
398
+ assert test( "123$" , Some ( 123 ) , 4 ) ;
399
+ }
400
+
401
+ #[ test]
402
+ fn test_parse_precision ( ) {
403
+ fn test ( s : & str , count : Count , next : uint ) -> bool {
404
+ parse_precision ( s, 0 , s. len ( ) ) == Parsed :: new ( count, next)
405
+ }
406
+
407
+ assert test( "" , CountImplied , 0 ) ;
408
+ assert test( "." , CountIs ( 0 ) , 1 ) ;
409
+ assert test( ".*" , CountIsNextParam , 2 ) ;
410
+ assert test( ".*1" , CountIsNextParam , 2 ) ;
411
+ assert test( ".*1$" , CountIsParam ( 1 ) , 4 ) ;
412
+ assert test( ".123" , CountIs ( 123 ) , 4 ) ;
413
+ }
414
+
415
+ #[ test]
416
+ fn test_parse_type ( ) {
417
+ fn test ( s : & str , ty : Ty ) -> bool {
418
+ parse_type ( s, 0 , s. len ( ) , die) == Parsed :: new ( ty, 1 )
419
+ }
420
+
421
+ assert test( "b" , TyBool ) ;
422
+ assert test( "c" , TyChar ) ;
423
+ assert test( "d" , TyInt ( Signed ) ) ;
424
+ assert test( "f" , TyFloat ) ;
425
+ assert test( "i" , TyInt ( Signed ) ) ;
426
+ assert test( "o" , TyOctal ) ;
427
+ assert test( "s" , TyStr ) ;
428
+ assert test( "t" , TyBits ) ;
429
+ assert test( "x" , TyHex ( CaseLower ) ) ;
430
+ assert test( "X" , TyHex ( CaseUpper ) ) ;
431
+ assert test( "?" , TyPoly ) ;
432
+ }
433
+
434
+ #[ test]
435
+ #[ should_fail]
436
+ fn test_parse_type_missing ( ) {
437
+ parse_type ( "" , 0 , 0 , die) ;
438
+ }
439
+
440
+ #[ test]
441
+ #[ should_fail]
442
+ fn test_parse_type_unknown ( ) {
443
+ parse_type ( "!" , 0 , 1 , die) ;
444
+ }
445
+
446
+ #[ test]
447
+ fn test_peek_num ( ) {
448
+ let s1 = "" ;
449
+ assert peek_num( s1, 0 , s1. len ( ) ) . is_none ( ) ;
450
+
451
+ let s2 = "foo" ;
452
+ assert peek_num( s2, 0 , s2. len ( ) ) . is_none ( ) ;
453
+
454
+ let s3 = "123" ;
455
+ assert peek_num( s3, 0 , s3. len ( ) ) == Some ( Parsed :: new ( 123 , 3 ) ) ;
456
+
457
+ let s4 = "123foo" ;
458
+ assert peek_num( s4, 0 , s4. len ( ) ) == Some ( Parsed :: new ( 123 , 3 ) ) ;
459
+ }
338
460
}
339
461
340
462
// Functions used by the fmt extension at runtime. For now there are a lot of
0 commit comments