Roundtrip test for PrintableDeps

korrat
Mar 6, 2024, 2:17 PM
YWFLYCHUZ3T3RDE5KUQ7BFZU2MM62FPQTI7QHETGB24XCTEGW2CAC

Dependencies

  • [2] 5FI6SBEZ Re-implement change printing and parsing

Change contents

  • edit in libpijul/src/tests/text_changes.rs at line 168
    [2.8976]
    [2.8976]
    }
    quickcheck! {
    fn printable_dep_roundtrip(d1: PrintableDep) -> () {
    let mut w = Vec::new();
    writeln!(&mut w, "{}", &d1).unwrap();
    let utf = std::str::from_utf8(&w).unwrap();
    let (_, d2) = parse_dependency(utf).unwrap();
    assert_eq!(d1, d2);
    }
  • edit in libpijul/src/change/printable.rs at line 171
    [2.40197]
    [2.40197]
    }
    impl fmt::Display for PrintableDep {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    write!(f, "{}{}", self.type_, self.hash)
    }
  • edit in libpijul/src/change/printable.rs at line 185
    [2.40417]
    [2.40417]
    }
    impl fmt::Display for DepType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    if let Self::Numbered(n, p) = self {
    write!(f, "[{n}]{}", if *p { '+' } else { ' ' })
    } else {
    write!(
    f,
    "[{}] ",
    if matches!(self, Self::ExtraKnown) {
    '*'
    } else {
    ' '
    }
    )
    }
    }
  • edit in libpijul/src/change/printable.rs at line 919
    [2.64679]
    #[cfg(test)]
    impl Arbitrary for PrintableDep {
    fn arbitrary(g: &mut Gen) -> Self {
    let hash = Hash::Blake3(std::array::from_fn(|_| Arbitrary::arbitrary(g))).to_base32();
    Self {
    type_: Arbitrary::arbitrary(g),
    hash,
    }
    }
    fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
    Box::new(
    (self.type_.clone(), self.hash.clone())
    .shrink()
    .map(|(type_, hash)| Self { type_, hash }),
    )
    }
    }
    #[cfg(test)]
    impl Arbitrary for DepType {
    fn arbitrary(g: &mut Gen) -> Self {
    let options = [
    Self::ExtraKnown,
    Self::ExtraUnknown,
    Self::Numbered(Arbitrary::arbitrary(g), Arbitrary::arbitrary(g)),
    ];
    g.choose(&options).unwrap().clone()
    }
    fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
    match self {
    DepType::ExtraKnown | DepType::ExtraUnknown => Box::new(std::iter::empty()),
    DepType::Numbered(n, p) => {
    Box::new((*n, *p).shrink().map(|(n, p)| Self::Numbered(n, p)))
    }
    }
    }
    }