0260-CLAP-编写测试函数

环境

  • Time 2022-12-03
  • WSL-Ubuntu 22.04
  • CLAP 4.0.29

前言

说明

参考:https://docs.rs/clap/latest/clap/index.html

目标

编写测试函数。

Cargo.toml

[package]
edition = "2021"
name = "game"
version = "1.0.0"

[dependencies]
clap = {version = "4", features = ["cargo"]}

main

use clap::{command, value_parser, Arg};

fn main() {
    let matches = cmd().get_matches();

    if let Some(param) = matches.get_one::<u8>("age") {
        println!("年龄是:{}", param);
    }
}

fn cmd() -> clap::Command {
    command!().arg(Arg::new("age").value_parser(value_parser!(u8)).help("年龄"))
}

test

fn verify_cmd() {
    cmd().debug_assert();
}

验证

Executing task: cargo test --package game --bin game -- verify_cmd --exact --nocapture

   Compiling game v1.0.0 (/root/git/game)
    Finished test [unoptimized + debuginfo] target(s) in 0.46s
     Running unittests src/main.rs (target/debug/deps/game-843776f63e3169be)

running 1 test
test verify_cmd ... ok

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

 *  Terminal will be reused by tasks, press any key to close it.

总结

编写测试函数。

附录

posted @ 2025-10-23 10:16  jiangbo4444  阅读(8)  评论(0)    收藏  举报