Add tests for truncate applet

This commit is contained in:
Nathan Fisher 2023-04-17 23:01:43 -04:00
parent 97679e43a6
commit edbabc11c1
2 changed files with 37 additions and 5 deletions

View File

@ -65,6 +65,7 @@ code between applets, making for an overall smaller binary.
- sync
- touch
- true
- truncate
- umount
- unlink
- wc

View File

@ -1,9 +1,7 @@
use std::fs::File;
use {
super::Cmd,
clap::{Arg, ArgAction, ArgGroup, Command, ValueHint},
std::{error::Error, fmt, fs, num::ParseIntError, path::PathBuf},
std::{error::Error, fmt, fs::{self, File}, num::ParseIntError, path::PathBuf},
};
#[derive(Debug, Default)]
@ -118,7 +116,7 @@ impl Cmd for Truncate {
}
}
#[derive(PartialEq)]
#[derive(Debug, PartialEq)]
enum Operator {
Equal,
Add,
@ -226,7 +224,7 @@ fn parse_size(size: &str) -> Result<Size, ParseSizeError> {
multiplier = Some(Multiplier::Tera);
}
}
ch if ch.is_digit(10) => {
ch if ch.is_ascii_digit() => {
if multiplier.is_some() {
return Err(ParseSizeError::InvalidChar);
} else {
@ -254,3 +252,36 @@ fn parse_size(size: &str) -> Result<Size, ParseSizeError> {
}),
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn parse_size_equal() {
let size = parse_size("10k").unwrap();
assert_eq!(size.num, 10240);
assert_eq!(size.operator, Operator::Equal);
}
#[test]
fn parse_size_add() {
let size = parse_size("+4M").unwrap();
assert_eq!(size.num, 4194304);
assert_eq!(size.operator, Operator::Add);
}
#[test]
fn parse_size_remove() {
let size = parse_size("-2G").unwrap();
assert_eq!(size.num, 2147483648);
assert_eq!(size.operator, Operator::Remove);
}
#[test]
fn parse_size_error() {
let size = parse_size("10.5");
assert!(size.is_err());
}
}