def test_text_region_render(self): content = 'a\nb\nc' text_region = TextRegion(content=content) render_context = MagicMock() result = text_region.render(context=render_context) render_context.render_text.assert_called_once_with(text=content) self.assertEqual(render_context.render_text.return_value, result)
def test_text_region_render(self): content = 'a\nb\nc' text_region = TextRegion(content=content) render_context = MagicMock() result = text_region.render(context=render_context) render_context.render_text.assert_called_once_with(text=content) self.assertEqual(render_context.render_text.return_value, result)
def test_text_region_equality(self): content = 'a\nb\nc' text_region_a = TextRegion(content=content) text_region_b = TextRegion(content=content) text_region_c = TextRegion(content=content + 'd') self.assertIsNot(text_region_a, text_region_b) self.assertTrue(text_region_a == text_region_b) self.assertFalse(text_region_a == text_region_c) self.assertFalse(text_region_a == 42)
def test_script_get_line_info_multiple_includes(self): name = 'a' c_regions = [ TextRegion(content='4\n5\n6'), ] c_script = Script( name='c', regions=c_regions, ) b_regions = [ TextRegion(content='1\n2\n3'), ScriptRegion( script=c_script, content='%include "c"', ), ] b_script = Script( name='b', regions=b_regions, ) a_regions = [ ScriptRegion( script=b_script, content='%include "b"', ), ScriptRegion( script=c_script, content='%include "c"', ), ScriptRegion( script=c_script, content='%include "c"', ), TextRegion(content='7'), ] script = Script( name=name, regions=a_regions, ) get_line_info = script.get_line_info with self.assertRaises(ValueError): get_line_info(0) self.assertEqual((1, 1, 1, 1, 1, 6, a_regions[0]), get_line_info(1)) self.assertEqual((1, 1, 1, 1, 2, 6, a_regions[0]), get_line_info(2)) self.assertEqual((1, 1, 1, 1, 3, 6, a_regions[0]), get_line_info(3)) self.assertEqual((1, 1, 1, 1, 4, 6, a_regions[0]), get_line_info(4)) self.assertEqual((1, 1, 1, 1, 5, 6, a_regions[0]), get_line_info(5)) self.assertEqual((1, 1, 1, 1, 6, 6, a_regions[0]), get_line_info(6)) self.assertEqual((4, 4, 1, 7, 7, 1, a_regions[3]), get_line_info(7)) with self.assertRaises(ValueError): get_line_info(8)
def test_script_line_count(self): name = 'foo' regions = [ TextRegion(content='a'), TextRegion(content='b'), TextRegion(content='c'), ] script = Script( name=name, regions=regions, ) self.assertEqual(3, script.line_count) self.assertEqual(3, script.real_line_count)
def test_script_equality(self): name = 'name' regions = [ TextRegion(content='a'), ] script_a = Script( name=name, regions=regions, ) script_b = Script( name=name, regions=regions, ) script_c = Script( name=name + 'different', regions=regions, ) script_d = Script( name=name, regions=regions + regions, ) self.assertTrue(script_a == script_a) self.assertTrue(hash(script_a) == hash(script_a)) self.assertIsNot(script_a, script_b) self.assertTrue(script_a == script_b) self.assertTrue(hash(script_a) == hash(script_b)) self.assertFalse(script_a == script_c) self.assertFalse(hash(script_a) == hash(script_c)) self.assertFalse(script_a == script_d) self.assertTrue(hash(script_a) == hash(script_d)) self.assertFalse(script_a == 42)
def test_text_region_representation(self): content = 'a\nb\nc' text_region = TextRegion(content=content) self.assertEqual( "TextRegion(line_count=3)", repr(text_region), )
def test_script_region_line_count(self): name = 'foo' regions = [ TextRegion(content='a'), TextRegion(content='b'), ] script = Script( name=name, regions=regions, ) script_region = ScriptRegion( script=script, content='%include "foo"', ) self.assertEqual(script.line_count, script_region.line_count) self.assertEqual(1, script_region.real_line_count)
def test_script_render_default(self): name = 'foo' regions = [ TextRegion(content='a'), TextRegion(content='b'), TextRegion(content='c'), ] script = Script( name=name, regions=regions, ) self.assertEqual('a\nb\nc', script.render()) # Make sure a second call (with the cached value) returns the same # script. self.assertEqual('a\nb\nc', script.render())
def test_script_instanciation_with_args(self): name = 'foo' subregions = [ ArgumentRegion( name="arg2", index=1, type_='string', content='%arg arg2', ), TextRegion(content='local b = 0;'), ] regions = [ ArgumentRegion( name="arg1", index=1, type_='string', content='%arg arg1', ), TextRegion(content='a'), ScriptRegion( script=Script( name='bar', regions=subregions, ), content='%include "bar"', ), ArgumentRegion( name="arg3", index=3, type_='integer', content='%arg arg3 integer', ), ] script = Script( name=name, regions=regions, ) self.assertEqual( [ ("arg1", str), ("arg2", str), ("arg3", int), ], script.args, )
def test_script_region_as_string(self): name = 'foo' regions = [ TextRegion(content='a'), TextRegion(content='b'), ] script = Script( name=name, regions=regions, ) script_region = ScriptRegion( script=script, content='%include "foo"', ) render_context = MagicMock() result = script_region.render(context=render_context) render_context.render_script.assert_called_once_with(script=script) self.assertEqual(render_context.render_script.return_value, result)
def test_script_instanciation_with_keys(self): name = 'foo' subregions = [ KeyRegion( name="key2", index=1, content='%key key2', ), TextRegion(content='local b = 0;'), ] regions = [ KeyRegion( name="key1", index=1, content='%key key1', ), TextRegion(content='a'), ScriptRegion( script=Script( name='bar', regions=subregions, ), content='%include "bar"', ), KeyRegion( name="key3", index=3, content='%key key3', ), ] script = Script( name=name, regions=regions, ) self.assertEqual( [ "key1", "key2", "key3", ], script.keys, )
def test_extract_regions_text_last(self): contents = [ '%arg arg1', '%include "foo"', '%key key2', 'a', 'b', 'c', ] content = '\n'.join(contents) script = Script( name='foo', regions=[ ArgumentRegion( name='arg2', index=1, type_='string', content='%arg arg2', ), KeyRegion( name='key1', index=1, content='%key key1', ), ], ) get_script_by_name = MagicMock(return_value=script) regions = self.parser.parse_regions( content=content, current_path=".", get_script_by_name=get_script_by_name, ) self.maxDiff = None self.assertEqual( [ ArgumentRegion( name='arg1', index=1, type_='string', content=contents[0], ), ScriptRegion( script=script, content='%include "foo"', ), KeyRegion( name='key2', index=2, content=contents[2], ), TextRegion(content='\n'.join(contents[3:6])), ], regions, )
def test_script_as_string(self): name = 'foo' regions = [ TextRegion(content='a'), ] script = Script( name=name, regions=regions, ) self.assertEqual('foo.lua', str(script))
def test_script_get_line_info(self): name = 'foo' subregions = [ TextRegion(content='e\nf\ng'), ] regions = [ TextRegion(content='a'), TextRegion(content='b'), TextRegion(content='c\nd'), ScriptRegion( script=Script( name='bar', regions=subregions, ), content='%include "bar"', ), TextRegion(content='h'), ] script = Script( name=name, regions=regions, ) get_line_info = script.get_line_info with self.assertRaises(ValueError): get_line_info(0) self.assertEqual((1, 1, 1, 1, 1, 1, regions[0]), get_line_info(1)) self.assertEqual((2, 2, 1, 2, 2, 1, regions[1]), get_line_info(2)) self.assertEqual((3, 3, 2, 3, 3, 2, regions[2]), get_line_info(3)) self.assertEqual((3, 4, 2, 3, 4, 2, regions[2]), get_line_info(4)) self.assertEqual((5, 5, 1, 5, 5, 3, regions[3]), get_line_info(5)) self.assertEqual((5, 5, 1, 5, 6, 3, regions[3]), get_line_info(6)) self.assertEqual((5, 5, 1, 5, 7, 3, regions[3]), get_line_info(7)) self.assertEqual((6, 6, 1, 8, 8, 1, regions[4]), get_line_info(8)) with self.assertRaises(ValueError): get_line_info(9)
def test_script_render_with_duplicate_includes_pragma_once(self): subsubscript = Script( name='a', regions=[ TextRegion(content='a'), ], ) subsubscript.multiple_inclusion = False subscript = Script( name='b', regions=[ TextRegion(content='b'), ScriptRegion( script=subsubscript, content='%include "a"', ), ], ) subscript.multiple_inclusion = False script = Script( name='c', regions=[ ScriptRegion( script=subsubscript, content='%include "a"', ), ScriptRegion( script=subscript, content='%include "b"', ), ScriptRegion( script=subscript, content='%include "b"', ), ], ) script.multiple_inclusion = False self.assertEqual('a\nb', script.render())
def test_script_get_scripts_for_line(self): script_a = Script( name='a', regions=[ TextRegion(content='>c\n>d'), ], ) script_b = Script( name='b', regions=[ TextRegion(content='>f\n>g'), ], ) script = Script( name='foo', regions=[ TextRegion(content='a'), TextRegion(content='b'), ScriptRegion( script=script_a, content='%include "a"', ), TextRegion(content='e'), ScriptRegion( script=script_b, content='%include "b"', ), # Next script inclusion will be ignored as the script was # already included. # It won't be possible to get an error from this inclusion line # so we expect a like skip. ScriptRegion( script=script_a, content='%include "a"', ), TextRegion(content='h\ni\nj'), ], ) get_scripts_for_line = script.get_scripts_for_line with self.assertRaises(ValueError): get_scripts_for_line(0) self.assertEqual([(script, 1)], get_scripts_for_line(1)) self.assertEqual([(script, 2)], get_scripts_for_line(2)) self.assertEqual([(script, 3), (script_a, 1)], get_scripts_for_line(3)) self.assertEqual([(script, 3), (script_a, 2)], get_scripts_for_line(4)) self.assertEqual([(script, 4)], get_scripts_for_line(5)) self.assertEqual([(script, 5), (script_b, 1)], get_scripts_for_line(6)) self.assertEqual([(script, 5), (script_b, 2)], get_scripts_for_line(7)) # Here goes the line skip. self.assertEqual([(script, 7)], get_scripts_for_line(8)) self.assertEqual([(script, 8)], get_scripts_for_line(9)) self.assertEqual([(script, 9)], get_scripts_for_line(10)) with self.assertRaises(ValueError): get_scripts_for_line(11)
def test_script_render(self): name = 'foo' regions = [ TextRegion(content='a'), ] script = Script( name=name, regions=regions, ) context = MagicMock() result = script.render(context=context) self.assertEqual(context.render_script.return_value, result) context.render_script.assert_called_once_with(script)
def test_script_representation(self): name = 'foo' regions = [ TextRegion(content='a'), ] script = Script( name=name, regions=regions, ) self.assertEqual( "Script(name='foo')", repr(script), )
def test_script_instanciation(self): name = 'foo' regions = [ TextRegion(content='a'), ] script = Script( name=name, regions=regions, ) self.assertEqual(name, script.name) self.assertEqual(regions, script.regions) self.assertIsNone(script.return_type) self.assertTrue(script.multiple_inclusion)
def test_script_get_real_line_content(self): name = 'foo' subregions = [ TextRegion(content='e\nf\ng'), ] regions = [ TextRegion(content='a'), TextRegion(content='b'), TextRegion(content='c\nd'), ScriptRegion( script=Script( name='bar', regions=subregions, ), content='%include "bar"', ), TextRegion(content='h'), ] script = Script( name=name, regions=regions, ) with self.assertRaises(ValueError): script.get_real_line_content(0) self.assertEqual('a', script.get_real_line_content(1)) self.assertEqual('b', script.get_real_line_content(2)) self.assertEqual('c', script.get_real_line_content(3)) self.assertEqual('d', script.get_real_line_content(4)) self.assertEqual('%include "bar"', script.get_real_line_content(5)) self.assertEqual('%include "bar"', script.get_real_line_content(6)) self.assertEqual('%include "bar"', script.get_real_line_content(7)) self.assertEqual('h', script.get_real_line_content(8)) with self.assertRaises(ValueError): script.get_real_line_content(9)
def test_script_region_instanciation(self): name = 'foo' regions = [ TextRegion(content='a'), ] script = Script( name=name, regions=regions, ) script_region = ScriptRegion( script=script, content='%include "foo"', ) self.assertEqual(script, script_region.script)
def test_script_render_with_duplicate_includes(self): subsubscript = Script( name='a', regions=[ TextRegion(content='a'), ], ) subscript = Script( name='b', regions=[ TextRegion(content='b'), ScriptRegion( script=subsubscript, content='%include "a"', ), ], ) script = Script( name='c', regions=[ ScriptRegion( script=subsubscript, content='%include "a"', ), ScriptRegion( script=subscript, content='%include "b"', ), ScriptRegion( script=subscript, content='%include "b"', ), ], ) self.assertEqual('a\nb\na\nb\na', script.render())
def test_parse_regions_text_only(self): content = 'local a = 1;\nlocal b = 2;' get_script_by_name = MagicMock() regions = self.parser.parse_regions( content=content, current_path=".", get_script_by_name=get_script_by_name, ) self.assertEqual( [ TextRegion(content=content), ], regions, ) self.assertEqual(2, regions[0].line_count) self.assertEqual(2, regions[0].real_line_count) self.assertEqual(0, get_script_by_name.call_count)
def test_script_region_representation(self): name = 'foo' regions = [ TextRegion(content='a'), ] script = Script( name=name, regions=regions, ) script_region = ScriptRegion( script=script, content='%include "foo"', ) self.assertEqual( ("ScriptRegion(real_line_count=1, line_count=1, " "script='foo.lua', content='%include \"foo\"')"), repr(script_region), )
def test_parse_with_empty_script(self): name = 'foo' content = "" script = self.parser.parse( name=name, content=content, script_class=self.ScriptClass, get_script_by_name=None, ) self.assertEqual(name, script.name) self.assertEqual(content, script.render()) self.assertEqual( [ TextRegion(content=content), ], script.regions, )
def test_script_region_keys(self): name = 'foo' regions = [ KeyRegion( name='key1', index=1, content='%key key1', ), TextRegion(content='b'), ] script = Script( name=name, regions=regions, ) script_region = ScriptRegion( script=script, content='%include "foo"', ) self.assertEqual(script.keys, script_region.keys)
def test_script_region_arguments(self): name = 'foo' regions = [ ArgumentRegion( name='arg1', index=1, type_='string', content='%arg arg1', ), TextRegion(content='b'), ] script = Script( name=name, regions=regions, ) script_region = ScriptRegion( script=script, content='%include "foo"', ) self.assertEqual(script.args, script_region.args)
def test_load_scripts_cache_hit(): names = ['sum', 'foo'] cache = { 'foo': Script( name='foo', regions=[TextRegion(content='')], ), } scripts = load_scripts( names=names, path=LUA_SEARCH_PATH, cache=cache, ) assert_equal(set(names), {script.name for script in scripts.values()}) assert_equal(set(names), set(scripts.keys())) assert_equal( { 'sum': scripts['sum'], 'foo': scripts['foo'], }, cache, )
def test_script_run(self, _): name = 'foo' regions = [ KeyRegion( name='key1', index=1, content='%key key1', ), ArgumentRegion( name='arg1', index=1, type_='string', content='%arg arg1', ), TextRegion(content='b'), KeyRegion( name='key2', index=2, content='%key key2', ), ArgumentRegion( name='arg2', index=2, type_='integer', content='%arg arg2', ), ArgumentRegion( name='arg3', index=3, type_='bool', content='%arg arg3', ), ArgumentRegion( name='arg4', index=4, type_='list', content='%arg arg4', ), ArgumentRegion( name='arg5', index=5, type_='dict', content='%arg arg5', ), ] script = Script( name=name, regions=regions, ) script._redis_script = MagicMock(return_value="result") result = script.get_runner(client=None)( arg1='ARG', arg2=2, arg3=False, arg4=(1, 2.5, None, 'a'), arg5={'b': None}, key1='KEY', key2='KEY 2', ) self.assertEqual("result", result) script._redis_script.assert_called_once_with( keys=['KEY', 'KEY 2'], args=[ 'ARG', 2, 0, jdumps([1, 2.5, None, 'a']), jdumps({'b': None}), ], client=None, )
def test_extract_regions_include(self): contents = [ 'local a = 1;', '%include "foo"', 'local d = 4;', '%include "foo"', '%include "foo"', 'local e = 4;', '%include "../foo"', '%include "./bar/../foo"', ] content = '\n'.join(contents) script = Script( name='foo', regions=[ TextRegion(content='local b = 2;\nlocal c = 3;'), ], ) get_script_by_name = MagicMock(return_value=script) regions = self.parser.parse_regions( content=content, current_path=".", get_script_by_name=get_script_by_name, ) self.maxDiff = None self.assertEqual( [ TextRegion(content=contents[0]), ScriptRegion( script=script, content='%include "foo"', ), TextRegion(content=contents[2]), ScriptRegion( script=script, content='%include "foo"', ), ScriptRegion( script=script, content='%include "foo"', ), TextRegion(content=contents[5]), ScriptRegion( script=script, content='%include "../foo"', ), ScriptRegion( script=script, content='%include "./bar/../foo"', ), ], regions, ) self.assertEqual( [ call(name="%s" % script.name), call(name="%s" % script.name), call(name="%s" % script.name), call(name="../%s" % script.name), call(name="%s" % script.name), ], get_script_by_name.mock_calls[:], )