def test_script_region_equality(self): script = MagicMock(spec=Script) other_script = MagicMock(spec=Script) script_region_a = ScriptRegion( script=script, content='%include "foo"', ) script_region_b = ScriptRegion( script=script, content='%include "foo"', ) script_region_c = ScriptRegion( script=other_script, content='%include "bar"', ) script_region_d = ScriptRegion( script=script, content='%include "bar"', ) self.assertIsNot(script_region_a, script_region_b) self.assertTrue(script_region_a == script_region_b) self.assertFalse(script_region_a == script_region_c) self.assertFalse(script_region_a == script_region_d) self.assertFalse(script_region_a == 42)
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_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_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_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_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_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_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_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_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_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_extract_regions_keys(self): contents = [ '%key key1', '%include "foo"', '%key key3', ] content = '\n'.join(contents) script = Script( name='foo', regions=[ KeyRegion( name='key2', index=1, content='%key key2', ), ], ) 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( [ KeyRegion( name='key1', index=1, content=contents[0], ), ScriptRegion( script=script, content='%include "foo"', ), KeyRegion( name='key3', index=3, content=contents[2], ), ], regions, )
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_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_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_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_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_extract_regions_return(self): contents = [ '%include "foo"', '%return integer', ] content = '\n'.join(contents) script = Script( name='foo', regions=[ ReturnRegion( type_='string', content='%return string', ), ], ) 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( [ ScriptRegion( script=script, content='%include "foo"', ), ReturnRegion( type_='integer', content=contents[1], ), ], regions, )
def test_extract_regions_pragma(self): contents = [ '%include "foo"', '%pragma once', ] content = '\n'.join(contents) script = Script( name='foo', regions=[ PragmaRegion( value='once', content='%pragma once', ), ], ) 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( [ ScriptRegion( script=script, content='%include "foo"', ), PragmaRegion( value='once', content=contents[1], ), ], regions, )
def test_extract_regions_arguments(self): contents = [ '%arg arg1', '%include "foo"', '%arg arg3 string', '%arg arg4 integer', '%arg arg5 boolean', ] content = '\n'.join(contents) script = Script( name='foo', regions=[ ArgumentRegion( name='arg2', index=1, type_='string', content='%arg arg2', ), ], ) 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"', ), ArgumentRegion( name='arg3', index=3, type_='string', content=contents[2], ), ArgumentRegion( name='arg4', index=4, type_='integer', content=contents[3], ), ArgumentRegion( name='arg5', index=5, type_='boolean', content=contents[4], ), ], regions, )
def test_script_render_with_duplicate_includes_nested_args(self): script_a = Script( name='a', regions=[ ArgumentRegion( name='arg1', type_='string', index=1, content='%arg arg1', ), ], ) script_b = Script( name='b', regions=[ ArgumentRegion( name='arg2', type_='string', index=1, content='%arg arg2', ), ], ) script_c = Script( name='c', regions=[ ArgumentRegion( name='arg3', type_='string', index=1, content='%arg arg3', ), ], ) script_d = Script( name='d', regions=[ ScriptRegion( script=script_c, content='%include "c"', ), ArgumentRegion( name='arg4', type_='string', index=2, content='%arg arg4', ), ], ) script = Script( name='abcd', regions=[ ScriptRegion( script=script_a, content='%include "a"', ), ScriptRegion( script=script_b, content='%include "b"', ), ScriptRegion( script=script_d, content='%include "d"', ), ], ) self.assertEqual( [ 'local arg1 = ARGV[1]', 'local arg2 = ARGV[2]', 'local arg3 = ARGV[3]', 'local arg4 = ARGV[4]', ], script.render().split('\n'), )
def test_script_render_with_duplicate_includes_nested_keys(self): script_a = Script( name='a', regions=[ KeyRegion( name='key1', index=1, content='%key key1', ), ], ) script_b = Script( name='b', regions=[ KeyRegion( name='key2', index=1, content='%key key2', ), ], ) script_c = Script( name='c', regions=[ KeyRegion( name='key3', index=1, content='%key key3', ), ], ) script_d = Script( name='d', regions=[ ScriptRegion( script=script_c, content='%include "c"', ), KeyRegion( name='key4', index=2, content='%key key4', ), ], ) script = Script( name='abcd', regions=[ ScriptRegion( script=script_a, content='%include "a"', ), ScriptRegion( script=script_b, content='%include "b"', ), ScriptRegion( script=script_d, content='%include "d"', ), ], ) self.assertEqual( [ 'local key1 = KEYS[1]', 'local key2 = KEYS[2]', 'local key3 = KEYS[3]', 'local key4 = KEYS[4]', ], script.render().split('\n'), )
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[:], )