Debian Codegraph @

tests/test_compilerop.py

raw
1 # coding: utf-8
2 """Tests for the compilerop module."""
3 # -----------------------------------------------------------------------------
4 # Copyright (C) 2010-2011 The IPython Development Team.
5 #
6 # Distributed under the terms of the BSD License.
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
9 # -----------------------------------------------------------------------------
10
11 # -----------------------------------------------------------------------------
12 # Imports
13 # -----------------------------------------------------------------------------
14
15 # Stdlib imports
16 import linecache
17 import sys
18
19 # Our own imports
20 from IPython.core import compilerop
21
22 # -----------------------------------------------------------------------------
23 # Test functions
24 # -----------------------------------------------------------------------------
25
26
27 def test_code_name():
28 code = "x=1"
29 name = compilerop.code_name(code)
30 assert name.startswith("<ipython-input-0")
31
32
33 def test_code_name2():
34 code = "x=1"
35 name = compilerop.code_name(code, 9)
36 assert name.startswith("<ipython-input-9")
37
38
39 def test_cache():
40 """Test the compiler correctly compiles and caches inputs"""
41 cp = compilerop.CachingCompiler()
42 ncache = len(linecache.cache)
43 cp.cache("x=1")
44 assert len(linecache.cache) > ncache
45
46
47 def test_proper_default_encoding():
48 # Check we're in a proper Python 2 environment (some imports, such
49 # as GTK, can change the default encoding, which can hide bugs.)
50 assert sys.getdefaultencoding() == "utf-8"
51
52
53 def test_cache_unicode():
54 cp = compilerop.CachingCompiler()
55 ncache = len(linecache.cache)
56 cp.cache("t = 'žćčšđ'")
57 assert len(linecache.cache) > ncache
58
59
60 def test_compiler_check_cache():
61 """Test the compiler properly manages the cache."""
62 # Rather simple-minded tests that just exercise the API
63 cp = compilerop.CachingCompiler()
64 cp.cache("x=1", 99)
65 # Ensure now that after clearing the cache, our entries survive
66 linecache.checkcache()
67 assert any(
68 k.startswith("<ipython-input-99") for k in linecache.cache
69 ), "Entry for input-99 missing from linecache"