Skip to content

Commit

Permalink
restore tests from 21811f0
Browse files Browse the repository at this point in the history
  • Loading branch information
keis committed Apr 29, 2015
2 parents 33c9177 + 21811f0 commit d2157a8
Show file tree
Hide file tree
Showing 14 changed files with 952 additions and 0 deletions.
30 changes: 30 additions & 0 deletions tests/event-manager/emtest.py
@@ -0,0 +1,30 @@
from mock import Mock
import logging
from uzbl.event_manager import Uzbl


class EventManagerMock(object):
def __init__(self,
global_plugins=(), instance_plugins=(),
global_mock_plugins=(), instance_mock_plugins=()
):
self.uzbls = {}
self.plugins = {}
self.instance_plugins = instance_plugins
self.instance_mock_plugins = instance_mock_plugins
for plugin in global_plugins:
self.plugins[plugin] = plugin(self)
for (plugin, mock) in global_mock_plugins:
self.plugins[plugin] = mock() if mock else Mock(plugin)

def add(self):
u = Mock(spec=Uzbl)
u.parent = self
u.logger = logging.getLogger('debug')
u.plugins = {}
for plugin in self.instance_plugins:
u.plugins[plugin] = plugin(u)
for (plugin, mock) in self.instance_mock_plugins:
u.plugins[plugin] = mock() if mock else Mock(plugin)
self.uzbls[Mock()] = u
return u
25 changes: 25 additions & 0 deletions tests/event-manager/testarguments.py
@@ -0,0 +1,25 @@
#!/usr/bin/env python

import unittest
from uzbl.arguments import Arguments


class ArgumentsTest(unittest.TestCase):
def test_empty(self):
a = Arguments('')
self.assertEqual(len(a), 0)
self.assertEqual(a.raw(), '')

def test_space(self):
a = Arguments(' foo bar')
self.assertEqual(len(a), 2)
self.assertEqual(a.raw(), 'foo bar')
self.assertEqual(a.raw(0, 0), 'foo')
self.assertEqual(a.raw(1, 1), 'bar')

def test_tab(self):
a = Arguments('\tfoo\t\tbar')
self.assertEqual(len(a), 2)
self.assertEqual(a.raw(), 'foo\t\tbar')
self.assertEqual(a.raw(0, 0), 'foo')
self.assertEqual(a.raw(1, 1), 'bar')
51 changes: 51 additions & 0 deletions tests/event-manager/testbind.py
@@ -0,0 +1,51 @@
#!/usr/bin/env python


import sys
if '' not in sys.path:
sys.path.insert(0, '')

import mock
import unittest
from emtest import EventManagerMock
from uzbl.plugins.bind import Bind, BindPlugin
from uzbl.plugins.config import Config


def justafunction():
pass


class BindTest(unittest.TestCase):
def test_unique_id(self):
a = Bind('spam', 'spam')
b = Bind('spam', 'spam')
self.assertNotEqual(a.bid, b.bid)


class BindPluginTest(unittest.TestCase):
def setUp(self):
self.event_manager = EventManagerMock((), (Config, BindPlugin))
self.uzbl = self.event_manager.add()

def test_add_bind(self):
b = BindPlugin[self.uzbl]
modes = 'global'
glob = 'test'
handler = justafunction
b.mode_bind(modes, glob, handler)

binds = b.bindlet.get_binds()
self.assertEqual(len(binds), 1)
self.assertIs(binds[0].function, justafunction)

def test_parse_bind(self):
b = BindPlugin[self.uzbl]
modes = 'global'
glob = 'test'
handler = 'handler'

b.parse_mode_bind('%s %s = %s' % (modes, glob, handler))
binds = b.bindlet.get_binds()
self.assertEqual(len(binds), 1)
self.assertEqual(binds[0].commands, [handler])
118 changes: 118 additions & 0 deletions tests/event-manager/testcompletion.py
@@ -0,0 +1,118 @@
#!/usr/bin/env python
# vi: set et ts=4:



import unittest
from emtest import EventManagerMock
from uzbl.plugins.config import Config
from uzbl.plugins.keycmd import KeyCmd
from uzbl.plugins.completion import CompletionPlugin


class DummyFormatter(object):
def format(self, partial, completions):
return '[%s] %s' % (partial, ', '.join(completions))

class TestAdd(unittest.TestCase):
def setUp(self):
self.event_manager = EventManagerMock(
(), (CompletionPlugin,),
(), ((Config, dict), (KeyCmd, None))
)
self.uzbl = self.event_manager.add()

def test_builtins(self):
c = CompletionPlugin[self.uzbl]
c.add_builtins(('spam', 'egg'))
self.assertIn('spam', c.completion)
self.assertIn('egg', c.completion)

def test_config(self):
c = CompletionPlugin[self.uzbl]
c.add_config_key('spam', 'SPAM')
self.assertIn('@spam', c.completion)


class TestCompletion(unittest.TestCase):
def setUp(self):
self.event_manager = EventManagerMock(
(), (KeyCmd, CompletionPlugin),
(), ((Config, dict),)
)
self.uzbl = self.event_manager.add()

c = CompletionPlugin[self.uzbl]
c.listformatter = DummyFormatter()
c.add_builtins(('spam', 'egg', 'bar', 'baz'))
c.add_config_key('spam', 'SPAM')
c.add_config_key('Something', 'Else')

def test_incomplete_keyword(self):
k, c = KeyCmd[self.uzbl], CompletionPlugin[self.uzbl]
k.keylet.keycmd = 'sp'
k.keylet.cursor = len(k.keylet.keycmd)

r = c.get_incomplete_keyword()
self.assertEqual(r, ('sp', False))

def test_incomplete_keyword_var(self):
k, c = KeyCmd[self.uzbl], CompletionPlugin[self.uzbl]
k.keylet.keycmd = 'set @sp'
k.keylet.cursor = len(k.keylet.keycmd)

r = c.get_incomplete_keyword()
self.assertEqual(r, ('@sp', False))

def test_incomplete_keyword_var_noat(self):
k, c = KeyCmd[self.uzbl], CompletionPlugin[self.uzbl]
k.keylet.keycmd = 'set Some'
k.keylet.cursor = len(k.keylet.keycmd)

r = c.get_incomplete_keyword()
self.assertEqual(r, ('@Some', True))

def test_stop_completion(self):
config, c = Config[self.uzbl], CompletionPlugin[self.uzbl]
c.completion.level = 99
config['completion_list'] = 'test'

c.stop_completion()
self.assertNotIn('completion_list', config)
self.assertEqual(c.completion.level, 0)

def test_completion(self):
k, c = KeyCmd[self.uzbl], CompletionPlugin[self.uzbl]
config = Config[self.uzbl]

comp = (
('sp', 'spam '),
('e', 'egg '),
('set @sp', 'set @spam '),
)

for i, o in comp:
k.keylet.keycmd = i
k.keylet.cursor = len(k.keylet.keycmd)

c.start_completion()
self.assertEqual(k.keylet.keycmd, o)
c.start_completion()
self.assertNotIn('completion_list', config)

def test_completion_list(self):
k, c = KeyCmd[self.uzbl], CompletionPlugin[self.uzbl]
config = Config[self.uzbl]

comp = (
('b', 'ba', '[ba] bar, baz'),
)

for i, o, l in comp:
k.keylet.keycmd = i
k.keylet.cursor = len(k.keylet.keycmd)

c.start_completion()
self.assertEqual(k.keylet.keycmd, o)
c.start_completion()
self.assertEqual(config['completion_list'], l)
77 changes: 77 additions & 0 deletions tests/event-manager/testconfig.py
@@ -0,0 +1,77 @@


import unittest
from emtest import EventManagerMock

from uzbl.plugins.config import Config


class ConfigTest(unittest.TestCase):
def setUp(self):
self.event_manager = EventManagerMock((), (Config,))
self.uzbl = self.event_manager.add()

def test_set(self):
cases = (
(True, '1'),
(False, '0'),
("test", "test"),
(5, '5')
)
c = Config[self.uzbl]
for input, expected in cases:
c.set('foo', input)
self.uzbl.send.assert_called_once_with(
'set foo = ' + expected)
self.uzbl.send.reset_mock()

def test_set_invalid(self):
cases = (
("foo\nbar", AssertionError), # Better Exception type
("bad'key", AssertionError)
)
c = Config[self.uzbl]
for input, exception in cases:
self.assertRaises(exception, c.set, input)

def test_parse(self):
cases = (
('foo str value', 'foo', 'value'),
('foo str "ba ba"', 'foo', 'ba ba'),
('foo float 5', 'foo', 5.0)
)
c = Config[self.uzbl]
for input, ekey, evalue in cases:
c.parse_set_event(input)
self.assertIn(ekey, c)
self.assertEqual(c[ekey], evalue)
self.uzbl.event.assert_called_once_with(
'CONFIG_CHANGED', ekey, evalue)
self.uzbl.event.reset_mock()

def test_parse_null(self):
cases = (
('foo str', 'foo'),
('foo str ""', 'foo'),
#('foo int', 'foo') # Not sure if this input is valid
)
c = Config[self.uzbl]
for input, ekey in cases:
c.update({'foo': '-'})
c.parse_set_event(input)
self.assertNotIn(ekey, c)
self.uzbl.event.assert_called_once_with(
'CONFIG_CHANGED', ekey, '')
self.uzbl.event.reset_mock()

def test_parse_invalid(self):
cases = (
('foo bar', AssertionError), # TypeError?
('foo bad^key', AssertionError),
('', Exception),
('foo int z', ValueError)
)
c = Config[self.uzbl]
for input, exception in cases:
self.assertRaises(exception, c.parse_set_event, input)
self.assertEqual(len(list(c.keys())), 0)
67 changes: 67 additions & 0 deletions tests/event-manager/testcookie.py
@@ -0,0 +1,67 @@
#!/usr/bin/env python


import sys
if '' not in sys.path:
sys.path.insert(0, '')

import unittest
from emtest import EventManagerMock

from uzbl.plugins.cookies import Cookies

cookies = (
r'".nyan.cat" "/" "__utmb" "183192761.1.10.1313990640" "http" "1313992440"',
r'".twitter.com" "/" "guest_id" "v1%3A131399064036991891" "http" "1377104460"'
)


class CookieFilterTest(unittest.TestCase):
def setUp(self):
self.event_manager = EventManagerMock((), (Cookies,))
self.uzbl = self.event_manager.add()
self.other = self.event_manager.add()

def test_add_cookie(self):
c = Cookies[self.uzbl]
c.add_cookie(cookies[0])
self.other.send.assert_called_once_with(
'add_cookie ' + cookies[0])

def test_whitelist_block(self):
c = Cookies[self.uzbl]
c.whitelist_cookie(r'domain "nyan\.cat$"')
c.add_cookie(cookies[1])
self.uzbl.send.assert_called_once_with(
'delete_cookie ' + cookies[1])

def test_whitelist_accept(self):
c = Cookies[self.uzbl]
c.whitelist_cookie(r'domain "nyan\.cat$"')
c.add_cookie(cookies[0])
self.other.send.assert_called_once_with(
'add_cookie ' + cookies[0])

def test_blacklist_block(self):
c = Cookies[self.uzbl]
c.blacklist_cookie(r'domain "twitter\.com$"')
c.add_cookie(cookies[1])
self.uzbl.send.assert_called_once_with(
'delete_cookie ' + cookies[1])

def test_blacklist_accept(self):
c = Cookies[self.uzbl]
c.blacklist_cookie(r'domain "twitter\.com$"')
c.add_cookie(cookies[0])
self.other.send.assert_called_once_with(
'add_cookie ' + cookies[0])

def test_filter_numeric(self):
c = Cookies[self.uzbl]
c.blacklist_cookie(r'0 "twitter\.com$"')
c.add_cookie(cookies[1])
self.uzbl.send.assert_called_once_with(
'delete_cookie ' + cookies[1])

if __name__ == '__main__':
unittest.main()

0 comments on commit d2157a8

Please sign in to comment.