Skip to content

Commit

Permalink
test internal
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoczim committed Dec 31, 2023
1 parent 8dc557f commit 50d2df8
Showing 1 changed file with 57 additions and 17 deletions.
74 changes: 57 additions & 17 deletions src/location/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,22 @@ impl Error for InvalidInternal {}
pub fn parse<'a>(
input: &'a str,
) -> Result<(&'a Internal, ViewRef<'a>), InvalidInternal> {
let view =
match input.char_indices().find(|(_, ch)| *ch == '#').map(|(i, _)| i) {
Some(i) => {
let (path_str, id_str) = input.split_at(i);
let id = Id::new(id_str)?;
if path_str == "." {
View::Id(id)
} else {
let path = Path::new(path_str)?;
View::PathWithId(path, id)
}
},
let view = match input.rsplit_once('#') {
Some((path_str, id_str)) => {
let id = Id::new(id_str)?;
if path_str == "." {
View::Id(id)
} else {
let path = Path::new(path_str)?;
View::PathWithId(path, id)
}
},

None => {
let path = Path::new(input)?;
View::Path(path)
},
};
None => {
let path = Path::new(input)?;
View::Path(path)
},
};

let internal_loc = Internal::from_ref_unchecked(input);
Ok((internal_loc, view))
Expand Down Expand Up @@ -402,3 +400,45 @@ impl ViewBuf {
.expect("attempt to append invalid component in path")
}
}

#[cfg(test)]
mod test {
use crate::location::{id::Id, internal::View, path::Path};

#[test]
fn valid_path_only() {
let (internal_loc, view) = super::parse("xyz/abcd/uv").unwrap();
assert_eq!(internal_loc.raw_contents(), "xyz/abcd/uv");
assert_eq!(view, View::Path(Path::new("xyz/abcd/uv").unwrap()));
}

#[test]
fn valid_id_only() {
let (internal_loc, view) = super::parse(".#foo-bar").unwrap();
assert_eq!(internal_loc.raw_contents(), ".#foo-bar");
assert_eq!(view, View::Id(Id::new("foo-bar").unwrap()));
}

#[test]
fn valid_path_with_id() {
let (internal_loc, view) = super::parse("xyz/jaja#foo-bar").unwrap();
assert_eq!(internal_loc.raw_contents(), "xyz/jaja#foo-bar");
assert_eq!(
view,
View::PathWithId(
Path::new("xyz/jaja").unwrap(),
Id::new("foo-bar").unwrap()
)
);
}

#[test]
fn invalid_id() {
super::parse("hello#!").unwrap_err();
}

#[test]
fn invalid_path() {
super::parse("..#abc").unwrap_err();
}
}

0 comments on commit 50d2df8

Please sign in to comment.