Skip to content

Commit f72aae9

Browse files
committed
Fix duplicated code blocks
1 parent 0e9afb2 commit f72aae9

3 files changed

Lines changed: 43 additions & 0 deletions

File tree

extensions/scarb-doc/src/doc_test/code_blocks.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ pub fn collect_code_blocks(crate_: &Crate<'_>) -> Vec<CodeBlock> {
136136
collect_from_module(module, &mut runnable_code_blocks);
137137
}
138138
runnable_code_blocks.sort_by_key(|block| block.id.clone());
139+
runnable_code_blocks.dedup_by_key(|block| block.id.clone());
139140
runnable_code_blocks
140141
}
141142

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
mod inner {
2+
/// A struct defined in a submodule, with a runnable example.
3+
/// ```cairo, runnable
4+
/// println!("hello from re-exported item");
5+
/// ```
6+
pub struct MyStruct {}
7+
}
8+
9+
pub use inner::MyStruct;

extensions/scarb-doc/tests/runnable_examples.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const CODE_WITH_MULTIPLE_CODE_BLOCKS_PER_ITEM: &str = include_str!("code/code_15
1616
const CODE_WITH_STARKNET_CONTRACT: &str = include_str!("code/code_16.cairo");
1717
const CODE_WITH_COMPILE_FAIL: &str = include_str!("code/code_17.cairo");
1818
const CODE_WITH_SHOULD_PANIC: &str = include_str!("code/code_18.cairo");
19+
const CODE_WITH_REEXPORTED_ITEM: &str = include_str!("code/code_19.cairo");
1920
const EXPECTED_WITH_EMBEDDINGS_PATH: &str = "tests/data/runnable_examples";
2021
const EXPECTED_WITH_EMBEDDINGS_MDX_PATH: &str = "tests/data/runnable_examples_mdx";
2122
const EXPECTED_MULTIPLE_PER_ITEM_PATH: &str = "tests/data/runnable_examples_multiple_per_item";
@@ -387,3 +388,35 @@ fn compile_fail() {
387388
error: doc tests failed
388389
"#});
389390
}
391+
392+
#[test]
393+
fn runnable_examples_not_duplicated_for_reexported_items() {
394+
let t = TempDir::new().unwrap();
395+
ProjectBuilder::start()
396+
.name("hello_world")
397+
.lib_cairo(CODE_WITH_REEXPORTED_ITEM)
398+
.build(&t);
399+
400+
Scarb::quick_command()
401+
.arg("doc")
402+
.args(["--output-format", "markdown"])
403+
.arg("--disable-remote-linking")
404+
.current_dir(&t)
405+
.assert()
406+
.success()
407+
.stdout_eq(formatdoc! {r#"
408+
[..] Running 1 doc examples for `hello_world`
409+
[..] Compiling hello_world_example_1 v0.1.0 ([..])
410+
[..] Finished `dev` profile target(s) in [..]
411+
test hello_world::inner::MyStruct ... ok
412+
413+
test result: ok. 1 passed; 0 failed; 0 ignored
414+
Saving output to: target/doc/hello_world
415+
416+
Run the following to see the results:[..]
417+
`mdbook serve target/doc/hello_world`
418+
(you will need to have mdbook installed)
419+
420+
Or build html docs by running `scarb doc --build`
421+
"#});
422+
}

0 commit comments

Comments
 (0)