History

move parsing tests close to actual parsing

Sebastian Thiel committed 6 years ago

Commit 3ca2c591 parent aa8efdd6 files · +79 −28Browse files

Unified · Split

6 files
  1. git-odb/src/object/mod.rs +0 −5
  2. git-odb/src/object/parsed/mod.rs +3 −0
  3. git-odb/src/object/parsed/tests.rs +64 −0
  4. git-odb/tests/loose.rs +10 −21
  5. src/main.rs +1 −1
  6. tasks.md +1 −1
git-odb/src/object/mod.rs+0−5
@@ -38,8 +38,3 @@ impl Kind { · 37 unmodified lines
38}38}
39 39
40pub mod parsed;40pub mod parsed;
41
42#[cfg(test)]
43mod tests {
44 use super::*;
45}
git-odb/src/object/parsed/mod.rsrenamed from git-odb/src/object/parsed.rs+3−0
@@ -2,6 +2,9 @@ use crate::{object, Sign, Time}; · 1 unmodified line
2use hex::FromHex;2use hex::FromHex;
3use std::str;3use std::str;
4 4
5#[cfg(test)]
6mod tests;
7
5quick_error! {8quick_error! {
6 #[derive(Debug)]9 #[derive(Debug)]
7 pub enum Error {10 pub enum Error {
git-odb/src/object/parsed/tests.rs+64−0
@@ -0,0 +1,64 @@
1use super::*;
2use crate::object::{parsed, Kind};
3use std::path::PathBuf;
4
5pub fn bin(hex: &str) -> [u8; 20] {
6 <[u8; 20]>::from_hex(hex).unwrap()
7}
8
9pub fn fixture(path: &str) -> PathBuf {
10 PathBuf::from("tests").join("fixtures").join(path)
11}
12
13fn fixture_bytes(path: &str) -> Vec<u8> {
14 std::fs::read(fixture(path)).unwrap()
15}
16
17#[test]
18fn 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
28fn 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-----
36Comment: GPGTools - https://gpgtools.org
37
38iQIzBAABCgAdFiEEw7xSvXbiwjusbsBqZl+Z+p2ZlmwFAlsapyYACgkQZl+Z+p2Z
39lmy6Ug/+KzvzqiNpzz1bMVVAzp8NCbiEO3QGYPyeQc521lBwpaTrRYR+oHJY15r3
40OdL5WDysTpjN8N5FNyfmvzkuPdTkK3JlYmO7VRjdA2xu/B6vIZLaOfAowFrhMvKo
418eoqwGcAP3rC5TuWEgzq2qhbjS4JXFLd4NLjWEFqT2Y2UKm+g8TeGOsa/0pF4Nq5
42xeW4qCYR0WcQLFedbpkKHxag2GfaXKvzNNJdqYhVQssNa6BeSmsfDvlWYNe617wV
43NvsR/zJT0wHb5SSH+h6QmwA7LQIQF//83Vc3aF7kv9D54r3ibXW5TjZ3WoeTUZO7
44kefkzJ12EYDCFLPhHvXPog518nO8Ot46dX+okrF0/B4N3RFTvjKr7VAGTzv2D/Dg
45DrD531S2F71b+JIRh641eeP7bjWFQi3tWLtrEOtjjsKPJfYRMKpYFnAO4UUJ6Rck
46Z5fFXEUCO8d5WT56jzKDjmVoY01lA87O1YsP/J+zQAlc9v1k6jqeQ53LZNgTN+ue
475fJuSPT3T43pSOD1VQSr3aZ2Anc4Qu7K8uX9lkpxF9Sc0tDbeCosFLZMWNVp6m+e
48cjHJZXWmV4CcRfmLsXzU8s2cR9A0DBvOxhPD1TlKC2JhBFXigjuL9U4Rbq9tdegB
492n8f2douw6624Tn/6Lm4a7AoxmU+CMiYagDxDL3RuZ8CAfh3bn0=
50=aIns
51-----END PGP SIGNATURE-----
52",
53 ),
54 signature: parsed::Signature {
55 name: b"Sebastian Thiel",
56 email: b"byronimo@gmail.com",
57 time: Time {
58 time: 1528473343,
59 offset,
60 sign: Sign::Plus,
61 },
62 },
63 }
64}
git-odb/tests/loose.rs+10−21
@@ -37,27 +37,6 @@ fn loose_iter() { · 36 unmodified lines
37 )37 )
38}38}
39 39
40#[test]
41fn 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]
51fn 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
61fn tag_fixture(offset: i32) -> parsed::Tag<'static> {40fn tag_fixture(offset: i32) -> parsed::Tag<'static> {
62 parsed::Tag {41 parsed::Tag {
63 target_raw: b"ffa700b4aca13b80cb6b98a078e7c96804f8e0ec",42 target_raw: b"ffa700b4aca13b80cb6b98a078e7c96804f8e0ec",
@@ -95,3 +74,13 @@ cjHJZXWmV4CcRfmLsXzU8s2cR9A0DBvOxhPD1TlKC2JhBFXigjuL9U4Rbq9tdegB · 31 unmodified lines
95 },74 },
96 }75 }
97}76}
77
78#[test]
79fn 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}
src/main.rs+1−1
@@ -3,8 +3,8 @@ use git_core; · 2 unmodified lines
3use structopt::StructOpt;3use structopt::StructOpt;
4 4
5mod options {5mod options {
6 use structopt::StructOpt;
7 use structopt::clap::AppSettings;6 use structopt::clap::AppSettings;
7 use structopt::StructOpt;
8 8
9 #[derive(Debug, StructOpt)]9 #[derive(Debug, StructOpt)]
10 #[structopt(about = "The git, simply swift")]10 #[structopt(about = "The git, simply swift")]
tasks.md+1−1
@@ -2,7 +2,7 @@ · 1 unmodified line
2 2
3* [x] remove failure and replace with quick-check/anyhow3* [x] remove failure and replace with quick-check/anyhow
4* [x] minimize dependencies and test-runs4* [x] minimize dependencies and test-runs
5* [ ] structopt for grit5* [x] structopt for grit
6* [ ] make parser tests unit tests that are close to the code they test6* [ ] make parser tests unit tests that are close to the code they test
7* [ ] parser tests handle binary strings nicely7* [ ] parser tests handle binary strings nicely
8* [ ] switch parsing to nom (binary only)8* [ ] switch parsing to nom (binary only)