Skip to content

Commit

Permalink
refactor: move ANSIEscapeCode and tests to own module
Browse files Browse the repository at this point in the history
  • Loading branch information
Syaw0 committed Nov 1, 2024
1 parent 6367638 commit d63eee2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
12 changes: 12 additions & 0 deletions src/ansi_escape_code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pub struct ANSIEscapeCode {
parameter: String,
}
impl ANSIEscapeCode {
pub fn new(parameter: String) -> Self {
ANSIEscapeCode { parameter }
}

pub fn code(&self) -> String {
format!("\\x1b[{}m", self.parameter)
}
}
30 changes: 2 additions & 28 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,3 @@
struct ANSIEscapeCode {
parameter: String,
}
impl ANSIEscapeCode {
fn new(parameter: String) -> Self {
ANSIEscapeCode { parameter }
}
mod ansi_escape_code;

fn code(&self) -> String {
format!("\\x1b[{}m", self.parameter)
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn create_simple_ansi_code() {
let p = ANSIEscapeCode::new(String::from("33"));
assert_eq!(p.code(), "\\x1b[33m")
}

#[test]
fn create_reset_ansi_code() {
let reset_ansi = ANSIEscapeCode::new(String::from("0"));
assert_eq!("\\x1b[0m", reset_ansi.code());
}
}
// =======================================================================
23 changes: 23 additions & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#[cfg(test)]
mod ansi_escape_code_test {
use crate::ansi_escape_code::ANSIEscapeCode;
use super::*;

#[test]
fn create_simple_ansi_code() {
let p = ANSIEscapeCode::new(String::from("33"));
assert_eq!(p.code(), "\\x1b[33m")
}

#[test]
fn create_reset_ansi_code() {
let reset_ansi = ANSIEscapeCode::new(String::from("0"));
assert_eq!("\\x1b[0m", reset_ansi.code());
}

#[test]
fn create_bright_cyan_ansi_code() {
let reset_ansi = ANSIEscapeCode::new(String::from("96"));
assert_eq!("\\x1b[96m", reset_ansi.code());
}
}

0 comments on commit d63eee2

Please sign in to comment.