git.delta.rocks / unique-network / refs/commits / d535fbacbf44

difftreelog

refactor abi tests

Trubnikov Sergey2022-11-14parent: #00d3414.patch.diff
in: master

3 files changed

modifiedCargo.lockdiffbeforeafterboth
before · Cargo.lock
1023 packageslockfile v3
modifiedcrates/evm-coder/Cargo.tomldiffbeforeafterboth
--- a/crates/evm-coder/Cargo.toml
+++ b/crates/evm-coder/Cargo.toml
@@ -26,7 +26,6 @@
 hex = "0.4.3"
 hex-literal = "0.3.4"
 similar-asserts = "1.4.2"
-concat-idents = "1.1.3"
 trybuild = "1.0"
 
 [features]
modifiedcrates/evm-coder/src/abi/test.rsdiffbeforeafterboth
--- a/crates/evm-coder/src/abi/test.rs
+++ b/crates/evm-coder/src/abi/test.rs
@@ -6,36 +6,25 @@
 use super::{AbiReader, AbiWriter};
 use hex_literal::hex;
 use primitive_types::{H160, U256};
-use concat_idents::concat_idents;
-
-macro_rules! test_impl {
-    ($name:ident, $type:ty, $function_identifier:expr, $decoded_data:expr, $encoded_data:expr) => {
-        concat_idents!(test_name = encode_decode_, $name {
-            #[test]
-            fn test_name() {
-                let function_identifier: u32 = $function_identifier;
-                let decoded_data = $decoded_data;
-                let encoded_data = $encoded_data;
 
-                let (call, mut decoder) = AbiReader::new_call(encoded_data).unwrap();
-                assert_eq!(call, u32::to_be_bytes(function_identifier));
-                let data = <$type>::abi_read(&mut decoder).unwrap();
-                assert_eq!(data, decoded_data);
+fn test_impl<T>(function_identifier: u32, decoded_data: T, encoded_data: &[u8])
+where
+	T: AbiWrite + AbiRead + std::cmp::PartialEq + std::fmt::Debug,
+{
+	let (call, mut decoder) = AbiReader::new_call(encoded_data).unwrap();
+	assert_eq!(call, u32::to_be_bytes(function_identifier));
+	let data = <T>::abi_read(&mut decoder).unwrap();
+	assert_eq!(data, decoded_data);
 
-                let mut writer = AbiWriter::new_call(function_identifier);
-                decoded_data.abi_write(&mut writer);
-                let ed = writer.finish();
-                similar_asserts::assert_eq!(encoded_data, ed.as_slice());
-            }
-        });
-    };
+	let mut writer = AbiWriter::new_call(function_identifier);
+	decoded_data.abi_write(&mut writer);
+	let ed = writer.finish();
+	similar_asserts::assert_eq!(encoded_data, ed.as_slice());
 }
 
 macro_rules! test_impl_uint {
 	($type:ident) => {
-		test_impl!(
-			$type,
-			$type,
+		test_impl::<$type>(
 			0xdeadbeef,
 			255 as $type,
 			&hex!(
@@ -43,100 +32,147 @@
                     deadbeef
                     00000000000000000000000000000000000000000000000000000000000000ff
                 "
-			)
+			),
 		);
 	};
 }
 
-test_impl_uint!(uint8);
-test_impl_uint!(uint32);
-test_impl_uint!(uint128);
+#[test]
+fn encode_decode_uint8() {
+	test_impl_uint!(uint8);
+}
 
-test_impl!(
-	uint256,
-	uint256,
-	0xdeadbeef,
-	U256([255, 0, 0, 0]),
-	&hex!(
-		"
-            deadbeef
-            00000000000000000000000000000000000000000000000000000000000000ff
-        "
-	)
-);
+#[test]
+fn encode_decode_uint32() {
+	test_impl_uint!(uint32);
+}
 
-test_impl!(
-    vec_tuple_address_uint256,
-    Vec<(address, uint256)>,
-    0x1ACF2D55,
-    vec![
-        (
-            H160(hex!("2D2FF76104B7BACB2E8F6731D5BFC184EBECDDBC")),
-            U256([10, 0, 0, 0]),
-        ),
-        (
-            H160(hex!("AB8E3D9134955566483B11E6825C9223B6737B10")),
-            U256([20, 0, 0, 0]),
-        ),
-        (
-            H160(hex!("8C582BDF2953046705FC56F189385255EFC1BE18")),
-            U256([30, 0, 0, 0]),
-        ),
-    ],
-    &hex!(
-        "
-            1ACF2D55
-            0000000000000000000000000000000000000000000000000000000000000020 // offset of (address, uint256)[]
-            0000000000000000000000000000000000000000000000000000000000000003 // length of (address, uint256)[]
+#[test]
+fn encode_decode_uint128() {
+	test_impl_uint!(uint128);
+}
 
-            0000000000000000000000002D2FF76104B7BACB2E8F6731D5BFC184EBECDDBC // address
-            000000000000000000000000000000000000000000000000000000000000000A // uint256
+#[test]
+fn encode_decode_uint256() {
+	test_impl::<uint256>(
+		0xdeadbeef,
+		U256([255, 0, 0, 0]),
+		&hex!(
+			"
+                deadbeef
+                00000000000000000000000000000000000000000000000000000000000000ff
+            "
+		),
+	);
+}
 
-            000000000000000000000000AB8E3D9134955566483B11E6825C9223B6737B10 // address
-            0000000000000000000000000000000000000000000000000000000000000014 // uint256
+#[test]
+fn encode_decode_string() {
+	test_impl::<String>(
+		0xdeadbeef,
+		"some string".to_string(),
+		&hex!(
+			"
+                deadbeef
+                0000000000000000000000000000000000000000000000000000000000000020
+                000000000000000000000000000000000000000000000000000000000000000b
+                736f6d6520737472696e67000000000000000000000000000000000000000000
+            "
+		),
+	);
+}
 
-            0000000000000000000000008C582BDF2953046705FC56F189385255EFC1BE18 // address
-            000000000000000000000000000000000000000000000000000000000000001E // uint256
-        "
-    )
-);
+#[test]
+fn encode_decode_tuple_string() {
+	test_impl::<(String,)>(
+		0xdeadbeef,
+		("some string".to_string(),),
+		&hex!(
+			"
+                deadbeef
+                0000000000000000000000000000000000000000000000000000000000000020
+                0000000000000000000000000000000000000000000000000000000000000020
+                000000000000000000000000000000000000000000000000000000000000000b
+                736f6d6520737472696e67000000000000000000000000000000000000000000
+            "
+		),
+	);
+}
 
-test_impl!(
-    vec_tuple_uint256_string,
-    Vec<(uint256, string)>,
-    0xdeadbeef,
-    vec![
-        (1.into(), "Test URI 0".to_string()),
-        (11.into(), "Test URI 1".to_string()),
-        (12.into(), "Test URI 2".to_string()),
-    ],
-    &hex!(
-        "
-            deadbeef
-            0000000000000000000000000000000000000000000000000000000000000020 // offset of (uint256, string)[]
-            0000000000000000000000000000000000000000000000000000000000000003 // length of (uint256, string)[]
+#[test]
+fn encode_decode_vec_tuple_address_uint256() {
+	test_impl::<Vec<(address, uint256)>>(
+        0x1ACF2D55,
+        vec![
+            (
+                H160(hex!("2D2FF76104B7BACB2E8F6731D5BFC184EBECDDBC")),
+                U256([10, 0, 0, 0]),
+            ),
+            (
+                H160(hex!("AB8E3D9134955566483B11E6825C9223B6737B10")),
+                U256([20, 0, 0, 0]),
+            ),
+            (
+                H160(hex!("8C582BDF2953046705FC56F189385255EFC1BE18")),
+                U256([30, 0, 0, 0]),
+            ),
+        ],
+        &hex!(
+            "
+                1ACF2D55
+                0000000000000000000000000000000000000000000000000000000000000020 // offset of (address, uint256)[]
+                0000000000000000000000000000000000000000000000000000000000000003 // length of (address, uint256)[]
 
-            0000000000000000000000000000000000000000000000000000000000000060 // offset of first elem
-            00000000000000000000000000000000000000000000000000000000000000e0 // offset of second elem
-            0000000000000000000000000000000000000000000000000000000000000160 // offset of third elem
+                0000000000000000000000002D2FF76104B7BACB2E8F6731D5BFC184EBECDDBC // address
+                000000000000000000000000000000000000000000000000000000000000000A // uint256
 
-            0000000000000000000000000000000000000000000000000000000000000001 // first token id?   					#60
-            0000000000000000000000000000000000000000000000000000000000000040 // offset of string
-            000000000000000000000000000000000000000000000000000000000000000a // size of string
-            5465737420555249203000000000000000000000000000000000000000000000 // string
+                000000000000000000000000AB8E3D9134955566483B11E6825C9223B6737B10 // address
+                0000000000000000000000000000000000000000000000000000000000000014 // uint256
 
-            000000000000000000000000000000000000000000000000000000000000000b // second token id? Why ==11?			#e0
-            0000000000000000000000000000000000000000000000000000000000000040 // offset of string
-            000000000000000000000000000000000000000000000000000000000000000a // size of string
-            5465737420555249203100000000000000000000000000000000000000000000 // string
+                0000000000000000000000008C582BDF2953046705FC56F189385255EFC1BE18 // address
+                000000000000000000000000000000000000000000000000000000000000001E // uint256
+            "
+        )
+    );
+}
+
+#[test]
+fn encode_decode_vec_tuple_uint256_string() {
+	test_impl::<Vec<(uint256, string)>>(
+        0xdeadbeef,
+        vec![
+            (1.into(), "Test URI 0".to_string()),
+            (11.into(), "Test URI 1".to_string()),
+            (12.into(), "Test URI 2".to_string()),
+        ],
+        &hex!(
+            "
+                deadbeef
+                0000000000000000000000000000000000000000000000000000000000000020 // offset of (uint256, string)[]
+                0000000000000000000000000000000000000000000000000000000000000003 // length of (uint256, string)[]
+
+                0000000000000000000000000000000000000000000000000000000000000060 // offset of first elem
+                00000000000000000000000000000000000000000000000000000000000000e0 // offset of second elem
+                0000000000000000000000000000000000000000000000000000000000000160 // offset of third elem
+
+                0000000000000000000000000000000000000000000000000000000000000001 // first token id?   					#60
+                0000000000000000000000000000000000000000000000000000000000000040 // offset of string
+                000000000000000000000000000000000000000000000000000000000000000a // size of string
+                5465737420555249203000000000000000000000000000000000000000000000 // string
+
+                000000000000000000000000000000000000000000000000000000000000000b // second token id? Why ==11?			#e0
+                0000000000000000000000000000000000000000000000000000000000000040 // offset of string
+                000000000000000000000000000000000000000000000000000000000000000a // size of string
+                5465737420555249203100000000000000000000000000000000000000000000 // string
 
-            000000000000000000000000000000000000000000000000000000000000000c // third token id?  Why ==12?			#160
-            0000000000000000000000000000000000000000000000000000000000000040 // offset of string
-            000000000000000000000000000000000000000000000000000000000000000a // size of string
-            5465737420555249203200000000000000000000000000000000000000000000 // string
-        "
-    )
-);
+                000000000000000000000000000000000000000000000000000000000000000c // third token id?  Why ==12?			#160
+                0000000000000000000000000000000000000000000000000000000000000040 // offset of string
+                000000000000000000000000000000000000000000000000000000000000000a // size of string
+                5465737420555249203200000000000000000000000000000000000000000000 // string
+            "
+        )
+    );
+}
 
 #[test]
 fn dynamic_after_static() {
@@ -235,63 +271,64 @@
 	similar_asserts::assert_eq!(encoded_data, ed.as_slice());
 }
 
-test_impl!(
-	vec_tuple_string_bytes,
-	Vec<(string, bytes)>,
-	0xdeadbeef,
-	vec![
-		(
-			"Test URI 0".to_string(),
-			bytes(vec![
-				0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
-				0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
-				0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
-				0x11, 0x11, 0x11, 0x11, 0x11, 0x11
-			])
-		),
-		(
-			"Test URI 1".to_string(),
-			bytes(vec![
-				0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
-				0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
-				0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
-				0x22, 0x22, 0x22, 0x22, 0x22
-			])
-		),
-		("Test URI 2".to_string(), bytes(vec![0x33, 0x33])),
-	],
-	&hex!(
-		"
-            deadbeef
-            0000000000000000000000000000000000000000000000000000000000000020
-            0000000000000000000000000000000000000000000000000000000000000003
-            
-            0000000000000000000000000000000000000000000000000000000000000060
-            0000000000000000000000000000000000000000000000000000000000000140
-            0000000000000000000000000000000000000000000000000000000000000220
+#[test]
+fn encode_decode_vec_tuple_string_bytes() {
+	test_impl::<Vec<(string, bytes)>>(
+		0xdeadbeef,
+		vec![
+			(
+				"Test URI 0".to_string(),
+				bytes(vec![
+					0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
+					0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
+					0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
+					0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
+				]),
+			),
+			(
+				"Test URI 1".to_string(),
+				bytes(vec![
+					0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
+					0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
+					0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
+					0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
+				]),
+			),
+			("Test URI 2".to_string(), bytes(vec![0x33, 0x33])),
+		],
+		&hex!(
+			"
+                deadbeef
+                0000000000000000000000000000000000000000000000000000000000000020
+                0000000000000000000000000000000000000000000000000000000000000003
 
-            0000000000000000000000000000000000000000000000000000000000000040
-            0000000000000000000000000000000000000000000000000000000000000080
-            000000000000000000000000000000000000000000000000000000000000000a
-            5465737420555249203000000000000000000000000000000000000000000000
-            0000000000000000000000000000000000000000000000000000000000000030
-            1111111111111111111111111111111111111111111111111111111111111111
-            1111111111111111111111111111111100000000000000000000000000000000
+                0000000000000000000000000000000000000000000000000000000000000060
+                0000000000000000000000000000000000000000000000000000000000000140
+                0000000000000000000000000000000000000000000000000000000000000220
+
+                0000000000000000000000000000000000000000000000000000000000000040
+                0000000000000000000000000000000000000000000000000000000000000080
+                000000000000000000000000000000000000000000000000000000000000000a
+                5465737420555249203000000000000000000000000000000000000000000000
+                0000000000000000000000000000000000000000000000000000000000000030
+                1111111111111111111111111111111111111111111111111111111111111111
+                1111111111111111111111111111111100000000000000000000000000000000
 
-            0000000000000000000000000000000000000000000000000000000000000040
-            0000000000000000000000000000000000000000000000000000000000000080
-            000000000000000000000000000000000000000000000000000000000000000a
-            5465737420555249203100000000000000000000000000000000000000000000
-            000000000000000000000000000000000000000000000000000000000000002f
-            2222222222222222222222222222222222222222222222222222222222222222
-            2222222222222222222222222222220000000000000000000000000000000000
+                0000000000000000000000000000000000000000000000000000000000000040
+                0000000000000000000000000000000000000000000000000000000000000080
+                000000000000000000000000000000000000000000000000000000000000000a
+                5465737420555249203100000000000000000000000000000000000000000000
+                000000000000000000000000000000000000000000000000000000000000002f
+                2222222222222222222222222222222222222222222222222222222222222222
+                2222222222222222222222222222220000000000000000000000000000000000
 
-            0000000000000000000000000000000000000000000000000000000000000040
-            0000000000000000000000000000000000000000000000000000000000000080
-            000000000000000000000000000000000000000000000000000000000000000a
-            5465737420555249203200000000000000000000000000000000000000000000
-            0000000000000000000000000000000000000000000000000000000000000002
-            3333000000000000000000000000000000000000000000000000000000000000
-        "
-	)
-);
+                0000000000000000000000000000000000000000000000000000000000000040
+                0000000000000000000000000000000000000000000000000000000000000080
+                000000000000000000000000000000000000000000000000000000000000000a
+                5465737420555249203200000000000000000000000000000000000000000000
+                0000000000000000000000000000000000000000000000000000000000000002
+                3333000000000000000000000000000000000000000000000000000000000000
+            "
+		),
+	);
+}