Package libxyz :: Package core :: Module tests
[hide private]
[frames] | no frames]

Source Code for Module libxyz.core.tests

  1  #-*- coding: utf8 -* 
  2  # 
  3  # Max E. Kuznecov ~syhpoon <mek@mek.uz.ua> 2008-2009 
  4  # 
  5  # This file is part of XYZCommander. 
  6  # XYZCommander is free software: you can redistribute it and/or modify 
  7  # it under the terms of the GNU Lesser Public License as published by 
  8  # the Free Software Foundation, either version 3 of the License, or 
  9  # (at your option) any later version. 
 10  # XYZCommander is distributed in the hope that it will be useful, 
 11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 13  # GNU Lesser Public License for more details. 
 14  # You should have received a copy of the GNU Lesser Public License 
 15  # along with XYZCommander. If not, see <http://www.gnu.org/licenses/>. 
 16   
 17  # Core tests 
 18   
 19  import __builtin__ 
 20  import locale 
 21  import tempfile 
 22  import os 
 23   
 24  import libxyz.core as core 
 25   
 26   
 27  from nose.tools import raises 
 28  from libxyz.exceptions import * 
 29  from libxyz.vfs.vfsobj import VFSFile 
 30   
 31  # Global data 
 32  xyz = None 
 33  files = {} 
34 35 -def setup():
36 global xyz, filesw 37 38 xyz = core.XYZData() 39 __builtin__._ = lambda x: x 40 __builtin__.xyzenc = locale.getpreferredencoding() 41 42 # Setup files 43 files["actions_good"], files["actions_bad"] = setup_actions()
44
45 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 46 -def setup_actions():
47 fd_good, path_good = tempfile.mkstemp(text=True) 48 fd_bad, path_bad = tempfile.mkstemp(text=True) 49 os.write(fd_good, """action(r'iname{".*\.pdf$"}', lambda obj: obj)""") 50 os.write(fd_bad, ":(") 51 52 return path_good, path_bad
53
54 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 55 56 -def teardown():
57 global files 58 59 for k in files: 60 os.unlink(files[k])
61
62 #### Tests 63 64 -class TestQueue(object):
65 """ 66 libxyz.core.Queue tests 67 """ 68
69 - def setUp(self):
70 self.q = core.Queue(1)
71 72 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 73 74 @raises(XYZValueError)
75 - def test_queue_input_arg(self):
76 core.Queue("wrong")
77 78 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 79 80 @raises(IndexError)
81 - def test_queue_pop(self):
82 self.q.pop()
83 84 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 85
86 - def test_queue_tail1(self):
87 assert self.q.tail() is None
88 89 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 90
91 - def test_queue_tail2(self):
92 self.q.push("abc") 93 assert self.q.tail() == "abc"
94 95 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 96
97 - def test_func(self):
98 size = 5 99 100 q = core.Queue(size) 101 102 for i in range(size): 103 q.push(i) 104 105 _res = [] 106 107 for i in range(size): 108 _res.append(q.pop()) 109 110 assert _res == list(range(size))
111
112 #++++++++++++++++++++++++++++++++++++++++++++++++ 113 114 -class TestActionManager(object):
115 """ 116 libxyz.core.ActionManager tests 117 """ 118
119 - def setUp(self):
120 self.xyz = core.XYZData() 121 self.am = core.ActionManager(xyz, [])
122 123 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 124 125 @raises(XYZRuntimeError)
127 self.am.register("WRONG", lambda: None)
128 129 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 130
131 - def testRegisterCorrectRule(self):
132 assert self.am.register("size{100}", lambda: None) is None
133 134 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 135 136 @raises(XYZRuntimeError)
137 - def testParseConfigIncorrect(self):
138 global files 139 xyz = core.XYZData() 140 141 am = core.ActionManager(xyz, [files["actions_bad"], "none"]) 142 xyz.am = am 143 144 am.parse_configs()
145 146 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 147
148 - def testParseConfigCorrect(self):
149 global files 150 xyz = core.XYZData() 151 152 am = core.ActionManager(xyz, [files["actions_good"], "none"]) 153 xyz.am = am 154 155 assert am.parse_configs() is None
156 157 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 158
159 - def testMatch(self):
160 vfs_size = VFSFile("/tmp/test_size") 161 vfs_size.size = 100 162 vfs_name = VFSFile("/tmp/test_name") 163 vfs_owner = VFSFile("/tmp/test_owner") 164 vfs_owner.uid = 500 165 vfs_owner.gid = 501 166 167 self.am.register("size{100}", lambda: "size") 168 self.am.register("name{test_name}", lambda: "name") 169 self.am.register("owner{500:501}", lambda: "owner") 170 171 assert self.am.match(vfs_size)() == "size" 172 assert self.am.match(vfs_name)() == "name" 173 assert self.am.match(vfs_owner)() == "owner"
174 175 #++++++++++++++++++++++++++++++++++++++++++++++++ 176