-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from str4d/53-test-harness
Test harness
- Loading branch information
Showing
19 changed files
with
924 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/usr/bin/env python3 | ||
# Helper script for running binaries on a connected Flipper Zero. | ||
|
||
import argparse | ||
import os | ||
import sys | ||
from pathlib import Path | ||
from subprocess import run | ||
|
||
TOOLS_PATH = '../tools' | ||
|
||
|
||
def parse_args(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('binary', type=Path) | ||
parser.add_argument('arguments', nargs=argparse.REMAINDER) | ||
return parser.parse_args() | ||
|
||
|
||
def main(): | ||
args = parse_args() | ||
|
||
# Run the given FAP binary on a connected Flipper Zero. | ||
result = run( | ||
[ | ||
'cargo', | ||
'run', | ||
'--quiet', | ||
'--release', | ||
'--bin', | ||
'run-fap', | ||
'--', | ||
os.fspath(args.binary), | ||
] + args.arguments, | ||
cwd=os.path.join(os.path.dirname(__file__), TOOLS_PATH), | ||
) | ||
if result.returncode: | ||
sys.exit(result.returncode) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#![no_std] | ||
#![no_main] | ||
|
||
#[flipperzero_test::tests] | ||
mod tests { | ||
use flipperzero::dolphin::Dolphin; | ||
|
||
#[test] | ||
fn stats() { | ||
let mut dolphin = Dolphin::open(); | ||
let stats = dolphin.stats(); | ||
assert!(stats.level >= 1); | ||
} | ||
} | ||
|
||
flipperzero_test::tests_runner!(name = "Dolphin Integration Test", [crate::tests]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[package] | ||
name = "flipperzero-test" | ||
version = "0.1.0" | ||
repository.workspace = true | ||
readme.workspace = true | ||
license.workspace = true | ||
edition.workspace = true | ||
rust-version.workspace = true | ||
autobins = false | ||
autotests = false | ||
autobenches = false | ||
|
||
[dependencies] | ||
flipperzero-sys.workspace = true | ||
flipperzero-test-macros = { version = "=0.1.0", path = "macros" } | ||
ufmt.workspace = true | ||
|
||
[lib] | ||
bench = false | ||
test = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "flipperzero-test-macros" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
proc-macro2 = "1" | ||
quote = "1" | ||
syn = { version = "1", features = ["full"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
use quote::quote; | ||
use syn::{parse, Block, Expr, ExprMacro, ExprTuple, Stmt}; | ||
|
||
/// Find and replace macro assertions inside the given block with `return Err(..)`. | ||
/// | ||
/// The following assertion macros are replaced: | ||
/// - [`assert`] | ||
/// - [`assert_eq`] | ||
/// - [`assert_ne`] | ||
pub(crate) fn box_block(mut block: Box<Block>) -> parse::Result<Box<Block>> { | ||
block.stmts = block_stmts(block.stmts)?; | ||
Ok(block) | ||
} | ||
|
||
/// Searches recursively through block statements to find and replace macro assertions | ||
/// with `return Err(..)`. | ||
/// | ||
/// The following assertion macros are replaced: | ||
/// - [`assert`] | ||
/// - [`assert_eq`] | ||
/// - [`assert_ne`] | ||
fn block_stmts(stmts: Vec<Stmt>) -> parse::Result<Vec<Stmt>> { | ||
stmts | ||
.into_iter() | ||
.map(|stmt| match stmt { | ||
Stmt::Expr(Expr::Block(mut e)) => { | ||
e.block.stmts = block_stmts(e.block.stmts)?; | ||
Ok(Stmt::Expr(Expr::Block(e))) | ||
} | ||
Stmt::Expr(Expr::Macro(m)) => expr_macro(m).map(Stmt::Expr), | ||
Stmt::Semi(Expr::Macro(m), trailing) => expr_macro(m).map(|m| Stmt::Semi(m, trailing)), | ||
_ => Ok(stmt), | ||
}) | ||
.collect::<Result<_, _>>() | ||
} | ||
|
||
/// Replaces macro assertions with `return Err(..)`. | ||
/// | ||
/// The following assertion macros are replaced: | ||
/// - [`assert`] | ||
/// - [`assert_eq`] | ||
/// - [`assert_ne`] | ||
fn expr_macro(m: ExprMacro) -> parse::Result<Expr> { | ||
if m.mac.path.is_ident("assert") { | ||
let tokens = m.mac.tokens; | ||
let tokens_str = tokens.to_string(); | ||
syn::parse( | ||
quote!( | ||
if !(#tokens) { | ||
return ::core::result::Result::Err( | ||
::core::concat!("assertion failed: ", #tokens_str).into(), | ||
); | ||
} | ||
) | ||
.into(), | ||
) | ||
} else if m.mac.path.is_ident("assert_eq") { | ||
let (left, right) = binary_macro(m.mac.tokens)?; | ||
let left_str = quote!(#left).to_string(); | ||
let right_str = quote!(#right).to_string(); | ||
syn::parse( | ||
quote!( | ||
if #left != #right { | ||
return ::core::result::Result::Err( | ||
::flipperzero_test::TestFailure::AssertEq { | ||
left: #left_str, | ||
right: #right_str, | ||
} | ||
); | ||
} | ||
) | ||
.into(), | ||
) | ||
} else if m.mac.path.is_ident("assert_ne") { | ||
let (left, right) = binary_macro(m.mac.tokens)?; | ||
let left_str = quote!(#left).to_string(); | ||
let right_str = quote!(#right).to_string(); | ||
syn::parse( | ||
quote!( | ||
if #left == #right { | ||
return ::core::result::Result::Err( | ||
::flipperzero_test::TestFailure::AssertNe { | ||
left: #left_str, | ||
right: #right_str, | ||
} | ||
); | ||
} | ||
) | ||
.into(), | ||
) | ||
} else { | ||
Ok(Expr::Macro(m)) | ||
} | ||
} | ||
|
||
fn binary_macro(tokens: proc_macro2::TokenStream) -> parse::Result<(Expr, Expr)> { | ||
let parts: ExprTuple = syn::parse(quote!((#tokens)).into())?; | ||
assert_eq!(parts.elems.len(), 2); | ||
let mut elems = parts.elems.into_iter(); | ||
Ok((elems.next().unwrap(), elems.next().unwrap())) | ||
} |
Oops, something went wrong.