Skip to content

Commit

Permalink
Replace usage of mockall with explicit TestReportBuilder
Browse files Browse the repository at this point in the history
The new `TestReportBuilder` collects all of the input fed into it.
This way one can run assertions on the collected inputs, instead of asserting functions being called.
  • Loading branch information
Swatinem committed Aug 8, 2024
1 parent 1641d22 commit 88861f1
Show file tree
Hide file tree
Showing 7 changed files with 428 additions and 465 deletions.
71 changes: 0 additions & 71 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ winnow = "0.5.34"

[dev-dependencies]
divan = "0.1.14"
mockall = { version = "0.13.0", features = ["nightly"] }
tempfile = "3.9.0"

[[bench]]
Expand Down
81 changes: 17 additions & 64 deletions src/parsers/pyreport/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,20 +552,22 @@ where

#[cfg(test)]
mod tests {
use mockall::predicate::*;
use winnow::error::AddContext;

use super::*;
use crate::report::{models::*, MockReport, MockReportBuilder};
use crate::report::{
models::*,
test::{TestReport, TestReportBuilder},
};

type TestStream<'a> = ReportOutputStream<&'a str, MockReport, MockReportBuilder<MockReport>>;
type TestStream<'a> = ReportOutputStream<&'a str, TestReport, TestReportBuilder>;

struct Ctx {
parse_ctx: ParseCtx<MockReport, MockReportBuilder<MockReport>>,
parse_ctx: ParseCtx<TestReport, TestReportBuilder>,
}

fn setup() -> Ctx {
let report_builder = MockReportBuilder::new();
let report_builder = TestReportBuilder::default();
let report_json_files = HashMap::from([(0, 0), (1, 1), (2, 2)]);
let report_json_sessions = HashMap::from([(0, 0), (1, 1), (2, 2)]);

Expand All @@ -590,27 +592,6 @@ mod tests {
ErrMode::Cut(create_context_error(contexts))
}

fn stub_report_builder(report_builder: &mut MockReportBuilder<MockReport>) {
report_builder
.expect_multi_insert_coverage_sample()
.returning(|_| Ok(()));
report_builder
.expect_multi_insert_branches_data()
.returning(|_| Ok(()));
report_builder
.expect_multi_insert_method_data()
.returning(|_| Ok(()));
report_builder
.expect_multi_insert_span_data()
.returning(|_| Ok(()));
report_builder
.expect_multi_associate_context()
.returning(|_| Ok(()));
report_builder
.expect_insert_context()
.returning(|name| Ok(Context::new(name)));
}

#[test]
fn test_pyreport_coverage() {
let test_ctx = setup();
Expand Down Expand Up @@ -1273,30 +1254,25 @@ mod tests {

// Parsing a label that is already in `labels_index` should just return it
buf.input = "\"already_inserted\"";
buf.state.db.report_builder.expect_insert_context().times(0);
assert_eq!(
label.parse_next(&mut buf),
Ok("already_inserted".to_string())
);

// If we parse a number like `1`, we should look for `"1"` in the labels index.
buf.input = "1";
buf.state.db.report_builder.expect_insert_context().times(0);
assert_eq!(label.parse_next(&mut buf), Ok("1".to_string()));

// Parsing a label that is not already in `labels_index` should insert it
buf.state
.db
.report_builder
.expect_insert_context()
.with(eq("not_already_inserted"))
.returning(|name| Ok(Context::new(name)))
.times(1);
buf.input = "\"not_already_inserted\"";
assert_eq!(
label.parse_next(&mut buf),
Ok("not_already_inserted".to_string())
);
assert_eq!(
buf.state.db.report_builder.report.contexts,
&[Context::new("not_already_inserted")]
);

// Malformed labels should never get to inserting
let malformed_test_cases = [
Expand Down Expand Up @@ -1326,14 +1302,6 @@ mod tests {
state: test_ctx.parse_ctx,
};

// See `test_label()` for testing this logic. Stub the report_builder stuff for
// these tests.
buf.state
.db
.report_builder
.expect_insert_context()
.returning(|name| Ok(Context::new(name)));

let valid_test_cases = [
(
"[1, \"2/2\", \"b\", [\"test_case\"]]",
Expand Down Expand Up @@ -1452,11 +1420,6 @@ mod tests {
};
buf.state.labels_index.insert("test_case".to_string(), 100);

// The logic which inserts all the data from a `ReportLine` into
// `buf.state.report_builder` is tested in
// `src/parsers/pyreport_shim/chunks/utils.rs`. Stub `report_builder` here.
stub_report_builder(&mut buf.state.db.report_builder);

let test_cases = [
(
"[1, null, [[0, 1]]]",
Expand Down Expand Up @@ -1831,7 +1794,6 @@ mod tests {
input: "",
state: test_ctx.parse_ctx,
};
stub_report_builder(&mut buf.state.db.report_builder);

// (input, (result, expected_line_count))
let test_cases = [
Expand Down Expand Up @@ -1933,20 +1895,6 @@ mod tests {
state: test_ctx.parse_ctx,
};

buf.state
.db
.report_builder
.expect_insert_context()
.with(eq("1".to_string()))
.returning(|name| Ok(Context::new(name)));

buf.state
.db
.report_builder
.expect_insert_context()
.with(eq("test_name".to_string()))
.returning(|name| Ok(Context::new(name)));

assert!(buf.state.labels_index.is_empty());
let test_cases = [
(
Expand Down Expand Up @@ -1980,6 +1928,12 @@ mod tests {
assert_eq!(buf.state.labels_index.len(), 2);
assert!(buf.state.labels_index.contains_key("1"));
assert!(buf.state.labels_index.contains_key("test_name"));

let report = buf.state.db.report_builder.build().unwrap();
assert_eq!(
report.contexts,
&[Context::new("test_name"), Context::new("test_name")]
);
}

#[test]
Expand All @@ -1990,7 +1944,6 @@ mod tests {
state: test_ctx.parse_ctx,
};
buf.state.labels_index.insert("test_case".to_string(), 100);
stub_report_builder(&mut buf.state.db.report_builder);

// (input, (result, expected_chunk_index, expected_line_count))
let test_cases = [
Expand Down
Loading

0 comments on commit 88861f1

Please sign in to comment.