fediloom/src/tests/manip.rs

195 lines
5.2 KiB
Rust
Raw Normal View History

#![cfg(test)]
use std::{collections::HashMap, fs};
use crate::manip::*;
#[test]
fn limit_add() {
let mut at = LimitIndices::default();
at.add_limit(Limit::Block, 123)
.add_limit(Limit::Silence, 456);
let test_at = LimitIndices {
block: 123,
silence: 456,
};
assert_eq!(at, test_at);
}
#[test]
fn limit_combine() {
let mut at = LimitIndices::default();
at.add_limit(Limit::Block, 123)
.add_limit(Limit::Block, 333)
.add_limit(Limit::Silence, 123);
let test_at = LimitIndices {
block: 456,
silence: 123,
};
assert_eq!(at, test_at);
}
#[test]
fn limitlist_from_map() {
let src1 = LimitList::from(HashMap::from([
(String::from("example.com"), Limit::Block),
(String::from("example.org"), Limit::Silence),
(String::from("example.net"), Limit::Block),
]));
assert_eq!(
src1.limits,
HashMap::from([
(String::from("example.com"), Limit::Block),
(String::from("example.org"), Limit::Silence),
(String::from("example.net"), Limit::Block),
])
);
assert_eq!(src1.trust, 100);
}
#[test]
fn limitlist_from_map_and_trust() {
let src2 = LimitList::build(
HashMap::from([
(String::from("example.com"), Limit::Block),
(String::from("example.org"), Limit::Silence),
(String::from("example.net"), Limit::Block),
]),
123,
);
assert_eq!(
src2.limits,
HashMap::from([
(String::from("example.com"), Limit::Block),
(String::from("example.org"), Limit::Silence),
(String::from("example.net"), Limit::Block),
])
);
assert_eq!(src2.trust, 123);
}
#[test]
fn limitlist_from_file() {
let mut src = LimitList::default();
src.import_file("test/example_blocklist.txt", Limit::Block)
.import_file("test/example_mutelist.txt", Limit::Silence);
let test_src = LimitList::from(HashMap::from([
(String::from("example.com"), Limit::Block),
(String::from("example.org"), Limit::Block),
(String::from("example.net"), Limit::Silence),
]));
assert_eq!(test_src, src);
}
#[test]
fn mergedlist_from_limitlist() {
let mut ml = MergedLimitList::default();
let src1 = LimitList::from(HashMap::from([
(String::from("example.com"), Limit::Block),
(String::from("example.org"), Limit::Silence),
(String::from("example.net"), Limit::Block),
]));
let mut src2 = LimitList::default();
src2.import_file("test/example_blocklist.txt", Limit::Block)
.import_file("test/example_mutelist.txt", Limit::Silence);
ml.add_limit_list(src1).add_limit_list(src2);
let test_ml = MergedLimitList {
map: HashMap::from([
(String::from("example.com"), LimitIndices::from((200, 0))),
2024-06-02 02:24:54 -04:00
(String::from("example.org"), LimitIndices::from((100, 100))),
(String::from("example.net"), LimitIndices::from((100, 100))),
]),
max: 200,
};
assert_eq!(ml, test_ml);
let src3 = LimitList::build(
HashMap::from([
(String::from("example.com"), Limit::Block),
(String::from("example.org"), Limit::Silence),
]),
200,
);
let src4 = LimitList::build(
HashMap::from([
(String::from("example.com"), Limit::Block),
(String::from("example.net"), Limit::Silence),
]),
50,
);
ml.add_limit_list(src3).add_limit_list(src4);
let test_ml = MergedLimitList {
map: HashMap::from([
(String::from("example.com"), LimitIndices::from((450, 0))),
2024-06-02 02:24:54 -04:00
(String::from("example.org"), LimitIndices::from((100, 300))),
(String::from("example.net"), LimitIndices::from((100, 150))),
]),
max: 450,
};
assert_eq!(ml, test_ml);
}
#[test]
fn mergedlist_export_txt() {
let mut ml = MergedLimitList::default();
let src1 = LimitList::from(HashMap::from([
(String::from("example.com"), Limit::Block),
(String::from("example.org"), Limit::Silence),
(String::from("example.net"), Limit::Block),
]));
let mut src2 = LimitList::default();
src2.import_file("test/example_blocklist.txt", Limit::Block)
.import_file("test/example_mutelist.txt", Limit::Silence);
let src3 = LimitList::build(
HashMap::from([
(String::from("example.com"), Limit::Block),
(String::from("example.org"), Limit::Silence),
]),
200,
);
let src4 = LimitList::build(
HashMap::from([
(String::from("example.com"), Limit::Block),
(String::from("example.net"), Limit::Silence),
]),
50,
);
ml.add_limit_list(src1)
.add_limit_list(src2)
.add_limit_list(src3)
.add_limit_list(src4);
2024-05-30 18:48:17 -04:00
let _ = ml.export_file("test/test_blocks.txt", "test/test_mutes.txt", (200, 150));
2024-05-30 18:48:17 -04:00
let file_blocks: String = fs::read_to_string("test/test_blocks.txt").unwrap();
let file_mutes: String = fs::read_to_string("test/test_mutes.txt").unwrap();
2024-05-30 18:48:17 -04:00
assert_eq!(file_blocks, "example.com\n");
assert_eq!(file_mutes, "example.net\nexample.org\n");
}