Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
39d7427
proof-of-concept for colored actual and expected values in failure me…
haraldmaida Mar 30, 2025
9c4949f
Merge branch 'main' into colored-differences-in-failure-output
haraldmaida Mar 30, 2025
7321949
fix compile errors after merge of main branch
haraldmaida Mar 30, 2025
7f8b584
provide method `Spec::with_diff_format` to set a custom diff format
haraldmaida Mar 31, 2025
4d1bce8
colored diff in failure messages for strings
haraldmaida Apr 3, 2025
fb91f2b
change colored highlighting modes to `red-green`, `red-blue` and `red…
haraldmaida Apr 3, 2025
feda163
common public function `color::mark_diff` for enabled and disabled fe…
haraldmaida Apr 3, 2025
8a4e8fe
do not use imports from `prelude` in implementation modules
haraldmaida Apr 3, 2025
d4d4c6c
test diff coloring for custom struct
haraldmaida Apr 4, 2025
70738fe
rename crate feature `color` to `colored`
haraldmaida Apr 4, 2025
8196113
move feature `colored` dependent functionality of function `diff_form…
haraldmaida Apr 4, 2025
0e7ccb5
rename function `diff_format()` to `configured_diff_format()`
haraldmaida Apr 4, 2025
bd5de86
make `colored` feature compile in no-std environment
haraldmaida Apr 4, 2025
753bf46
change MSRV back to 1.78.0
haraldmaida Apr 4, 2025
6882111
Merge branch 'main' into colored-differences-in-failure-output
haraldmaida Apr 5, 2025
c68aa85
move tests regarding highlighting to the test modules related to the …
haraldmaida Apr 5, 2025
edc751d
rename module `color` to `colored`
haraldmaida Apr 5, 2025
cb6a5ce
highlight diffs in failure message for bool `is_true()` and `is_false…
haraldmaida Apr 5, 2025
c8100cc
rename fields of `DiffFormat` struct to `unexpected` and `missing`
haraldmaida Apr 5, 2025
be41265
write tests for highlight diffs for `ìs_not_equal` assertion
haraldmaida Apr 5, 2025
a8d1248
Merge branch 'main' into colored-differences-in-failure-output
haraldmaida Apr 5, 2025
4bb5d85
fix typo in test function name
haraldmaida Apr 5, 2025
f02f612
highlight diffs in failure message for f32/f64 `is_close_to` assertion
haraldmaida Apr 5, 2025
96ca467
highlight diffs in failure message for Option's `is_some`, `is_none` …
haraldmaida Apr 5, 2025
97b295c
highlight diffs in failure message for Result's `is_ok`, `is_err`, `h…
haraldmaida Apr 5, 2025
1f00161
highlight diffs in failure message for `is_in_range` and `is_not_in_r…
haraldmaida Apr 6, 2025
0d715c8
highlight diffs in failure messages for `is_empty` and `is_not_empty`…
haraldmaida Apr 6, 2025
ea742b9
highlight diffs in failure messages for `has_length` and `has_length_…
haraldmaida Apr 6, 2025
6cc82e0
highlight diffs in failure messages for string assertions
haraldmaida Apr 6, 2025
2e65a85
highlight diffs in failure messages for code `panics` and `does not p…
haraldmaida Apr 8, 2025
fd0048d
highlight diffs in failure messages for assertions of `AssertIterator…
haraldmaida Apr 13, 2025
47425b0
highlight diffs in failure messages for assertions of `AssertIterator…
haraldmaida Apr 13, 2025
9c32ab0
move impl modules inside the `colored` module to the end before the `…
haraldmaida Apr 13, 2025
3782d06
fix unknown task in justfile
haraldmaida Apr 14, 2025
05eaf29
write documentation for "colored diffs" feature
haraldmaida Apr 14, 2025
93280ec
highlight diffs in failure messages for order assertions
haraldmaida Apr 14, 2025
bc9082e
write hint to API-docs that the method `Spec::with_diff_format` must …
haraldmaida Apr 14, 2025
554647e
mention in README that "colored diffs" are only available with crate …
haraldmaida Apr 14, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test
args: --no-default-features --features "float"
args: --no-default-features --features "colored, float"

msrv:
name: Build with MSRV
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Clippy no-features" type="CargoCommandRunConfiguration" factoryName="Cargo Command">
<option name="command" value="clippy --all-targets --no-default-features" />
<option name="workingDirectory" value="file://$PROJECT_DIR$" />
<envs />
<option name="emulateTerminal" value="false" />
<option name="channel" value="DEFAULT" />
<option name="requiredFeatures" value="false" />
<option name="allFeatures" value="false" />
<option name="withSudo" value="false" />
<option name="buildTarget" value="REMOTE" />
<option name="backtrace" value="NO" />
<option name="isRedirectInput" value="false" />
<option name="redirectInputPath" value="" />
<method v="2">
<option name="CARGO.BUILD_TASK_PROVIDER" enabled="true" />
</method>
</configuration>
</component>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Clippy no-std" type="CargoCommandRunConfiguration" factoryName="Cargo Command">
<option name="command" value="clippy --all-targets --no-default-features" />
<option name="command" value="clippy --all-targets --no-default-features --features &quot;colored, float&quot;" />
<option name="workingDirectory" value="file://$PROJECT_DIR$" />
<envs />
<option name="emulateTerminal" value="false" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Test lib no-features" type="CargoCommandRunConfiguration" factoryName="Cargo Command">
<option name="buildProfileId" value="test" />
<option name="command" value="test --lib --no-default-features" />
<option name="workingDirectory" value="file://$PROJECT_DIR$" />
<envs />
<option name="emulateTerminal" value="false" />
<option name="channel" value="DEFAULT" />
<option name="requiredFeatures" value="false" />
<option name="allFeatures" value="false" />
<option name="withSudo" value="false" />
<option name="buildTarget" value="REMOTE" />
<option name="backtrace" value="NO" />
<option name="isRedirectInput" value="false" />
<option name="redirectInputPath" value="" />
<method v="2">
<option name="CARGO.BUILD_TASK_PROVIDER" enabled="true" />
</method>
</configuration>
</component>
20 changes: 20 additions & 0 deletions .ide-settings/jetbrains/runConfigurations/Test lib no-std.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Test lib no-std" type="CargoCommandRunConfiguration" factoryName="Cargo Command">
<option name="buildProfileId" value="test" />
<option name="command" value="test --lib --no-default-features --features &quot;colored, float&quot;" />
<option name="workingDirectory" value="file://$PROJECT_DIR$" />
<envs />
<option name="emulateTerminal" value="false" />
<option name="channel" value="DEFAULT" />
<option name="requiredFeatures" value="false" />
<option name="allFeatures" value="false" />
<option name="withSudo" value="false" />
<option name="buildTarget" value="REMOTE" />
<option name="backtrace" value="NO" />
<option name="isRedirectInput" value="false" />
<option name="redirectInputPath" value="" />
<method v="2">
<option name="CARGO.BUILD_TASK_PROVIDER" enabled="true" />
</method>
</configuration>
</component>
20 changes: 20 additions & 0 deletions .ide-settings/jetbrains/runConfigurations/Test lib.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Test lib" type="CargoCommandRunConfiguration" factoryName="Cargo Command">
<option name="buildProfileId" value="test" />
<option name="command" value="test --lib --all-features" />
<option name="workingDirectory" value="file://$PROJECT_DIR$" />
<envs />
<option name="emulateTerminal" value="false" />
<option name="channel" value="DEFAULT" />
<option name="requiredFeatures" value="false" />
<option name="allFeatures" value="false" />
<option name="withSudo" value="false" />
<option name="buildTarget" value="REMOTE" />
<option name="backtrace" value="NO" />
<option name="isRedirectInput" value="false" />
<option name="redirectInputPath" value="" />
<method v="2">
<option name="CARGO.BUILD_TASK_PROVIDER" enabled="true" />
</method>
</configuration>
</component>
20 changes: 20 additions & 0 deletions .ide-settings/jetbrains/runConfigurations/Test no-features.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Test no-features" type="CargoCommandRunConfiguration" factoryName="Cargo Command">
<option name="buildProfileId" value="test" />
<option name="command" value="test --no-default-features" />
<option name="workingDirectory" value="file://$PROJECT_DIR$" />
<envs />
<option name="emulateTerminal" value="false" />
<option name="channel" value="DEFAULT" />
<option name="requiredFeatures" value="false" />
<option name="allFeatures" value="false" />
<option name="withSudo" value="false" />
<option name="buildTarget" value="REMOTE" />
<option name="backtrace" value="NO" />
<option name="isRedirectInput" value="false" />
<option name="redirectInputPath" value="" />
<method v="2">
<option name="CARGO.BUILD_TASK_PROVIDER" enabled="true" />
</method>
</configuration>
</component>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Test no-std" type="CargoCommandRunConfiguration" factoryName="Cargo Command">
<option name="buildProfileId" value="test" />
<option name="command" value="test --no-default-features" />
<option name="command" value="test --no-default-features --features &quot;colored, float&quot;" />
<option name="workingDirectory" value="file://$PROJECT_DIR$" />
<envs />
<option name="emulateTerminal" value="false" />
Expand Down
10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ categories = ["development-tools::testing", "no-std"]
all-features = true

[features]
default = ["std", "panic", "float"]
default = ["std", "colored", "float", "panic"]
colored = ["dep:sdiff"]
float = ["dep:float-cmp"]
panic = ["std"]
std = []
Expand All @@ -28,9 +29,12 @@ hashbrown = "0.15"

# optional
float-cmp = { version = "0.10", optional = true }
sdiff = { version = "0.1", optional = true }

[dev-dependencies]
anyhow = "1"
proptest = "1"
serial_test = "3"
version-sync = "0.9"

[lints.rust]
Expand All @@ -55,7 +59,7 @@ unused_lifetimes = "warn"
unused_qualifications = "warn"
dead_code = "warn"
missing_docs = "warn" # uncomment when working on docs
#variant_size_differences = "warn" # enable when working on performance
variant_size_differences = "warn" # enable when working on performance

[lints.clippy]
pedantic = { level = "warn", priority = -1 }
Expand All @@ -70,5 +74,3 @@ option_if_let_else = "allow"
print_stderr = "deny"
print_stdout = "deny"
unwrap_used = "deny"
missing_errors_doc = "allow" # remove when working on docs
missing_panics_doc = "allow" # remove when working on docs
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,39 @@ asserting = { version = "0.2", default-features = false, features = "float" }

An allocator is still needed for no-std.

## Highlighted differences

`asserting` can highlight the differences between the expected value(s) and the actual value(s) when
printing assertion failures to the terminal. It supports different variants of how differences are
highlighted.

| Mode | Effect |
|------------|-----------------------------------------------------------------------------------------------------------------------------------|
| bold | Differences are printed in bold letters, without coloring. |
| red-green | Differences are printed in the colors <span style="color: green">green</span> and <span style="color: red">red</span>. |
| red-blue | Differences are printed in the CVD-friendly colors <span style="color: blue">blue</span> and <span style="color: red">red</span>. |
| red-yellow | Differences are printed in the colors <span style="color: yellow">yellow</span> and <span style="color: red">red</span>. |
| off | Switches off highlighting. The differences are not highlighted at all. |

The mode can be configured by setting the environment variable `ASSERTING_HIGHLIGHT_DIFFS` to one
of the modes in the table above. The value is case-insensitive. E.g. setting the environment
variable to values like `Red-Blue`, `Bold` or `OFF` works as well.

The intended way for configuring the highlighting mode is to set the environment variable in the
configuration for `Cargo` by adding it to the `[env]` section in your `~/.cargo/config.toml` file:

```toml,no_sync
[env]
ASSERTING_HIGHLIGHT_DIFFS = "red-blue"
```

By default the mode `red-green` is used. Differences are colored in
<span style="color: green">green</span> and <span style="color: red">red</span>.

Differences are only highlighted if the crate feature `colored` is enabled. The configuration via
the environment variable only works when the crate feature `std` is enabled too. In no-std projects
the default colors red and green are used.

## Available Assertions

This chapter gives an overview for the assertions provided by `asserting`. For a comprehensive list
Expand Down
20 changes: 15 additions & 5 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,37 @@ check:
lint:
just lint-all-features
just lint-no-std
just lint-no-features

# linting code using Clippy --all-features
# linting code using Clippy with all features enabled
lint-all-features:
cargo clippy --all-targets --all-features

# linting code using Clippy - no-std
# linting code using Clippy for no-std environment
lint-no-std:
cargo clippy --all-targets --no-default-features --features "float"
cargo clippy --all-targets --no-default-features --features "colored, float"

# linting code using Clippy with no features enabled
lint-no-features:
cargo clippy --all-targets --no-default-features

# run all tests
test:
just test-all-features
just test-no-std
just test-no-features

# run tests for all features
test-all-features:
cargo test --all-features

# run tests for no-std
# run tests for no-std environment
test-no-std:
cargo test --no-default-features --features "float"
cargo test --no-default-features --features "colored, float"

# run tests with no features enabled
test-no-features:
cargo test --no-default-features

# run code coverage (does not include doc-tests)
code-coverage:
Expand Down
19 changes: 12 additions & 7 deletions src/boolean/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! Implementation of assertions for values of type `bool`.

use crate::assertions::AssertBoolean;
use crate::colored::{mark_missing, mark_unexpected};
use crate::expectations::{IsFalse, IsTrue};
use crate::spec::{Expectation, Expression, FailingStrategy, Spec};
use crate::spec::{DiffFormat, Expectation, Expression, FailingStrategy, Spec};
use crate::std::format;
use crate::std::string::String;

Expand All @@ -24,10 +25,12 @@ impl Expectation<bool> for IsTrue {
*subject
}

fn message(&self, expression: Expression<'_>, actual: &bool) -> String {
fn message(&self, expression: Expression<'_>, actual: &bool, format: &DiffFormat) -> String {
let marked_actual = mark_unexpected(actual, format);
let marked_expected = mark_missing(&true, format);
format!(
"expected {expression} is {:?}\n but was: {actual:?}\n expected: {:?}",
true, true
"expected {expression} is {:?}\n but was: {marked_actual}\n expected: {marked_expected}",
true
)
}
}
Expand All @@ -37,10 +40,12 @@ impl Expectation<bool> for IsFalse {
!*subject
}

fn message(&self, expression: Expression<'_>, actual: &bool) -> String {
fn message(&self, expression: Expression<'_>, actual: &bool, format: &DiffFormat) -> String {
let marked_actual = mark_unexpected(actual, format);
let marked_expected = mark_missing(&false, format);
format!(
"expected {expression} is {:?}\n but was: {actual:?}\n expected: {:?}",
false, false
"expected {expression} is {:?}\n but was: {marked_actual}\n expected: {marked_expected}",
false
)
}
}
Expand Down
37 changes: 37 additions & 0 deletions src/boolean/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,40 @@ fn verify_bool_is_false_fails() {
"]
);
}

#[cfg(feature = "colored")]
mod colored {
use crate::prelude::*;

#[test]
fn highlight_diffs_bool_is_true() {
let failures = verify_that(37 == 42)
.with_diff_format(DIFF_FORMAT_RED_GREEN)
.is_true()
.display_failures();

assert_eq!(
failures,
&["assertion failed: expected subject is true\n \
but was: \u{1b}[31mfalse\u{1b}[0m\n \
expected: \u{1b}[32mtrue\u{1b}[0m\n\
"]
);
}

#[test]
fn highlight_diffs_bool_is_false() {
let failures = verify_that(42 == 42)
.with_diff_format(DIFF_FORMAT_RED_YELLOW)
.is_false()
.display_failures();

assert_eq!(
failures,
&["assertion failed: expected subject is false\n \
but was: \u{1b}[31mtrue\u{1b}[0m\n \
expected: \u{1b}[33mfalse\u{1b}[0m\n\
"]
);
}
}
Loading