mirror of
https://github.com/loot/libloot.git
synced 2026-07-27 14:16:01 -07:00
Add a few simple unit tests
This commit is contained in:
@@ -72,3 +72,80 @@ impl<T: Fn(LogLevel, &str) + Send + Sync> log::Log for CallbackLogger<T> {
|
||||
|
||||
fn flush(&self) {}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
mod set_logging_callback {
|
||||
use super::*;
|
||||
|
||||
use std::sync::{Arc, LazyLock, Mutex};
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn set_logging_callback_should_be_callable_multiple_times() {
|
||||
let callback = |_, _: &str| {};
|
||||
set_logging_callback(callback);
|
||||
|
||||
let messages = Arc::new(Mutex::new(Vec::<(LogLevel, String)>::new()));
|
||||
let cloned_messages = messages.clone();
|
||||
let callback = move |level, message: &str| {
|
||||
if let Ok(mut messages) = cloned_messages.lock() {
|
||||
messages.push((level, message.to_string()));
|
||||
}
|
||||
};
|
||||
|
||||
set_logging_callback(callback);
|
||||
|
||||
log::error!("Test message");
|
||||
|
||||
assert_eq!(
|
||||
vec![(LogLevel::Error, "Test message".into())],
|
||||
*messages.lock().unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_support_a_closure_with_captured_state() {
|
||||
let messages = Arc::new(Mutex::new(Vec::<(LogLevel, String)>::new()));
|
||||
let cloned_messages = messages.clone();
|
||||
let callback = move |level, message: &str| {
|
||||
if let Ok(mut messages) = cloned_messages.lock() {
|
||||
messages.push((level, message.to_string()));
|
||||
}
|
||||
};
|
||||
|
||||
set_logging_callback(callback);
|
||||
|
||||
log::error!("Test message");
|
||||
|
||||
assert_eq!(
|
||||
vec![(LogLevel::Error, "Test message".into())],
|
||||
*messages.lock().unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn should_support_a_function() {
|
||||
static MESSAGES: LazyLock<Mutex<Vec<(LogLevel, String)>>> =
|
||||
LazyLock::new(|| Mutex::new(Vec::new()));
|
||||
|
||||
fn callback(level: LogLevel, message: &str) {
|
||||
if let Ok(mut messages) = MESSAGES.lock() {
|
||||
messages.push((level, message.to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
set_logging_callback(callback);
|
||||
|
||||
log::error!("Test message");
|
||||
|
||||
assert_eq!(
|
||||
vec![(LogLevel::Error, "Test message".into())],
|
||||
*MESSAGES.lock().unwrap()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -321,3 +321,27 @@ impl TryFrom<&MarkedYaml> for Message {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
mod select_message_content {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn should_return_none_if_the_slice_is_empty() {
|
||||
let content = select_message_content(&[], MessageContent::DEFAULT_LANGUAGE);
|
||||
|
||||
assert!(content.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_return_the_only_element_of_a_single_element_slice() {
|
||||
let slice = &[MessageContent::new("test".into()).with_language("de".into())];
|
||||
let content = select_message_content(slice, "fr");
|
||||
|
||||
assert_eq!(&slice[0], content.unwrap());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,3 +45,68 @@ const fn libloot_revision_const() -> &'static str {
|
||||
"unknown"
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
mod is_compatible {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn should_return_true_if_given_the_current_version() {
|
||||
assert!(is_compatible(
|
||||
LIBLOOT_VERSION_MAJOR,
|
||||
LIBLOOT_VERSION_MINOR,
|
||||
LIBLOOT_VERSION_PATCH
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_return_true_if_given_a_different_patch_version() {
|
||||
assert!(is_compatible(
|
||||
LIBLOOT_VERSION_MAJOR,
|
||||
LIBLOOT_VERSION_MINOR,
|
||||
LIBLOOT_VERSION_PATCH + 1
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_return_false_if_given_a_different_major_version() {
|
||||
assert!(!is_compatible(
|
||||
LIBLOOT_VERSION_MAJOR + 1,
|
||||
LIBLOOT_VERSION_MINOR,
|
||||
LIBLOOT_VERSION_PATCH
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_return_false_if_given_a_different_minor_version() {
|
||||
assert!(!is_compatible(
|
||||
LIBLOOT_VERSION_MAJOR,
|
||||
LIBLOOT_VERSION_MINOR + 1,
|
||||
LIBLOOT_VERSION_PATCH
|
||||
));
|
||||
}
|
||||
}
|
||||
mod libloot_version {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn should_be_version_numbers_separated_by_periods() {
|
||||
let expected =
|
||||
format!("{LIBLOOT_VERSION_MAJOR}.{LIBLOOT_VERSION_MINOR}.{LIBLOOT_VERSION_PATCH}",);
|
||||
|
||||
assert_eq!(expected, libloot_version());
|
||||
}
|
||||
}
|
||||
|
||||
mod libloot_revision {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn should_not_be_empty() {
|
||||
assert!(!libloot_revision().is_empty());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user