Skip to content

Commit

Permalink
Merge pull request #123 from ltratt/fix_example
Browse files Browse the repository at this point in the history
Fix example
  • Loading branch information
vext01 authored Feb 26, 2024
2 parents 4403381 + 9c5c958 commit 239a449
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,30 @@ For example, a Rust language tester, loosely in the spirit of
[`compiletest_rs`](https://crates.io/crates/compiletest_rs), looks as follows:

```rust
use std::{fs::read_to_string, path::PathBuf, process::Command};
use std::{env, fs::read_to_string, path::PathBuf, process::Command};

use lang_tester::LangTester;
use tempfile::TempDir;

static COMMENT_PREFIX: &str = "//";

fn main() {
// We use rustc to compile files into a binary: we store those binary files
// into `tempdir`. This may not be necessary for other languages.
let tempdir = TempDir::new().unwrap();
LangTester::new()
.test_dir("examples/rust_lang_tester/lang_tests")
// Only use files named `*.rs` as test files.
.test_file_filter(|p| p.extension().unwrap().to_str().unwrap() == "rs")
// Treat lines beginning with "#" as comments.
.test_path_filter(|p| p.extension().and_then(|x| x.to_str()) == Some("rs"))
// Treat lines beginning with "#" inside a test as comments.
.comment_prefix("#")
// Extract the first sequence of commented line(s) as the tests.
.test_extract(|p| {
read_to_string(p)
.unwrap()
.lines()
// Skip non-commented lines at the start of the file.
.skip_while(|l| !l.starts_with(COMMENT_PREFIX))
.skip_while(|l| !l.starts_with("//"))
// Extract consecutive commented lines.
.take_while(|l| l.starts_with(COMMENT_PREFIX))
.take_while(|l| l.starts_with("//"))
.map(|l| &l[COMMENT_PREFIX.len()..])
.collect::<Vec<_>>()
.join("\n")
Expand Down
10 changes: 4 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
//! [`compiletest_rs`](https://crates.io/crates/compiletest_rs), looks as follows:
//!
//! ```rust,ignore
//! use std::{fs::read_to_string, path::PathBuf, process::Command};
//! use std::{env, fs::read_to_string, path::PathBuf, process::Command};
//!
//! use lang_tester::LangTester;
//! use tempfile::TempDir;
//!
//! static COMMENT_PREFIX: &str = "//";
//!
//! fn main() {
//! // We use rustc to compile files into a binary: we store those binary files
//! // into `tempdir`. This may not be necessary for other languages.
Expand All @@ -24,17 +22,17 @@
//! .test_dir("examples/rust_lang_tester/lang_tests")
//! // Only use files named `*.rs` as test files.
//! .test_path_filter(|p| p.extension().and_then(|x| x.to_str()) == Some("rs"))
//! // Treat lines beginning with "#" as comments.
//! // Treat lines beginning with "#" inside a test as comments.
//! .comment_prefix("#")
//! // Extract the first sequence of commented line(s) as the tests.
//! .test_extract(|p| {
//! read_to_string(p)
//! .unwrap()
//! .lines()
//! // Skip non-commented lines at the start of the file.
//! .skip_while(|l| !l.starts_with(COMMENT_PREFIX))
//! .skip_while(|l| !l.starts_with("//"))
//! // Extract consecutive commented lines.
//! .take_while(|l| l.starts_with(COMMENT_PREFIX))
//! .take_while(|l| l.starts_with("//"))
//! .map(|l| &l[COMMENT_PREFIX.len()..])
//! .collect::<Vec<_>>()
//! .join("\n")
Expand Down

0 comments on commit 239a449

Please sign in to comment.