All Projects → csparpa → fluentcheck

csparpa / fluentcheck

Licence: MIT license
Fluent assertions for Python

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to fluentcheck

beJS
Simple, light-weight assertions framework for javascript
Stars: ✭ 12 (-84.81%)
Mutual labels:  assertions, assertion-library
php-underscore
PHP underscore inspired &/or cloned from _.js, with extra goodies like higher order messaging
Stars: ✭ 42 (-46.84%)
Mutual labels:  fluent-interface, fluent
Acf Fluent
✒️ A fluent interface for the Advanced Custom Fields WordPress plugin
Stars: ✭ 264 (+234.18%)
Mutual labels:  fluent-interface, fluent
Enzyme
JavaScript Testing utilities for React
Stars: ✭ 19,781 (+24939.24%)
Mutual labels:  assertions, assertion-library
Fluentassertions
A very extensive set of extension methods that allow you to more naturally specify the expected outcome of a TDD or BDD-style unit tests. Targets .NET Framework 4.7, .NET Core 2.1 and 3.0, as well as .NET Standard 2.0 and 2.1. Supports the unit test frameworks MSTest2, NUnit3, XUnit2, MSpec, and NSpec3.
Stars: ✭ 2,449 (+3000%)
Mutual labels:  assertions, fluent-assertions
jsonassert
A Go test assertion library for verifying that two representations of JSON are semantically equal
Stars: ✭ 102 (+29.11%)
Mutual labels:  assertions, assertion-library
json matcher
Library for simplifying data verification in functional tests for your JSON-based APIs
Stars: ✭ 24 (-69.62%)
Mutual labels:  assertions, assertion-library
kiwi
Fluent assertions for Kotlin
Stars: ✭ 17 (-78.48%)
Mutual labels:  assertions, fluent-assertions
slf4j-fluent
A fluent api for SLF4J
Stars: ✭ 19 (-75.95%)
Mutual labels:  fluent
nim-contra
Lightweight Self-Documenting Design by Contract Programming and Security Hardened mode.
Stars: ✭ 46 (-41.77%)
Mutual labels:  assertion-library
FluentExcel
Use Fluent API to configure POCO excel behaviors, and then provides IEnumerable<T> has save to and load from excel functionalities.
Stars: ✭ 73 (-7.59%)
Mutual labels:  fluent
es-feature-detection
ECMAScript feature and API detection
Stars: ✭ 16 (-79.75%)
Mutual labels:  check
empty-dir
Check if a directory is empty.
Stars: ✭ 19 (-75.95%)
Mutual labels:  check
ugo
Simple and expressive toolbox written in Go
Stars: ✭ 27 (-65.82%)
Mutual labels:  fluent-interface
is-primitive
Is the typeof value a javascript primitive?
Stars: ✭ 35 (-55.7%)
Mutual labels:  check
picolisp-unit
Unit Testing framework for PicoLisp
Stars: ✭ 22 (-72.15%)
Mutual labels:  assertions
jasmine2-custom-message
custom failure message on any jasmine v2 assertion
Stars: ✭ 27 (-65.82%)
Mutual labels:  assertions
Dexiom.EPPlusExporter
A very simple, yet incredibly powerfull library to generate Excel documents out of objects, arrays, lists, collections, etc.
Stars: ✭ 19 (-75.95%)
Mutual labels:  fluent-interface
vim-UT
Unit Testing plugin for Vim
Stars: ✭ 18 (-77.22%)
Mutual labels:  assertions
jasmine-custom-message
custom failure message on any jasmine v1.3 assertion
Stars: ✭ 15 (-81.01%)
Mutual labels:  assertions

Fluentcheck

Fluent assertion framework for Python

Bored of using assert multiple times to check types, intervals, etc. on data passed as input to your Python functions? This generates a lot of boilerplate code.

fluentcheck helps you reducing the lines of code providing a human-friendly and fluent way to make assertions.

Instead of:

def my_function(n, obj):
    assert n is not None
    assert isinstance(n, float)
    assert 0. < n < 1.
    assert obj is not None
    assert isinstance(obj, MyCustomType)

you just streamline the assertions ("checks") you want to make, in a fluent way:

from fluentcheck import Check

def my_function(n, obj):
    Check(n).is_not_None().is_float().is_between(0., 1.)
    Check(obj).is_not_None().is_subtype_of(MyCustomType)

You can even use the alternate Is syntax:

from fluentcheck import Is

def my_function(n, obj):
    Is(n).not_none.float.between(0, 1)
    Is(obj).not_none.subtype_of(MyCustomType)

Fluentcheck can also be used as an assertion framework in tests.

Installation

pip install fluentcheck

or

python setup.py install

Only Python 3 is supported (because you DID update your code to Python3, didn't you?)

Usage

Check API

Simply instantiate the Check wrapper around the Python value you want to check out, then fluently append assertions like this:

from fluentcheck import Check

Check(my_value).assertion1().assertion2().assertion3() # and so on

Is API

Simply instantiate the Is wrapper around the Python value you want to check out, then fluently append assertions like this:

from fluentcheck import Is

Is(my_value).assertion1.assertion2_with_params(a, b).assertion3 # and so on

When an assertion fails

What if an assertion fails? A CheckError is raised: just catch it!

Please notice: if the order of assertions matters to your overall goal, then take care of it!

Linters and Is API

You may run into warnings for code such as this when using the Is API:

n = .4
Is(n).not_none.float.non_negative

# WARNING: Statement seems to have no effect ...

While not required for assertions to work, you can always "call" the last assertion to make linters happy:

n = .4
Is(n).not_none.float.non_negative()

# Happy, calling non_negative() has no effect but the linter warnings are gone.

What can I actually check with it?

To date, here's a list of assertions you can make:

Check API

# Check(value).X where X is one of:

# Numbers
is_none()
is_not_none()
is_number()
is_not_number()
is_integer()
is_not_integer()
is_long()
is_not_long()
is_float()
is_not_float()
is_real()
is_not_real()
is_complex()
is_not_complex()
is_positive()
is_not_positive()
is_negative()
is_not_negative()
is_zero()
is_not_zero()
is_at_least(lower)
is_at_most(upper)
is_between(lower, upper)
is_not_between(lower, upper)

# Sequences
is_empty()
is_not_empty()
is_iterable()
is_not_iterable()
is_couple()
is_triplet()
is_nuple(dimension)
has_dimensionality(dimensionality)

# Tuples
is_tuple()

# Lists
is_list()

# Strings
is_string()
is_not_string()
contains_numbers()
not_contains_numbers()
contains_numbers_only()
contains_chars()
not_contains_chars()
contains_chars_only()
contains_spaces()
not_contains_spaces()
contains_char(_char)
not_contains_char(_char)
is_shorter_than(n_chars)
is_longer_than(n_chars)
has_length(n_chars)
has_not_length(n_chars)
is_lowercase()
is_not_lowercase()
is_uppercase()
is_not_uppercase()
is_camelcase()
is_not_camelcase()
is_snakecase()
is_not_snakecase()
is_unicode()
is_not_unicode()
is_json()
is_not_json()
is_yaml()
is_not_yaml()
is_xml()
is_not_xml()
matches(regex)
not_matches(regex)

# Booleans
is_boolean()
is_not_boolean()
is_true()
is_not_true()
is_truthy()
is_not_truthy()
is_false()
is_not_false()
is_falsy()
is_not_falsy()
has_same_truth_of(val)
has_opposite_truth_of(val)

# Dictionaries
is_dict()
is_not_dict()
has_keys(*args)
has_not_keys(*args)

# Sets
is_set()
is_not_set()
is_subset_of(_set)
is_not_subset_of(_set)
is_superset_of(_set)
is_not_superset_of(_set)
intersects(_set)
not_intersects(_set)

# Functions
is_runnable()
is_not_runnable()

# Modules
is_module()
is_not_module()

# Type hierarchy
is_subtype_of(_type)
is_not_subtype_of(_type)

# Custom types
is_of_type(_type)
is_not_of_type(_type)

# Geographic coords
is_latitude()
is_longitude()
is_azimuth()
is_geopoint()

# UUIDs
is_uuid1()
is_not_uuid1()
is_uuid4()
is_not_uuid4()

Is API

# Is(value).X where X is one of:

# Numbers
none
not_none
number
not_number
integer
not_integer
float
not_float
real
not_real
complex
not_complex
positive
not_positive
negative
not_negative
zero
not_zero
at_least(lower)
at_most(upper)
between(lower, upper)
not_between(lower, upper)

# Sequences
empty
not_empty
iterable
not_iterable
has_dimensionality(dimensionality)

# Tuples
tuple

# Lists
list

# Strings
string
not_string
contains_numbers
not_contains_numbers
only_numbers
contains_chars
not_contains_chars
only_chars
contains_spaces
not_contains_spaces
contains_char(_char)
not_contains_char(_char)
shorter_than(n_chars)
longer_than(n_chars)
length(n_chars)
not_length(n_chars)

json
not_json
matches(regex)
not_matches(regex)

# Booleans
boolean
not_boolean
true
not_true
truthy
not_truthy
false
not_false
falsy
not_falsy
has_same_truth_of(val)
has_opposite_truth_of(val)

# Dictionaries
dict
not_dict
has_keys(*args)
has_not_keys(*args)

# Sets
set
not_set
subset_of(_set)
not_subset_of(_set)
superset_of(_set)
not_superset_of(_set)
intersects(_set)
not_intersects(_set)

# Type hierarchy
subtype_of(_type)
not_subtype_of(_type)

# UUIDs
is_uuid1()
is_not_uuid1()
is_uuid4()
is_not_uuid4()

Coming soon

The following checks will be added in the upcoming versions:

# Dates
is_date()
is_not_date()
is_datetime()
is_not_datetime()
is_before(_datetime)
is_not_before(_datetime)
is_after(_datetime)
is_not_after(_datetime)
is_today()
is_not_today()
is_yesterday()
is_not_yesterday()
is_tomorrow()
is_not_tomorrow()
is_weekend()
is_not_weekend()
is_this_month()
is_not_this_month()
is_previous_month()
is_not_previous_month()
is_next_month()
is_not_next_month()
is_this_year()
is_not_this_year()
is_last_year()
is_not_last_year()
is_next_year()
is_not_next_year()
is_leap_year()
is_not_leap_year()
is_this_century()
is_not_this_century()
is_before(date)
is_not_before(date)
is_after(date)
is_not_after(date)
is_between_dates(lower, upper)
is_not_between_dates(lower, upper)
is_timezone_aware()
is_not_timezone_aware()
has_timezone(tz)
has_not_timezone(tz)

# Geographic coords
is_plus_code()  # https://plus.codes/
is_not_plus_code()

# Check against a custom rule (lambda)
conforms_to(func)
not_conforms_to(func)

# Sequences
is_sorted(rule=func)
is_not_sorted(rule=func)
is_subsequence_of(subseq)
is_not_subsequence_of(subseq)
has_duplicates()
has_not_duplicates()

# Objects
contains(element)
not_contains(element)
has_attribute(_attr)
has_not_attribute(_attr)

License

MIT license

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].