@@ -368,7 +368,61 @@ prettyItems = testGroup "printing items"
368
368
, testFlatten " impl std::Debug for .. { }" (printItem (Item (mkIdent " " ) [] (DefaultImpl Normal (TraitRef (Path False [std,debug] () ) () )) InheritedV () ))
369
369
, testFlatten " unsafe impl Debug for .. { }" (printItem (Item (mkIdent " " ) [] (DefaultImpl Unsafe (TraitRef (Path False [debug] () ) () )) InheritedV () ))
370
370
, testFlatten " impl Debug for i32 { }" (printItem (Item (mkIdent " " ) [] (Impl Normal Positive (Generics [] [] (WhereClause [] () ) () ) (Just (TraitRef (Path False [debug] () ) () )) i32 [] ) InheritedV () ))
371
- {- TODO unfinished -}
371
+ , testFlatten " pub impl !Debug for i32 where 'lt: 'gt { }" (printItem (Item (mkIdent " " ) [] (Impl Normal Negative (Generics [] [] (WhereClause [RegionPredicate (Lifetime (Name " lt" ) () ) [Lifetime (Name " gt" ) () ] () ] () ) () ) (Just (TraitRef (Path False [debug] () ) () )) i32 [] ) PublicV () ))
372
+ , testFlatten " impl <T> GenVal<T> {\n fn value(&self) -> &T { return 1; }\n }"
373
+ (printItem (Item (mkIdent " " ) [] (Impl Normal Positive
374
+ (Generics [] [TyParam [] (mkIdent " T" ) [] Nothing () ] (WhereClause [] () ) () )
375
+ Nothing
376
+ (PathTy Nothing (Path False [(" GenVal" , AngleBracketed [] [PathTy Nothing (Path False [(mkIdent " T" , AngleBracketed [] [] [] () )] () ) () ] [] () )] () ) () )
377
+ [ ImplItem (mkIdent " value" ) InheritedV Final []
378
+ (MethodI (MethodSig Normal NotConst Rust
379
+ (FnDecl [Arg (Rptr Nothing Immutable (ImplicitSelf () ) () ) (Just (IdentP (ByValue Immutable ) " self" Nothing () )) () ]
380
+ (Just (Rptr Nothing Immutable (PathTy Nothing (Path False [(mkIdent " T" , AngleBracketed [] [] [] () )] () ) () ) () ))
381
+ False () )
382
+ (Generics [] [] (WhereClause [] () ) () ))
383
+ retBlk)
384
+ ()
385
+ ]) InheritedV () ))
386
+ , testFlatten " #[cfgo]\n impl i32 {\n #![cfgi]\n fn value(&self) -> i32 { return 1; }\n pub const pi: i32 = 1;\n default type Size = i32;\n }"
387
+ (printItem (Item (mkIdent " " ) [cfgI,cfgO] (Impl Normal Positive
388
+ (Generics [] [] (WhereClause [] () ) () )
389
+ Nothing
390
+ i32
391
+ [ ImplItem (mkIdent " value" ) InheritedV Final []
392
+ (MethodI (MethodSig Normal NotConst Rust
393
+ (FnDecl [Arg (Rptr Nothing Immutable (ImplicitSelf () ) () ) (Just (IdentP (ByValue Immutable ) " self" Nothing () )) () ]
394
+ (Just i32)
395
+ False () )
396
+ (Generics [] [] (WhereClause [] () ) () ))
397
+ retBlk)
398
+ ()
399
+ , ImplItem (mkIdent " pi" ) PublicV Final [] (ConstI i32 _1) ()
400
+ , ImplItem (mkIdent " Size" ) InheritedV Default [] (TypeI i32) ()
401
+ ]) InheritedV () ))
402
+ , testFlatten " unsafe trait Show { }" (printItem (Item (mkIdent " Show" ) [] (Trait Unsafe (Generics [] [] (WhereClause [] () ) () ) [] [] ) InheritedV () ))
403
+ , testFlatten " trait Show<T> : 'l1 + for<'l3: 'l1 + 'l2> Debug + 'l2 { }" (printItem (Item (mkIdent " Show" ) []
404
+ (Trait Normal
405
+ (Generics [] [TyParam [] (mkIdent " T" ) [] Nothing () ] (WhereClause [] () ) () )
406
+ [ RegionTyParamBound (Lifetime (Name " l1" ) () )
407
+ , TraitTyParamBound (PolyTraitRef [LifetimeDef [] (Lifetime (Name " l3" ) () ) [Lifetime (Name " l1" ) () , Lifetime (Name " l2" ) () ] () ] (TraitRef (Path False [debug] () ) () ) () ) None
408
+ , RegionTyParamBound (Lifetime (Name " l2" ) () )]
409
+ [] ) InheritedV () ))
410
+ , testFlatten " pub trait Show {\n fn value(&self) -> i32 ;\n const pi: i32 = 1;\n const e: i32;\n type Size = i32;\n type Length : 'l3;\n type SomeType : 'l1 = f64;\n }"
411
+ (printItem (Item (mkIdent " Show" ) [] (Trait Normal (Generics [] [] (WhereClause [] () ) () ) []
412
+ [ TraitItem (mkIdent " value" ) []
413
+ (MethodT (MethodSig Normal NotConst Rust
414
+ (FnDecl [Arg (Rptr Nothing Immutable (ImplicitSelf () ) () ) (Just (IdentP (ByValue Immutable ) " self" Nothing () )) () ]
415
+ (Just i32)
416
+ False () )
417
+ (Generics [] [] (WhereClause [] () ) () ))
418
+ Nothing )
419
+ ()
420
+ , TraitItem (mkIdent " pi" ) [] (ConstT i32 (Just _1)) ()
421
+ , TraitItem (mkIdent " e" ) [] (ConstT i32 Nothing ) ()
422
+ , TraitItem (mkIdent " Size" ) [] (TypeT [] (Just i32)) ()
423
+ , TraitItem (mkIdent " Length" ) [] (TypeT [RegionTyParamBound (Lifetime (Name " l3" ) () )] Nothing ) ()
424
+ , TraitItem (mkIdent " SomeType" ) [] (TypeT [RegionTyParamBound (Lifetime (Name " l1" ) () )] (Just f64)) ()
425
+ ]) PublicV () ))
372
426
]
373
427
374
428
-- | Test pretty-printing of statements (flattened).
@@ -392,11 +446,16 @@ prettyStatements = testGroup "printing statements"
392
446
, testFlatten " println!(foo)" (printStmt (MacStmt (Mac (Path False [println] () ) [ Token mempty (IdentTok (mkIdent " foo" )) ] () ) NoBracesMac [] () ))
393
447
]
394
448
395
-
449
+ -- | Default pretty-printing
396
450
testRender :: String -> Doc a -> Test
397
- testRender str doc = testCase str $ str @=? display (renderPrettyDefault doc)
451
+ testRender str doc = testCase (escapeNewlines str) $ str @=? display (renderPrettyDefault doc)
398
452
453
+ -- | This tries to make it so that the `Doc` gets rendered onto only one line.
399
454
testFlatten :: String -> Doc a -> Test
400
- testFlatten str doc = testCase str $ str @=? display (renderPretty 0.5 1000 (flatten doc))
455
+ testFlatten str doc = testCase (escapeNewlines str) $ str @=? display (renderPretty 0.5 1000 (flatten doc))
456
+
457
+ -- | Utility function for escaping newlines (and only newlines)
458
+ escapeNewlines :: String -> String
459
+ escapeNewlines = concatMap (\ c -> if c == ' \n ' then " \\ n" else [c])
401
460
402
461
0 commit comments