Git Product home page Git Product logo

assertor's Introduction

Assertor

Assertor makes test assertions and failure messages more human-readable.

crates.io license docs.rs OpenSSF Scorecard

Assertor is heavily affected by Java Truth in terms of API design and error messages, but this is a totally different project from Java Truth.

Disclaimer

This is not an official Google product, it is just code that happens to be owned by Google.

โš  The API is not fully stable and may be changed until version 1.0.

Example

use assertor::*;

#[test]
fn test_it() {
    assert_that!("foobarbaz").contains("bar");
    assert_that!("foobarbaz").ends_with("baz");

    assert_that!(0.5).with_abs_tol(0.2).is_approx_equal_to(0.6);

    assert_that!(vec!["a", "b"]).contains("a");
    assert_that!(vec!["a", "b"]).has_length(2);
    assert_that!(vec!["a", "b"]).contains_exactly(vec!["a", "b"]);

    assert_that!(Option::Some("Foo")).has_value("Foo");
}

Failure cases

use assertor::*;

fn test_it() {
    assert_that!(vec!["a", "b", "c"]).contains_exactly(vec!["b", "c", "d"]);
    // missing (1)   : ["d"]
    // unexpected (1): ["a"]
    // ---
    // expected      : ["b", "c", "d"]
    // actual        : ["a", "b", "c"]
}

Feature ideas

  • Color / Bold
  • Better diff: vec
  • Better diff: set
  • Better diff: HashMap

assertor's People

Contributors

cocuh avatar dependabot[bot] avatar kelebra avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

assertor's Issues

Better diff representation using bold / color

Adding the semantics of swap, insertion or deletion.

This feature should be able to be turned on/off by feature.

Example 1:

assert_that!(vec![1,4,3,2]).contains_exactly_in_order(vec![1,2,3,4]);

Idea of better message:

contents match, but order was wrong
---
expected: [1, 2, 3, 4]
actual  : [1, <bold>4</bold>, 3, <bold>2</bold>]

Example 2:

assert_that!(vec![1, 3, 4, 5]).contains_exactly_in_order(vec![1,2,3,4]);

Idea of better message:

contents match, but order was wrong
---
expected: [1, <green>2</green>, 3, 4]
actual  : [1,<space/>           3, 4, <red>5</red>]

Color usage guideline: Green should emphasize the normal case, and red should emphasize the unexpected case, imo.

Estimation/design: This needs quite big structural change in Fact struct. Specifically, more semantic structs may need to be defined to represent added or deleted object, because generating Fact should be decoupled from formatting in order to change the formatting logic in runtime.

Error: impl method assumes more implied bounds than the corresponding trait method in assertor v0.0.1 during cargo build

When trying to run cargo build in the assertor repository, the following error message is displayed:

error: impl method assumes more implied bounds than the corresponding trait method
   --> src/assertions/map.rs:171:16
    |
171 |     fn key_set(&self) -> Subject<'a, Keys<K, V>, (), R> {
    |                ^^^^^     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace this type to make the impl signature compatible: `Subject<'_, std::collections::hash_map::Keys<'_, K, V>, (), R>`
    |
    = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
    = note: for more information, see issue #105572 <https://github.com/rust-lang/rust/issues/105572>
    = note: `#[deny(implied_bounds_entailment)]` on by default

error: could not compile `assertor` due to 2 previous errors

Expected Behavior
cargo build should compile the assertor repository successfully without any errors.

Actual Behavior
The build process fails with the error message described above.

Steps to Reproduce
Clone the assertor repository from GitHub: git clone https://github.com/google/assertor.git
Navigate to the assertor directory: cd assertor
Run cargo build

Additional Information
assertor version: v0.0.1
Operating System: Ubuntu 22.04
Rust: 1.68.2

Possible Solution
It looks like the error is related to the key_set method implementation in map.rs. The error message suggests replacing Subject<'a, Keys<K, V>, (), R> with Subject<', std::collections::hash_map::Keys<', K, V>, (), R> in the method signature to make it compatible with the trait method.

Better representation of diffs for map when values are different though key is same.

Derived from #30.

Current formatting logic for different values can be ugly in some case; specifically 1) when formatted string length of key is quite long, 2) the formatted string length of values are quite long, 3) there are many diffs.

Current:

key {:?} was mapped to an unexpected value
expected value {:?}, but found {:?}

Improvement idea from this comment

keys were mapped to an unexpected values: [
  "key1": {
    "expected" : "val1",
    "actual"      : "val2" 
  }
  ...
]

Feature idea - enumeration variant matching

Hey ๐Ÿ‘‹๐Ÿพ

I really like the fluid syntax of this library. While working with it, a few assertions would be great to have. Some of them are for ergonomics when working with enumeration variants. Like simple matching. So I could imagine for example something like:

enum Color {
   RED,
   GREEN,
   BLUE,
   RGB(u8, u8, u8),
}

assert_that!(Color::RED).matches(Color::RED);
assert_that!(Color::RGB(255, 0, 0)).matches(Color::RGB(_, _, _));
assert_that!(Color::RGB(255, 0, 0)).matches(Color::RGB(_, _, 0));

With the "default" API you can write these simply with assert!(matches!(...)). But then we have the syntax of the assertor it's assert_that! mixed with the other. Which does not read and feel great. So I though it might be easy enough to add this? ๐Ÿ™ˆ

In any case: thanks for this library. Like it so far!

Add support for `BTreeMap` and `BTreeSet`

Map and Vec have a very decent support at this point. However, sorted/heap types (BTreeMap, BTreeSet) are lacking because we have no way to verify both ordering/sequencing and presence of map entries. I would like to add support for BTreeMap (separate from Map) and BTreeSet (separate from Set).

Make multi-lines when diffing objects have long `Debug` outputs

For objects having long Debug outputs, current debug message is messed up.

One improvable idea is

Example:

#[derive(Debug, PartialEq)]
struct LongDebugObject<> {
    values: Vec<&'static str>,
    another_value: Option<&'static str>,
}


let actual = vec![
    LongDebugObject {
        values: vec!["a", "c", "t", "u", "a", "l"],
        another_value: None,
    },
    LongDebugObject {
        values: vec![],
        another_value: Some("same value"),
    },
];
let expected = vec![
    LongDebugObject {
        values: vec![],
        another_value: Some("same value"),
    },
    LongDebugObject {
        values: vec!["e", "x", "p", "e", "c", "t", "e", "d"],
        another_value: None,
    },
];
assert_that!(actual).contains_exactly_in_order(expected);

Current

missing (1)   : [LongDebugObject { values: ["e", "x", "p", "e", "c", "t", "e", "d"], another_value: None }]
unexpected (1): [LongDebugObject { values: ["a", "c", "t", "u", "a", "l"], another_value: None }]
---
expected      : [LongDebugObject { values: [], another_value: Some("same value") }, LongDebugObject { values: ["e", "x", "p", "e", "c", "t", "e", "d"], another_value: None }]
actual        : [LongDebugObject { values: ["a", "c", "t", "u", "a", "l"], another_value: None }, LongDebugObject { values: [], another_value: Some("same value") }]

Improvement Idea

Make the output of expected / actual values multiple lines instead of single line.

missing (1)   : [LongDebugObject { values: ["e", "x", "p", "e", "c", "t", "e", "d"], another_value: None }]
unexpected (1): [LongDebugObject { values: ["a", "c", "t", "u", "a", "l"], another_value: None }]
---
expected      :  [
   - LongDebugObject { values: [], another_value: Some("same value") }
   - LongDebugObject { values: ["e", "x", "p", "e", "c", "t", "e", "d"], another_value: None }
]
actual        : [
   - LongDebugObject { values: ["a", "c", "t", "u", "a", "l"], another_value: None },
   - LongDebugObject { values: [], another_value: Some("same value") }
]

Informations related to this project

Hello everyone!
I have a few questions about this project:

  • This project seems to be the only crate for fluent assertion in rust and I would love to understand in order to contribute. Could we possibly start creating some issues in order to group some features to implement?
  • I saw there is a section into the readme.md file that indicates some features, are they still not implemented?
    Thanks in advance for your time

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.