#[cfg(test)]
mod tests {
use crate::matrix::{self, MatrixConfig, MatrixCombination};
use serde_yaml::Value;
use std::collections::HashMap;
use indexmap::IndexMap;
fn create_test_matrix() -> MatrixConfig {
let mut matrix = MatrixConfig::default();
let mut params = IndexMap::new();
let os_array = vec![
Value::String("ubuntu".to_string()),
Value::String("windows".to_string()),
Value::String("macos".to_string()),
];
params.insert("os".to_string(), Value::Sequence(os_array));
let node_array = vec![
Value::Number(serde_yaml::Number::from(14)),
Value::Number(serde_yaml::Number::from(16)),
];
params.insert("node".to_string(), Value::Sequence(node_array));
matrix.parameters = params;
let mut exclude_item = HashMap::new();
exclude_item.insert(
"os".to_string(),
Value::String("windows".to_string())
);
exclude_item.insert(
"node".to_string(),
Value::Number(serde_yaml::Number::from(14))
);
matrix.exclude = vec![exclude_item];
let mut include_item = HashMap::new();
include_item.insert(
"os".to_string(),
Value::String("ubuntu".to_string())
);
include_item.insert(
"node".to_string(),
Value::Number(serde_yaml::Number::from(18))
);
include_item.insert(
"experimental".to_string(),
Value::Bool(true)
);
matrix.include = vec![include_item];
matrix.max_parallel = Some(2);
matrix.fail_fast = Some(true);
matrix
}
#[test]
fn test_matrix_expansion() {
let matrix = create_test_matrix();
let combinations = matrix::expand_matrix(&matrix).unwrap();
assert_eq!(combinations.len(), 6);
let excluded = combinations.iter().find(|c| {
match (c.values.get("os"), c.values.get("node")) {
(Some(Value::String(os)), Some(Value::Number(node))) => {
os == "windows" && node.as_u64() == Some(14)
}
_ => false,
}
});
assert!(excluded.is_none(), "Excluded combination should not be present");
let included = combinations.iter().find(|c| {
match (c.values.get("os"), c.values.get("node"), c.values.get("experimental")) {
(Some(Value::String(os)), Some(Value::Number(node)), Some(Value::Bool(exp))) => {
os == "ubuntu" && node.as_u64() == Some(18) && *exp
}
_ => false,
}
});
assert!(included.is_some(), "Included combination should be present");
assert!(included.unwrap().is_included, "Combination should be marked as included");
}
#[test]
fn test_format_combination_name() {
let mut values = HashMap::new();
values.insert("os".to_string(), Value::String("ubuntu".to_string()));
values.insert("node".to_string(), Value::Number(serde_yaml::Number::from(14)));
let combination = MatrixCombination {
values,
is_included: false,
};
let formatted = matrix::format_combination_name("test-job", &combination);
assert!(formatted.contains("test-job"));
assert!(formatted.contains("os: ubuntu"));
assert!(formatted.contains("node: 14"));
}
}