mode::parser - add tests, fix failures

This commit is contained in:
Nathan Fisher 2023-01-16 00:30:21 -05:00
parent 4724041d90
commit 735c5bb8e8

View File

@ -164,7 +164,7 @@ impl Parser {
fn remove_bits(&mut self) {
for bit in &self.bits {
self.mode &= *bit as u32;
self.mode &= !(*bit as u32);
}
}
@ -175,9 +175,9 @@ impl Parser {
Some(Op::Equals) => {
for w in &self.who {
match w {
Who::User => self.mode &= 0o4444,
Who::Group => self.mode &= 0o2222,
Who::Other => self.mode &= 0o1111,
Who::User => self.mode &= !0o4444,
Who::Group => self.mode &= !0o2222,
Who::Other => self.mode &= !0o1111,
}
}
self.add_bits();
@ -238,3 +238,36 @@ impl Parser {
Ok(self.mode)
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn add() {
let mut parser = Parser::new(0o644);
let mode = parser.parse("ug+x");
assert_eq!(mode, Ok(0o754));
}
#[test]
fn remove() {
let mut parser = Parser::new(0o777);
let mode = parser.parse("go-wx");
assert_eq!(mode, Ok(0o744));
}
#[test]
fn octal() {
let mut parser = Parser::default();
let mode = parser.parse("4755");
assert_eq!(mode, Ok(0o4755));
}
#[test]
fn equals() {
let mut parser = Parser::new(0o701);
let mode = parser.parse("ugo=rw");
assert_eq!(mode, Ok(0o666));
}
}