Skip to content

Commit

Permalink
Add code coverage step to CI and add more tests (#15)
Browse files Browse the repository at this point in the history
* add code coverage ci, and improve code coverage in general

* add testsuite changes

* added json test coverage

* Fmt
  • Loading branch information
eserilev authored Sep 5, 2024
1 parent ce30c93 commit 2be3515
Show file tree
Hide file tree
Showing 10 changed files with 483 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/test-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,20 @@ jobs:
run: rustup update stable
- name: Run tests
run: cargo test --release
coverage:
runs-on: ubuntu-latest
name: cargo-tarpaulin
steps:
- uses: actions/checkout@v3
- name: Get latest version of stable Rust
run: rustup update stable
- name: Install cargo-tarpaulin
uses: taiki-e/install-action@cargo-tarpaulin
- name: Check code coverage with cargo-tarpaulin
run: make coverage
- name: Upload to codecov.io
uses: codecov/codecov-action@v4
with:
fail_ci_if_error: true
token: ${{ secrets.CODECOV_TOKEN }}
informational: true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
/Cargo.lock
/*.xml
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]
},
);
}
}
71 changes: 71 additions & 0 deletions src/json_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,74 @@ where
let json_str = String::deserialize(deserializer)?;
serde_json::from_str(&json_str).map_err(D::Error::custom)
}

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

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Wrapper {
val_1: String,
val_2: u8,
val_3: Vec<u8>,
val_4: Option<u32>,
}

#[test]
fn encoding() {
assert_eq!(
&serde_json::to_string(&Wrapper {
val_1: "Test".to_string(),
val_2: 5,
val_3: vec![9],
val_4: Some(10)
})
.unwrap(),
"{\"val_1\":\"Test\",\"val_2\":5,\"val_3\":[9],\"val_4\":10}"
);
assert_eq!(
&serde_json::to_string(&Wrapper {
val_1: "Test".to_string(),
val_2: 5,
val_3: vec![9],
val_4: None
})
.unwrap(),
"{\"val_1\":\"Test\",\"val_2\":5,\"val_3\":[9],\"val_4\":null}"
);
}

#[test]
fn decoding() {
assert_eq!(
serde_json::from_str::<Wrapper>(
"{\"val_1\":\"Test\",\"val_2\":5,\"val_3\":[9],\"val_4\":10}"
)
.unwrap(),
Wrapper {
val_1: "Test".to_string(),
val_2: 5,
val_3: vec![9],
val_4: Some(10)
},
);
assert_eq!(
serde_json::from_str::<Wrapper>(
"{\"val_1\":\"Test\",\"val_2\":5,\"val_3\":[9],\"val_4\":null}"
)
.unwrap(),
Wrapper {
val_1: "Test".to_string(),
val_2: 5,
val_3: vec![9],
val_4: None
},
);

// Violating type constraints.
serde_json::from_str::<Wrapper>("{\"val_1\":1,\"val_2\":5,\"val_3\":[9],\"val_4\":null}")
.unwrap_err();
serde_json::from_str::<Wrapper>("{\"val_1\":\"Test\",\"val_2\":5,\"val_4\":null}")
.unwrap_err();
}
}
Loading

0 comments on commit 2be3515

Please sign in to comment.