Skip to content

Commit

Permalink
feat(hydroflow): Move hydroflow_expect_warnings into library, use i…
Browse files Browse the repository at this point in the history
…n `tests/surface_flow_props.rs` (#897)
  • Loading branch information
MingweiSamuel committed Sep 6, 2023
1 parent 1bdbf73 commit d03ffe7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 39 deletions.
31 changes: 31 additions & 0 deletions hydroflow/src/declarative_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,37 @@ macro_rules! assert_var_impl {
};
}

/// Tests that the given warnings are emitted by the hydroflow macro invocation.
///
/// For example usage, see `hydroflow/tests/surface_warnings.rs`.
#[macro_export]
macro_rules! hydroflow_expect_warnings {
(
$hf:tt,
$( $msg:literal ),*
$( , )?
) => {
{
let __file = std::file!();
let __line = std::line!() as usize;
let __hf = hydroflow::hydroflow_syntax_noemit! $hf;

let diagnostics = __hf.diagnostics().expect("Expected `diagnostics()` to be set.");
let expecteds = &[
$( $msg , )*
];
assert_eq!(diagnostics.len(), expecteds.len(), "Wrong number of diagnostics.");
for (expected, diagnostic) in expecteds.iter().zip(diagnostics.iter()) {
let mut diagnostic = diagnostic.clone();
diagnostic.span.line = diagnostic.span.line.saturating_sub(__line);
assert_eq!(expected.to_string(), diagnostic.to_string().replace(__file, "$FILE"));
}

__hf
}
};
}

/// Test helper, emits and checks snapshots for the mermaid and dot graphs.
#[doc(hidden)]
#[macro_export]
Expand Down
15 changes: 9 additions & 6 deletions hydroflow/tests/surface_flow_props.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use hydroflow::lattices::collections::SingletonSet;
use hydroflow::lattices::set_union::{SetUnion, SetUnionHashSet, SetUnionSingletonSet};
use hydroflow::{assert_graphvis_snapshots, hydroflow_syntax};
use hydroflow::{assert_graphvis_snapshots, hydroflow_expect_warnings, hydroflow_syntax};

#[test]
pub fn test_basic() {
Expand Down Expand Up @@ -29,11 +29,14 @@ pub fn test_basic() {

#[test]
pub fn test_union_warning() {
let mut hf = hydroflow_syntax! {
source_iter_delta((0..10).map(SetUnionSingletonSet::new_from)) -> [0]my_union;
source_iter((0..10).map(SetUnionSingletonSet::new_from)) -> [1]my_union;

my_union = union() -> for_each(|s| println!("{:?}", s));
let mut hf = hydroflow_expect_warnings! {
{
source_iter_delta((0..10).map(SetUnionSingletonSet::new_from)) -> [0]my_union;
source_iter((0..10).map(SetUnionSingletonSet::new_from)) -> [1]my_union;

my_union = union() -> for_each(|s| println!("{:?}", s));
},
"Warning: Input to `union()` will be downcast to `None` to match other inputs.\n --> $FILE:0:0"
};
hf.run_available();

Expand Down
40 changes: 7 additions & 33 deletions hydroflow/tests/surface_warnings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,15 @@
use std::cell::RefCell;
use std::rc::Rc;

use hydroflow::hydroflow_expect_warnings;
use hydroflow::scheduled::graph::Hydroflow;
use hydroflow::util::collect_ready;

macro_rules! test_warnings {
(
$hf:tt,
$( $msg:literal ),*
$( , )?
) => {
{
let __file = std::file!();
let __line = std::line!() as usize;
let __hf = hydroflow::hydroflow_syntax_noemit! $hf;

let diagnostics = __hf.diagnostics().expect("Expected `diagnostics()` to be set.");
let expecteds = &[
$( $msg , )*
];
assert_eq!(diagnostics.len(), expecteds.len(), "Wrong number of diagnostics.");
for (expected, diagnostic) in expecteds.iter().zip(diagnostics.iter()) {
let mut diagnostic = diagnostic.clone();
diagnostic.span.line = diagnostic.span.line.saturating_sub(__line);
assert_eq!(expected.to_string(), diagnostic.to_string().replace(__file, "$FILE"));
}

__hf
}
};
}

#[test]
fn test_degenerate_union() {
let (result_send, mut result_recv) = hydroflow::util::unbounded_channel::<usize>();

let mut df = test_warnings! {
let mut df = hydroflow_expect_warnings! {
{
source_iter([1, 2, 3]) -> union() -> for_each(|x| result_send.send(x).unwrap());
},
Expand All @@ -52,7 +26,7 @@ fn test_degenerate_union() {

#[test]
fn test_empty_union() {
let mut df = test_warnings! {
let mut df = hydroflow_expect_warnings! {
{
union() -> for_each(|x: usize| println!("{}", x));
},
Expand All @@ -65,7 +39,7 @@ fn test_empty_union() {
fn test_degenerate_tee() {
let (result_send, mut result_recv) = hydroflow::util::unbounded_channel::<usize>();

let mut df = test_warnings! {
let mut df = hydroflow_expect_warnings! {
{
source_iter([1, 2, 3]) -> tee() -> for_each(|x| result_send.send(x).unwrap());
},
Expand All @@ -81,7 +55,7 @@ fn test_empty_tee() {
let output = <Rc<RefCell<Vec<usize>>>>::default();
let output_inner = Rc::clone(&output);

let mut df = test_warnings! {
let mut df = hydroflow_expect_warnings! {
{
source_iter([1, 2, 3]) -> inspect(|&x| output_inner.borrow_mut().push(x)) -> tee();
},
Expand All @@ -96,7 +70,7 @@ fn test_empty_tee() {
// But in a different edge order.
#[test]
pub fn test_warped_diamond() {
let mut df = test_warnings! {
let mut df = hydroflow_expect_warnings! {
{
// active nodes
nodes = union();
Expand All @@ -119,7 +93,7 @@ pub fn test_warped_diamond() {

#[test]
pub fn test_warped_diamond_2() {
let mut hf: Hydroflow = test_warnings! {
let mut hf: Hydroflow = hydroflow_expect_warnings! {
{
// active nodes
nodes = union();
Expand Down

0 comments on commit d03ffe7

Please sign in to comment.