Xion / pyqcy

QuickCheck-like testing framework for Python
http://xion.io/pyqcy
Other
41 stars 0 forks source link

Piping arbitraries through functions #6

Closed Xion closed 12 years ago

Xion commented 12 years ago

It would be useful (especially in the context of #5) to be able to quickly create an arbitrary generator that works by putting a value from another generator through a function. Essentially this is a map:

from pyqcy import *

import string
import json

import flask

@qc
def creating_user_works(
    request=map(json.dumps, schema({
        'login': str_(of=string.ascii_letters, | string.digits,
            min_length=3, max_length=20),
        'password': str_(min_length=8, max_length=128),
    }))
):
    http_client = flask.current_app.test_client()
    response = http_client.post('/api/create_user', request)
    assert json.loads(response).get('status') == 'OK'

map should probably be replaced by something else. We could even do it "applicative style" or "Unix style", with function overloading like that:

@qc
def creating_user_works(
    request=func(json.dumps) << schema( ...),
    # or
    request=schema(...) | func(json.dumps)
):

Note that this is mostly syntactic now; as long as arbitrary generators are normal generators, list comprehension can do the same. However, if we make those generators into custom objects at some point (due to variety of possible reasons), a special syntax for mapping will be necessary.

Xion commented 12 years ago

Implemented as apply function. Sugaring with overloaded operators will be left for later.