1import os 2import sys 3 4import pytest 5 6from mako import compat 7from mako import exceptions 8from mako import util 9from mako.testing.assertions import assert_raises_message 10from mako.testing.assertions import eq_ 11from mako.testing.assertions import in_ 12from mako.testing.assertions import ne_ 13from mako.testing.assertions import not_in 14 15 16class UtilTest: 17 def test_fast_buffer_write(self): 18 buf = util.FastEncodingBuffer() 19 buf.write("string a ") 20 buf.write("string b") 21 eq_(buf.getvalue(), "string a string b") 22 23 def test_fast_buffer_truncate(self): 24 buf = util.FastEncodingBuffer() 25 buf.write("string a ") 26 buf.write("string b") 27 buf.truncate() 28 buf.write("string c ") 29 buf.write("string d") 30 eq_(buf.getvalue(), "string c string d") 31 32 def test_fast_buffer_encoded(self): 33 s = "drôl m’a rée « S’il" 34 buf = util.FastEncodingBuffer(encoding="utf-8") 35 buf.write(s[0:10]) 36 buf.write(s[10:]) 37 eq_(buf.getvalue(), s.encode("utf-8")) 38 39 def test_read_file(self): 40 fn = os.path.join(os.path.dirname(__file__), "test_util.py") 41 data = util.read_file(fn, "rb") 42 assert b"test_util" in data 43 44 @pytest.mark.skipif(compat.pypy, reason="Pypy does this differently") 45 def test_load_module(self): 46 path = os.path.join(os.path.dirname(__file__), "module_to_import.py") 47 some_module = compat.load_module("test.module_to_import", path) 48 49 not_in("test.module_to_import", sys.modules) 50 in_("some_function", dir(some_module)) 51 import test.module_to_import 52 53 ne_(some_module, test.module_to_import) 54 55 def test_load_plugin_failure(self): 56 loader = util.PluginLoader("fakegroup") 57 assert_raises_message( 58 exceptions.RuntimeException, 59 "Can't load plugin fakegroup fake", 60 loader.load, 61 "fake", 62 ) 63