Skip to content

Commit

Permalink
test: ✅ 更新「输入转译器」以通过测试(CXinNARS、ONA)
Browse files Browse the repository at this point in the history
为 CXinNARS 与 ONA 增加对指令 `EXI` 的输入转译:CXinNARS→特殊命令`/q`,ONA→无效输入(强制shell退出)
  • Loading branch information
ARCJ137442 committed Jun 12, 2024
1 parent 7646350 commit 974f88e
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "babel_nar"
version = "0.24.2"
version = "0.24.3"
edition = "2021"
description = """
Implementation and application supports of the NAVM model
Expand Down
15 changes: 8 additions & 7 deletions src/bin/babelnar_cli/config_launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ pub fn launch_by_config(
Ok((runtime, config))
}

/// 根据「运行时启动参数」启动虚拟机
/// * 🚩生成、配置、启动虚拟机
/// * 🎯在「初次启动」与「二次重启」中共用代码
pub fn launch_by_runtime_config(config: &RuntimeConfig) -> Result<impl VmRuntime> {
// 生成虚拟机
let config_command = &config.command;
Expand Down Expand Up @@ -196,6 +199,7 @@ pub type TranslatorDict<'a> = &'a [(
fn(Cmd) -> Result<String>,
fn(String) -> Result<Output>,
)];

/// 输入转译器的索引字典
/// * 🚩静态存储映射,后续遍历可有序可无序
pub const TRANSLATOR_DICT: TranslatorDict = &[
Expand Down Expand Up @@ -229,6 +233,7 @@ pub const TRANSLATOR_DICT: TranslatorDict = &[
),
];

/// 根据名字查找「输入转译器」
pub fn get_input_translator_by_name(cin_name: &str) -> Result<Box<InputTranslator>> {
// 根据「匹配度」的最大值选取
let translator = TRANSLATOR_DICT
Expand All @@ -239,6 +244,7 @@ pub fn get_input_translator_by_name(cin_name: &str) -> Result<Box<InputTranslato
Ok(Box::new(translator))
}

/// 根据名字查找「输出转译器」
pub fn get_output_translator_by_name(cin_name: &str) -> Result<Box<OutputTranslator>> {
// 根据「匹配度」的最大值选取
let translator = TRANSLATOR_DICT
Expand All @@ -255,16 +261,11 @@ mod tests {
use super::*;
use nar_dev_utils::{asserts, f_parallel};

#[test]
fn t() {
dbg!(format!("{:p}", opennars::input_translate as fn(_) -> _));
}

/// 测试
/// 测试「根据名字查找转译器」
/// * 🚩仅能测试「是否查找成功」,无法具体地比较函数是否相同
/// * 📝函数在被装进[`Box`]后,对原先结构的完整引用就丧失了
#[test]
fn test() {
fn get_translator_by_name() {
fn t(name: &str) {
asserts! {
get_input_translator_by_name(name).is_ok()
Expand Down
9 changes: 7 additions & 2 deletions src/bin/babelnar_cli/vm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -774,10 +774,15 @@ pub mod tests {
/// * 🎯被重定向到`./executables`,以便启动其下的`.jar`文件
#[test]
fn test_read() {
// 使用OpenNARS配置文件的路径作测试
// * 🚩使用OpenNARS配置文件的路径作测试
let path: PathBuf = config_paths::OPENNARS.into();
let launch_config = read_config_extern(&path).expect("路径读取失败");
let expected_path = "./executables".into();
// * 🚩构造预期路径
let root_path = PathBuf::from(".");
let mut expected_path: PathBuf = "./executables".into();
LaunchConfig::rebase_relative_path(&root_path, &mut expected_path)
.expect("【2024-06-12 23:53:59】现在应该是绝对路径");
// * 🚩测试&比对
asserts! {
// * 🎯启动命令中的「当前目录」应该被追加到配置自身的路径上
// * ✅即便拼接后路径是`"./src/tests/cli/config\\root/nars/test"`,也和上边的路径相等
Expand Down
2 changes: 2 additions & 0 deletions src/cin_implements/cxin_js/translators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub fn input_translate(cmd: Cmd) -> Result<String> {
Cmd::CYC(n) => n.to_string(),
// 注释 ⇒ 忽略 | ❓【2024-04-02 22:43:05】可能需要打印,但这样却没法统一IO(到处print的习惯不好)
Cmd::REM { .. } => String::new(),
// 退出 ⇒ 特殊命令 | // * 🚩【2024-06-13 00:16:38】最新版本行为
Cmd::EXI { .. } => "/q".into(),
// 其它类型
// * 📌【2024-03-24 22:57:18】基本足够支持
_ => return Err(TranslateError::UnsupportedInput(cmd).into()),
Expand Down
2 changes: 2 additions & 0 deletions src/cin_implements/ona/translators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ pub fn input_translate(cmd: Cmd) -> Result<String> {
},
// 注释 ⇒ 忽略 | ❓【2024-04-02 22:43:05】可能需要打印,但这样却没法统一IO(到处print的习惯不好)
Cmd::REM { .. } => String::new(),
// 退出 ⇒ 无效输入 | // ! 🚩故意使用ONA中会「报错退出」的输入,强制ONA shell退出(其后不会再接收输入)
Cmd::EXI { .. } => "*quit".into(),
// 其它类型
// * 📌【2024-03-24 22:57:18】基本足够支持
_ => return Err(TranslateError::UnsupportedInput(cmd).into()),
Expand Down

0 comments on commit 974f88e

Please sign in to comment.