1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# Copyright (C) 2009, Tutorius.org
# Copyright (C) 2009, Simon Poirier <simpoir@gmail.com>
#
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import unittest
from sugar.tutorius import addon, properties
import os, sys
class AddonTest(unittest.TestCase):
def test_create_constructor_fail(self):
try:
obj = addon.create("BubbleMessage", wrong_param=True, second_wrong="This", last_wrong=12, unknown=13.4)
assert False, "Constructor with wrong parameter should raise an exception"
except:
pass
def test_create_wrong_addon(self):
try:
obj = addon.create("Non existing addon name")
assert False, "Addon creator should raise an exception when the requested addon is unknown"
except:
pass
def test_create(self):
obj = addon.create("BubbleMessage", message="Hi!", position=[12,31])
assert obj is not None
def test_reload_addons(self):
addon.__cache__.clear()
assert len(addon.list_addons()) > 0, "Addons should be reloaded upon cache clear"
def test_get_addon_meta(self):
addon.__cache__.clear()
meta = addon.get_addon_meta("BubbleMessage")
expected = set(['mandatory_props', 'class', 'display_name', 'name', 'ts', 'type', 'icon'])
assert not set(meta.keys()).difference(expected), "%s == %s"%(meta.keys(), expected)
def test_reverse_lookup(self):
obj = addon.create("BubbleMessage", message="Hi!", position=[12,31])
assert "BubbleMessage" == addon.get_name_from_type(type(obj))
for name in addon.list_addons():
klass = addon.get_addon_meta(name)['class']
assert name == addon.get_name_from_type(klass),\
"could not reverse lookup from type '%s'"%klass.__name__
def test_addon_constructor(self):
for name in addon.list_addons():
obj = addon.create(name)
# __init__ can have locals, but should not initialize attributes on
# self as non-properties attributes won't survive serialization
# through DBUS. Assignation in do() for actions or install_handlers
# for event filters is the correct way.
attribs = set(obj.__dict__.keys()).difference(obj._props.keys()+['_props', '_callback'])
assert not attribs,\
"assignation of attribute(s) %s detected in '%s.__init__'"%(attribs, type(obj).__name__)
def test_addon_reimport(self):
obj = addon.create("BubbleMessage", message="Hi!", position=[12,31])
mod_name = type(obj).__module__
# alter module
type(obj).testvar = True
# recreate without touching the module file
obj = addon.create("BubbleMessage", message="Hi!", position=[12,31])
assert hasattr(type(obj), 'testvar') and type(obj).testvar == True
# touch the module
os.system('touch %s'%sys.modules.get(mod_name).__file__)
obj = addon.create("BubbleMessage", message="Hi!", position=[12,31])
mod = type(obj).__module__
assert not hasattr(mod, 'testvar')
|