|
| 1 | +//! Comprehensive test for contract-level derives applying to all generated types. |
| 2 | +//! Tests all_derives and extra_derives on contracts with events and errors. |
| 3 | +
|
| 4 | +use alloy_primitives::{Address, U256}; |
| 5 | +use alloy_sol_types::sol; |
| 6 | +use std::{collections::HashSet, hash::Hash}; |
| 7 | + |
| 8 | +#[test] |
| 9 | +fn test_all_derives() { |
| 10 | + sol! { |
| 11 | + #[sol(all_derives)] |
| 12 | + contract AllDerivesContract { |
| 13 | + function transfer(address to, uint256 amount) external returns (bool); |
| 14 | + event Transfer(address indexed from, address indexed to, uint256 value); |
| 15 | + error InsufficientBalance(uint256 requested, uint256 available); |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + use AllDerivesContract::*; |
| 20 | + |
| 21 | + let event1 = Transfer { from: Address::ZERO, to: Address::ZERO, value: U256::from(50) }; |
| 22 | + let event2 = Transfer { from: Address::ZERO, to: Address::ZERO, value: U256::from(50) }; |
| 23 | + let events_enum1 = AllDerivesContractEvents::Transfer(event1); |
| 24 | + let events_enum2 = AllDerivesContractEvents::Transfer(event2); |
| 25 | + |
| 26 | + let error1 = InsufficientBalance { requested: U256::from(100), available: U256::from(50) }; |
| 27 | + let error2 = InsufficientBalance { requested: U256::from(100), available: U256::from(50) }; |
| 28 | + let errors_enum1 = AllDerivesContractErrors::InsufficientBalance(error1); |
| 29 | + let errors_enum2 = AllDerivesContractErrors::InsufficientBalance(error2); |
| 30 | + |
| 31 | + // Test PartialEq and Debug |
| 32 | + assert_eq!(errors_enum1, errors_enum2); |
| 33 | + assert_eq!(events_enum1, events_enum2); |
| 34 | + |
| 35 | + // Test Hash and Eq derives |
| 36 | + let mut events_set = HashSet::new(); |
| 37 | + events_set.insert(events_enum1); |
| 38 | + events_set.insert(events_enum2); |
| 39 | + // Should not increase size since they're equal |
| 40 | + assert_eq!(events_set.len(), 1); |
| 41 | + |
| 42 | + let mut errors_set = HashSet::new(); |
| 43 | + errors_set.insert(errors_enum1); |
| 44 | + errors_set.insert(errors_enum2); |
| 45 | + // Should not increase size since they're equal |
| 46 | + assert_eq!(errors_set.len(), 1); |
| 47 | +} |
| 48 | + |
| 49 | +#[test] |
| 50 | +fn test_extra_derives() { |
| 51 | + sol! { |
| 52 | + #[sol(extra_derives(PartialEq, Eq, Hash, Debug))] |
| 53 | + contract ExtraDerivesContract { |
| 54 | + function transfer(address to, uint256 amount) external returns (bool); |
| 55 | + event Transfer(address indexed from, address indexed to, uint256 value); |
| 56 | + error InsufficientBalance(uint256 requested, uint256 available); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + use ExtraDerivesContract::*; |
| 61 | + |
| 62 | + let event1 = Transfer { from: Address::ZERO, to: Address::ZERO, value: U256::from(50) }; |
| 63 | + let event2 = Transfer { from: Address::ZERO, to: Address::ZERO, value: U256::from(50) }; |
| 64 | + let events_enum1 = ExtraDerivesContractEvents::Transfer(event1); |
| 65 | + let events_enum2 = ExtraDerivesContractEvents::Transfer(event2); |
| 66 | + |
| 67 | + let error1 = InsufficientBalance { requested: U256::from(100), available: U256::from(50) }; |
| 68 | + let error2 = InsufficientBalance { requested: U256::from(100), available: U256::from(50) }; |
| 69 | + let errors_enum1 = ExtraDerivesContractErrors::InsufficientBalance(error1); |
| 70 | + let errors_enum2 = ExtraDerivesContractErrors::InsufficientBalance(error2); |
| 71 | + |
| 72 | + // Test PartialEq and Debug |
| 73 | + assert_eq!(errors_enum1, errors_enum2); |
| 74 | + assert_eq!(events_enum1, events_enum2); |
| 75 | + |
| 76 | + // Test Hash and Eq derives |
| 77 | + let mut events_set = HashSet::new(); |
| 78 | + events_set.insert(events_enum1); |
| 79 | + events_set.insert(events_enum2); |
| 80 | + // Should not increase size since they're equal |
| 81 | + assert_eq!(events_set.len(), 1); |
| 82 | + |
| 83 | + let mut errors_set = HashSet::new(); |
| 84 | + errors_set.insert(errors_enum1); |
| 85 | + errors_set.insert(errors_enum2); |
| 86 | + // Should not increase size since they're equal |
| 87 | + assert_eq!(errors_set.len(), 1); |
| 88 | +} |
0 commit comments