summary refs log tree commit diff stats
path: root/test/tc_signal.py
blob: feba3e5b5e1a6aba6979919b90c46ca3c602227b (plain) (blame)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# Copyright (C) 2009, 2010  Roman Zimbelmann <romanz@lavabit.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 3 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, see <http://www.gnu.org/licenses/>.

if __name__ == '__main__': from __init__ import init; init()
import unittest
import gc
from ranger.ext.signal_dispatcher import *

class TestSignal(unittest.TestCase):
	def setUp(self):
		self.sd = SignalDispatcher()

	def test_signal_register_emit(self):
		sd = self.sd
		def poo(sig):
			self.assert_('works' in sig)
			self.assertEqual('yes', sig.works)
		handler = sd.signal_bind('x', poo)

		sd.signal_emit('x', works='yes')
		sd.signal_unbind(handler)
		sd.signal_emit('x')

	def test_signal_order(self):
		sd = self.sd
		lst = []
		def addn(n):
			return lambda _: lst.append(n)

		sd.signal_bind('x', addn(6))
		sd.signal_bind('x', addn(3), priority=1)
		sd.signal_bind('x', addn(2), priority=1)
		sd.signal_bind('x', addn(9), priority=0)
		sd.signal_bind('x', addn(1337), priority=0.7)
		sd.signal_emit('x')

		self.assert_(lst.index(3) < lst.index(6))
		self.assert_(lst.index(2) < lst.index(6))
		self.assert_(lst.index(6) < lst.index(9))
		self.assert_(lst.index(1337) < lst.index(6))
		self.assert_(lst.index(1337) < lst.index(9))
		self.assert_(lst.index(1337) > lst.index(2))

	def test_modifying_arguments(self):
		sd = self.sd
		lst = []
		def modify(s):
			s.number = 5
		def set_number(s):
			lst.append(s.number)
		def stopit(s):
			s.stop()

		sd.signal_bind('setnumber', set_number)
		sd.signal_emit('setnumber', number=100)
		self.assertEqual(100, lst[-1])

		sd.signal_bind('setnumber', modify, priority=1)
		sd.signal_emit('setnumber', number=100)
		self.assertEqual(5, lst[-1])

		lst.append(None)
		sd.signal_bind('setnumber', stopit, priority=1)
		sd.signal_emit('setnumber', number=100)
		self.assertEqual(None, lst[-1])

	def test_weak_refs(self):
		sd = self.sd
		is_deleted = [False]

		class Foo(object):
			def __init__(self):
				self.alphabet = ['a']
			def calc(self, signal):
				self.alphabet.append(chr(ord(self.alphabet[-1]) + 1))
			def __del__(self):
				is_deleted[0] = True

		foo = Foo()
		alphabet = foo.alphabet
		calc = foo.calc

		del foo
		self.assertEqual('a', ''.join(alphabet))
		sd.signal_bind('mysignal', calc, weak=True)
		sd.signal_emit('mysignal')
		self.assertEqual('ab', ''.join(alphabet))
		self.assertFalse(is_deleted[0])

		del calc
		self.assertTrue(is_deleted[0])

	def test_weak_refs_dead_on_arrival(self):
		sd = self.sd
		is_deleted = [False]

		class Foo(object):
			def __init__(self):
				self.alphabet = ['a']
			def calc(self, signal):
				self.alphabet.append(chr(ord(self.alphabet[-1]) + 1))
			def __del__(self):
				is_deleted[0] = True

		foo = Foo()
		alphabet = foo.alphabet

		self.assertEqual('a', ''.join(alphabet))
		sd.signal_bind('mysignal', foo.calc, weak=True)

		sd.signal_emit('mysignal')
		self.assertEqual('ab', ''.join(alphabet))
		self.assertFalse(is_deleted[0])

		del foo

		sd.signal_emit('mysignal')
		self.assertEqual('ab', ''.join(alphabet))
		self.assertTrue(is_deleted[0])


	def test_regexp_signals(self):
		sd = RegexpSignalDispatcher()
		lst = []
		def modify(s):
			s.number = 5
		def set_number(s):
			lst.append(s.number)
		def stopit(s):
			s.stop()

		h1 = sd.signal_bind_regexp('.*ar', modify, priority=1)

		sd.signal_bind('xyz', set_number)
		sd.signal_bind('foobar', set_number)

		sd.signal_emit('xyz', number=10)
		self.assertEqual(10, lst[-1])
		sd.signal_emit('foobar', number=10)
		self.assertEqual(5, lst[-1])

		h2 = sd.signal_bind_regexp('x.z', modify, priority=1)
#		print(sd._signals)

		sd.signal_emit('xyz', number=10)
		self.assertEqual(5, lst[-1])
		sd.signal_emit('foobar', number=10)
		self.assertEqual(5, lst[-1])

		sd.signal_unbind(h2)

		sd.signal_emit('xyz', number=10)
		self.assertEqual(10, lst[-1])
		sd.signal_emit('foobar', number=10)
		self.assertEqual(5, lst[-1])

		sd.signal_unbind(h1)

		sd.signal_emit('xyz', number=10)
		self.assertEqual(10, lst[-1])
		sd.signal_emit('foobar', number=10)
		self.assertEqual(10, lst[-1])

if __name__ == '__main__':
	unittest.main()
an class="kt">int mask; const char *language; const char *territory; const char *codeset; const char *normalized_codeset; const char *modifier; const char *special; const char *sponsor; const char *revision; const char *filename; int do_allocate; { char *abs_filename; struct loaded_l10nfile *last = NULL; struct loaded_l10nfile *retval; char *cp; size_t entries; int cnt; /* Allocate room for the full file name. */ abs_filename = (char *) malloc (dirlist_len + strlen (language) + ((mask & TERRITORY) != 0 ? strlen (territory) + 1 : 0) + ((mask & XPG_CODESET) != 0 ? strlen (codeset) + 1 : 0) + ((mask & XPG_NORM_CODESET) != 0 ? strlen (normalized_codeset) + 1 : 0) + (((mask & XPG_MODIFIER) != 0 || (mask & CEN_AUDIENCE) != 0) ? strlen (modifier) + 1 : 0) + ((mask & CEN_SPECIAL) != 0 ? strlen (special) + 1 : 0) + (((mask & CEN_SPONSOR) != 0 || (mask & CEN_REVISION) != 0) ? (1 + ((mask & CEN_SPONSOR) != 0 ? strlen (sponsor) + 1 : 0) + ((mask & CEN_REVISION) != 0 ? strlen (revision) + 1 : 0)) : 0) + 1 + strlen (filename) + 1); if (abs_filename == NULL) return NULL; retval = NULL; last = NULL; /* Construct file name. */ memcpy (abs_filename, dirlist, dirlist_len); __argz_stringify (abs_filename, dirlist_len, ':'); cp = abs_filename + (dirlist_len - 1); *cp++ = '/'; cp = stpcpy (cp, language); if ((mask & TERRITORY) != 0) { *cp++ = '_'; cp = stpcpy (cp, territory); } if ((mask & XPG_CODESET) != 0) { *cp++ = '.'; cp = stpcpy (cp, codeset); } if ((mask & XPG_NORM_CODESET) != 0) { *cp++ = '.'; cp = stpcpy (cp, normalized_codeset); } if ((mask & (XPG_MODIFIER | CEN_AUDIENCE)) != 0) { /* This component can be part of both syntaces but has different leading characters. For CEN we use `+', else `@'. */ *cp++ = (mask & CEN_AUDIENCE) != 0 ? '+' : '@'; cp = stpcpy (cp, modifier); } if ((mask & CEN_SPECIAL) != 0) { *cp++ = '+'; cp = stpcpy (cp, special); } if ((mask & (CEN_SPONSOR | CEN_REVISION)) != 0) { *cp++ = ','; if ((mask & CEN_SPONSOR) != 0) cp = stpcpy (cp, sponsor); if ((mask & CEN_REVISION) != 0) { *cp++ = '_'; cp = stpcpy (cp, revision); } } *cp++ = '/'; stpcpy (cp, filename); /* Look in list of already loaded domains whether it is already available. */ last = NULL; for (retval = *l10nfile_list; retval != NULL; retval = retval->next) if (retval->filename != NULL) { int compare = strcmp (retval->filename, abs_filename); if (compare == 0) /* We found it! */ break; if (compare < 0) { /* It's not in the list. */ retval = NULL; break; } last = retval; } if (retval != NULL || do_allocate == 0) { free (abs_filename); return retval; } retval = (struct loaded_l10nfile *) malloc (sizeof (*retval) + (__argz_count (dirlist, dirlist_len) * (1 << pop (mask)) * sizeof (struct loaded_l10nfile *))); if (retval == NULL) return NULL; retval->filename = abs_filename; retval->decided = (__argz_count (dirlist, dirlist_len) != 1 || ((mask & XPG_CODESET) != 0 && (mask & XPG_NORM_CODESET) != 0)); retval->data = NULL; if (last == NULL) { retval->next = *l10nfile_list; *l10nfile_list = retval; } else { retval->next = last->next; last->next = retval; } entries = 0; /* If the DIRLIST is a real list the RETVAL entry corresponds not to a real file. So we have to use the DIRLIST separation mechanism of the inner loop. */ cnt = __argz_count (dirlist, dirlist_len) == 1 ? mask - 1 : mask; for (; cnt >= 0; --cnt) if ((cnt & ~mask) == 0 && ((cnt & CEN_SPECIFIC) == 0 || (cnt & XPG_SPECIFIC) == 0) && ((cnt & XPG_CODESET) == 0 || (cnt & XPG_NORM_CODESET) == 0)) { /* Iterate over all elements of the DIRLIST. */ char *dir = NULL; while ((dir = __argz_next ((char *) dirlist, dirlist_len, dir)) != NULL) retval->successor[entries++] = _nl_make_l10nflist (l10nfile_list, dir, strlen (dir) + 1, cnt, language, territory, codeset, normalized_codeset, modifier, special, sponsor, revision, filename, 1); } retval->successor[entries] = NULL; return retval; } /* Normalize codeset name. There is no standard for the codeset names. Normalization allows the user to use any of the common names. */ const char * _nl_normalize_codeset (codeset, name_len) const unsigned char *codeset; size_t name_len; { int len = 0; int only_digit = 1; char *retval; char *wp; size_t cnt; for (cnt = 0; cnt < name_len; ++cnt) if (isalnum (codeset[cnt])) { ++len; if (isalpha (codeset[cnt])) only_digit = 0; } retval = (char *) malloc ((only_digit ? 3 : 0) + len + 1); if (retval != NULL) { if (only_digit) wp = stpcpy (retval, "iso"); else wp = retval; for (cnt = 0; cnt < name_len; ++cnt) if (isalpha (codeset[cnt])) *wp++ = tolower (codeset[cnt]); else if (isdigit (codeset[cnt])) *wp++ = codeset[cnt]; *wp = '\0'; } return (const char *) retval; } /* @@ begin of epilog @@ */ /* We don't want libintl.a to depend on any other library. So we avoid the non-standard function stpcpy. In GNU C Library this function is available, though. Also allow the symbol HAVE_STPCPY to be defined. */ #if !_LIBC && !HAVE_STPCPY static char * stpcpy (dest, src) char *dest; const char *src; { while ((*dest++ = *src++) != '\0') /* Do nothing. */ ; return dest - 1; } #endif