Softpedia
 


LINUX CATEGORIES:



GLOBAL PAGES >>
NEWS ARCHIVE >>
SOFTPEDIA REVIEWS >>
MEET THE EDITORS >>
WEEK'S BEST
  • Linux Kernel 3.9.6 / 3....
  • Linux Kernel 3.0.82 LTS...
  • KDE Software Compilatio...
  • PulseAudio 4.0
  • Wireshark 1.10.0
  • NetworkManager 0.9.8.2
  • LibreOffice 3.6.6 / 4.0...
  • SystemRescueCd 3.7.0
  • Linux Kernel 3.10 RC6
  • Ubuntu Tweak 0.8.5
  • Home > Linux > Programming > Libraries

    redef 1.3

    Download button

    No screenshots available
    Downloads: 80  View global page NEW!  Tell us about an update
    User Rating:
    Rated by:
    NOT RATED
    0 user(s)
    Developer:

    License / Price:

    Last Updated:

    Category:
    Joe Heyming | More programs
    GPL v3 / FREE
    July 18th, 2012, 05:51 GMT
    ROOT / Programming / Libraries

     Read user reviews (0)  Refer to a friend  Subscribe

    redef description

    Test utility for redefining functions

    redef is a Python test utility for redefining functions.

    To install, just run:

    python ./setup.py build
    python ./setup.py install


    See test_redef.py under your install path for examples.

    Redef is inteded to create lexically scope variables that when destroyed undo mock behavior.

    It was inspired by a perl module, Test::Resub - http://search.cpan.org/~airwave/Test-Resub-1.02/lib/Test/Resub.pm

    The best examples use unittest, but it should work with any testing framework.

    import unittest
    from redef import redef

    class MyClass:
          def lame_function(self):
              return "something I don't want"

    def MyTest(unittest.TestCase):
        def test1(self):
            myobj = MyClass()
            self.assertEqual(myobj.lame_function(), "something I don't want")
            want = 'something I want'
            rd_lf = redef(MyClass, 'lame_function', lambda s: want)
            self.assertEqual(myobj.lame_function(), want)

            # after test1, rd_lf gets deleted and resets

        def test2(self):
            myobj = MyClass()
            # test2 is uneffected by test1
            self.assertEqual(myobj.lame_function(), "something I don't want")

    This doesn't have to be a function, you can also redefine attributes directly on an object.

    class MyClass:
          unpredictable = 'random string'

    my_global_object = MyClass()
    ...
         def test3(self):
             rd_u = redef(my_global_object, 'unpredictable', 'unit testable string')
             # ... test something awesome!
             self.assertEqual(my_global_object.unpredictable, 'unit testable string')
         def test4(self):
             # hey, my_global_object is back to being unpredictable
             self.assertEqual(my_global_object.unpredictable, 'unpredictable')


    There are other useful functions provided on the redef object itself: Class Redef:

    called():
            Stores how many times a redef'd function was called.
    method_args():
            Stores the most recent *args to the redef'd function.
    named_method_args():
            Stores the most recent **kwargs to the redef'd function.

    reset():
            Sets called, method_args, and name_method_args back to the default state of 0, None, None


    Redef also provides a freebie static functions:
    redef(obj, key, value):
            Static constructor of a Redef object

    # these static functions were provieded to show the usefulness of redef:
    #   for example, you could capture stdout of a function call, and after capturing it,
    #   sys.stdout goes back to normal
    Class WriteCapturer:
        Has 2 variables you want: output, returned

    stdout_of(func, *args, **kwargs):
            Call a function and capture the stdout
            Returns a WriteCapturerobject that has the stdout and the return value of calling func

    stderr_of(func, *args, **kwargs):
            Call a function and capture the stderr
            Returns a WriteCapturerobject that has the stderr and the return value of calling func



    Product's homepage

    Requirements:

    · Python

      


    TAGS:

    redefine functions | Python library | Python | redefine | functions

    Go to top

    WindowsGamesDriversMacLinuxScriptsMobileHandheldNews

    SUBMIT PROGRAM   |   ADVERTISE   |   GET HELP   |   SEND US FEEDBACK   |   RSS FEEDS   |   UPDATE YOUR SOFTWARE   |   ROMANIAN FORUM