Skip to content

Commit

Permalink
fix(sol-macro): dedup json abi items
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Oct 8, 2023
1 parent 2168ae3 commit 127b459
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 61 deletions.
63 changes: 25 additions & 38 deletions crates/json-abi/tests/abi.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,21 @@
use alloy_json_abi::{AbiItem, EventParam, JsonAbi, Param};
use std::collections::HashMap;

macro_rules! abi_parse_tests {
($( $(#[$attr:meta])* $name:ident($path:literal, $len:literal))*) => {$(
#[test]
$(#[$attr])*
fn $name() {
parse_test(include_str!($path), $len, $path);
}
)*};
}

abi_parse_tests! {
abiencoderv2("abi/Abiencoderv2Test.json", 1)
#[cfg_attr(miri, ignore = "takes too long")]
console("abi/console.json", 379)
event_with_struct("abi/EventWithStruct.json", 1)
large_array("abi/LargeArray.json", 1)
large_struct("abi/LargeStruct.json", 1)
#[cfg_attr(miri, ignore = "takes too long")]
large_structs("abi/LargeStructs.json", 4)
large_tuple("abi/LargeTuple.json", 1)
#[cfg_attr(miri, ignore = "takes too long")]
seaport("abi/Seaport.json", 69)
udvts("abi/Udvts.json", 1)
}

#[allow(unused_variables)]
fn load_test(path: &str, abi: &JsonAbi) {
#[cfg(all(feature = "std", feature = "serde_json", not(miri)))]
{
use std::{fs::File, io::BufReader};
let file_path: String = format!("tests/{path}");
let file: File = File::open(file_path).unwrap();
let buffer: BufReader<File> = BufReader::new(file);
let loaded_abi: JsonAbi = JsonAbi::load(buffer).unwrap();

assert_eq!(*abi, loaded_abi);
#[test]
fn abi() {
for file in std::fs::read_dir("../../json-abi/tests/abi").unwrap() {
let path = file.unwrap().path();
assert_eq!(path.extension(), Some("json".as_ref()));
parse_test(
&std::fs::read_to_string(&path).unwrap(),
path.to_str().unwrap(),
);
}
}

fn parse_test(s: &str, len: usize, path: &str) {
fn parse_test(s: &str, path: &str) {
let abi_items: Vec<AbiItem<'_>> = serde_json::from_str(s).unwrap();
assert_eq!(abi_items.len(), len);
let len = abi_items.len();

let json: String = serde_json::to_string(&abi_items).unwrap();
let abi1: JsonAbi = serde_json::from_str(&json).unwrap();
Expand All @@ -66,6 +39,20 @@ fn parse_test(s: &str, len: usize, path: &str) {
iterator_test(abi1.clone().into_items(), abi1.into_items().rev(), len);
}

#[allow(unused_variables)]
fn load_test(path: &str, abi: &JsonAbi) {
#[cfg(all(feature = "std", feature = "serde_json", not(miri)))]
{
use std::{fs::File, io::BufReader};
let file_path: String = format!("tests/{path}");
let file: File = File::open(file_path).unwrap();
let buffer: BufReader<File> = BufReader::new(file);
let loaded_abi: JsonAbi = JsonAbi::load(buffer).unwrap();

assert_eq!(*abi, loaded_abi);
}
}

fn iterator_test<T, I, R>(items: I, rev: R, len: usize)
where
T: PartialEq + std::fmt::Debug,
Expand Down
Loading

0 comments on commit 127b459

Please sign in to comment.