Skip to content

Commit

Permalink
add code coverage ci, and improve code coverage in general
Browse files Browse the repository at this point in the history
  • Loading branch information
eserilev committed Sep 4, 2024
1 parent ce30c93 commit 2eec6b5
Show file tree
Hide file tree
Showing 7 changed files with 394 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
coverage:
cargo-tarpaulin --workspace --all-features --out xml

.PHONY: coverage
95 changes: 95 additions & 0 deletions src/address_hex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,98 @@ where
array.copy_from_slice(&decoded);
Ok(array.into())
}

#[cfg(test)]
mod test {
use std::str::FromStr;

use alloy_primitives::Address;
use serde::{Deserialize, Serialize};
use serde_json;

#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(transparent)]
struct Wrapper {
#[serde(with = "super")]
val: Address,
}

#[test]
fn encoding() {
assert_eq!(
&serde_json::to_string(&Wrapper {
val: Address::from_str("0000000000000000000000000000000000000000").unwrap()
})
.unwrap(),
"\"0x0000000000000000000000000000000000000000\""
);
assert_eq!(
&serde_json::to_string(&Wrapper {
val: Address::from_str("0000000000000000000000000000000000000001").unwrap()
})
.unwrap(),
"\"0x0000000000000000000000000000000000000001\""
);
assert_eq!(
&serde_json::to_string(&Wrapper {
val: Address::from_str("1000000000000000000000000000000000000000").unwrap()
})
.unwrap(),
"\"0x1000000000000000000000000000000000000000\""
);
assert_eq!(
&serde_json::to_string(&Wrapper {
val: Address::from_str("1234567890000000000000000000000000000000").unwrap()
})
.unwrap(),
"\"0x1234567890000000000000000000000000000000\""
);
assert_eq!(
&serde_json::to_string(&Wrapper { val: Address::ZERO }).unwrap(),
"\"0x0000000000000000000000000000000000000000\""
);
}

#[test]
fn decoding() {
assert_eq!(
serde_json::from_str::<Wrapper>("\"0x0000000000000000000000000000000000000000\"")
.unwrap(),
Wrapper { val: Address::ZERO },
);
assert_eq!(
serde_json::from_str::<Wrapper>("\"0x0000000000000000000000000000000000000001\"")
.unwrap(),
Wrapper {
val: Address::from_str("0000000000000000000000000000000000000001").unwrap()
},
);
assert_eq!(
serde_json::from_str::<Wrapper>("\"0x1000000000000000000000000000000000000000\"")
.unwrap(),
Wrapper {
val: Address::from_str("1000000000000000000000000000000000000000").unwrap()
},
);
assert_eq!(
serde_json::from_str::<Wrapper>("\"0x1234567890000000000000000000000000000000\"")
.unwrap(),
Wrapper {
val: Address::from_str("1234567890000000000000000000000000000000").unwrap()
},
);
// Wrong length.
serde_json::from_str::<Wrapper>("\"0x0\"").unwrap_err();
serde_json::from_str::<Wrapper>("\"0x0400\"").unwrap_err();
serde_json::from_str::<Wrapper>("\"0x12345678900000000000000000000000000000001\"")
.unwrap_err();
// Requires 0x.
serde_json::from_str::<Wrapper>("\"1234567890000000000000000000000000000000\"")
.unwrap_err();
serde_json::from_str::<Wrapper>("\"ff34567890000000000000000000000000000000\"")
.unwrap_err();
// Contains invalid characters.
serde_json::from_str::<Wrapper>("\"0x-100000000000000000000000000000000000000\"")
.unwrap_err();
}
}
93 changes: 93 additions & 0 deletions src/fixed_bytes_hex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,99 @@ macro_rules! bytes_hex {
array.copy_from_slice(&decoded);
Ok(array)
}

#[cfg(test)]
mod test {
use super::*;
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(transparent)]
struct Wrapper {
#[serde(with = "super")]
val: [u8; BYTES_LEN],
}

fn generate_string_value(v1: &str, v2: &str) -> String {
let mut i = 0;
let mut value = String::new();
while i < BYTES_LEN * 2 {
if i % 2 == 0 {
value.push_str(v1);
} else {
value.push_str(v2);
}
i += 1;
}
value
}

#[test]
fn encoding() {
let zero = "0".repeat(BYTES_LEN * 2);
assert_eq!(
&serde_json::to_string(&Wrapper {
val: [0; BYTES_LEN]
})
.unwrap(),
&format!("\"0x{}\"", zero)
);

assert_eq!(
&serde_json::to_string(&Wrapper {
val: [123; BYTES_LEN]
})
.unwrap(),
&format!("\"0x{}\"", generate_string_value("7", "b"))
);

let max = "f".repeat(BYTES_LEN * 2);
assert_eq!(
&serde_json::to_string(&Wrapper {
val: [u8::MAX; BYTES_LEN]
})
.unwrap(),
&format!("\"0x{}\"", max)
);
}

#[test]
fn decoding() {
let zero = "0".repeat(BYTES_LEN * 2);
assert_eq!(
serde_json::from_str::<Wrapper>(&format!("\"0x{}\"", zero)).unwrap(),
Wrapper {
val: [0; BYTES_LEN]
},
);
assert_eq!(
serde_json::from_str::<Wrapper>(&format!(
"\"0x{}\"",
generate_string_value("7", "b")
))
.unwrap(),
Wrapper {
val: [123; BYTES_LEN]
},
);

let max = "f".repeat(BYTES_LEN * 2);
assert_eq!(
serde_json::from_str::<Wrapper>(&format!("\"0x{}\"", max)).unwrap(),
Wrapper {
val: [u8::MAX; BYTES_LEN]
},
);

// Require 0x.
serde_json::from_str::<Wrapper>(&format!("\"{}\"", "0".repeat(BYTES_LEN * 2)))
.unwrap_err();

let exceed_max = "f".repeat((BYTES_LEN * 2) + 1);
// Wrong length.
serde_json::from_str::<Wrapper>(&format!("\"0x{}\"", exceed_max)).unwrap_err();
}
}
};
}

Expand Down
49 changes: 49 additions & 0 deletions src/hex_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,52 @@ where
{
deserializer.deserialize_str(PrefixedHexVisitor)
}

#[cfg(test)]
mod test {
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(transparent)]
struct Wrapper {
#[serde(with = "super")]
val: Vec<u8>,
}

#[test]
fn encoding() {
assert_eq!(
&serde_json::to_string(&Wrapper { val: vec![0] }).unwrap(),
"\"0x00\""
);
assert_eq!(
&serde_json::to_string(&Wrapper { val: vec![0, 1] }).unwrap(),
"\"0x0001\""
);
assert_eq!(
&serde_json::to_string(&Wrapper {
val: vec![0, 1, 2, 3]
})
.unwrap(),
"\"0x00010203\""
);
}

#[test]
fn decoding() {
assert_eq!(
serde_json::from_str::<Wrapper>("\"0x00\"").unwrap(),
Wrapper { val: vec![0] },
);
assert_eq!(
serde_json::from_str::<Wrapper>("\"0x0001\"").unwrap(),
Wrapper { val: vec![0, 1] },
);
assert_eq!(
serde_json::from_str::<Wrapper>("\"0x00010203\"").unwrap(),
Wrapper {
val: vec![0, 1, 2, 3]
},
);
}
}
54 changes: 54 additions & 0 deletions src/list_of_bytes_lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,57 @@ where
{
deserializer.deserialize_any(ListOfBytesListVisitor)
}

#[cfg(test)]
mod test {
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(transparent)]
struct Wrapper {
#[serde(with = "super")]
val: Vec<Vec<u8>>,
}

#[test]
fn encoding() {
assert_eq!(
&serde_json::to_string(&Wrapper { val: vec![vec![0]] }).unwrap(),
"[\"0x00\"]"
);
assert_eq!(
&serde_json::to_string(&Wrapper {
val: vec![vec![0, 1, 2]]
})
.unwrap(),
"[\"0x000102\"]"
);
assert_eq!(
&serde_json::to_string(&Wrapper {
val: vec![vec![0], vec![0, 1, 2]]
})
.unwrap(),
"[\"0x00\",\"0x000102\"]"
);
}

#[test]
fn decoding() {
assert_eq!(
serde_json::from_str::<Wrapper>("[\"0x00\"]").unwrap(),
Wrapper { val: vec![vec![0]] },
);
assert_eq!(
serde_json::from_str::<Wrapper>("[\"0x000102\"]").unwrap(),
Wrapper {
val: vec![vec![0, 1, 2]]
},
);
assert_eq!(
serde_json::from_str::<Wrapper>("[\"0x00\",\"0x000102\"]").unwrap(),
Wrapper {
val: vec![vec![0], vec![0, 1, 2]]
},
);
}
}
50 changes: 50 additions & 0 deletions src/u32_hex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,53 @@ where
{
bytes_4_hex::deserialize(deserializer).map(u32::from_le_bytes)
}

#[cfg(test)]
pub mod test {
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(transparent)]
struct Wrapper {
#[serde(with = "super")]
val: u32,
}

#[test]
fn encoding() {
assert_eq!(
&serde_json::to_string(&Wrapper { val: 0 }).unwrap(),
"\"0x00000000\""
);
assert_eq!(
&serde_json::to_string(&Wrapper { val: 5 }).unwrap(),
"\"0x05000000\""
);

assert_eq!(
&serde_json::to_string(&Wrapper { val: u32::MAX }).unwrap(),
"\"0xffffffff\""
);
}

#[test]
fn decoding() {
assert_eq!(
serde_json::from_str::<Wrapper>("\"0x00000000\"").unwrap(),
Wrapper { val: 0 },
);
assert_eq!(
serde_json::from_str::<Wrapper>("\"0x05000000\"").unwrap(),
Wrapper { val: 5 },
);
assert_eq!(
serde_json::from_str::<Wrapper>("\"0xffffffff\"").unwrap(),
Wrapper { val: u32::MAX },
);

// Wrong length.
serde_json::from_str::<Wrapper>("\"0xfffffffff\"").unwrap_err();
// Requires 0x.
serde_json::from_str::<Wrapper>("\"00000000\"").unwrap_err();
}
}
Loading

0 comments on commit 2eec6b5

Please sign in to comment.