On Thursday, October 11, 2018 at 4:17:07 PM UTC-5, MN wrote:

> What is a dev node?

Any node, but @test or @button nodes are easiest to use.​

Is that defined somewhere? 
>

Yes, in the next sentence.

Suppose I have a node whose contents are:
>
> from operator import mul
>
> def factorial(number):
>     if number < 0:
>         raise ValueError
>     if number == 0:
>         return 1
>     return reduce(mul, range(number+1), 1)
>
> It's a simple function. Imagine this is a helper function that will be 
> used by some other function that interacts with Leo. It is not saved to any 
> file (beyond the .leo file, of course). I would like to create a unit test 
> for this. What is the best way? 
>

There are several ways.


*1. Use Python doctests <https://docs.python.org/2/library/doctest.html>*

Here is a tested code: Create a node, @test factorial, whose body is: 

import doctest
from operator import mul

def factorial(number):
    """
    >>> factorial(5)
    120
    """
    if number < 0:
        raise ValueError
    if number == 0:
        return 1
    return reduce(mul, range(number+1), 1)

doctest.run_docstring_examples(factorial, globals(), name='factorial')

Execute the code with with Ctrl-B, or with one of the 
run-*-unit-tests-locally commands.

This will fail, for different reasons, on both Python 2 and 3.

*2. Define unit tests using UnitTest.TestCase*

Alternatively, you can define explicit tests.  Here is tested code.

import unittest
from operator import mul

def factorial(number):
    if number < 0:
        raise ValueError
    if number == 0:
        return 1
    return reduce(mul, range(number+1), 1)
    
class TestFactorial(unittest.TestCase):

    def test1(self):
        self.assertTrue(factorial(5)==120)
  
suite = unittest.TestLoader().loadTestsFromTestCase(TestFactorial)
unittest.TextTestRunner(verbosity=1).run(suite)

Again, you can run this with Ctrl-B, or with one of Leo's unit test 
commands.

Edward

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.

Reply via email to