1 # coding: utf-8 2 """Tests for the compilerop module. 3 """ 4 #----------------------------------------------------------------------------- 5 # Copyright (C) 2010-2011 The IPython Development Team. 6 # 7 # Distributed under the terms of the BSD License. 8 # 9 # The full license is in the file COPYING.txt, distributed with this software. 10 #----------------------------------------------------------------------------- 11 12 #----------------------------------------------------------------------------- 13 # Imports 14 #----------------------------------------------------------------------------- 15 16 # Stdlib imports 17 import linecache 18 import sys 19 20 # Our own imports 21 from IPython.core import compilerop 22 23 #----------------------------------------------------------------------------- 24 # Test functions 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 """ 42 cp = compilerop.CachingCompiler() 43 ncache = len(linecache.cache) 44 cp.cache('x=1') 45 assert len(linecache.cache) > ncache 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 def test_cache_unicode(): 53 cp = compilerop.CachingCompiler() 54 ncache = len(linecache.cache) 55 cp.cache(u"t = 'žćčšđ'") 56 assert len(linecache.cache) > ncache 57 58 def test_compiler_check_cache(): 59 """Test the compiler properly manages the cache. 60 """ 61 # Rather simple-minded tests that just exercise the API 62 cp = compilerop.CachingCompiler() 63 cp.cache('x=1', 99) 64 # Ensure now that after clearing the cache, our entries survive 65 linecache.checkcache() 66 assert any( 67 k.startswith("<ipython-input-99") for k in linecache.cache 68 ), "Entry for input-99 missing from linecache" 69