use std::sync::Mutex;
use windjammer_runtime::*;
#[allow(dead_code)]
static TEST_MUTEX: Mutex<()> = Mutex::new(());
#[test]
fn test_fs_write_read() {
use std::fs as std_fs;
let test_file = "/tmp/windjammer_test_fs.txt";
let content = "Hello, Windjammer!";
let write_result = fs::write(test_file, content);
assert!(write_result.is_ok(), "Failed to write: {:?}", write_result);
let read_result = fs::read_to_string(test_file);
assert!(read_result.is_ok(), "Failed to read: {:?}", read_result);
assert_eq!(read_result.unwrap(), content);
let _ = std_fs::remove_file(test_file);
}
#[test]
fn test_fs_exists() {
let result = fs::exists("/tmp");
assert!(result);
let result = fs::exists("/nonexistent_path_12345");
assert!(!result);
}
#[test]
fn test_fs_metadata() {
let result = fs::is_dir("/tmp");
assert!(result);
}
#[test]
#[ignore] fn test_http_post_json() {
let data = serde_json::json!({"test": "data"});
let _ = data; }
#[test]
fn test_json_parse_stringify() {
let json_str = r#"{"name": "Alice", "age": 30}"#;
let parse_result = json::parse(json_str);
assert!(parse_result.is_ok(), "Failed to parse: {:?}", parse_result);
let value = parse_result.unwrap();
let stringify_result = json::stringify(&value);
assert!(stringify_result.is_ok());
}
#[test]
fn test_json_get_set() {
let json_str = r#"{"name": "Alice", "age": 30}"#;
let mut value = json::parse(json_str).unwrap();
let name = json::get(&value, "name");
assert!(name.is_some());
let new_value = json::parse(r#""Bob""#).unwrap();
let set_result = json::set(&mut value, "name", new_value);
assert!(set_result.is_ok());
}
#[test]
fn test_json_array_operations() {
let json_str = r#"[1, 2, 3, 4, 5]"#;
let value = json::parse(json_str).unwrap();
let len = json::len(&value);
assert_eq!(len, 5);
assert!(!json::is_empty(&value));
}
#[test]
fn test_mime_from_filename() {
let mime = mime::from_filename("test.html");
assert_eq!(mime, "text/html");
let mime = mime::from_filename("image.png");
assert_eq!(mime, "image/png");
let mime = mime::from_filename("data.json");
assert_eq!(mime, "application/json");
}
#[test]
fn test_mime_from_extension() {
let mime = mime::from_extension("js");
assert!(mime == "application/javascript" || mime == "text/javascript");
let mime = mime::from_extension("css");
assert_eq!(mime, "text/css");
}
#[test]
fn test_mime_type_checks() {
assert!(mime::is_text("text/html"));
assert!(mime::is_image("image/png"));
assert!(mime::is_application("application/json"));
assert!(!mime::is_audio("text/html"));
}
#[test]
fn test_async_sleep() {
use std::time::Instant;
let start = Instant::now();
std::thread::sleep(std::time::Duration::from_millis(100));
let elapsed = start.elapsed().as_millis();
assert!(
elapsed >= 100,
"Sleep didn't wait long enough: {}ms",
elapsed
);
}
#[test]
fn test_cli_app_creation() {
}
#[test]
fn test_collections_hashmap() {
let mut map = collections::HashMap::new();
map.insert("key1".to_string(), "value1".to_string());
assert_eq!(map.len(), 1);
let value = map.get("key1");
assert_eq!(value, Some(&"value1".to_string()));
assert!(map.contains_key("key1"));
assert!(!map.contains_key("key2"));
}
#[test]
fn test_collections_hashset() {
let mut set = collections::HashSet::new();
set.insert("item1".to_string());
set.insert("item2".to_string());
assert_eq!(set.len(), 2);
assert!(set.contains("item1"));
assert!(!set.contains("item3"));
}
#[test]
fn test_collections_vecdeque() {
let mut deque = collections::VecDeque::new();
deque.push_back("back".to_string());
deque.push_front("front".to_string());
assert_eq!(deque.len(), 2);
assert_eq!(deque.front(), Some(&"front".to_string()));
assert_eq!(deque.back(), Some(&"back".to_string()));
}
#[test]
fn test_crypto_sha256() {
let hash = crypto::sha256(b"hello world");
assert!(!hash.is_empty());
assert_eq!(hash.len(), 64); }
#[test]
fn test_crypto_password_hashing() {
let password = "my_secure_password";
let hash_result = crypto::hash_password(password);
assert!(
hash_result.is_ok(),
"Failed to hash password: {:?}",
hash_result
);
let hash = hash_result.unwrap();
let verify_result = crypto::verify_password(password, &hash);
assert!(verify_result.is_ok());
assert!(verify_result.unwrap(), "Password verification failed");
let wrong_verify = crypto::verify_password("wrong_password", &hash);
assert!(wrong_verify.is_ok());
assert!(!wrong_verify.unwrap(), "Wrong password should not verify");
}
#[test]
fn test_crypto_base64() {
let data = b"Hello, World!";
let encoded = crypto::base64_encode(data);
assert!(!encoded.is_empty());
let decoded_result = crypto::base64_decode(&encoded);
assert!(decoded_result.is_ok());
assert_eq!(decoded_result.unwrap(), data);
}
#[test]
fn test_csv_parse() {
let csv_data = "name,age,city\nAlice,30,NYC\nBob,25,LA";
let result = csv_mod::parse(csv_data);
assert!(result.is_ok(), "Failed to parse CSV: {:?}", result);
let rows = result.unwrap();
assert_eq!(rows.len(), 3); }
#[test]
fn test_csv_write() {
let rows = vec![
vec!["name".to_string(), "age".to_string()],
vec!["Alice".to_string(), "30".to_string()],
];
let result = csv_mod::write(&rows);
assert!(result.is_ok());
let csv = result.unwrap();
assert!(csv.contains("name,age"));
assert!(csv.contains("Alice,30"));
}
#[test]
fn test_db_open_sqlite() {
let result = db::open_sqlite(":memory:");
assert!(result.is_ok(), "Failed to open SQLite: {:?}", result);
let conn = result.unwrap();
assert_eq!(conn.db_type(), &db::DatabaseType::SQLite);
}
#[test]
fn test_db_open_postgres() {
let result = db::open_postgres("postgres://localhost/test");
assert!(
result.is_ok(),
"Failed to create Postgres connection: {:?}",
result
);
let conn = result.unwrap();
assert_eq!(conn.db_type(), &db::DatabaseType::Postgres);
}
#[test]
fn test_db_auto_detect() {
let sqlite = db::open(":memory:").unwrap();
assert_eq!(sqlite.db_type(), &db::DatabaseType::SQLite);
let postgres = db::open("postgres://localhost/test").unwrap();
assert_eq!(postgres.db_type(), &db::DatabaseType::Postgres);
}
#[test]
fn test_encoding_base64() {
let data = b"Hello, World!";
let encoded = encoding::base64_encode(data);
assert!(!encoded.is_empty());
let decoded = encoding::base64_decode(&encoded);
assert!(decoded.is_ok());
assert_eq!(decoded.unwrap(), data);
}
#[test]
fn test_encoding_hex() {
let data = b"test";
let encoded = encoding::hex_encode(data);
assert_eq!(encoded, "74657374");
let decoded = encoding::hex_decode(&encoded);
assert!(decoded.is_ok());
assert_eq!(decoded.unwrap(), data.to_vec());
}
#[test]
fn test_env_set_get() {
env::set_var("WINDJAMMER_TEST_VAR", "test_value");
let result = env::var("WINDJAMMER_TEST_VAR");
assert!(result.is_ok());
assert_eq!(result.unwrap(), "test_value");
env::remove_var("WINDJAMMER_TEST_VAR");
let result = env::var("WINDJAMMER_TEST_VAR");
assert!(result.is_err());
}
#[test]
fn test_env_current_dir() {
let result = env::current_dir();
assert!(result.is_ok());
assert!(!result.unwrap().is_empty());
}
#[test]
fn test_env_temp_dir() {
let result = std::env::temp_dir();
assert!(!result.to_string_lossy().is_empty());
}
#[test]
fn test_log_init() {
use std::panic;
let result = panic::catch_unwind(|| {
log_mod::init();
});
assert!(result.is_ok() || result.is_err());
}
#[test]
fn test_log_messages() {
let _ = std::panic::catch_unwind(log_mod::init);
log_mod::info("Test info message");
log_mod::warn("Test warning message");
log_mod::error("Test error message");
log_mod::debug("Test debug message");
log_mod::trace("Test trace message");
}
#[test]
fn test_math_basic_ops() {
assert_eq!(math::abs_f64(-5.0), 5.0);
assert_eq!(math::sqrt(16.0), 4.0);
assert_eq!(math::pow_f64(2.0, 3.0), 8.0);
assert_eq!(math::min_f64(5.0, 3.0), 3.0);
assert_eq!(math::max_f64(5.0, 3.0), 5.0);
}
#[test]
fn test_math_trig() {
use std::f64::consts::PI;
assert!((math::sin(0.0) - 0.0).abs() < 0.0001);
assert!((math::cos(0.0) - 1.0).abs() < 0.0001);
assert!((math::sin(PI / 2.0) - 1.0).abs() < 0.0001);
}
#[test]
fn test_math_rounding() {
assert_eq!(math::floor(3.7), 3.0);
assert_eq!(math::ceil(3.2), 4.0);
assert_eq!(math::round(3.5), 4.0);
}
#[test]
fn test_process_run() {
let result = process::run("echo", &[String::from("hello")]);
assert!(result.is_ok(), "Failed to run process: {:?}", result);
let output = result.unwrap();
assert!(output.contains("hello"));
}
#[test]
fn test_process_run_failure() {
let result = process::run("nonexistent_command_12345", &[]);
assert!(result.is_err());
}
#[test]
fn test_random_int() {
let num = random::int_range(1, 10);
assert!((1..=10).contains(&num));
}
#[test]
fn test_random_float() {
let num = random::float_range(0.0, 1.0);
assert!((0.0..=1.0).contains(&num));
}
#[test]
fn test_random_bool() {
let _ = random::bool();
}
#[test]
fn test_random_choice() {
let items = vec!["a".to_string(), "b".to_string(), "c".to_string()];
let choice = random::choice(&items);
assert!(choice.is_some());
assert!(items.contains(&choice.unwrap()));
}
#[test]
fn test_regex_new() {
let result = regex_mod::new(r"\d+");
assert!(result.is_ok(), "Failed to create regex: {:?}", result);
let re = result.unwrap();
assert!(regex_mod::is_match_compiled(&re, "123"));
assert!(!regex_mod::is_match_compiled(&re, "abc"));
}
#[test]
fn test_regex_is_match() {
assert!(regex_mod::is_match(r"\d+", "123").unwrap());
assert!(!regex_mod::is_match(r"\d+", "abc").unwrap());
let re = regex_mod::new(r"\d+").unwrap();
assert!(regex_mod::is_match_compiled(&re, "123"));
assert!(!regex_mod::is_match_compiled(&re, "abc"));
}
#[test]
fn test_regex_find() {
let result = regex_mod::find(r"\d+", "abc123def");
assert_eq!(result.unwrap(), Some("123".to_string()));
let re = regex_mod::new(r"\d+").unwrap();
assert_eq!(
regex_mod::find_compiled(&re, "abc123def"),
Some("123".to_string())
);
}
#[test]
fn test_regex_replace() {
let result = regex_mod::replace(r"\d+", "abc123def", "XXX");
assert_eq!(result.unwrap(), "abcXXXdef");
let re = regex_mod::new(r"\d+").unwrap();
assert_eq!(
regex_mod::replace_compiled(&re, "abc123def", "XXX"),
"abcXXXdef"
);
}
#[test]
fn test_strings_basic() {
let s = "Hello, World!";
assert_eq!(strings::len(s), 13);
assert!(!strings::is_empty(s));
assert!(strings::contains(s, "World"));
assert!(strings::starts_with(s, "Hello"));
assert!(strings::ends_with(s, "!"));
}
#[test]
fn test_strings_case() {
let s = "Hello";
assert_eq!(strings::to_upper(s), "HELLO");
assert_eq!(strings::to_lower(s), "hello");
}
#[test]
fn test_strings_split_join() {
let s = "a,b,c";
let parts = strings::split(s, ",");
assert_eq!(parts.len(), 3);
assert_eq!(parts[0], "a");
let joined = strings::join(&parts, "-");
assert_eq!(joined, "a-b-c");
}
#[test]
fn test_strings_trim() {
let s = " hello ";
assert_eq!(strings::trim(s), "hello");
}
#[test]
fn test_testing_assert() {
testing::assert(true, "Should not panic");
}
#[test]
#[should_panic]
fn test_testing_assert_fail() {
testing::assert(false, "Should panic");
}
#[test]
fn test_testing_assert_eq() {
testing::assert_eq(5, 5, "Should be equal");
}
#[test]
fn test_testing_should_panic() {
let panics = testing::should_panic(|| {
panic!("test");
});
assert!(panics);
let no_panic = testing::should_panic(|| {
});
assert!(!no_panic);
}
#[test]
fn test_time_now() {
let _instant = time::now(); let timestamp = time::now_millis();
assert!(timestamp > 0);
}
#[test]
fn test_time_now_string() {
let s = time::now_string();
assert!(!s.is_empty());
assert!(s.contains('T')); }
#[test]
fn test_time_parse_format() {
let s = "2024-01-15T10:30:00Z";
let result = time::parse(s);
assert!(result.is_ok(), "Failed to parse time: {:?}", result);
let timestamp = result.unwrap();
let formatted = time::format(timestamp, "%Y-%m-%d");
assert!(formatted.starts_with("2024-01-15"));
}
#[test]
fn test_time_duration() {
let secs = time::duration_secs(2, 5);
assert_eq!(secs, 3);
let millis = time::duration_millis(1, 2);
assert_eq!(millis, 1000); }