import unittest, sys, os
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), ".."))
import framework.util as util

class UtilTests (unittest.TestCase):
    def test_try_f(self):
        self.assertEqual(util.try_f(int, "10", -1), 10, "Did not convert string to int.")
        self.assertEqual(util.try_f(int, "foo", -1), -1, "Did not get given default value as expected.")
        self.assertEqual(util.try_f(int, "foo"), None, "Did not get None as expected.")
        self.assertEqual(util.try_f(str, 10, "ten"), "10", "Did not convert int to string.")

    def test_safeuni(self):
        class HasUnicode(object):
            def __unicode__(self):
                return u"unicode here"
        
        uni = u"unicode"
        base = "basestring"
        hasUni = HasUnicode()
        noUni = 5
        
        self.assertEqual(type(util.safeuni(uni)), unicode, "Did not return a unicode string from uni.")
        self.assertEqual(type(util.safeuni(base)), unicode, "Did not return a unicode string from base.")
        self.assertEqual(type(util.safeuni(hasUni)), unicode, "Did not return a unicode string from hasUni.")
        self.assertEqual(util.safeuni(hasUni), "unicode here", "Did not return the expected string from hasUni.")
        self.assertEqual(type(util.safeuni(noUni)), unicode, "Did not return a unicode string from noUni.")

if __name__ == "__main__":
    unittest.main()
@@ -1367,16 +1367,1268 @@
 Uni.%22)%0A%0A
+    def test_safestr(self):%0A        class HasUnicode(object):%0A            def __unicode__(self):%0A                return u%22unicode here%22%0A        %0A        uni = u%22unicode%22%0A        base = %22basestring%22%0A        hasUni = HasUnicode()%0A        noUni = 5%0A        uniList = %5Bu%22z%22, u%22y%22%5D%0A        strList = %5B%22a%22, %22b%22, %22c%22%5D%0A        numList = %5B1, 2, 3%5D%0A        %0A        self.assertEqual(type(util.safestr(uni)), str, %22Did not return a str string from uni.%22)%0A        self.assertEqual(type(util.safestr(base)), str, %22Did not return a str string from base.%22)%0A        self.assertEqual(type(util.safestr(hasUni)), str, %22Did not return a str string from hasUni.%22)%0A        self.assertEqual(type(util.safestr(noUni)), str, %22Did not return a str string from noUni.%22)%0A        self.assertEqual(type(util.safestr(strList)%5B0%5D), str, %22Did not return a str string from str list.%22)%0A        self.assertNotEqual(type(uniList%5B0%5D), str, %22Returned a str string from uni list.%22)%0A        self.assertEqual(type(util.safestr(uniList)%5B0%5D), str, %22Did not return a str string from uni list.%22)%0A        self.assertNotEqual(type(numList%5B0%5D), str, %22Returned a str string from num list.%22)%0A        self.assertEqual(type(util.safestr(numList)%5B0%5D), str, %22Did not return a str string from num list.%22)%0A%0A%0A
 if __nam

add safestr test