Skip to content

Commit

Permalink
feat(rust): finalize Rust testing template
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Lubinets committed Apr 1, 2019
1 parent 6eb5566 commit b450a81
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ const compileTemplate = async (name: string, schema: types.OpenRPC, language: st
const template = language === "rust" ? rsTemplate : jsTemplate;
return template({
className: name,
getParamTypings: (method: types.MethodObject, typeDefs: IMethodTypingsMap): string[] => {
getParams: (method: types.MethodObject, typeDefs: IMethodTypingsMap) => {
return map(
method.params as types.ContentDescriptorObject[],
(param) => `${typeDefs[generateMethodParamId(method, param)].typeName}`,
(param) => [param.name, `${typeDefs[generateMethodParamId(method, param)].typeName}`],
);
},
generateMethodParamId,
Expand Down
2 changes: 1 addition & 1 deletion templates/rs/static/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

[dev-dependencies]
autorand = "0.1"
autorand = { version = "0.2", features = ["json", "json-value-always-null"] }
futures = "0.1.25"
failure = "0.1.5"
6 changes: 3 additions & 3 deletions templates/rs/static/src/test_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use serde_json::Value;
use serde::{Serialize, Deserialize};

pub struct MockTransport {
method: String,
params: Vec<Value>,
result: Value,
pub method: String,
pub params: Vec<Value>,
pub result: Value,
}

impl MockTransport {
Expand Down
22 changes: 14 additions & 8 deletions templates/rs/templated/client.template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ jsonrpc_client!(pub struct <%= className %> {
mod tests {
use super::*;
use test_harness::*;
use serde::{Serialize, Deserialize};
use autorand::Random;
use futures::Future;
<% methods.forEach((method) => { %>
#[test]
Expand All @@ -37,9 +37,10 @@ mod tests {
//- params query template start
let mut params = Vec::new();
//-- loop over params (name, type) pairs start
<% getParamTypings(method, typeDefs).forEach((paramType) => { %>
let <%= paramType + "_value" %> = <%= paramType + "::random()" %>;
let serialized = serde_json::to_value(&<%= paramType + "_value" %>).unwrap();
<% getParams(method, typeDefs).forEach((param) => { %>
<% const paramName = param[0] + "_value" %>
let <%= paramName %> = <%= param[1] + "::random()" %>;
let serialized = serde_json::to_value(&<%= paramName %>).unwrap();
params.push(serialized);
<% }); %>
//-- loop over params end
Expand All @@ -48,6 +49,9 @@ mod tests {
//- result query template start
<% const resultType = typeDefs[generateMethodResultId(method, method.result)].typeName; %>
let result = <%= resultType + "::random()" %>;
// transcode result to workaround Some(Null) -> Null serialization detail loss
let result_serialized = serde_json::to_vec(&result).unwrap();
let result: <%= resultType %> = serde_json::from_slice(&result_serialized).unwrap();
//- result query template end
let transport = MockTransport {
Expand All @@ -56,15 +60,17 @@ mod tests {
result: serde_json::to_value(&result).unwrap(),
};
let mut client = PetStore::new(transport);
let received_result = client.get_pet(
let mut client = <%= className + "::new(transport);" %>
let received_result = client.<%= method.name %>(
//- loop over params start
<% getParamTypings(method, typeDefs).forEach((paramType) => { %>
<%= paramType + "_value" %>,
<% getParams(method, typeDefs).forEach((param) => { %>
<%= param[0] + "_value" %>,
<% }); %>
//- loop over params end
).wait().unwrap();
// transcode result to workaround Some(Null) -> Null serialization detail loss
let result_s =
assert_eq!(result, received_result);
}
<% }); %>
Expand Down

0 comments on commit b450a81

Please sign in to comment.