Unverified Commit f08d790b authored by Robin Freyler's avatar Robin Freyler
Browse files

[lang] Add test framework and add noop contract test

parent 2aca9167
......@@ -37,6 +37,9 @@ mod hir;
mod ident_ext;
mod parser;
#[cfg(test)]
mod tests;
use errors::Result;
/// Simple wrapper from `proc_macro` to `proc_macro2` and back again.
......@@ -52,16 +55,10 @@ fn contract_gen_impl(input: proc_macro::TokenStream) -> Result<proc_macro::Token
/// Parses the given token stream as pDSL contract, performs some checks and returns
/// the corresponding contract as token stream.
fn contract_gen_impl2(input: proc_macro2::TokenStream) -> Result<proc_macro2::TokenStream> {
pub(crate) fn contract_gen_impl2(input: proc_macro2::TokenStream) -> Result<proc_macro2::TokenStream> {
let ast_contract = parser::parse_contract(input.clone())?;
let hir_contract = hir::Contract::from_ast(&ast_contract)?;
// gen::gir::generate(&hir_program)?;
let tokens = gen::codegen(&hir_contract);
Ok(tokens.into())
}
#[test]
fn empty_contract_input() {
use quote::quote;
assert!(contract_gen_impl2(quote!{}).is_err());
}
mod utils;
mod noop;
pub(crate) use quote::quote;
pub(crate) use utils::assert_eq_tokenstreams;
pub(crate) use crate::contract_gen_impl2;
#[test]
fn empty_contract_input() {
assert!(contract_gen_impl2(quote!{}).is_err());
}
use super::*;
#[test]
fn noop_contract() {
assert_eq_tokenstreams(
quote!{
/// The contract that does nothing.
///
/// # Note
///
/// Can be deployed, cannot be called.
struct Noop {}
impl Deploy for Noop {
/// Does nothing to initialize itself.
fn deploy(&mut self) {}
}
/// Provides no way to call it as extrinsic.
impl Noop {}
},
quote!{
pdsl_model::state! {
/// The contract that does nothing.
///
/// # Note
///
/// Can be deployed, cannot be called.
struct Noop {}
}
use pdsl_model::messages;
pdsl_model::messages! {}
impl Noop {
/// Does nothing to initialize itself.
pub fn deploy(&mut self, env: &mut pdsl_model::EnvHandler) { }
}
impl Noop {}
use pdsl_model::Contract;
fn instantiate() -> impl pdsl_model::Contract {
pdsl_model::ContractDecl::using::<Noop>()
.on_deploy(|env, ()| {
let (handler, state) = env.split_mut();
state.deploy(handler,)
})
.instantiate()
}
#[no_mangle] fn deploy() { instantiate().deploy() }
#[no_mangle] fn call() { instantiate().dispatch() }
}
)
}
use crate::contract_gen_impl2;
pub fn assert_eq_tokenstreams(
input: proc_macro2::TokenStream,
expected: proc_macro2::TokenStream,
) {
assert_eq!(
contract_gen_impl2(input)
.map(|result| result.to_string())
.map_err(|err| err.to_string()),
Ok(expected.to_string())
)
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment