###############################################################################
# Name: testFileUtil.py                                                       #
# Purpose: Unit tests for the fileutil functions of ebmlib                    #
# Author: Cody Precord <cprecord@editra.org>                                  #
# Copyright: (c) 2009 Cody Precord <staff@editra.org>                         #
# License: wxWindows License                                                  #
###############################################################################

"""Unittest cases for testing the fileutil functions"""

__author__ = "Cody Precord <cprecord@editra.org>"
__svnid__ = "$Id: testFileUtil.py 60508 2009-05-03 21:54:15Z CJP $"
__revision__ = "$Revision: 60508 $"

#-----------------------------------------------------------------------------#
# Imports
import os
import wx
import unittest

# Local modules
import common

# Module to test
import ebmlib

#-----------------------------------------------------------------------------#
# Test Class

class FileUtilTest(unittest.TestCase):
    """Tests for the fileutil functions of ebmlib"""
    def setUp(self):
        self.ddir = common.GetDataDir()
        self.tdir = common.GetTempDir()
        self.fpath = common.GetDataFilePath(u'test_read_utf8.txt')

    def tearDown(self):
        common.CleanTempDir()

    #---- Tests ----#

    def testGetFileExtension(self):
        """Test getting a files extension"""
        ext = ebmlib.GetFileExtension(u'hello.txt')
        self.assertTrue(ext == u'txt')

        ext = ebmlib.GetFileExtension(u'hello.py')
        self.assertTrue(ext == u'py')

        ext = ebmlib.GetFileExtension(u'hello.txt.tmp')
        self.assertTrue(ext == u'tmp')

    def testGetFileModTime(self):
        """Test getting a files modtime"""
        mtime = ebmlib.GetFileModTime(self.fpath)
        self.assertNotEqual(mtime, 0, "Mtime was: " + str(mtime))

    def testGetFileName(self):
        """Test that getting the file name from a string returns the correct
        string.

        """
        roots = (("Home", "foo", "projects"), ("usr", "bin"),
                 ("Users", "bar", "Desktop"))
        fname = "test.py"
        paths = [os.path.join(os.sep.join(root), fname) for root in roots]
        for path in paths:
            self.assertEqual(fname, ebmlib.GetFileName(path),
                             "ebmlib.GetFileName(%s) != %s" % (path, fname))

    def testGetFileSize(self):
        """Test getting a files size"""
        self.assertTrue(ebmlib.GetFileSize(self.fpath) != 0)
        self.assertTrue(ebmlib.GetFileSize(u'SomeFakeFile.txt') == 0)

    def testGetPathName(self):
        """Test that getting the path name from a string returns the correct
        string.

        """
        roots = (("Home", "foo", "projects"), ("usr", "bin"),
                 ("Users", "bar", "Desktop"))
        fname = "test.py"
        paths = [os.sep.join(root) for root in roots]
        for path in paths:
            tmp = os.path.join(path, fname)
            self.assertEqual(path, ebmlib.GetPathName(tmp),
                             "ebmlib.GetPathName(%s) != %s" % (tmp, path))

    def testGetUniqueName(self):
        """Test getting a unique file name at a given path"""
        path = ebmlib.GetUniqueName(self.ddir, u'test_read_utf8.txt')
        self.assertTrue(path != self.fpath)

        # File that does not yet exist
        path = common.GetDataFilePath(u'newfile.txt')
        uname = ebmlib.GetUniqueName(self.ddir, u'newfile.txt')
        self.assertTrue(path == uname)

    def testMakeNewFile(self):
        """Test the MakeNewFile utility"""
        result = ebmlib.MakeNewFile(self.tdir, u'test_new_file.txt')
        self.assertTrue(result[0])
        self.assertTrue(os.path.exists(result[1]))
        
        result2 = ebmlib.MakeNewFile(self.tdir, u'test_new_file.txt')
        self.assertTrue(result2[1])
        self.assertTrue(result[1] != result2[1])

    def testMakeNewFolder(self):
        """Test the MakeNewFoloder utility"""
        result = ebmlib.MakeNewFolder(self.tdir, u'test_new_folder')
        self.assertTrue(result[0])
        self.assertTrue(os.path.exists(result[1]))
        
        result2 = ebmlib.MakeNewFolder(self.tdir, u'test_new_folder')
        self.assertTrue(result2[1])
        self.assertTrue(result[1] != result2[1])
