-
-
Notifications
You must be signed in to change notification settings - Fork 44
Test harness #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Test harness #57
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0554a27
Tools: Add `run-fap` to run a given FAP on a connected Flipper Zero
str4d 7c0060a
Set up a test harness for the `flipperzero` crate
str4d 09c5788
flipperzero: Add some tests
str4d ada906a
CI: Install required package for building tools
str4d 1c316d9
CI: Check for expected error
str4d 33865cc
Require that crates are only compiled for the Flipper Zero target
str4d File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
//! High-level bindings for the Flipper Zero. | ||
|
||
#![no_std] | ||
#![cfg_attr(test, no_main)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we just apply |
||
|
||
#[cfg(feature = "alloc")] | ||
extern crate alloc; | ||
|
@@ -18,3 +19,8 @@ pub mod __internal { | |
// Re-export for use in macros | ||
pub use ufmt; | ||
} | ||
|
||
flipperzero_test::tests_runner!( | ||
name = "flipperzero-rs Unit Tests", | ||
[crate::furi::message_queue::tests, crate::furi::sync::tests] | ||
); |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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`] | ||
dcoles marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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)> { | ||
dcoles marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For something as simple as this script, we could probably even get away with just
+ sys.argv
.