Skip to main content

Alternative syntax for comparing/asserting expressions in Python 3.

Project description

Overview


This project primarily updates compare to be used on accessible on Python3

Compare is a compact utility that provides an alternative, expressive syntax for comparing data values. You may use it as a drop-in replacement for the "assert..." style of assertions in python.

The compare API exposes the expect constructs which allows you to compare values with readable and extensible syntax. It was designed to be a stand-alone alternative assertion syntax. As such you may use it as-is with your favorite testing/specification framework.

Requirements


The core implementation of compare is a single file module with no additional requirements beyond the Python Standard Library_.

Installation


The simplest and recommended way to install compare is with Pip. You may install the latest stable release from PyPI with pip::

> pip install compare3

If you do not have pip, you may use easy_install::

> easy_install compare3

Alternatively, you may download the source package from the compare3 page on PyPI_, extract it and install it using::

> python setup.py install

What's in it?


You get the "expect" starter, a simple class that allows you to compare two values and fail if the outcome does not meet your expectation. This class enables you to describe the expected outcome in a pythonic BDD manner.

Compare shines brightest when you are crafting executable specifications for your software. It helps you maintain your flow of thought without succumbing to test-focused non-pythonic distrations like "self.assertEqual(s)...", "self.assertTrue", etc.

#Examples


Basic Usage

from compare3 import expect

test_value="grapes"
expect(test_value).equal_to("grapes")
expect(test_value).is_not_.equal_to("apples")

Chaining

expect expressions can be chained ot validate more than one assertion with a single line

from compare3 import expect
test_value=30
expect(test_value).is_.numeric().and_.greater_than(15)

On Failure of Assertion

When an expectation fails, An UnexpectedExpectation error is raised. This Error inherits from AssertionError maintaining compatibility with standard python assertions.

from compare3 import UnmetExpectation,expect
test_value=20
try:
    expect(test_value).equal_to("apples")
except UnmetExpectation as e:
    expect(str(e)).equal_to("'20' is not equal to 'apples'")

try:
    expect(test_value).equal_to("apples")
except AssertionError as e:
    expect(str(e)).equal_to("'20' is not equal to 'apples'")

Callables

compare3 also comes with expect_call which allows for additional validation on callable objects.

validation can occur on the return value or when raising an error.

.returns will validate the return of the callable with it's associated arguments with the expected value argument.

.raises will call the callable and check if the expected Exception was raised. If either a different exception was raised or no exception was raised it will cause an UnmetExpectation.

You can optionally also define an error message. This uses re.fullMatch to match the error message.

from compare3 import expect_call
def summer(*args):
    return sum([int(i) for i in args])

expect_call(summer,12,50,45).returns(107)
expect_call(summer,"25","apples","75").raises(ValueError,
                                  "invalid literal for .* 'apples'")

Forked From Source: https://github.com/rudylattae/compare

Thank you rudylattae for an interesting idea on making assertions.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

compare3-1.0.4.tar.gz (7.0 kB view details)

Uploaded Source

File details

Details for the file compare3-1.0.4.tar.gz.

File metadata

  • Download URL: compare3-1.0.4.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.2

File hashes

Hashes for compare3-1.0.4.tar.gz
Algorithm Hash digest
SHA256 7680e3fb1b2ff26f9b8432b835ad21dfe4efb2fb5a58b001e4f5b8df42411478
MD5 c49c21c6ba6cd826dcd2ba14c789f28b
BLAKE2b-256 6c3c275a68e059cf1794d06d0f6c03e4f5e8630e092b06d1c53cfa379d9204e7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page