Skip to content

Commit 244a646

Browse files
committed
refactor (#251)
Move tests closer to what they are actually testing
1 parent a216d89 commit 244a646

File tree

2 files changed

+65
-64
lines changed

2 files changed

+65
-64
lines changed

git-ref/tests/file/store/find.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod existing {
22
use git_testtools::hex_to_id;
3+
use std::convert::{TryFrom, TryInto};
34

45
use crate::file::store_at;
56

@@ -13,6 +14,69 @@ mod existing {
1314
assert_eq!(r.name.as_bstr(), "refs/heads/main");
1415
Ok(())
1516
}
17+
18+
/// Gain an understanding how uses might want to call this function, and see what happens
19+
#[test]
20+
fn possible_inputs() -> crate::Result {
21+
let store = crate::file::store()?;
22+
store.find_loose("dt1")?;
23+
store.find_loose(&String::from("dt1"))?; // Owned Strings don't have an impl for PartialName
24+
25+
struct CustomType(String);
26+
impl<'a> TryFrom<&'a CustomType> for git_ref::PartialNameRef<'a> {
27+
type Error = git_ref::name::Error;
28+
29+
fn try_from(value: &'a CustomType) -> Result<Self, Self::Error> {
30+
git_ref::PartialNameRef::try_from(&value.0)
31+
}
32+
}
33+
store.find_loose(&CustomType("dt1".into()))?;
34+
35+
struct CustomName {
36+
remote: &'static str,
37+
branch: &'static str,
38+
}
39+
40+
impl CustomName {
41+
fn to_partial_name(&self) -> String {
42+
format!("{}/{}", self.remote, self.branch)
43+
}
44+
fn to_partial_name_from_string(&self) -> git_ref::PartialNameRef<'static> {
45+
self.to_partial_name().try_into().expect("cannot fail")
46+
}
47+
fn to_partial_name_from_bstring(&self) -> git_ref::PartialNameRef<'static> {
48+
git_object::bstr::BString::from(self.to_partial_name())
49+
.try_into()
50+
.expect("cannot fail")
51+
}
52+
fn to_full_name(&self) -> git_ref::FullName {
53+
format!("{}/{}", self.remote, self.branch)
54+
.try_into()
55+
.expect("always valid")
56+
}
57+
}
58+
59+
impl<'a> TryFrom<&'a CustomName> for git_ref::PartialNameRef<'static> {
60+
type Error = git_ref::name::Error;
61+
62+
fn try_from(value: &'a CustomName) -> Result<Self, Self::Error> {
63+
git_ref::PartialNameRef::try_from(value.to_partial_name())
64+
}
65+
}
66+
67+
let name = CustomName {
68+
remote: "origin",
69+
branch: "main",
70+
};
71+
store.find_loose(&name.to_partial_name())?;
72+
store.find_loose(name.to_partial_name())?;
73+
store.find_loose(name.to_partial_name_from_string())?;
74+
store.find_loose(name.to_partial_name_from_bstring())?;
75+
store.find_loose(name.to_full_name().to_partial())?;
76+
store.find_loose(&name)?;
77+
78+
Ok(())
79+
}
1680
}
1781

1882
mod loose {

git-repository/tests/easy/reference.rs

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mod find {
2-
use std::convert::{TryFrom, TryInto};
2+
use std::convert::TryInto;
33

44
use git_ref as refs;
55
use git_repository::prelude::ReferenceAccessExt;
@@ -9,69 +9,6 @@ mod find {
99
crate::repo("make_references_repo.sh").map(Into::into)
1010
}
1111

12-
/// Gain an understanding how uses might want to call this function, and see what happens
13-
#[test]
14-
fn possible_inputs() -> crate::Result {
15-
let repo = repo()?;
16-
repo.find_reference("dt1")?;
17-
repo.find_reference(&String::from("dt1"))?; // Owned Strings don't have an impl for PartialName
18-
19-
struct CustomType(String);
20-
impl<'a> TryFrom<&'a CustomType> for refs::PartialNameRef<'a> {
21-
type Error = refs::name::Error;
22-
23-
fn try_from(value: &'a CustomType) -> Result<Self, Self::Error> {
24-
refs::PartialNameRef::try_from(&value.0)
25-
}
26-
}
27-
repo.find_reference(&CustomType("dt1".into()))?;
28-
29-
struct CustomName {
30-
remote: &'static str,
31-
branch: &'static str,
32-
}
33-
34-
impl CustomName {
35-
fn to_partial_name(&self) -> String {
36-
format!("{}/{}", self.remote, self.branch)
37-
}
38-
fn to_partial_name_from_string(&self) -> git_ref::PartialNameRef<'static> {
39-
self.to_partial_name().try_into().expect("cannot fail")
40-
}
41-
fn to_partial_name_from_bstring(&self) -> git_ref::PartialNameRef<'static> {
42-
git_object::bstr::BString::from(self.to_partial_name())
43-
.try_into()
44-
.expect("cannot fail")
45-
}
46-
fn to_full_name(&self) -> git_ref::FullName {
47-
format!("{}/{}", self.remote, self.branch)
48-
.try_into()
49-
.expect("always valid")
50-
}
51-
}
52-
53-
impl<'a> TryFrom<&'a CustomName> for refs::PartialNameRef<'static> {
54-
type Error = refs::name::Error;
55-
56-
fn try_from(value: &'a CustomName) -> Result<Self, Self::Error> {
57-
refs::PartialNameRef::try_from(value.to_partial_name())
58-
}
59-
}
60-
61-
let name = CustomName {
62-
remote: "origin",
63-
branch: "main",
64-
};
65-
repo.find_reference(&name.to_partial_name())?;
66-
repo.find_reference(name.to_partial_name())?;
67-
repo.find_reference(name.to_partial_name_from_string())?;
68-
repo.find_reference(name.to_partial_name_from_bstring())?;
69-
repo.find_reference(name.to_full_name().to_partial())?;
70-
repo.find_reference(&name)?;
71-
72-
Ok(())
73-
}
74-
7512
#[test]
7613
fn and_peel() -> crate::Result {
7714
let repo = repo()?;

0 commit comments

Comments
 (0)