testanything/
lib.rs

1//! The Test Anything Protocol (TAP) is a plaintext format for expressing test results. It has been around since 1987 when it was invented to help test Perl. With this crate, this wonderfully-useful tool has been brought to Rust!
2//!
3//! This crate provides the machinery needed for producing and emitting TAP streams.
4//!
5//! For working, executable examples, please see the `examples` directory.
6//!
7//! # Examples
8//!
9//! The first method for producing a TAP stream is the `TapSuite` mechanism. This will come in handy when you're iterating over a collection and want to `map` it to a collection of `TapTest` results. We supply a `TapSuiteBuilder` and a `TapTestBuilder` to make this as nice as possible.
10//!
11//! Behold! The `TapSuite`
12//!
13//! ```
14//! use testanything::tap_test_builder::TapTestBuilder;
15//! use testanything::tap_suite_builder::TapSuiteBuilder;
16//!
17//! use std::io;
18//!
19//! // Build a failing test
20//! let failing_tap_test = TapTestBuilder::new()
21//!     .name("Example TAP test")
22//!     .passed(false)
23//!     .diagnostics(&vec!["This test failed because of X"])
24//!     .finalize();
25//!
26//! // Construct a test result suite
27//! let tap_suite = TapSuiteBuilder::new()
28//!     .name("Example TAP suite")
29//!     .tests(vec![failing_tap_test])
30//!     .finalize();
31//!
32//! // Print TAP to standard output in one chunk
33//! match tap_suite.print(io::stdout().lock()) {
34//!     Ok(_) => {}
35//!     Err(reason) => eprintln!("{}", reason),
36//! }
37//! ```
38//!
39//! The second method uses the `TapWriter` facility and may be thought of as the direct approach. This mechanism allows you to write a semi-customizable TAP stream to STDOUT from anywhere in your program. Since the TAP specification requires that TAP be emitted to STDOUT, the `TapWriter` doesn't get fancy with any of the stream interfaces.
40//!
41//! Behold, the `TapWriter`!
42//!
43//! ```
44//! use testanything::tap_writer::TapWriter;
45//!
46//! let writer = TapWriter::new("Example TAP stream");
47//!
48//! // Write the plan out. This can come before or after the test results themselves.
49//! writer.plan(1, 6);
50//!
51//! // Give me the name as a diagnostic line
52//! writer.name();
53//!
54//! // Print out some test results
55//! writer.ok(1, "Panda");
56//! writer.ok(2, "Bamboo");
57//! writer.ok(3, "Curry");
58//! // This one failed, so explain why with a diagnostic line
59//! writer.not_ok(4, "Noodle");
60//! writer.diagnostic("The above test failed because of XYZ reason");
61//! writer.ok(5, "Tree");
62//!
63//! // Uh oh! something went horribly wrong and we need to stop before
64//! // we print out the results from test 6!
65//! writer.bail_out_with_message("Destabilized warp core! Can't continue!");
66//! ```
67//!
68//!
69
70#![forbid(unsafe_code)]
71#![deny(clippy::all)]
72// Support using TAP without the standard library
73#![cfg_attr(not(feature = "std"), no_std)]
74#[cfg(all(feature = "alloc", not(feature = "std")))]
75extern crate alloc;
76
77/// Global constant for the "ok"
78const OK_SYMBOL: &str = "ok";
79/// Global constant for the "not ok"
80const NOT_OK_SYMBOL: &str = "not ok";
81
82pub mod tap_suite;
83pub mod tap_suite_builder;
84pub mod tap_test;
85pub mod tap_test_builder;
86#[cfg(feature = "std")]
87pub mod tap_writer;