#![cfg(test)] use std::{collections::HashMap, fs}; use crate::manip::*; #[test] fn modaction_add() { let mut at = ModActionTrust::default(); at.add_action(ModAction::Block, 123) .add_action(ModAction::Silence, 456); let test_at = ModActionTrust { block: 123, silence: 456, }; assert_eq!(at, test_at); } #[test] fn modaction_combine() { let mut at = ModActionTrust::default(); at.add_action(ModAction::Block, 123) .add_action(ModAction::Block, 333) .add_action(ModAction::Silence, 123); let test_at = ModActionTrust { block: 456, silence: 123, }; assert_eq!(at, test_at); } #[test] fn modsource_from_map() { let src1 = ModSource::from(HashMap::from([ (String::from("example.com"), ModAction::Block), (String::from("example.org"), ModAction::Silence), (String::from("example.net"), ModAction::Block), ])); assert_eq!( src1.actions, HashMap::from([ (String::from("example.com"), ModAction::Block), (String::from("example.org"), ModAction::Silence), (String::from("example.net"), ModAction::Block), ]) ); assert_eq!(src1.trust, 100); } #[test] fn modsource_from_map_and_trust() { let src2 = ModSource::build( HashMap::from([ (String::from("example.com"), ModAction::Block), (String::from("example.org"), ModAction::Silence), (String::from("example.net"), ModAction::Block), ]), 123, ); assert_eq!( src2.actions, HashMap::from([ (String::from("example.com"), ModAction::Block), (String::from("example.org"), ModAction::Silence), (String::from("example.net"), ModAction::Block), ]) ); assert_eq!(src2.trust, 123); } #[test] fn modsource_from_file() { let mut src = ModSource::default(); src.import_file("test/example_blocklist.txt", ModAction::Block) .import_file("test/example_mutelist.txt", ModAction::Silence); let test_src = ModSource::from(HashMap::from([ (String::from("example.com"), ModAction::Block), (String::from("example.org"), ModAction::Block), (String::from("example.net"), ModAction::Silence), ])); assert_eq!(test_src, src); } #[test] fn modmap_from_modsource() { let mut ml = ModMap::default(); let src1 = ModSource::from(HashMap::from([ (String::from("example.com"), ModAction::Block), (String::from("example.org"), ModAction::Silence), (String::from("example.net"), ModAction::Block), ])); let mut src2 = ModSource::default(); src2.import_file("test/example_blocklist.txt", ModAction::Block) .import_file("test/example_mutelist.txt", ModAction::Silence); ml.add_source(src1).add_source(src2); let test_ml = ModMap(HashMap::from([ (String::from("example.com"), ModActionTrust::from((200, 0))), ( String::from("example.org"), ModActionTrust::from((100, 100)), ), ( String::from("example.net"), ModActionTrust::from((100, 100)), ), ])); assert_eq!(ml, test_ml); let src3 = ModSource::build( HashMap::from([ (String::from("example.com"), ModAction::Block), (String::from("example.org"), ModAction::Silence), ]), 200, ); let src4 = ModSource::build( HashMap::from([ (String::from("example.com"), ModAction::Block), (String::from("example.net"), ModAction::Silence), ]), 50, ); ml.add_source(src3).add_source(src4); let test_ml = ModMap(HashMap::from([ (String::from("example.com"), ModActionTrust::from((450, 0))), ( String::from("example.org"), ModActionTrust::from((100, 300)), ), ( String::from("example.net"), ModActionTrust::from((100, 150)), ), ])); assert_eq!(ml, test_ml); } #[test] fn modmap_export_txt() { let mut ml = ModMap::default(); let src1 = ModSource::from(HashMap::from([ (String::from("example.com"), ModAction::Block), (String::from("example.org"), ModAction::Silence), (String::from("example.net"), ModAction::Block), ])); let mut src2 = ModSource::default(); src2.import_file("test/example_blocklist.txt", ModAction::Block) .import_file("test/example_mutelist.txt", ModAction::Silence); let src3 = ModSource::build( HashMap::from([ (String::from("example.com"), ModAction::Block), (String::from("example.org"), ModAction::Silence), ]), 200, ); let src4 = ModSource::build( HashMap::from([ (String::from("example.com"), ModAction::Block), (String::from("example.net"), ModAction::Silence), ]), 50, ); ml.add_source(src1) .add_source(src2) .add_source(src3) .add_source(src4); let _ = ml.export_file("test/test_blocks.txt", "test/test_mutes.txt", (200, 150)); 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(); assert_eq!(file_blocks, "example.com\n"); assert_eq!(file_mutes, "example.net\nexample.org\n"); }