Skip to content

Commit

Permalink
Add some tests for string untangling
Browse files Browse the repository at this point in the history
  • Loading branch information
nokyan committed Jul 5, 2024
1 parent a333267 commit 118cec1
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/utils/battery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,44 @@ impl Battery {
.context("unable to parse power_now sysfs file")
}
}

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

#[test]
fn dont_untangle_untangled_string() {
let untangled_string = String::from("This is a normal string");
assert_eq!(
Battery::untangle_weird_encoding(untangled_string),
String::from("This is a normal string")
)
}

#[test]
fn dont_untangle_non_hex_bytes() {
let non_hex_bytes = String::from("0xxy 0x4g");
assert_eq!(
Battery::untangle_weird_encoding(non_hex_bytes),
String::from("0xxy 0x4g")
)
}

#[test]
fn untangle_tangled_string() {
let tangled_string = String::from("0x41 0x42 0x43 0x58 0x59 0x5A");
assert_eq!(
Battery::untangle_weird_encoding(tangled_string),
String::from("ABCXYZ")
);
}

#[test]
fn untangle_tangled_string_with_nul() {
let tangled_string = String::from("0x41 0x42 0x43 0x00 0x44");
assert_eq!(
Battery::untangle_weird_encoding(tangled_string),
String::from("ABC D")
);
}
}

0 comments on commit 118cec1

Please sign in to comment.