# Path: vimswitch/Application.py
# class Application:
#     """
#     A container for components that are used application-wide.
# 
#     Examples of how this class can be used:
# 
#       - Set a component
#         `app.foo = myFoo`
# 
#       - Get (or create) a component:
#         `foo = app.get('foo', Foo())`
#         This will try to return `app.foo`. If that does not exist, then it will
#         set `app.foo = Foo()` and return that instead.
# 
#       - Get an already created component
#         `foo = app.foo`
#         Use this when you are sure that app.foo has already been set
#     """
# 
#     def get(self, attr, default):
#         """
#         Returns self.attr. If attr does not exist, then make self.attr = default
#         and then return default.
#         """
#         if hasattr(self, attr):
#             return getattr(self, attr)
#         else:
#             setattr(self, attr, default)
#             return default
# 
# Path: vimswitch/Profile.py
# class Profile:
#     def __init__(self, name):
#         self.name = name
# 
#     def getName(self):
#         return self.name
# 
#     def getDirName(self):
#         return self.name.replace('/', '.')
# 
#     def __eq__(self, other):
#         return isinstance(other, Profile) and self.name == other.name
# 
#     def __ne__(self, other):
#         return not self.__eq__(other)
# 
#     def __repr__(self):
#         return "Profile('%s')" % self.name
# 
# Path: vimswitch/Settings.py
# def getSettings(app):
#     return app.get('settings', createSettings(app))
# 
# Path: vimswitch/SwitchProfileAction.py
# class SwitchProfileAction(Action):
# 
#     def __init__(self, settings, profileCache, profileCopier, profileRetriever):
#         Action.__init__(self)
#         self.settings = settings
#         self.profileCache = profileCache
#         self.profileCopier = profileCopier
#         self.profileRetriever = profileRetriever
#         self.update = False
#         self.profile = None
# 
#     def execute(self):
#         self._saveCurrentProfile()
#         self._retrieveProfile(self.profile)
#         self.profileCopier.copyToHome(self.profile)
#         self.settings.currentProfile = self.profile
#         print('Switched to profile: %s' % self.profile.name)
# 
#     def _saveCurrentProfile(self):
#         currentProfile = self._getCurrentProfile()
#         print('Saving profile: %s' % currentProfile.name)
#         self.profileCopier.copyFromHome(currentProfile)
# 
#     def _retrieveProfile(self, profile):
#         if not self.profileCache.contains(profile) or self.update:
#             self.profileRetriever.retrieve(profile)
# 
#     def _getCurrentProfile(self):
#         if self.settings.currentProfile is None:
#             currentProfile = self.settings.defaultProfile
#         else:
#             currentProfile = self.settings.currentProfile
#         return currentProfile
# 
# Path: vimswitch/UpdateProfileAction.py
# class UpdateProfileAction(Action):
#     def __init__(self, settings, switchProfileAction):
#         Action.__init__(self)
#         self.settings = settings
#         self.switchProfileAction = switchProfileAction
#         self.profile = None
# 
#     def execute(self):
#         self.profile = self._getProfile()
# 
#         if self.profile == self.settings.defaultProfile:
#             print('Cannot update default profile')
#             self.exitCode = -1
#             return
# 
#         self.switchProfileAction.update = True
#         self.switchProfileAction.profile = self.profile
#         self.switchProfileAction.execute()
# 
#     def _getProfile(self):
#         if self.profile is None:
#             if self.settings.currentProfile is None:
#                 return self.settings.defaultProfile
#             else:
#                 return self.settings.currentProfile
#         else:
#             return self.profile
# 
# Path: vimswitch/test/BaseTestCase.py
# class BaseTestCase(unittest.TestCase):
#     def __init__(self, *args, **kwargs):
#         unittest.TestCase.__init__(self, *args, **kwargs)
# 
#         # Python < 3.2 does not have assertNotRegex
#         if not hasattr(self, 'assertNotRegex'):
#             self.assertNotRegex = self.assertNotRegexpMatches
# 
#     def assertMultilineRegexpMatches(self, string, regexp):
#         """
#         Asserts that the entirety of `string` matches `regexp`. `regexp` can be
#         a multiline string with indentation.
#         """
#         regexp = textwrap.dedent(regexp)
#         regexp = regexp.strip()
#         regexp = '^' + regexp + '$'
#         regexp = re.compile(regexp, re.DOTALL)
# 
#         self.assertRegexpMatches(string, regexp)
# 
#     def assertStdout(self, stdout, regexp):
#         "Asserts that the `stdout` io stream matches `regexp`"
#         stdoutText = stdout.getvalue().strip()
#         self.assertMultilineRegexpMatches(stdoutText, regexp)
# 
#     def resetStdout(self, stdout):
#         stdout.seek(0)
#         stdout.truncate(0)


from mock import MagicMock, patch
from vimswitch.Application import Application
from vimswitch.Profile import Profile
from vimswitch.Settings import getSettings
from vimswitch.SwitchProfileAction import SwitchProfileAction
from vimswitch.UpdateProfileAction import UpdateProfileAction
from vimswitch.six import StringIO
from .BaseTestCase import BaseTestCase


class TestUpdateProfileAction(BaseTestCase):
    def setUp(self):
        app = Application()
        self.settings = getSettings(app)
        self.switchProfileAction = MagicMock(SwitchProfileAction)
        self.updateProfileAction = UpdateProfileAction(self.settings, self.switchProfileAction)

    def test_execute_withProfile_updatesProfile(self):

# Path: vimswitch/Application.py
# class Application:
#     """
#     A container for components that are used application-wide.
# 
#     Examples of how this class can be used:
# 
#       - Set a component
#         `app.foo = myFoo`
# 
#       - Get (or create) a component:
#         `foo = app.get('foo', Foo())`
#         This will try to return `app.foo`. If that does not exist, then it will
#         set `app.foo = Foo()` and return that instead.
# 
#       - Get an already created component
#         `foo = app.foo`
#         Use this when you are sure that app.foo has already been set
#     """
# 
#     def get(self, attr, default):
#         """
#         Returns self.attr. If attr does not exist, then make self.attr = default
#         and then return default.
#         """
#         if hasattr(self, attr):
#             return getattr(self, attr)
#         else:
#             setattr(self, attr, default)
#             return default
# 
# Path: vimswitch/Profile.py
# class Profile:
#     def __init__(self, name):
#         self.name = name
# 
#     def getName(self):
#         return self.name
# 
#     def getDirName(self):
#         return self.name.replace('/', '.')
# 
#     def __eq__(self, other):
#         return isinstance(other, Profile) and self.name == other.name
# 
#     def __ne__(self, other):
#         return not self.__eq__(other)
# 
#     def __repr__(self):
#         return "Profile('%s')" % self.name
# 
# Path: vimswitch/Settings.py
# def getSettings(app):
#     return app.get('settings', createSettings(app))
# 
# Path: vimswitch/SwitchProfileAction.py
# class SwitchProfileAction(Action):
# 
#     def __init__(self, settings, profileCache, profileCopier, profileRetriever):
#         Action.__init__(self)
#         self.settings = settings
#         self.profileCache = profileCache
#         self.profileCopier = profileCopier
#         self.profileRetriever = profileRetriever
#         self.update = False
#         self.profile = None
# 
#     def execute(self):
#         self._saveCurrentProfile()
#         self._retrieveProfile(self.profile)
#         self.profileCopier.copyToHome(self.profile)
#         self.settings.currentProfile = self.profile
#         print('Switched to profile: %s' % self.profile.name)
# 
#     def _saveCurrentProfile(self):
#         currentProfile = self._getCurrentProfile()
#         print('Saving profile: %s' % currentProfile.name)
#         self.profileCopier.copyFromHome(currentProfile)
# 
#     def _retrieveProfile(self, profile):
#         if not self.profileCache.contains(profile) or self.update:
#             self.profileRetriever.retrieve(profile)
# 
#     def _getCurrentProfile(self):
#         if self.settings.currentProfile is None:
#             currentProfile = self.settings.defaultProfile
#         else:
#             currentProfile = self.settings.currentProfile
#         return currentProfile
# 
# Path: vimswitch/UpdateProfileAction.py
# class UpdateProfileAction(Action):
#     def __init__(self, settings, switchProfileAction):
#         Action.__init__(self)
#         self.settings = settings
#         self.switchProfileAction = switchProfileAction
#         self.profile = None
# 
#     def execute(self):
#         self.profile = self._getProfile()
# 
#         if self.profile == self.settings.defaultProfile:
#             print('Cannot update default profile')
#             self.exitCode = -1
#             return
# 
#         self.switchProfileAction.update = True
#         self.switchProfileAction.profile = self.profile
#         self.switchProfileAction.execute()
# 
#     def _getProfile(self):
#         if self.profile is None:
#             if self.settings.currentProfile is None:
#                 return self.settings.defaultProfile
#             else:
#                 return self.settings.currentProfile
#         else:
#             return self.profile
# 
# Path: vimswitch/test/BaseTestCase.py
# class BaseTestCase(unittest.TestCase):
#     def __init__(self, *args, **kwargs):
#         unittest.TestCase.__init__(self, *args, **kwargs)
# 
#         # Python < 3.2 does not have assertNotRegex
#         if not hasattr(self, 'assertNotRegex'):
#             self.assertNotRegex = self.assertNotRegexpMatches
# 
#     def assertMultilineRegexpMatches(self, string, regexp):
#         """
#         Asserts that the entirety of `string` matches `regexp`. `regexp` can be
#         a multiline string with indentation.
#         """
#         regexp = textwrap.dedent(regexp)
#         regexp = regexp.strip()
#         regexp = '^' + regexp + '$'
#         regexp = re.compile(regexp, re.DOTALL)
# 
#         self.assertRegexpMatches(string, regexp)
# 
#     def assertStdout(self, stdout, regexp):
#         "Asserts that the `stdout` io stream matches `regexp`"
#         stdoutText = stdout.getvalue().strip()
#         self.assertMultilineRegexpMatches(stdoutText, regexp)
# 
#     def resetStdout(self, stdout):
#         stdout.seek(0)
#         stdout.truncate(0)


# Path: vimswitch/test/test_UpdateProfileAction.py
from mock import MagicMock, patch
from vimswitch.Application import Application
from vimswitch.Profile import Profile
from vimswitch.Settings import getSettings
from vimswitch.SwitchProfileAction import SwitchProfileAction
from vimswitch.UpdateProfileAction import UpdateProfileAction
from vimswitch.six import StringIO
from .BaseTestCase import BaseTestCase



class TestUpdateProfileAction(BaseTestCase):
    def setUp(self):
        app = Application()
        self.settings = getSettings(app)
        self.switchProfileAction = MagicMock(SwitchProfileAction)
        self.updateProfileAction = UpdateProfileAction(self.settings, self.switchProfileAction)

    def test_execute_withProfile_updatesProfile(self):

        self.updateProfileAction.profile = Profile('test/vimrc')