2 unstable releases

0.1.0 Sep 17, 2024
0.0.0 Sep 8, 2024

#496 in Rust patterns

32 downloads per month
Used in 2 crates

BSD-3-Clause

62KB
976 lines

test_help-rs

Test helpers for Rust

Introduction

Rust has powerful and easy-to-use unit-testing mechanisms, but there are some missing elements, particular around the use of floating-point values - f32 and f64 - that are provided by this crate to allowing for assertion of approximate equality, as in:

use test_helpers::{
	assert_scalar_eq_approx,
	assert_vector_eq_approx,
	margin,
	multiplier,
};

#[test]
fn example_test_of_scalar_evaluation() {
	let expected = 3.0;
	let actual = 3.0001;
	assert_scalar_eq_approx!(expected, actual, margin(0.0001));
}

#[test]
fn example_test_of_vector_evaluation() {
	let expected = &[ 3.0, -40404.0, 1.23456 ];
	let actual = Vec::from([ 3.0, -40410.0, 1.234567 ]);
	assert_vector_eq_approx!(expected, actual, multiplier(0.00015));
}

Table of Contents

Installation

Reference in Cargo.toml in the usual way:

test_help-rs = { version = "~0.1" }

Components

Constants

The following constants are defined:

  • DEFAULT_MARGIN - specifies the default comparison margin value, which is a xxxx;
  • DEFAULT_MULTIPLIER - specifies the default comparison multiplier value, which is a xxxx;

Enumerations

The following enuemrations are defined:

  • ComparisonResult - ... TBC;
  • VectorComparisonResult - ... TBC;

Functions

The following functions are defined:

  • margin() -> impl ApproximateEqualityEvaluator - creates an implementation of the ApproximateEqualityEvaluator trait that defines a margin-based evaluator instance;
  • multiplier() -> impl ApproximateEqualityEvaluator - creates an implementation of the ApproximateEqualityEvaluator trait that defines a multiplier-based evaluator instance;
  • zero_margin_or_multiplier() -> impl ApproximateEqualityEvaluator - creates an implementation of the ApproximateEqualityEvaluator trait that defines both a margin to be used when expected value and/or actual value is zero, and a multiplier to be used in all other cases;
  • evaluate_scalar_eq_approx() - a generic function that may be used to compare expected and actual scalar values of types that are logically f64, along with an evaluator (of type &dyn ApproximateEqualityEvaluator). This function is used in the crate macros, but may also be used as part of the implementation of such macros for testing application-defined types;
  • evaluate_vector_eq_approx() - a generic function that may be used to compare expected and actual values that are vectors of types that are logically f64, along with an evaluator (of type &dyn ApproximateEqualityEvaluator). This function is used in the crate macros, but may also be used as part of the implementation of such macros for testing application-defined types;

Macros

The following macros are defined:

  • assert_scalar_eq_approx!() - asserts approximate equality of expected and actual values, with an optional evaluator;
  • assert_scalar_ne_approx!() - asserts approximate inequality of expected and actual values, with an optional evaluator;
  • assert_vector_eq_approx!() - asserts approximate equality of expected and actual vectors of values, with an optional evaluator;
  • assert_vector_ne_approx!() - asserts approximate inequality of expected and actual vectors of values, with an optional evaluator;

Structures

No public structures are defined at this time.

Traits

The following traits are defined:

  • ApproximateEqualityEvaluator - prescribes the (non-mutating) instance method #evaluate(), allowing custom evaluators to be defined for use with the assertion macros;
  • TestableAsF64 - prescribes the (non-mutating) instance method #testable_as_f64() : f64, and provides implementation for any type that implements the ToF64 trait defined in the base-traits crate;

Examples

T.B.C.

Project Information

Where to get help

GitHub Page

Contribution guidelines

Defect reports, feature requests, and pull requests are welcome on https://github.com/synesissoftware/test_help-rs.

Dependencies

Crates upon which test_help-rs depend:

Dev Dependencies

None currently.

  • shwild.Rust defines functionality for matching strings against SHell-compatible WILDcard patterns, including matching assertion macros that are useful in unit-testing;

License

test_help-rs is released under the 3-clause BSD license. See LICENSE for details.

Dependencies

~145KB