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
     25  import libxyz.core as core
     26  import libxyz.pselector
     27
     28  from nose.tools import raises
     29  from libxyz.exceptions import *
     30  from libxyz.vfs import VFSDispatcher
     31  from libxyz.vfs.local import LocalVFSObject
     32  from libxyz import const
     33
     34  # Global data
     35  xyz = None
     36  files = {}
     37
     38 -def setup():
     39      global xyz, filesw
     40
     41      xyz = core.XYZData()
     42      __builtin__._ = lambda x: x
     43      __builtin__.xyzenc = locale.getpreferredencoding()
     44
     45      # Setup files
     46      setup_actions(files)
     47
     48  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     49
     50 -def setup_actions(files):
     51      fd_good, path_good = tempfile.mkstemp(text=True)
     52      fd_bad, path_bad = tempfile.mkstemp(text=True)
     53      _, test_name = tempfile.mkstemp(text=True)
     54      _, test_size = tempfile.mkstemp(text=True)
     55      _, test_owner = tempfile.mkstemp(text=True)
     56
     57      os.write(fd_good, """action(r'iname{".*\.pdf$"}', lambda obj: obj)""")
     58      os.write(fd_bad, ":(")
     59
     60      files["actions_good"] = path_good
     61      files["actions_bad"] = path_bad
     62      files["test_name"] = test_name
     63      files["test_size"] = test_size
     64      files["test_owner"] = test_owner
     65
     66  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     67
     68 -def teardown():
     69      global files
     70
     71      for k in files:
     72          os.unlink(files[k])
     73
     74  #### Tests
     75
     76 -class TestQueue(object):
     77      """
     78      libxyz.core.Queue tests
     79      """
     80
     81 -    def setUp(self):
     82          self.q = core.Queue(1)
     83
     84      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     85
     86      @raises(XYZValueError)
     87 -    def test_queue_input_arg(self):
     88          core.Queue("wrong")
     89
     90      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     91
     92      @raises(IndexError)
     93 -    def test_queue_pop(self):
     94          self.q.pop()
     95
     96      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     97
     98 -    def test_queue_tail1(self):
     99          assert self.q.tail() is None
    100
    101      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    102
    103 -    def test_queue_tail2(self):
    104          self.q.push("abc")
    105          assert self.q.tail() == "abc"
    106
    107      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    108
    109 -    def test_func(self):
    110          size = 5
    111
    112          q = core.Queue(size)
    113
    114          for i in range(size):
    115              q.push(i)
    116
    117          _res = []
    118
    119          for i in range(size):
    120              _res.append(q.pop())
    121
    122          assert _res == list(range(size))
    123
    124  #++++++++++++++++++++++++++++++++++++++++++++++++
    125
    126 -class TestActionManager(object):
    127      """
    128      libxyz.core.ActionManager tests
    129      """
    130
    131 -    def setUp(self):
    132          global files
    133
    134          self.xyz = core.XYZData()
    135          self.dsl = core.dsl.XYZ(self.xyz)
    136          self.am = core.ActionManager(xyz)
    137          self.xyz.vfs = VFSDispatcher(self.xyz)
    138          self.files = files
    139
    140          # Empty prefix for local filesystem
    141          self.xyz.vfs.register(None, LocalVFSObject)
    142
    143      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    144
    145 -    def tearDown(self):
    146          core.dsl.XYZ._clear()
    147
    148      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    149
    150      @raises(XYZRuntimeError)
    151 -    def testRegisterIncorrectRule(self):
    152          self.am.register("WRONG", lambda: None)
    153
    154      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    155
    156 -    def testRegisterCorrectRule(self):
    157          assert self.am.register("size{100}", lambda: None) is None
    158
    159      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    160
    161 -    def testMatch(self):
    162          vfs_size = self.xyz.vfs.dispatch(self.files["test_size"])
    163          vfs_size.size = 100
    164          vfs_name = self.xyz.vfs.dispatch(self.files["test_name"])
    165          vfs_owner = self.xyz.vfs.dispatch(self.files["test_owner"])
    166          vfs_owner.uid = 500
    167          vfs_owner.gid = 501
    168
    169          self.am.register("size{100}", lambda: "size")
    170          self.am.register("name{'''%s'''}" % vfs_name.name, lambda: "name")
    171          self.am.register("owner{500:501}", lambda: "owner")
    172
    173          assert self.am.match(vfs_size)() == "size"
    174          assert self.am.match(vfs_name)() == "name"
    175          assert self.am.match(vfs_owner)() == "owner"
    176
    177  #++++++++++++++++++++++++++++++++++++++++++++++++
    178
    179 -class TestDSL(object):
    180 -    def setUp(self):
    181          self.xyz = core.XYZData()
    182          self.xyz.conf = {"xyz": {}}
    183          self.xyz.conf["xyz"]["plugins"] = {":core:shell": "ENABLE"}
    184          self.xyz.km = core.KeyManager(self.xyz)
    185          self.xyz.pm = core.plugins.PluginManager(
    186              self.xyz, libxyz.PathSelector().get_plugins_dir())
    187          self.xyz.hm = core.HookManager()
    188          self.dsl = core.dsl.XYZ(self.xyz)
    189
    190          ps = libxyz.pselector.PathSelector()
    191          main_conf = ps.get_conf(const.XYZ_CONF_FILE)[0]
    192          plugins_conf = ps.get_conf(const.PLUGINS_CONF_FILE)[0]
    193
    194          confs = [main_conf, plugins_conf]
    195
    196          for conf in confs:
    197              self.dsl.exec_file(conf)
    198
    199      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    200
    201 -    def tearDown(self):
    202          self.dsl._clear()
    203
    204      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    205
    206 -    def setupValues(self):
    207          # Local section
    208          self.dsl.let("var1", "val1")
    209          # Test section
    210          self.dsl.let("var2", "val2", sect="test")
    211          # Dict value
    212          self.dsl.let("dictvar", {1: 2, 3: 4})
    213          self.dsl.let("dictvar", {3: 33, 5: 6})
    214
    215      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    216
    217      @raises(DSLError)
    218 -    def testNotInstantiated(self):
    219          self.dsl._clear()
    220          self.dsl.let("a", "b")
    221
    222      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    223
    224 -    def testLetVal(self):
    225          self.setupValues()
    226
    227          assert self.xyz.conf["local"]["var1"] == "val1"
    228          assert self.xyz.conf["test"]["var2"] == "val2"
    229          assert self.xyz.conf["local"]["dictvar"] == {1: 2, 3: 33, 5: 6}
    230
    231          assert self.dsl.val("var1") == "val1"
    232          assert self.dsl.val("var2", "test") == "val2"
    233          assert self.dsl.val("dictvar") == {1: 2, 3: 33, 5: 6}
    234
    235      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    236
    237 -    def testSection(self):
    238          data = {
    239              "a1": "z1",
    240              "a2": "z2",
    241              "a3": [1,2,3]
    242              }
    243
    244          for k, v in data.iteritems():
    245              self.dsl.let(k, v, "section-test")
    246
    247          assert self.dsl.section("section-test") == data
    248
    249      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    250
    251 -    def testUnlet(self):
    252          self.setupValues()
    253
    254          assert self.dsl.val("var1") == "val1"
    255          self.dsl.unlet("var1")
    256          assert self.dsl.val("var1") is None
    257
    258      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    259
    260 -    def testLoad(self):
    261          self.dsl.load(":core:shell:execute")
    262
    263      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    264
    265 -    def testBind(self):
    266          method = lambda x: x
    267          sc = self.dsl.kbd("Ctrl", "a")
    268
    269          self.dsl.bind(method, sc)
    270          self.dsl.bind(method, sc, "XYZ")
    271
    272          assert self.xyz.km.get_binds()["DEFAULT"][sc] == method
    273          assert self.xyz.km.get_binds()["XYZ"][sc] == method
    274
    275      #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    276
    277 -    def testKbd(self):
    278          sc1 = self.dsl.kbd("Ctrl", "z")
    279          sc2 = self.dsl.kbd("Ctrl", "x", "a")
    280
    281          assert sc1 == libxyz.ui.Shortcut(sc=["Ctrl", "z"])
    282          assert sc2 == libxyz.ui.Shortcut(sc=["Ctrl", "x", "a"])
    283