Skip to content
Permalink
Browse files

add GotoException, magic.rename_shortcut and rename tests

  • Loading branch information...
technocake committed Jul 9, 2019
1 parent 5b52fdd commit e13216e03b235fa73867a268845ddb2934d8e6b8
@@ -3,6 +3,9 @@
# active project is stored in this file
active-project

*.sublime-workspace


*.swp
*.pyc

@@ -0,0 +1,23 @@
{
"build_systems":
[
{
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"name": "Anaconda Python Builder",
"selector": "source.python",
"shell_cmd": "\"python3\" -u \"$file\""
}
],
"folders":
[
{
"folder_exclude_patterns":
[
".tox",
"build",
"dist"
],
"path": "."
}
]
}
@@ -1,4 +1,5 @@
from ..gotomagic.text import GotoWarning
from ..gotomagic.exceptions import warnings


def rename(magic, command, args):
@@ -8,19 +9,16 @@ def rename(magic, command, args):
from_magicword = args[0]

if len(args) == 1:
return None, GotoWarning("missing_to_magicword", command=command, magicword=from_magicword)
return None, GotoWarning("missing_to_magicword",
command=command, magicword=from_magicword)

to_magicword = args[1]
overwrite = '-f' in args or '--force' in args

if from_magicword not in magic.magic:
return None, GotoWarning("magicword_does_not_exist", magicword=from_magicword)
try:
magic.rename_shortcut(from_magicword, to_magicword, overwrite)
magic.save()
except warnings.GotoException as warning:
return None, warning

if to_magicword in magic.magic:
return None, GotoWarning("adding_existing_magicword_short", magicword=to_magicword, uri=magic[to_uri])

from_uri = magic[from_magicword]
del magic[from_magicword]
magic[to_magicword] = from_uri
magic.save()

return "Renamed %s to %s" % (from_magicword, to_magicword), None
return "Renamed {} to {}".format(from_magicword, to_magicword), None
@@ -0,0 +1,23 @@
''' Goto exceptions
Somehow related to GotoWarnings and GotoErrors
'''
from .text import GotoWarning, GotoError


class GotoException(Exception):
'''
Usage:
>>> raise GotoException(
>>> 'adding_existing_magicword_short',
>>> magicword=magicword)
'''

def __init__(self, msg=None, **kwargs):
if msg is None:
raise Exception("Programming error - msg not specified")
self.message = GotoWarning(msg, **kwargs).message

# Hack
warnings = type('warnings', (object,), {'GotoException': GotoException}) # noqa
@@ -8,6 +8,7 @@

from . import text
from .text import print_text
from .exceptions import warnings


class GotoMagic():
@@ -61,6 +62,19 @@ def update_shortcut(self, magicword, uri):
)
exit(1)

def rename_shortcut(self, from_magicword, to_magicword, overwrite=False):
""" Renaming a shortcut """
if not overwrite and to_magicword in self.magic:
uri = self.magic[to_magicword]
raise warnings.GotoException('adding_existing_magicword_short', magicword=to_magicword, uri=uri) # noqa

if from_magicword not in self.magic:
raise warnings.GotoException('magicword_does_not_exist', magicword=from_magicword) # noqa

from_uri = self.magic[from_magicword]
del self.magic[from_magicword]
self.magic[to_magicword] = from_uri

def remove_shortcut(self, magicword):
""" Simply removes a shortcut """
try:
@@ -3,7 +3,6 @@

messages = dict(
adding_existing_magicword="""
Ah hoy!
- You already have a magic word named {magicword},
@@ -14,7 +13,6 @@
""",

adding_existing_magicword_short="""
Ah hoy!
- You already have a magic word named {magicword},
@@ -114,6 +114,15 @@ function _projectfile_should_contain {
fi
}

function _projectfile_should_not_contain {
local magicword="$1"
if [[ $(cat $PROJECTFILE | grep $magicword | wc -l) -ne 0 ]]; then
_fail_test "magicword '$magicword' should not be in project file"
else
return 0
fi
}


function test_01_not_initialized_should_fail {
echo "... not initialized and deactived"
@@ -300,16 +309,73 @@ function test_11_goto_update {
function test_12_only_one_ah_hoy_at_the_time_please {
nonexisting_magicword="IDoNotExist"

for command in '' show add update rm; do
for command in '' show add update rm rename mv; do
_cmd_should_fail "goto $command $nonexisting_magicword"
_failing_cmd_should_give_human_message "goto $command $nonexisting_magicword"
_failing_cmd_should_not_print_ah_hoy_twice "goto $command $nonexisting_magicword"
done
}


function test_13_goto_rename {
existing_magicword1="test_1"
existing_magicword2="test_2"
new_magicword="test_3"
nonexisting_magicword="IDoNotExist"
uri="http://example.com"

_cmd_should_succeed "goto add $existing_magicword1 $uri"
_cmd_should_succeed "goto add $existing_magicword2 $uri"

# Invoking rename without any magic words
_cmd_should_fail "goto rename"
_failing_cmd_should_give_human_message "goto rename"

_projectfile_should_contain $existing_magicword1
_projectfile_should_contain $existing_magicword2


# Invoking rename with one magicword
_cmd_should_fail "goto rename $existing_magicword1"
_failing_cmd_should_give_human_message "goto rename $existing_magicword1"

_projectfile_should_contain $existing_magicword1
_projectfile_should_contain $existing_magicword2


# Invoking rename with both magicwords
_cmd_should_succeed "goto rename $existing_magicword1 $new_magicword"
_cmd_should_fail "goto rename $existing_magicword1 $new_magicword"
_failing_cmd_should_give_human_message "goto rename $existing_magicword1 $new_magicword"

_projectfile_should_not_contain $existing_magicword1
_projectfile_should_contain $existing_magicword2
_projectfile_should_contain $new_magicword


# re add existing_magicword1
_cmd_should_succeed "goto add $existing_magicword1 $uri"


# Invoking rename targeting existing magicword
_cmd_should_fail "goto rename $existing_magicword1 $existing_magicword2"
_failing_cmd_should_give_human_message "goto rename $existing_magicword1 $existing_magicword2"

_projectfile_should_contain $existing_magicword1
_projectfile_should_contain $existing_magicword2
_projectfile_should_contain $new_magicword


# Invoking rename targeting existing magicword and setting force flag to true
_cmd_should_succeed "goto rename $existing_magicword1 $existing_magicword2 --force"

_projectfile_should_not_contain $existing_magicword1
_projectfile_should_contain $existing_magicword2
_projectfile_should_contain $new_magicword
}


function TODO_test_13_goto_copy {
function TODO_test_14_goto_copy {
# By using the python pyperclip module,
# It would be possible to inspect the content of the clipboard:

0 comments on commit e13216e

Please sign in to comment.
You can’t perform that action at this time.