Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add json serialization test
  • Loading branch information
ascjones committed Aug 5, 2020
commit 0f80cd5822072e2b522f5c56106253b3f37e2668
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ documentation = "https://docs.rs/seal-contract-metadata"
homepage = "https://www.substrate.io/"
description = "Library defining metadata for SEAL smart contracts on substrate"
keywords = ["parity", "blockchain"]
include = ["Cargo.toml", "*.rs", "README.md", "LICENSE"]
include = ["Cargo.toml", "*.rs", "LICENSE"]

[lib]
path = "lib.rs"
Expand All @@ -21,3 +21,6 @@ semver = { version = "0.10.0", features = ["serde"] }
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde_json = "1.0"
url = { version = "2.1.1", features = ["serde"] }

[dev-dependencies]
pretty_assertions = "0.6.1"
1 change: 0 additions & 1 deletion metadata/README.md

This file was deleted.

84 changes: 83 additions & 1 deletion metadata/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct ContractMetadata {
contract: Contract,
#[serde(skip_serializing_if = "Option::is_none")]
user: Option<User>,
/// Raw JSON of the contract abi metadata
/// Raw JSON of the contract abi metadata, generated during contract compilation.
#[serde(flatten)]
abi: Map<String, Value>,
}
Expand Down Expand Up @@ -239,3 +239,85 @@ where
}
serializer.serialize_str(&hex)
}

#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
use serde_json::json;

#[test]
fn json_with_optional_fields() {
let language = SourceLanguage::new(Language::Ink, Version::new(2, 1, 0));
let compiler = SourceCompiler::new(Compiler::RustC, Version::parse("1.46.0-nightly").unwrap());
let source = Source::new([0u8; 32], language, compiler);
let contract = Contract::new(
"incrementer".to_string(),
Version::new(2, 1, 0),
vec!["Parity Technologies <admin@parity.io>".to_string()],
Some("increment a value".to_string()),
Some(Url::parse("http://docs.rs/").unwrap()),
Some(Url::parse("http://github.com/paritytech/ink/").unwrap()),
Some(Url::parse("http://example.com/").unwrap()),
Some("Apache-2.0".to_string()),
);
let user_json = json! {
{
"more-user-provided-fields": [
"and",
"their",
"values"
],
"some-user-provided-field": "and-its-value"
}
};
let user = User::new(user_json.as_object().unwrap().clone());
let abi_json = json! {
{
"spec": {},
"storage": {},
"types": []
}
}.as_object().unwrap().clone();

let metadata = ContractMetadata::new(source, contract, Some(user), abi_json);
let json = serde_json::to_value(&metadata).unwrap();

let expected = json! {
{
"metadata_version": "0.1.0",
"source": {
"hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"language": "ink! 2.1.0",
"compiler": "rustc 1.46.0-nightly"
},
"contract": {
"name": "incrementer",
"version": "2.1.0",
"authors": [
"Parity Technologies <admin@parity.io>"
],
"description": "increment a value",
"documentation": "http://docs.rs/",
"repository": "http://github.com/paritytech/ink/",
"homepage": "http://example.com/",
"license": "Apache-2.0",
},
"user": {
"more-user-provided-fields": [
"and",
"their",
"values"
],
"some-user-provided-field": "and-its-value"
},
// these fields are part of the flattened raw json for the contract ABI
"spec": {},
"storage": {},
"types": []
}
};

assert_eq!(json, expected);
}
}