Skip to content

Commit

Permalink
some necessary tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoczim committed Dec 30, 2023
1 parent 01c6c24 commit 30294b2
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 6 deletions.
33 changes: 33 additions & 0 deletions src/location/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,36 @@ impl AsRef<Self> for Component {
self
}
}

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

#[test]
fn valid_alphanumeric() {
let component = Component::parse("hell0").unwrap();
assert_eq!(component.raw_contents(), "hell0");
}

#[test]
fn valid_slug() {
let component = Component::parse("hello-world").unwrap();
assert_eq!(component.raw_contents(), "hello-world");
}

#[test]
fn valid_with_spaces_and_punct() {
let component = Component::parse("Hello, world!").unwrap();
assert_eq!(component.raw_contents(), "Hello, world!");
}

#[test]
fn invalid_bar() {
Component::parse("ha/he").unwrap_err();
}

#[test]
fn invalid_hash() {
Component::parse("ha#he").unwrap_err();
}
}
33 changes: 27 additions & 6 deletions src/location/dsl.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
use super::{
component::{Component, InvalidComponent},
id::{Id, InvalidId},
path::{InvalidPath, Path},
};

pub trait StrExt {
pub trait LocationStrExt {
fn try_id(&self) -> Result<&Id, InvalidId>;

fn try_component(&self) -> Result<&Component, InvalidComponent>;

fn try_path(&self) -> Result<&Path, InvalidPath>;

fn try_into_id(self: Box<Self>) -> Result<Box<Id>, InvalidId>;

fn try_into_component(
self: Box<Self>,
) -> Result<Box<Component>, InvalidComponent>;

fn try_into_path(self: Box<Self>) -> Result<Box<Path>, InvalidPath>;

fn id(&self) -> &Id {
self.try_id().expect("failed to parse id")
}
Expand All @@ -22,31 +27,47 @@ pub trait StrExt {
self.try_component().expect("failed to parse path component")
}

fn path(&self) -> &Path {
self.try_path().expect("failed to parse path")
}

fn into_id(self: Box<Self>) -> Box<Id> {
self.try_into_id().expect("failed to parse into id")
}

fn into_component(self: Box<Self>) -> Box<Component> {
self.try_into_component().expect("failed to parse into path component")
}

fn into_path(self: Box<Self>) -> Box<Path> {
self.try_into_path().expect("failed to parse into path")
}
}

impl StrExt for str {
impl LocationStrExt for str {
fn try_id(&self) -> Result<&Id, InvalidId> {
Id::parse(self)
}

fn try_into_id(self: Box<Self>) -> Result<Box<Id>, InvalidId> {
Id::parse_boxed(self)
}

fn try_component(&self) -> Result<&Component, InvalidComponent> {
Component::parse(self)
}

fn try_path(&self) -> Result<&Path, InvalidPath> {
Path::parse(self)
}

fn try_into_id(self: Box<Self>) -> Result<Box<Id>, InvalidId> {
Id::parse_boxed(self)
}

fn try_into_component(
self: Box<Self>,
) -> Result<Box<Component>, InvalidComponent> {
Component::parse_boxed(self)
}

fn try_into_path(self: Box<Self>) -> Result<Box<Path>, InvalidPath> {
Path::parse_boxed(self)
}
}
32 changes: 32 additions & 0 deletions src/location/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,35 @@ impl AsRef<Self> for Id {
self
}
}

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

#[test]
fn valid_alphanumeric() {
let id = Id::parse("hell0").unwrap();
assert_eq!(id.raw_contents(), "hell0");
}

#[test]
fn valid_slug() {
let id = Id::parse("hello-world_yahoo").unwrap();
assert_eq!(id.raw_contents(), "hello-world_yahoo");
}

#[test]
fn invalid_space() {
Id::parse("h a").unwrap_err();
}

#[test]
fn invalid_bar() {
Id::parse("ha/he").unwrap_err();
}

#[test]
fn invalid_hash() {
Id::parse("ha#he").unwrap_err();
}
}
Empty file removed src/location/parse.rs
Empty file.
86 changes: 86 additions & 0 deletions src/location/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,89 @@ impl Deref for PathBuf {
self.as_path()
}
}

#[cfg(test)]
mod test {
use crate::location::component::Component;

use super::Path;

#[test]
fn valid_root() {
let path0 = Path::parse("").unwrap();
let path1 = Path::ROOT;

assert_eq!(path0, path1);
assert_eq!(path0.raw_contents(), "");
assert_eq!(path1.raw_contents(), "");
}

#[test]
fn valid_single_alphanumeric() {
let path = Path::parse("hell0").unwrap();
assert_eq!(path.raw_contents(), "hell0");
}

#[test]
fn valid_slug() {
let path = Path::parse("hello-world/foo-bar").unwrap();
assert_eq!(path.raw_contents(), "hello-world/foo-bar");
}

#[test]
fn valid_with_spaces_and_punct() {
let path = Path::parse("Hello, world!/test/done").unwrap();
assert_eq!(path.raw_contents(), "Hello, world!/test/done");
}

#[test]
fn invalid_hash() {
Path::parse("ha#he").unwrap_err();
}

#[test]
fn iter_root() {
let expected: Vec<&Component> = Vec::new();
let actual = Path::ROOT.into_iter().collect::<Vec<_>>();

assert_eq!(expected, actual);
}

#[test]
fn iter_single() {
let expected: Vec<&Component> = vec![Component::parse("bl0g").unwrap()];
let actual =
Path::parse("bl0g").unwrap().into_iter().collect::<Vec<_>>();

assert_eq!(expected, actual);
}

#[test]
fn iter_two() {
let expected: Vec<&Component> = vec![
Component::parse("bl0g").unwrap(),
Component::parse("new-post").unwrap(),
];
let actual = Path::parse("bl0g/new-post")
.unwrap()
.into_iter()
.collect::<Vec<_>>();

assert_eq!(expected, actual);
}

#[test]
fn iter_three() {
let expected: Vec<&Component> = vec![
Component::parse("bl0g").unwrap(),
Component::parse("new-post").unwrap(),
Component::parse("actual").unwrap(),
];
let actual = Path::parse("bl0g/new-post/actual")
.unwrap()
.into_iter()
.collect::<Vec<_>>();

assert_eq!(expected, actual);
}
}

0 comments on commit 30294b2

Please sign in to comment.