# testfdemo.py
# Lillian Lee (LJL2) and Steve Marschner (SRM2)
# Feb 19, 2014

"""test module for fdemo"""

import fdemo
import cornelltest

def test(fn):
    """Test whether plucking a flower with fn reduces the number of petals,
    flips the he_loves_me variable, prints the right thing"""

    # pushes checking the print output to the author/tester,
    # for simplicity.  (More complex possiblities are possible)

    f = fdemo.Flower() # need a flower to test

    nplist = [1,2,3,4,5] # list of number of petals
    loveslist = [True, False, True, False, True] # list of he_loves_me values
    printlist = ['he loves me!', 'he loves me not', 'he loves me', 'he loves me not',
                 'he loves me']

    # not doing all possible test cases; this is just a demo

    f.num_petals = 5
    cornelltest.assert_true(f.he_loves_me)
    print "Eyeball this: the next should be 'he loves me'"
    fn(f)
    print
    cornelltest.assert_equals(4, f.num_petals)
    cornelltest.assert_false(f.he_loves_me)

    print "Eyeball this: the next should be 'he loves me not'"
    fn(f)
    print
    cornelltest.assert_equals(3, f.num_petals)
    cornelltest.assert_true(f.he_loves_me)

    print "Eyeball this: the next should be 'he loves me'"
    fn(f)
    print
    cornelltest.assert_equals(2, f.num_petals)
    cornelltest.assert_false(f.he_loves_me)

    f.num_petals = 1
    f.he_loves_me = True
    print "Eyeball this: the next should be 'he loves me!'"
    fn(f)
    print
    cornelltest.assert_equals(0, f.num_petals)
    cornelltest.assert_equals(None, f.he_loves_me)

    f.num_petals = 1
    f.he_loves_me = False
    print "Eyeball this: the next should be 'he loves me not!'"
    fn(f)
    print
    cornelltest.assert_equals(0, f.num_petals)
    cornelltest.assert_equals(None, f.he_loves_me)


if __name__ == '__main__':

    # demonstration of creating a list, doing some operations on it
    fnlist = [fdemo.pluck, fdemo.pluck1] # list of functions! Whoa!
    fnlist.append(fdemo.pluck2) # Add another function to the end.
    fnlist.append(fdemo.pluck3) # Add another function to the end
    fnlist.append('badstring')   # Can add anything we want to a list
    fnlist.pop()    # Remove the thing at the end (giving pop an argument i
                    # removes the item at index i).      
                    # In this case, we aren't storing the returned
                    # value anywhere.

    # this "for" statement makes variable fn take each of the values in fnlist,
    # in turn
    for fn in fnlist:
        print "testing function " + fn.__name__ # prints name of function
        test(fn) # yes, functions can be arguments - they are objects, actually!

    print "All tests for fdemo passed"