Add proc macro for tests

next
Sayan Nandan 4 years ago
parent 913cef85e2
commit 0b901b0492
No known key found for this signature in database
GPG Key ID: C31EFD7DDA12AEE0

13
Cargo.lock generated

@ -553,9 +553,9 @@ checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "syn"
version = "1.0.34"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "936cae2873c940d92e697597c5eee105fb570cd5689c695806f672883653349b"
checksum = "963f7d3cc59b59b9325165add223142bbf1df27655d07789f109896d353d8350"
dependencies = [
"proc-macro2",
"quote",
@ -588,6 +588,15 @@ dependencies = [
"devtimer",
"libtdb",
"rand",
"tdb-derive",
]
[[package]]
name = "tdb-derive"
version = "0.1.0"
dependencies = [
"quote",
"syn",
]
[[package]]

@ -3,7 +3,8 @@ members = [
"cli",
"server",
"libtdb",
"tdb-bench"
"tdb-bench",
"tdb-derive"
]
[profile.release]

@ -0,0 +1,14 @@
[package]
name = "tdb-derive"
version = "0.1.0"
authors = ["Sayan Nandan <nandansayan@outlook.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
proc-macro = true
[dependencies]
quote = "1.0.7"
syn = {version="1.0.40", features=["full"]}

@ -0,0 +1,73 @@
/*
* Created on Sun Sep 13 2020
*
* This file is a part of TerrabaseDB
* Copyright (c) 2020, Sayan Nandan <ohsayan at outlook dot com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
use proc_macro::TokenStream;
use quote::quote;
use syn;
fn parse_toks(mut input: syn::ItemFn) -> Result<TokenStream, syn::Error> {
let sig = &mut input.sig;
let body = &input.block;
let attrs = &input.attrs;
let vis = input.vis;
let header = quote! {
#[::core::prelude::v1::test]
};
let result = quote! {
#header
#(#attrs)*
#vis #sig {
let runtime = service::BackGroundTask::new();
runtime.execute(|| {#body});
drop(runtime);
}
};
Ok(result.into())
}
fn test(_args: TokenStream, item: TokenStream) -> TokenStream {
let input = syn::parse_macro_input!(item as syn::ItemFn);
for attr in &input.attrs {
if attr.path.is_ident("test") {
let msg = "second test attribute is supplied";
return syn::Error::new_spanned(&attr, msg)
.to_compile_error()
.into();
}
}
if !input.sig.inputs.is_empty() {
let msg = "the test function cannot accept arguments";
return syn::Error::new_spanned(&input.sig.inputs, msg)
.to_compile_error()
.into();
}
parse_toks(input).unwrap_or_else(|e| e.to_compile_error().into())
}
#[proc_macro_attribute]
/// Execute the function as a test
/// This function starts the server in the background and terminates it when
/// the test is over. Do note that, at the moment, this expects a `service` module
/// to have a `BackgroundTask` object which should start the background the server
pub fn dbtest(args: TokenStream, item: TokenStream) -> TokenStream {
test(args, item)
}
Loading…
Cancel
Save