Skip to content

Commit

Permalink
feat: better operator info and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wangl-cc committed Dec 12, 2023
1 parent a388049 commit 68bb7a5
Show file tree
Hide file tree
Showing 6 changed files with 467 additions and 50 deletions.
66 changes: 63 additions & 3 deletions maa-cli/src/config/asst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,8 @@ impl InstanceOptions {
mod tests {
use super::*;

use crate::assert_matches;

mod serde {
use super::*;

Expand Down Expand Up @@ -754,8 +756,6 @@ mod tests {
mod connection_config {
use super::*;

use crate::assert_matches;

#[test]
fn default() {
assert_matches!(
Expand Down Expand Up @@ -818,7 +818,7 @@ mod tests {
mod resource_config {
use super::*;

use crate::{assert_matches, dirs::Ensure};
use crate::dirs::Ensure;

use std::{env::temp_dir, fs};

Expand Down Expand Up @@ -889,6 +889,17 @@ mod tests {
..
} if *path == PathBuf::from("iOS")
);

assert_matches!(
ResourceConfig {
platform_diff_resource: Some(PathBuf::from("iOS")),
..Default::default()
}.use_platform_diff_resource("Other"),
ResourceConfig {
platform_diff_resource: Some(path),
..
} if *path == PathBuf::from("iOS")
);
}

#[test]
Expand All @@ -903,6 +914,28 @@ mod tests {
);
}

#[test]
fn test_push_resource() {
let test_root = temp_dir().join("push_resource");

let resource_dir = test_root.join("resource");
let unexists_resource_dir = test_root.join("unexists_resource");

resource_dir.ensure().unwrap();

assert_eq!(
push_resource(&mut Vec::new(), resource_dir.clone()),
&[resource_dir.clone()]
);

assert_eq!(
push_resource(&mut Vec::new(), unexists_resource_dir.clone()),
&Vec::<PathBuf>::new()
);

fs::remove_dir_all(test_root).unwrap();
}

#[test]
fn resource_dir() {
let test_root = temp_dir().join("resource_config");
Expand Down Expand Up @@ -971,4 +1004,31 @@ mod tests {
fs::remove_dir_all(test_root).unwrap();
}
}

#[test]
fn instance_options() {
assert_matches!(
InstanceOptions {
touch_mode: None,
..Default::default()
}
.force_playtools(),
InstanceOptions {
touch_mode: Some(TouchMode::MacPlayTools),
..
}
);

assert_matches!(
InstanceOptions {
touch_mode: Some(TouchMode::ADB),
..Default::default()
}
.force_playtools(),
InstanceOptions {
touch_mode: Some(TouchMode::MacPlayTools),
..
}
);
}
}
73 changes: 73 additions & 0 deletions maa-cli/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,76 @@ pub mod asst;
pub mod cli;

pub mod task;

#[cfg(test)]
mod tests {
use crate::assert_matches;

use super::*;
use std::env::temp_dir;

use serde::Deserialize;

#[test]
fn find_file() {
#[derive(Deserialize, PartialEq, Debug)]
struct TestConfig {
a: i32,
b: String,
}

impl Default for TestConfig {
fn default() -> Self {
Self {
a: 0,
b: String::new(),
}
}
}

impl FromFile for TestConfig {}

let test_root = temp_dir().join("find_file");
std::fs::create_dir_all(&test_root).unwrap();

let test_file = test_root.join("test");
let non_exist_file = test_root.join("not_exist");

std::fs::write(
&test_file.with_extension("json"),
r#"{
"a": 1,
"b": "test"
}"#,
)
.unwrap();

assert_eq!(
TestConfig::find_file(&test_file).unwrap(),
TestConfig {
a: 1,
b: "test".to_string()
}
);

assert_matches!(
TestConfig::find_file(&non_exist_file).unwrap_err(),
Error::FileNotFound(s) if s == non_exist_file.to_str().unwrap()
);

assert_eq!(
TestConfig::find_file_or_default(&test_file).unwrap(),
TestConfig {
a: 1,
b: "test".to_string()
}
);

assert_eq!(
TestConfig::find_file_or_default(&non_exist_file).unwrap(),
TestConfig::default()
);

std::fs::remove_dir_all(&test_root).unwrap();
}
}
5 changes: 3 additions & 2 deletions maa-cli/src/config/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,8 @@ mod tests {
MAATask::StartUp,
object!( "start_game_enabled" => false)
),
Task::new_with_default(MAATask::Fight, object!("stage" => "1-7"))
Task::new_with_default(MAATask::Fight, object!("stage" => "1-7")),
Task::new_with_default(MAATask::CloseDown, object!("enable" => false)),
],
}
.init()
Expand All @@ -717,7 +718,7 @@ mod tests {
)
),
(MAATask::Fight.into(), object!("stage" => "1-7")),
(MAATask::CloseDown.into(), object!()),
(MAATask::CloseDown.into(), object!("enable" => true)),
]
},
);
Expand Down
47 changes: 47 additions & 0 deletions maa-cli/src/dirs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,5 +659,52 @@ mod tests {
assert!(!test_dir.exists());
assert_eq!(test_dir.ensure().unwrap(), test_dir);
assert!(test_dir.exists());
remove_dir_all(&test_root).unwrap();
}

#[test]
fn global_path_and_find() {
let test_root = temp_dir().join("maa-test-global-path");
let test_dir1 = test_root.join("test1");
let test_dir2 = test_root.join("test2");
let test_file = test_dir1.join("test");

test_dir1.ensure_clean().unwrap();
test_dir2.ensure_clean().unwrap();

std::fs::File::create(&test_file).unwrap();

assert_eq!(
global_path(&[&test_dir1, &test_dir2], "test"),
vec![test_file.clone()]
);
assert_eq!(
global_path(&[&test_dir1, &test_dir2], "not_exist"),
Vec::<PathBuf>::new()
);

assert_eq!(
global_find(&[&test_dir1, &test_dir2], |dir| {
if dir.join("test").exists() {
Some(dir.join("test"))
} else {
None
}
}),
vec![test_file.clone()]
);

assert_eq!(
global_find(&[&test_dir1, &test_dir2], |dir| {
if dir.join("not_exist").exists() {
Some(dir.join("not_exist"))
} else {
None
}
}),
Vec::<PathBuf>::new()
);

remove_dir_all(&test_root).unwrap();
}
}
42 changes: 36 additions & 6 deletions maa-cli/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ impl<T: Into<u8>> From<T> for LogLevel {
impl LogLevel {
pub fn to_git_flag(self) -> &'static str {
match self {
Self::Error => "-qq",
Self::Warning => "-q",
Self::Normal => "",
Self::Info => "-v",
Self::Debug => "-vv",
Self::Trace => "-vvv",
Self::Error | Self::Warning | Self::Normal => "-q",
Self::Info => "",
Self::Debug | Self::Trace => "-v",
}
}
}
Expand Down Expand Up @@ -204,3 +201,36 @@ macro_rules! trace {
}
};
}

#[cfg(test)]
mod tests {
use super::*;

use crate::assert_matches;

#[test]
fn log_level() {
assert_matches!(LogLevel::from(0), LogLevel::Error);
assert_matches!(LogLevel::from(1), LogLevel::Warning);
assert_matches!(LogLevel::from(2), LogLevel::Normal);
assert_matches!(LogLevel::from(3), LogLevel::Info);
assert_matches!(LogLevel::from(4), LogLevel::Debug);
assert_matches!(LogLevel::from(5), LogLevel::Trace);
assert_matches!(LogLevel::from(6), LogLevel::Trace);

assert_eq!(LogLevel::Error.to_git_flag(), "-q");
assert_eq!(LogLevel::Warning.to_git_flag(), "-q");
assert_eq!(LogLevel::Normal.to_git_flag(), "-q");
assert_eq!(LogLevel::Info.to_git_flag(), "");
assert_eq!(LogLevel::Debug.to_git_flag(), "-v");
assert_eq!(LogLevel::Trace.to_git_flag(), "-v");
}

#[test]
fn logger() {
let mut logger = Logger::new(LogLevel::Error);
assert_matches!(logger.level(), LogLevel::Error);
logger.set_level(LogLevel::Warning);
assert_matches!(logger.level(), LogLevel::Warning);
}
}
Loading

0 comments on commit 68bb7a5

Please sign in to comment.