Skip to content

Commit

Permalink
move parsing tests close to actual parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jun 15, 2020
1 parent aa8efdd commit 3ca2c59
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 28 deletions.
5 changes: 0 additions & 5 deletions git-odb/src/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,3 @@ impl Kind {
}

pub mod parsed;

#[cfg(test)]
mod tests {
use super::*;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use crate::{object, Sign, Time};
use hex::FromHex;
use std::str;

#[cfg(test)]
mod tests;

quick_error! {
#[derive(Debug)]
pub enum Error {
Expand Down
64 changes: 64 additions & 0 deletions git-odb/src/object/parsed/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use super::*;
use crate::object::{parsed, Kind};
use std::path::PathBuf;

pub fn bin(hex: &str) -> [u8; 20] {
<[u8; 20]>::from_hex(hex).unwrap()
}

pub fn fixture(path: &str) -> PathBuf {
PathBuf::from("tests").join("fixtures").join(path)
}

fn fixture_bytes(path: &str) -> Vec<u8> {
std::fs::read(fixture(path)).unwrap()
}

#[test]
fn tag_parse() {
let fixture = fixture_bytes("objects/tag.txt");
let actual = parsed::Tag::from_bytes(&fixture).unwrap();
assert_eq!(actual, tag_fixture(9000));
assert_eq!(
actual.target(),
bin("ffa700b4aca13b80cb6b98a078e7c96804f8e0ec")
);
}

fn tag_fixture(offset: i32) -> parsed::Tag<'static> {
parsed::Tag {
target_raw: b"ffa700b4aca13b80cb6b98a078e7c96804f8e0ec",
name_raw: b"1.0.0",
target_kind: Kind::Commit,
message: Some(b"for the signature\n"),
pgp_signature: Some(
b"-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - https://gpgtools.org
iQIzBAABCgAdFiEEw7xSvXbiwjusbsBqZl+Z+p2ZlmwFAlsapyYACgkQZl+Z+p2Z
lmy6Ug/+KzvzqiNpzz1bMVVAzp8NCbiEO3QGYPyeQc521lBwpaTrRYR+oHJY15r3
OdL5WDysTpjN8N5FNyfmvzkuPdTkK3JlYmO7VRjdA2xu/B6vIZLaOfAowFrhMvKo
8eoqwGcAP3rC5TuWEgzq2qhbjS4JXFLd4NLjWEFqT2Y2UKm+g8TeGOsa/0pF4Nq5
xeW4qCYR0WcQLFedbpkKHxag2GfaXKvzNNJdqYhVQssNa6BeSmsfDvlWYNe617wV
NvsR/zJT0wHb5SSH+h6QmwA7LQIQF//83Vc3aF7kv9D54r3ibXW5TjZ3WoeTUZO7
kefkzJ12EYDCFLPhHvXPog518nO8Ot46dX+okrF0/B4N3RFTvjKr7VAGTzv2D/Dg
DrD531S2F71b+JIRh641eeP7bjWFQi3tWLtrEOtjjsKPJfYRMKpYFnAO4UUJ6Rck
Z5fFXEUCO8d5WT56jzKDjmVoY01lA87O1YsP/J+zQAlc9v1k6jqeQ53LZNgTN+ue
5fJuSPT3T43pSOD1VQSr3aZ2Anc4Qu7K8uX9lkpxF9Sc0tDbeCosFLZMWNVp6m+e
cjHJZXWmV4CcRfmLsXzU8s2cR9A0DBvOxhPD1TlKC2JhBFXigjuL9U4Rbq9tdegB
2n8f2douw6624Tn/6Lm4a7AoxmU+CMiYagDxDL3RuZ8CAfh3bn0=
=aIns
-----END PGP SIGNATURE-----
",
),
signature: parsed::Signature {
name: b"Sebastian Thiel",
email: b"[email protected]",
time: Time {
time: 1528473343,
offset,
sign: Sign::Plus,
},
},
}
}
31 changes: 10 additions & 21 deletions git-odb/tests/loose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,6 @@ fn loose_iter() {
)
}

#[test]
fn loose_find() {
let mut o = ldb()
.find(&bin("722fe60ad4f0276d5a8121970b5bb9dccdad4ef9"))
.unwrap();
assert_eq!(o.kind, Kind::Tag);
assert_eq!(o.size, 1024);
assert_eq!(o.parsed().unwrap(), parsed::Object::Tag(tag_fixture(7200)))
}

#[test]
fn loose_tag_parse() {
let fixture = fixture_bytes("objects/tag.txt");
let actual = parsed::Tag::from_bytes(&fixture).unwrap();
assert_eq!(actual, tag_fixture(9000));
assert_eq!(
actual.target(),
bin("ffa700b4aca13b80cb6b98a078e7c96804f8e0ec")
);
}

fn tag_fixture(offset: i32) -> parsed::Tag<'static> {
parsed::Tag {
target_raw: b"ffa700b4aca13b80cb6b98a078e7c96804f8e0ec",
Expand Down Expand Up @@ -95,3 +74,13 @@ cjHJZXWmV4CcRfmLsXzU8s2cR9A0DBvOxhPD1TlKC2JhBFXigjuL9U4Rbq9tdegB
},
}
}

#[test]
fn loose_find() {
let mut o = ldb()
.find(&bin("722fe60ad4f0276d5a8121970b5bb9dccdad4ef9"))
.unwrap();
assert_eq!(o.kind, Kind::Tag);
assert_eq!(o.size, 1024);
assert_eq!(o.parsed().unwrap(), parsed::Object::Tag(tag_fixture(7200)))
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use git_core;
use structopt::StructOpt;

mod options {
use structopt::StructOpt;
use structopt::clap::AppSettings;
use structopt::StructOpt;

#[derive(Debug, StructOpt)]
#[structopt(about = "The git, simply swift")]
Expand Down
2 changes: 1 addition & 1 deletion tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

0 comments on commit 3ca2c59

Please sign in to comment.