2015-09-09 5 views
5
mod simulation; 

use simulation::factory::FactoryType; 

main.rsではなく、simulation/factory.rs内部doctestの中で正常に動作します:doctest内でカスタムモジュールを使用するにはどうすればよいですか?

impl product_type::ProductType for FactoryType { 
    /// Lorem Ipsum 
    /// 
    /// # Examples 
    /// 
    /// ``` 
    /// use simulation::factory::FactoryType; 
    /// 
    /// ... 
    /// ``` 
    fn human_id(&self) -> &String { 
     ... 
    } 
} 

cargo testは私がdoctestを仕事を得ることができますどのようにエラー

---- simulation::factory::human_id_0 stdout ---- 
    <anon>:2:9: 2:19 error: unresolved import `simulation::factory::FactoryType`. Maybe a missing `extern crate simulation`? 
<anon>:2  use simulation::factory::FactoryType; 
       ^~~~~~~~~~ 
error: aborting due to previous error 
thread 'simulation::factory::human_id_0' panicked at 'Box<Any>', /home/rustbuild/src/rust-buildbot/slave/stable-dist-rustc-linux/build/src/libsyntax/diagnostic.rs:192 

を与えますか?

+1

バイナリを作成している場合(たとえば、src/lib.rsの代わりに 'src/main.rs'がある場合)、doctestの関数を使用することはできません。doc tests import彼らがライブラリであるならば、それらはライブラリである(もしあれば)。 – huon

+0

ヘルプを求めるときは[MCVE](/ help/mcve)を作成してください。あなたの質問を今述べたように、何が存在するかを正確に知るためには、多くの推測が必要です。 – Shepmaster

答えて

6

ドキュメントテストを書くときには、コードのユーザーがとなる必要があります。

のsrc/lib.rs

pub mod simulation { 
    pub mod factory { 
     pub struct FactoryType; 

     impl FactoryType { 
      /// ``` 
      /// use foo::simulation::factory::FactoryType; 
      /// 
      /// let f = FactoryType; 
      /// assert_eq!(42, f.human_id()) 
      /// ``` 
      pub fn human_id(&self) -> u8 { 41 } 
     } 
    } 
} 

のsrc/main.rs

extern crate foo; 
use foo::simulation::factory::FactoryType; 

fn main() { 
    let f = FactoryType; 
    println!("{}", f.human_id()); 
} 

すべての作品:これらのファイルを考えます。 main.rsには、extern crateと言う必要があります。すべての参照にクレート名を含める必要があります。 doctestは同じですが、extern crateがあなたに自動的に含まれています。

3

huon-dbauppに記載されているように、binテストはdocテストからインポートできません。

解決策は、ほとんどのコードをライブラリクレートとして定義し、それを囲む単なるバイナリを1つ持つことです。

たとえば、racerはこの手法を採用しています。

+0

exesでのテストのために修正するまで、C#についても同じことが言えました。錆びて、邪魔を取り除いてください。 – Squirrel