Skip to content

Commit 3ca2c59

Browse files
committed
move parsing tests close to actual parsing
1 parent aa8efdd commit 3ca2c59

File tree

6 files changed

+79
-28
lines changed

6 files changed

+79
-28
lines changed

Diff for: git-odb/src/object/mod.rs

-5
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,3 @@ impl Kind {
3838
}
3939

4040
pub mod parsed;
41-
42-
#[cfg(test)]
43-
mod tests {
44-
use super::*;
45-
}

Diff for: git-odb/src/object/parsed.rs renamed to git-odb/src/object/parsed/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ use crate::{object, Sign, Time};
22
use hex::FromHex;
33
use std::str;
44

5+
#[cfg(test)]
6+
mod tests;
7+
58
quick_error! {
69
#[derive(Debug)]
710
pub enum Error {

Diff for: git-odb/src/object/parsed/tests.rs

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use super::*;
2+
use crate::object::{parsed, Kind};
3+
use std::path::PathBuf;
4+
5+
pub fn bin(hex: &str) -> [u8; 20] {
6+
<[u8; 20]>::from_hex(hex).unwrap()
7+
}
8+
9+
pub fn fixture(path: &str) -> PathBuf {
10+
PathBuf::from("tests").join("fixtures").join(path)
11+
}
12+
13+
fn fixture_bytes(path: &str) -> Vec<u8> {
14+
std::fs::read(fixture(path)).unwrap()
15+
}
16+
17+
#[test]
18+
fn tag_parse() {
19+
let fixture = fixture_bytes("objects/tag.txt");
20+
let actual = parsed::Tag::from_bytes(&fixture).unwrap();
21+
assert_eq!(actual, tag_fixture(9000));
22+
assert_eq!(
23+
actual.target(),
24+
bin("ffa700b4aca13b80cb6b98a078e7c96804f8e0ec")
25+
);
26+
}
27+
28+
fn tag_fixture(offset: i32) -> parsed::Tag<'static> {
29+
parsed::Tag {
30+
target_raw: b"ffa700b4aca13b80cb6b98a078e7c96804f8e0ec",
31+
name_raw: b"1.0.0",
32+
target_kind: Kind::Commit,
33+
message: Some(b"for the signature\n"),
34+
pgp_signature: Some(
35+
b"-----BEGIN PGP SIGNATURE-----
36+
Comment: GPGTools - https://gpgtools.org
37+
38+
iQIzBAABCgAdFiEEw7xSvXbiwjusbsBqZl+Z+p2ZlmwFAlsapyYACgkQZl+Z+p2Z
39+
lmy6Ug/+KzvzqiNpzz1bMVVAzp8NCbiEO3QGYPyeQc521lBwpaTrRYR+oHJY15r3
40+
OdL5WDysTpjN8N5FNyfmvzkuPdTkK3JlYmO7VRjdA2xu/B6vIZLaOfAowFrhMvKo
41+
8eoqwGcAP3rC5TuWEgzq2qhbjS4JXFLd4NLjWEFqT2Y2UKm+g8TeGOsa/0pF4Nq5
42+
xeW4qCYR0WcQLFedbpkKHxag2GfaXKvzNNJdqYhVQssNa6BeSmsfDvlWYNe617wV
43+
NvsR/zJT0wHb5SSH+h6QmwA7LQIQF//83Vc3aF7kv9D54r3ibXW5TjZ3WoeTUZO7
44+
kefkzJ12EYDCFLPhHvXPog518nO8Ot46dX+okrF0/B4N3RFTvjKr7VAGTzv2D/Dg
45+
DrD531S2F71b+JIRh641eeP7bjWFQi3tWLtrEOtjjsKPJfYRMKpYFnAO4UUJ6Rck
46+
Z5fFXEUCO8d5WT56jzKDjmVoY01lA87O1YsP/J+zQAlc9v1k6jqeQ53LZNgTN+ue
47+
5fJuSPT3T43pSOD1VQSr3aZ2Anc4Qu7K8uX9lkpxF9Sc0tDbeCosFLZMWNVp6m+e
48+
cjHJZXWmV4CcRfmLsXzU8s2cR9A0DBvOxhPD1TlKC2JhBFXigjuL9U4Rbq9tdegB
49+
2n8f2douw6624Tn/6Lm4a7AoxmU+CMiYagDxDL3RuZ8CAfh3bn0=
50+
=aIns
51+
-----END PGP SIGNATURE-----
52+
",
53+
),
54+
signature: parsed::Signature {
55+
name: b"Sebastian Thiel",
56+
email: b"[email protected]",
57+
time: Time {
58+
time: 1528473343,
59+
offset,
60+
sign: Sign::Plus,
61+
},
62+
},
63+
}
64+
}

Diff for: git-odb/tests/loose.rs

+10-21
Original file line numberDiff line numberDiff line change
@@ -37,27 +37,6 @@ fn loose_iter() {
3737
)
3838
}
3939

40-
#[test]
41-
fn loose_find() {
42-
let mut o = ldb()
43-
.find(&bin("722fe60ad4f0276d5a8121970b5bb9dccdad4ef9"))
44-
.unwrap();
45-
assert_eq!(o.kind, Kind::Tag);
46-
assert_eq!(o.size, 1024);
47-
assert_eq!(o.parsed().unwrap(), parsed::Object::Tag(tag_fixture(7200)))
48-
}
49-
50-
#[test]
51-
fn loose_tag_parse() {
52-
let fixture = fixture_bytes("objects/tag.txt");
53-
let actual = parsed::Tag::from_bytes(&fixture).unwrap();
54-
assert_eq!(actual, tag_fixture(9000));
55-
assert_eq!(
56-
actual.target(),
57-
bin("ffa700b4aca13b80cb6b98a078e7c96804f8e0ec")
58-
);
59-
}
60-
6140
fn tag_fixture(offset: i32) -> parsed::Tag<'static> {
6241
parsed::Tag {
6342
target_raw: b"ffa700b4aca13b80cb6b98a078e7c96804f8e0ec",
@@ -95,3 +74,13 @@ cjHJZXWmV4CcRfmLsXzU8s2cR9A0DBvOxhPD1TlKC2JhBFXigjuL9U4Rbq9tdegB
9574
},
9675
}
9776
}
77+
78+
#[test]
79+
fn loose_find() {
80+
let mut o = ldb()
81+
.find(&bin("722fe60ad4f0276d5a8121970b5bb9dccdad4ef9"))
82+
.unwrap();
83+
assert_eq!(o.kind, Kind::Tag);
84+
assert_eq!(o.size, 1024);
85+
assert_eq!(o.parsed().unwrap(), parsed::Object::Tag(tag_fixture(7200)))
86+
}

Diff for: src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use git_core;
33
use structopt::StructOpt;
44

55
mod options {
6-
use structopt::StructOpt;
76
use structopt::clap::AppSettings;
7+
use structopt::StructOpt;
88

99
#[derive(Debug, StructOpt)]
1010
#[structopt(about = "The git, simply swift")]

Diff for: tasks.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
* [x] remove failure and replace with quick-check/anyhow
44
* [x] minimize dependencies and test-runs
5-
* [ ] structopt for grit
5+
* [x] structopt for grit
66
* [ ] make parser tests unit tests that are close to the code they test
77
* [ ] parser tests handle binary strings nicely
88
* [ ] switch parsing to nom (binary only)

0 commit comments

Comments
 (0)