4 ms·
Making tests part of language syntax is an interesting idea. Of course it's not equivalent, but Python doctest module at least allows you to keep tests close t
by ash 13y ago
Making tests part of language syntax is an interesting idea.
Of course it's not equivalent, but Python doctest module at least allows you to keep tests close to the code. Here's Pyret example, ported to doctest:
def sum(l):
"""
>>> sum([])
0
>>> sum([1, 2, 3])
6
"""
return reduce(lambda x, y: x+y, l, 0)
if __name__ == "__main__":
import doctest; doctest.testmod()
- laureny 13y ago> Making tests part of language syntax is an interesting idea. It's been tried many times before and it has always failed. A few reasons: - It clutters the code - If you need simple tests, asserts suffice - If you need more sophisticated tests, write functional tests, separately The approach offered by Pyret (along with similar ones, such as design by contract) are compromises that are the worst of both worlds.
- skrishnamurthi 13y agoIt's actually widely used in Racket already. Yesterday a lead programmer at a major financial institution told me that he liked that style so much, he'd incorporated it into his company's OCaml system, and they use it in their production systems (lots of high-volume trading). Hardly "failure"s. Nevertheless, perhaps you could provide some pointers to the "many" projects that have tried it before? Assertions are not tests. This is a fundamental misunderstanding of the difference. Pyret's `check` blocks exist to write complex tests, separate tests from definitions, or write tests that cross multiple functional units. So they offer the power of regular testing frameworks. Thus, `where` is the bonus, not a "compromise".
- anaphor 13y agoHave you thought about adding some kind of QuickCheck-ish model where test cases can be randomly generated for various types?
- tel 13y agoThis exists in Haskell's doctest library -- | Documentation goes here -- -- > 2 + 2 == 4 -- True -- -- prop> \lst -> reverse (reverse lst) == lst
- skrishnamurthi 13y agoYes, of course, we've thought about it. Equally importantly, we have a "satisfies" keyword for stating checks about properties. (See the example on the Pyret home page.) We use test oracles extensively in teaching; see these two assignments: http://cs.brown.edu/courses/cs019/2012/assignments/sortacle http://cs.brown.edu/courses/cs019/2012/assignments/sortacle , http://cs.brown.edu/courses/cs019/2012/assignments/oracle http://cs.brown.edu/courses/cs019/2012/assignments/oracle .
- mercurial 13y agoThis might work better for pure functions. I can't imagine it working very well for complex, stateful code which requires mocking and resetting the state between tests.
- acjohnson55 13y agoThey didn't say one way or the other whether the language was pure FP, but I don't think any of the examples featured mutuality
- skrishnamurthi 13y agoPyret is not pure. We have mutation. But mutation has its own syntax different from regular binding. Variables (as opposed to identifiers) are preceded by "var", and variable mutation uses ":=". [Similarly for fields.] This it to make clear to all forms of readers -- programmers, compilers, IDEs -- when something is mutable.
- tel 13y agoGiven that Pyret is functional, there's a good chance there will be an emphasis on simple, pure functions. Beyond that, I'd agree completely. Doctests, for instance, are quite terrible when you can't fit all of the logic into one line, but then I think it's usually a sign of a well-designed function when it's good a good enough interface to get a few pithy doctests in.
- skrishnamurthi 13y agoThough Pyret is not pure, our emphasis is heavily on "functional first", even for objects. We believe state should be used carefully and for good reasons. HtDP (www.htdp.org) has two whole chapters on state at the end, giving design recipes for their use, and I'm revising some of these in PAPL (papl.cs.brown.edu/2013/).
- skrishnamurthi 13y agoThis is why a test block is a block. Writing inline tests for stateful functions in Racket was a bit painful because it is so heavily expression-oriented. We want to make this feel more natural. (Another virtue of test blocks: you can write little local helper functions. There's an example of this on the Pyret home page.)
- ducklord 13y ago> Yesterday a lead programmer at a major financial institution told me that he liked that style so much, he'd incorporated it into his company's OCaml system, and they use it in their production systems (lots of high-volume trading). Hmm...Jane Street?