Skip to main content

marine_test_macro/
lib.rs

1/*
2 * Copyright 2020 Fluence Labs Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#![doc(html_root_url = "https://docs.rs/marine-test-macro/0.8.1")]
18#![deny(
19    dead_code,
20    nonstandard_style,
21    unused_imports,
22    unused_mut,
23    unused_variables,
24    unused_unsafe,
25    unreachable_patterns
26)]
27#![feature(proc_macro_span)]
28#![warn(rust_2018_idioms)]
29#![recursion_limit = "1024"]
30
31use marine_test_macro_impl::marine_test_impl;
32use proc_macro::TokenStream;
33use proc_macro_error::proc_macro_error;
34use syn::spanned::Spanned;
35
36/// This macro allows user to write tests for services in the following form:
37///```rust
38/// #[marine_test(config = "/path/to/Config.toml", modules_dir = "path/to/service/modules")]
39/// fn test(greeting: marine_test_env::greeting::ModuleInterface) {
40///     let service_result = greeting.greeting("John".to_string());
41///     assert_eq!(&service_result, "Hi, name!");
42/// }
43///```
44#[proc_macro_error]
45#[proc_macro_attribute]
46pub fn marine_test(attrs: TokenStream, input: TokenStream) -> TokenStream {
47    let attrs: proc_macro2::TokenStream = attrs.into();
48    let attrs_span = attrs.span();
49    // here it obtains a path to the current file where macro is applied
50    let mut file_path = proc_macro::Span::call_site().source_file().path();
51    let _ = file_path.pop();
52
53    match marine_test_impl(attrs, input.into(), file_path) {
54        Ok(stream) => stream.into(),
55        Err(e) => proc_macro_error::abort!(attrs_span, format!("{}", e)),
56    }
57}
58
59// deprecated macro for backwards compatibility
60#[deprecated(since = "0.6.2", note = "please use the #[marine] macro instead")]
61#[proc_macro_error]
62#[proc_macro_attribute]
63pub fn fce_test(attrs: TokenStream, input: TokenStream) -> TokenStream {
64    let attrs: proc_macro2::TokenStream = attrs.into();
65    let attrs_span = attrs.span();
66    // here it obtains a path to the current file where macro is applied
67    let mut file_path = proc_macro::Span::call_site().source_file().path();
68    let _ = file_path.pop();
69
70    match marine_test_impl(attrs, input.into(), file_path) {
71        Ok(stream) => stream.into(),
72        Err(e) => proc_macro_error::abort!(attrs_span, format!("{}", e)),
73    }
74}