refine tests for call deserialization

This commit is contained in:
Robert Habermeier 2016-05-24 12:21:40 +02:00
parent 72031d6f95
commit 56b020987e
3 changed files with 44 additions and 4 deletions

View File

@ -19,6 +19,7 @@
use rustc_serialize::hex::FromHex;
use serde::{Deserialize, Deserializer, Error};
use serde::de::Visitor;
use std::ops::Deref;
/// Lenient bytes json deserialization for test json files.
#[derive(Default, Debug, PartialEq, Clone)]
@ -30,6 +31,14 @@ impl Into<Vec<u8>> for Bytes {
}
}
impl Deref for Bytes {
type Target = Vec<u8>;
fn deref(&self) -> &Vec<u8> {
&self.0
}
}
impl Deserialize for Bytes {
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
where D: Deserializer {

View File

@ -39,16 +39,47 @@ pub struct Call {
mod tests {
use serde_json;
use vm::Call;
use util::numbers::U256;
use uint::Uint;
use util::hash::Address as Hash160;
use hash::Address;
use maybe::MaybeEmpty;
use std::str::FromStr;
#[test]
fn call_deserialization() {
fn call_deserialization_empty_dest() {
let s = r#"{
"data" : "0x1111222233334444555566667777888899990000aaaabbbbccccddddeeeeffff",
"destination" : "",
"gasLimit" : "0x1748766aa5",
"value" : "0x00"
}"#;
let _deserialized: Call = serde_json::from_str(s).unwrap();
// TODO: validate all fields
let call: Call = serde_json::from_str(s).unwrap();
assert_eq!(&call.data[..],
&[0x11, 0x11, 0x22, 0x22, 0x33, 0x33, 0x44, 0x44, 0x55, 0x55, 0x66, 0x66, 0x77, 0x77,
0x88, 0x88, 0x99, 0x99, 0x00, 0x00, 0xaa, 0xaa, 0xbb, 0xbb, 0xcc, 0xcc, 0xdd, 0xdd,
0xee, 0xee, 0xff, 0xff]);
assert_eq!(call.destination, MaybeEmpty::None);
assert_eq!(call.gas_limit, Uint(U256::from(0x1748766aa5u64)));
assert_eq!(call.value, Uint(U256::from(0)));
}
#[test]
fn call_deserialization_full_dest() {
let s = r#"{
"data" : "0x1234",
"destination" : "5a39ed1020c04d4d84539975b893a4e7c53eab6c",
"gasLimit" : "0x1748766aa5",
"value" : "0x00"
}"#;
let call: Call = serde_json::from_str(s).unwrap();
assert_eq!(&call.data[..], &[0x12, 0x34]);
assert_eq!(call.destination, MaybeEmpty::Some(Address(Hash160::from_str("5a39ed1020c04d4d84539975b893a4e7c53eab6c").unwrap())));
assert_eq!(call.gas_limit, Uint(U256::from(0x1748766aa5u64)));
assert_eq!(call.value, Uint(U256::from(0)));
}
}

View File

@ -22,7 +22,7 @@ use std::sync::{Arc, Weak};
use std::ops::Deref;
use std::collections::BTreeMap;
use jsonrpc_core::*;
use ethminer::{MinerService};
use ethminer::MinerService;
use v1::traits::Ethcore;
use v1::types::Bytes;