// Copyright (C) 2023 Sebastien Guerri // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . use colored_str::*; #[cfg(test)] mod tests { use super::*; #[test] fn incorrect_assertions() { assert_eq!(colored("<>").to_string(), "<>"); assert_eq!(colored("<+>").to_string(), "<+>"); assert_eq!(colored("<->").to_string(), "<->"); assert_eq!(colored("").to_string(), ""); assert_eq!(colored("<>toto").to_string(), "<>toto"); assert_eq!(colored("toto<>").to_string(), "toto<>"); assert_eq!(colored("").to_string(), ""); assert_eq!(colored("toto").to_string(), "toto"); assert_eq!(colored("totototo").to_string(), "totototo"); assert_eq!(colored("toto").to_string(), "toto"); assert_eq!(colored("totototo").to_string(), "totototo"); assert_eq!(colored("toto").to_string(), "toto"); assert_eq!(colored("toto").to_string(), "toto"); assert_eq!(colored("toto").to_string(), "toto"); assert_eq!(colored("<#FF000>toto").to_string(), "<#FF000>toto"); assert_eq!(colored("toto").to_string(), "toto"); assert_eq!(colored("<+red>toto").to_string(), "<+red>toto"); assert_eq!(colored("<+red>toto<->").to_string(), "<+red>toto<->"); assert_eq!(colored("<+bold><->").to_string(), "<+bold><->"); assert_eq!(colored("<+blue>toto").to_string(), "\x1B[31m<+blue>toto\x1B[0m"); } #[test] fn correct_assertions() { assert_eq!(colored("").to_string(), ""); assert_eq!(colored("toto").to_string(), "\x1B[31mtoto\x1B[0m"); assert_eq!(colored("toto").to_string(), "\x1B[41mtoto\x1B[0m"); assert_eq!(colored("totototo").to_string(), "toto\x1B[31mtoto\x1B[0m"); assert_eq!(colored("totototo").to_string(), "\x1B[31mtoto\x1B[0mtoto"); assert_eq!(colored("totototototo").to_string(), "toto\x1B[31mtoto\x1B[0mtoto"); assert_eq!(colored("<#FF0000>toto").to_string(), "\x1B[38;2;255;0;0mtoto\x1B[0m"); assert_eq!(colored("toto").to_string(), "\x1B[48;2;255;0;0mtoto\x1B[0m"); assert_eq!(colored("totototo").to_string(), "\x1B[31mtoto\x1B[0m\x1B[34mtoto\x1B[0m"); assert_eq!(colored("totototototo").to_string(), "toto\x1B[31mtoto\x1B[0m\x1B[34mtoto\x1B[0m"); assert_eq!(colored("totototototo").to_string(), "\x1B[31mtoto\x1B[0mtoto\x1B[34mtoto\x1B[0m"); assert_eq!(colored("totototototo").to_string(), "\x1B[31mtoto\x1B[0m\x1B[34mtoto\x1B[0mtoto"); assert_eq!(colored("totototototototototo").to_string(), "toto\x1B[31mtoto\x1B[0mtoto\x1B[34mtoto\x1B[0mtoto"); assert_eq!(colored("toto").to_string(), "\x1B[1;31mtoto\x1B[0m"); assert_eq!(colored("toto").to_string(), "\x1B[1;31mtoto\x1B[0m"); assert_eq!(colored("toto").to_string(), "\x1B[1;3;31mtoto\x1B[0m"); assert_eq!(colored("toto").to_string(), "\x1B[44;31mtoto\x1B[0m"); assert_eq!(colored("toto").to_string(), "\x1B[44;31mtoto\x1B[0m"); assert_eq!(colored("toto").to_string(), "\x1B[1;44;31mtoto\x1B[0m"); assert_eq!(colored("toto").to_string(), "\x1B[34mtoto\x1B[0m"); assert_eq!(colored("toto").to_string(), "\x1B[31mtoto\x1B[0m"); assert_eq!(colored("toto").to_string(), "\x1B[44mtoto\x1B[0m"); assert_eq!(colored("toto").to_string(), "\x1B[41mtoto\x1B[0m"); assert_eq!(colored("toto").to_string(), "\x1B[1mtoto\x1B[0m"); assert_eq!(colored("toto").to_string(), "\x1B[1;3mtoto\x1B[0m"); assert_eq!(colored("toto\ntoto").to_string(), "\x1B[31mtoto\ntoto\x1B[0m"); assert_eq!(colored("toto<toto>toto").to_string() , "toto<\x1B[31mtoto\x1B[0m>toto"); assert_eq!(colored("totototototo>toto").to_string() , "totototo"); } #[test] fn correct_assertions_subtypes() { assert_eq!(colored("<+bold><->").to_string(), ""); assert_eq!(colored("<+bold>toto<->").to_string(), "\x1B[1;31mtoto\x1B[0m"); assert_eq!(colored("<+blue>toto<->").to_string(), "\x1B[34mtoto\x1B[0m"); assert_eq!(colored("<+bold+on_blue>toto<->").to_string(), "\x1B[1;44;31mtoto\x1B[0m"); assert_eq!(colored("toto<+bold>toto<->").to_string(), "\x1B[31mtoto\x1B[0m\x1B[1;31mtoto\x1B[0m"); assert_eq!(colored("<+bold>toto<->toto").to_string(), "\x1B[1;31mtoto\x1B[0m\x1B[31mtoto\x1B[0m"); assert_eq!(colored("toto<+bold>toto<->toto").to_string(), "\x1B[31mtoto\x1B[0m\x1B[1;31mtoto\x1B[0m\x1B[31mtoto\x1B[0m"); assert_eq!(colored("totototo<+bold>toto<->toto").to_string(), "toto\x1B[31mtoto\x1B[0m\x1B[1;31mtoto\x1B[0m\x1B[31mtoto\x1B[0m"); assert_eq!(colored("toto<+bold>toto<->totototo").to_string(), "\x1B[31mtoto\x1B[0m\x1B[1;31mtoto\x1B[0m\x1B[31mtoto\x1B[0mtoto"); assert_eq!(colored("totototo<+bold>toto<->totototo").to_string(), "toto\x1B[31mtoto\x1B[0m\x1B[1;31mtoto\x1B[0m\x1B[31mtoto\x1B[0mtoto"); assert_eq!(colored("toto<+bold>toto<-><+italic>toto<->toto").to_string(), "\x1B[31mtoto\x1B[0m\x1B[1;31mtoto\x1B[0m\x1B[3;31mtoto\x1B[0m\x1B[31mtoto\x1B[0m"); assert_eq!(colored("toto<+bold>toto<->toto<+italic>toto<->toto").to_string(), "\x1B[31mtoto\x1B[0m\x1B[1;31mtoto\x1B[0m\x1B[31mtoto\x1B[0m\x1B[3;31mtoto\x1B[0m\x1B[31mtoto\x1B[0m"); assert_eq!(colored("<+bold>toto\ntoto<->").to_string(), "\x1B[1;31mtoto\ntoto\x1B[0m"); } #[test] fn str_trait() { assert_eq!("".colored().to_string(), ""); assert_eq!("toto".colored().to_string(), "\x1B[31mtoto\x1B[0m"); assert_eq!("<#FF0000>toto".colored().to_string(), "\x1B[38;2;255;0;0mtoto\x1B[0m"); assert_eq!("toto".colored().to_string(), "\x1B[48;2;255;0;0mtoto\x1B[0m"); assert_eq!("totototo".colored().to_string(), "\x1B[31mtoto\x1B[0m\x1B[34mtoto\x1B[0m"); assert_eq!("toto".colored().to_string(), "\x1B[1;31mtoto\x1B[0m"); assert_eq!("toto".colored().to_string(), "\x1B[34mtoto\x1B[0m"); assert_eq!("toto".colored().to_string(), "\x1B[1mtoto\x1B[0m"); assert_eq!("toto\ntoto".colored().to_string(), "\x1B[31mtoto\ntoto\x1B[0m"); assert_eq!("<+bold><->".colored().to_string(), ""); assert_eq!("<+bold>toto<->".colored().to_string(), "\x1B[1;31mtoto\x1B[0m"); assert_eq!("toto<+bold>toto<->".colored().to_string(), "\x1B[31mtoto\x1B[0m\x1B[1;31mtoto\x1B[0m"); assert_eq!("toto<+bold>toto<-><+italic>toto<->toto".colored().to_string(), "\x1B[31mtoto\x1B[0m\x1B[1;31mtoto\x1B[0m\x1B[3;31mtoto\x1B[0m\x1B[31mtoto\x1B[0m"); assert_eq!("<+bold>toto\ntoto<->".colored().to_string(), "\x1B[1;31mtoto\ntoto\x1B[0m"); } #[test] fn str_macro() { let toto = "toto"; assert_eq!(cformat!(), ""); assert_eq!(cformat!("{toto}"), "\x1B[31mtoto\x1B[0m"); assert_eq!(cformat!("{}", toto), "\x1B[31mtoto\x1B[0m"); assert_eq!(cformat!("<#FF0000>{toto}"), "\x1B[38;2;255;0;0mtoto\x1B[0m"); assert_eq!(cformat!("{toto}"), "\x1B[48;2;255;0;0mtoto\x1B[0m"); assert_eq!(cformat!("{toto}{toto}"), "\x1B[31mtoto\x1B[0m\x1B[34mtoto\x1B[0m"); assert_eq!(cformat!("{toto}"), "\x1B[1;31mtoto\x1B[0m"); assert_eq!(cformat!("{toto}"), "\x1B[34mtoto\x1B[0m"); assert_eq!(cformat!("{toto}"), "\x1B[1mtoto\x1B[0m"); assert_eq!(cformat!("{toto}\n{toto}"), "\x1B[31mtoto\ntoto\x1B[0m"); assert_eq!(cformat!("<+bold>{toto}<->"), "\x1B[1;31mtoto\x1B[0m"); assert_eq!(cformat!("{toto}<+bold>{toto}<->"), "\x1B[31mtoto\x1B[0m\x1B[1;31mtoto\x1B[0m"); assert_eq!(cformat!("{toto}<+bold>{toto}<-><+italic>{toto}<->{toto}"), "\x1B[31mtoto\x1B[0m\x1B[1;31mtoto\x1B[0m\x1B[3;31mtoto\x1B[0m\x1B[31mtoto\x1B[0m"); assert_eq!(cformat!("<+bold>{toto}\n{toto}<->"), "\x1B[1;31mtoto\ntoto\x1B[0m"); } }