返回

visual studio代码-为什么vscode的“运行Doctest”助手会过滤我的板条箱中的所有Doctest?

发布时间:2022-03-09 18:44:26 396

预期行为:

点击;运行doctest“;在vscode中,应该从doctest片段执行一个测试。

终端输出应为(“1通过”或“1失败”),及;1.过滤掉&引用;。

实际行为:

点击;运行doctest“;在vscode中,执行0个测试,并显示2个被过滤掉。

终端输出:

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 2 filtered out; finished in 0.00s

源代码:

  • 我行为不端的板条箱:https://github.com/darrow-olykos/rusty-gists/blob/main/crates/my_cache/src/lib.rs

  • 行为板条箱(如果这不是问题):https://github.com/darrow-olykos/rusty-gists/blob/main/crates/math/src/lib.rs

我的机器:

  • macOS 12
  • rustc 1.57.0
  • rust analyzer v0.3.954

我为缩小问题范围所做的努力:

  1. 运行;“相同”;终端中的命令演示了预期的行为。终端输出显示test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out; finished in 0.40s当我跑的时候cargo test --doc --package my_cache -- "Cacher::new" --nocapture,这正是终端所说的,当我点击;运行Doctest“;.
  2. 点击;运行Doctest“;在另一个箱子里,我有(叫做“数学”)在同一回购协议中,表明了预期的行为。

看看我行为不端的板条箱和工作板条箱之间的区别:

答:这个行为不端的板条箱上有一个标签,它的医生检测结果是在一个箱子里impl其中,另一个板条箱的doctest位于文件的根级别。

B.这个行为不端的板条箱的doctest是针对接受closure类型

C.执行cargo test从…起crates/my_cache通过以下终端输出演示预期行为:

// ... some output omitted

   Doc-tests my_cache

running 2 tests
test src/lib.rs - Cacher::new (line 26) ... ok
test src/lib.rs - Cacher::value (line 42) ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.44s

同样,以下是源代码:

  • 行为不端的板条箱:https://github.com/darrow-olykos/rusty-gists/blob/main/crates/my_cache/src/lib.rs

  • 行为板条箱:https://github.com/darrow-olykos/rusty-gists/blob/main/crates/math/src/lib.rs

大概值得注意的细节:

  • 我已经按照https://github.com/rust-analyzer/rust-analyzer所以我用的是货物workspacescrates/*他们是会员。我有根cargo.toml它可以指定本地依赖项,比如my_cache = {path = "crates/my_cache}然而,我想不出这是一个促成因素的原因,因为我已经证明了我的math板条箱可以在这种结构中,而不需要vscode弄糊涂了,过滤掉了板条箱里的医生。

我的怀疑?:

  • 发生了一些事情,导致doctest在不应该被过滤掉的情况下被过滤掉。
    • 可能是我点击时声称正在执行的命令Run Doctest不是真正的命令被执行。
    • 也许是虫子与闭包类型有关。我忘了我在哪里读到过这篇文章,但我隐约记得锈封类型是;“未命名”;在某种程度上,引用它们会让人感到奇怪。不幸的是,我找不到详细介绍这一点的资源。(这可能是一个资源,涵盖了Rust编译器以及Rust编译器如何在内存中显示数据类型,但我不记得细节(也许有人读过这篇文章就会知道我在这里指的是什么))。

以下是行为不端的板条箱的源代码复制到这个答案中,以防我对github repo进行更改:

// Credit to: https://doc.rust-lang.org/book/ch13-01-closures.html
// (I've modified their example to use a HashMap instead of a single value)
use std::collections::HashMap;

/// cacher for calculation with two u64's as input and u64 as output
/// can be generalized more
pub struct Cacher
where
    T: FnMut(u64, u64) -> u64,
{
    calculation: T,
    values: HashMap,
}

impl Cacher
where
    T: FnMut(u64, u64) -> u64,
{
    /// Returns a Cacher which can cache results of calculations for the provided closure.
    ///
    /// # Arguments
    ///
    /// `T` - Closure that computes produces a value. Value is cached based on args. Cached value is returend on subsequent calls if args are the same.
    ///
    /// # Examples
    /// ```rust
    /// use my_cache::Cacher;
    /// let mut cacher = Cacher::new(|x,y|x+y);
    /// ```
    pub fn new(calculation: T) -> Self {
        let values = HashMap::new();
        Cacher {
            calculation,
            values,
        }
    }

    /// Returns value of calculation `T`. Cached value is returned if unique `n`, `k` pair provided, otherwise calcuation runs and then value is cached.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use std::rc::Rc;
    /// use std::cell::{RefCell, RefMut};
    ///
    /// use my_cache::Cacher;
    ///
    /// let mut count = Rc::new(RefCell::new(0));
    /// let add = |x, y| {
    ///     let mut count_mut_ref = count.borrow_mut();
    ///     *count_mut_ref += 1; x + y
    /// };
    /// let mut cacher = Cacher::new(add);
    ///
    /// assert_eq!(*count.borrow(), 0);
    /// assert_eq!(cacher.value(2, 3), 5); // new calculation, count += 1
    /// assert_eq!(*count.borrow(), 1);
    /// assert_eq!(cacher.value(2, 3), 5); // repeat, use cache
    /// assert_eq!(*count.borrow(), 1);
    /// assert_eq!(cacher.value(2, 4), 6); // new calculation, count += 1
    /// assert_eq!(*count.borrow(), 2);
    /// ```
    pub fn value(&mut self, n: u64, k: u64) -> u64 {
        let key = n.to_string() + &k.to_string();
        let cached_result = self.values.get(&key);
        if let Some(value) = cached_result {
            *value
        } else {
            let v = (self.calculation)(n, k);
            self.values.insert(key, v);
            v
        }
    }
}
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(0)
按点赞数排序
用户头像
相关帖子