types: add testutil module to help writing tests

Summary:
Building test objects can be tedious using various of our bottom bytes.
This diff addresses that issue by adding helper functions in a new module
in the types crate.

Handling this case could be improved in rust.

Differential Revision: D14660307

fbshipit-source-id: a866c1f3ede60ba1b87eb17d35817b8a8d7674a4
This commit is contained in:
Stefan Filip 2019-04-08 16:16:01 -07:00 committed by Facebook Github Bot
parent 12ad4df265
commit 1e2816f7c6
3 changed files with 65 additions and 15 deletions

View File

@ -391,6 +391,7 @@ impl<'a, S: Store> Iterator for Diff<'a, S> {
mod tests {
use super::*;
use types::testutil::*;
use types::PathComponentBuf;
use self::store::TestStore;
@ -399,15 +400,6 @@ mod tests {
fn meta(node: u8) -> FileMetadata {
FileMetadata::regular(Node::from_u8(node))
}
fn repo_path(s: &str) -> &RepoPath {
RepoPath::from_str(s).unwrap()
}
fn repo_path_buf(s: &str) -> RepoPathBuf {
RepoPathBuf::from_string(s.to_owned()).unwrap()
}
fn path_component_buf(s: &str) -> PathComponentBuf {
PathComponentBuf::from_string(s.to_owned()).unwrap()
}
fn store_element(path: &str, node: u8, flag: store::Flag) -> Fallible<store::Element> {
Ok(store::Element::new(
path_component_buf(path),
@ -442,7 +434,7 @@ mod tests {
])
.unwrap();
store
.insert(repo_path_buf(""), Node::from_u8(1), root_entry)
.insert(RepoPathBuf::new(), Node::from_u8(1), root_entry)
.unwrap();
let foo_entry = store::Entry::from_elements(vec![store_element(
"bar",
@ -511,7 +503,7 @@ mod tests {
tree.remove(repo_path("a1/b3")).unwrap(); // does nothing
tree.remove(repo_path("a1/b1/c1/d2")).unwrap(); // does nothing
tree.remove(repo_path("a1/b1/c1/d1/e1")).unwrap(); // does nothing
assert!(tree.remove(repo_path("")).is_err());
assert!(tree.remove(RepoPath::empty()).is_err());
assert_eq!(tree.get(repo_path("a1/b1/c1/d1")).unwrap(), None);
assert_eq!(tree.get(repo_path("a1/b1/c1")).unwrap(), None);
assert_eq!(tree.get(repo_path("a1/b2")).unwrap(), Some(&meta(20)));
@ -522,7 +514,7 @@ mod tests {
tree.remove(repo_path("a2/b2/c2")).unwrap();
assert_eq!(tree.get(repo_path("a2")).unwrap(), None);
assert!(tree.get_link(repo_path("")).unwrap().is_some());
assert!(tree.get_link(RepoPath::empty()).unwrap().is_some());
}
#[test]
@ -534,7 +526,7 @@ mod tests {
])
.unwrap();
store
.insert(repo_path_buf(""), Node::from_u8(1), root_entry)
.insert(RepoPathBuf::new(), Node::from_u8(1), root_entry)
.unwrap();
let a1_entry = store::Entry::from_elements(vec![
store_element("b1", 11, store::Flag::File(FileType::Regular)),
@ -559,7 +551,7 @@ mod tests {
tree.remove(repo_path("a2")).unwrap();
assert_eq!(tree.get(repo_path("a2")).unwrap(), None);
assert!(tree.get_link(repo_path("")).unwrap().is_some());
assert!(tree.get_link(RepoPath::empty()).unwrap().is_some());
}
#[test]
@ -606,7 +598,7 @@ mod tests {
let mut cursor = tree.root_cursor();
step(&mut cursor);
assert_eq!(cursor.path(), RepoPath::from_str("").unwrap());
assert_eq!(cursor.path(), RepoPath::empty());
step(&mut cursor);
assert_eq!(cursor.path(), RepoPath::from_str("a1").unwrap());
// Skip leaf

View File

@ -21,3 +21,6 @@ pub use crate::node::Node;
pub use crate::nodeinfo::NodeInfo;
pub use crate::parents::Parents;
pub use crate::path::{PathComponent, PathComponentBuf, RepoPath, RepoPathBuf};
#[cfg(any(test, feature = "for-tests"))]
pub mod testutil;

55
lib/types/src/testutil.rs Normal file
View File

@ -0,0 +1,55 @@
// Copyright 2019 Facebook, Inc.
//
// This software may be used and distributed according to the terms of the
// GNU General Public License version 2 or any later version.
use crate::{
key::Key,
node::Node,
path::{PathComponent, PathComponentBuf, RepoPath, RepoPathBuf},
};
pub fn repo_path(s: &str) -> &RepoPath {
if s == "" {
panic!(format!(
"the empty repo path is special, use RepoPath::empty() to build"
));
}
RepoPath::from_str(s).unwrap()
}
pub fn repo_path_buf(s: &str) -> RepoPathBuf {
if s == "" {
panic!(format!(
"the empty repo path is special, use RepoPathBuf::new() to build"
));
}
RepoPathBuf::from_string(s.to_owned()).unwrap()
}
pub fn path_component(s: &str) -> &PathComponent {
PathComponent::from_str(s).unwrap()
}
pub fn path_component_buf(s: &str) -> PathComponentBuf {
PathComponentBuf::from_string(s.to_owned()).unwrap()
}
pub fn node(hex: &str) -> Node {
if hex.len() > Node::hex_len() {
panic!(format!("invalid length for hex node: {}", hex));
}
if hex == "0" {
panic!(format!("node 0 is special, use Node::null_id() to build"));
}
let mut buffer = String::new();
for _i in 0..Node::hex_len() - hex.len() {
buffer.push('0');
}
buffer.push_str(hex);
Node::from_str(&buffer).unwrap()
}
pub fn key(path: &str, hexnode: &str) -> Key {
Key::new(path.as_bytes().to_vec(), node(hexnode))
}