'''
@date 19-Feb-12
@author: Tyson
@summary: A unittest for the find module.
'''
import unittest
import os
import sys
from find import find
from StringIO import StringIO

class Test(unittest.TestCase):      
    def test_empty_regex_exception(self):
        path = "C:\\"
        regexString = ""
        self.assertRaises(ValueError, find().find_file, path, regexString)
    
    def test_invalid_path_exception(self):
        path = "INVAL#D" 
        regexString = ".*"
        self.assertRaises(ValueError, find().find_file, path, regexString)

    def test_invalid_regex(self):
        path = "C:\\Users\\Tyson\\workspace"
        regexString = "Starbuck*)#@.txt"
        self.assertRaises(ValueError, find().find_file, path, regexString)
    
    def test_empty_parameters(self):
        path = ""
        regexString = ""
        self.assertRaises(ValueError, find().find_file, path, regexString)
        
    def test_find_myself_regex(self):
        path = os.getcwd()
        regexString = "test_find.*py$"
        expected = "%s%s%s" % (path, os.sep, "test_find.py\n")
        # Redirect stdout to capture results
        sys.stdout = result = StringIO()
        find().find_file(path, regexString)
        sys.stdout = sys.__stdout__
        self.assertEquals(expected, result.getvalue())
    
    def test_find_this_dir_regex(self):
        (head, tail) = os.path.split(os.getcwd())
        path = head
        regexString = "^src$"
        expected = "%s%s%s" % (path, os.sep, "src\n")
        # Redirect stdout to capture results
        sys.stdout = result = StringIO()
        find().find_file(path, regexString)
        sys.stdout = sys.__stdout__
        self.assertEquals(expected, result.getvalue())    
        
if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()