From 4caaf60a7e74100bacf10953da312afb71e3d239 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Tue, 4 Jul 2023 21:29:43 +0100 Subject: [PATCH] Cover remaining lines while we're at it --- src/codec.rs | 13 ++++++++++++- src/packet_line.rs | 2 +- src/util.rs | 41 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/codec.rs b/src/codec.rs index 78af676..244f660 100644 --- a/src/codec.rs +++ b/src/codec.rs @@ -94,9 +94,20 @@ impl codec::Decoder for GitCodec { #[cfg(test)] mod test { + use crate::PktLine; use bytes::{Bytes, BytesMut}; use std::fmt::Write; - use tokio_util::codec::Decoder; + use tokio_util::codec::{Decoder, Encoder}; + + #[test] + fn encode() { + let mut bytes = BytesMut::new(); + super::Encoder + .encode(PktLine::Data(&[1, 2, 3, 4]), &mut bytes) + .unwrap(); + + assert_eq!(bytes.to_vec(), b"0008\x01\x02\x03\x04"); + } #[test] fn decode() { diff --git a/src/packet_line.rs b/src/packet_line.rs index 1a37c10..40c8c9f 100644 --- a/src/packet_line.rs +++ b/src/packet_line.rs @@ -69,7 +69,7 @@ mod test { #[test] fn test_pkt_line() { let mut buffer = BytesMut::new(); - super::PktLine::Data(b"agent=git/2.32.0\n") + super::PktLine::from("agent=git/2.32.0\n") .encode_to(&mut buffer) .unwrap(); assert_eq!(buffer.as_ref(), b"0015agent=git/2.32.0\n"); diff --git a/src/util.rs b/src/util.rs index 7375df2..6df9967 100644 --- a/src/util.rs +++ b/src/util.rs @@ -5,12 +5,18 @@ use std::{ sync::Arc, }; -#[derive(Debug, Hash, PartialEq, Eq)] +#[derive(Debug, Hash, Eq)] pub enum ArcOrCowStr { Arc(Arc), Cow(Cow<'static, str>), } +impl PartialEq for ArcOrCowStr { + fn eq(&self, other: &ArcOrCowStr) -> bool { + **self == **other + } +} + impl From> for ArcOrCowStr { fn from(v: Arc) -> Self { Self::Arc(v) @@ -57,3 +63,36 @@ impl Display for ArcOrCowStr { std::fmt::Display::fmt(&**self, f) } } + +#[cfg(test)] +mod test { + mod arc_or_cow_str { + use crate::util::ArcOrCowStr; + use std::borrow::Cow; + use std::sync::Arc; + + #[test] + fn from_arc() { + assert_eq!( + ArcOrCowStr::from(Arc::from("hello world".to_string())), + "hello world".into() + ); + } + + #[test] + fn from_cow() { + assert_eq!( + ArcOrCowStr::from(Cow::Borrowed("hello world")), + "hello world".into() + ); + } + + #[test] + fn from_string() { + assert_eq!( + ArcOrCowStr::from("hello world".to_string()), + "hello world".into() + ); + } + } +} -- libgit2 1.7.2