def test4special_characters_escape(self): """In this test case special character escaping is tested.""" match_context = MatchContext( b'error: the command \\"python run.py\\" was not found" ') delimited_data_model_element = DelimitedDataModelElement( 'id', b'"', b'\\') match_element = delimited_data_model_element.get_match_element( 'match', match_context) self.assertEqual( match_element.get_match_string(), b'error: the command \\"python run.py\\" was not found') match_context = MatchContext( rb'^This is a simple regex string. It costs 10\$.$') delimited_data_model_element = DelimitedDataModelElement( 'id', b'$', b'\\') match_element = delimited_data_model_element.get_match_element( 'match', match_context) self.assertEqual(match_element.get_match_string(), rb'^This is a simple regex string. It costs 10\$.') match_context = MatchContext(b'the searched file is .gitignore.') delimited_data_model_element = DelimitedDataModelElement( 'id', b'.', b' ') match_element = delimited_data_model_element.get_match_element( 'match', match_context) self.assertEqual(match_element.get_match_string(), b'the searched file is .gitignore')
def test14get_match_element_match_context_input_validation(self): """Check if an exception is raised, when other classes than MatchContext are used in get_match_element.""" model_element = DelimitedDataModelElement(self.id_, self.delimiter) data = b"one, two, three" model_element.get_match_element(self.path, DummyMatchContext(data)) model_element.get_match_element(self.path, MatchContext(data)) self.assertRaises(AttributeError, model_element.get_match_element, self.path, MatchElement(None, data, None, None)) self.assertRaises(AttributeError, model_element.get_match_element, self.path, data) self.assertRaises(AttributeError, model_element.get_match_element, self.path, data.decode()) self.assertRaises(AttributeError, model_element.get_match_element, self.path, True) self.assertRaises(AttributeError, model_element.get_match_element, self.path, 123) self.assertRaises(AttributeError, model_element.get_match_element, self.path, 123.22) self.assertRaises(AttributeError, model_element.get_match_element, self.path, None) self.assertRaises(AttributeError, model_element.get_match_element, self.path, []) self.assertRaises(AttributeError, model_element.get_match_element, self.path, {"key": MatchContext(data)}) self.assertRaises(AttributeError, model_element.get_match_element, self.path, set()) self.assertRaises(AttributeError, model_element.get_match_element, self.path, ()) self.assertRaises(AttributeError, model_element.get_match_element, self.path, model_element)
def test8special_characters_escape_no_match(self): """In this test case special character escaping is tested without matching.""" data = b'error: the command \\"python run.py\\" was not found\\" ' match_context = DummyMatchContext(data) delimited_data_model_element = DelimitedDataModelElement( self.id_, b'"', b"\\") match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) data = rb"^This is a simple regex string. It costs 10\$.\$" match_context = DummyMatchContext(data) delimited_data_model_element = DelimitedDataModelElement( self.id_, b"$", b"\\") match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) data = b"the searched file is .gitignore ." match_context = DummyMatchContext(data) delimited_data_model_element = DelimitedDataModelElement( self.id_, b".", b" ") match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context)
def test7special_characters_escape(self): """In this test case special character escaping is tested. The delimiter is not consumed (consume_delimiter=False).""" data = b'error: the command \\"python run.py\\" was not found" ' value = b'error: the command \\"python run.py\\" was not found' match_context = DummyMatchContext(data) delimited_data_model_element = DelimitedDataModelElement( self.id_, b'"', b"\\") match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_, self.path, value, value, None) data = rb"^This is a simple regex string. It costs 10\$.$" value = rb"^This is a simple regex string. It costs 10\$." match_context = DummyMatchContext(data) delimited_data_model_element = DelimitedDataModelElement( self.id_, b"$", b"\\") match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_, self.path, value, value, None) data = b"the searched file is .gitignore." value = b"the searched file is .gitignore" match_context = DummyMatchContext(data) delimited_data_model_element = DelimitedDataModelElement( self.id_, b".", b" ") match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_, self.path, value, value, None)
def test6delimiter_string_no_match(self): """In this test case a whole string is searched for in the match_data with no match.""" data = b"this is a match context.\n" match_context = DummyMatchContext(data) delimited_data_model_element = DelimitedDataModelElement( self.id_, b"other data") match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) match_context = DummyMatchContext(data) delimited_data_model_element = DelimitedDataModelElement( self.id_, b"isa") match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) match_context = DummyMatchContext(data) delimited_data_model_element = DelimitedDataModelElement( self.id_, b"context\n") match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) match_context = DummyMatchContext(data) delimited_data_model_element = DelimitedDataModelElement( self.id_, b"this is a match context.\n") match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context)
def test5consume_delimeter(self): """In this test case check if the consume_delimeter parameter is working properly.""" match_context = MatchContext(self.match_context_string) delimited_data_model_element = DelimitedDataModelElement( 'id', b'c', consume_delimiter=False) match_element = delimited_data_model_element.get_match_element( 'match', match_context) self.assertEqual(match_element.get_match_string(), b'this is a mat') match_context = MatchContext(self.match_context_string) delimited_data_model_element = DelimitedDataModelElement( 'id', b'c', consume_delimiter=True) match_element = delimited_data_model_element.get_match_element( 'match', match_context) self.assertEqual(match_element.get_match_string(), b'this is a matc') match_context = MatchContext(self.match_context_string) delimited_data_model_element = DelimitedDataModelElement( 'id', b' is', consume_delimiter=False) match_element = delimited_data_model_element.get_match_element( 'match', match_context) self.assertEqual(match_element.get_match_string(), b'this') match_context = MatchContext(self.match_context_string) delimited_data_model_element = DelimitedDataModelElement( 'id', b' is', consume_delimiter=True) match_element = delimited_data_model_element.get_match_element( 'match', match_context) self.assertEqual(match_element.get_match_string(), b'this is')
def test1delimeter_single_char(self): """A single character is used as delimeter.""" match_context = MatchContext(self.match_context_string) delimited_data_model_element = DelimitedDataModelElement('id', b'c') match_element = delimited_data_model_element.get_match_element( 'match', match_context) self.assertEqual(match_element.get_match_string(), b'this is a mat') match_context = MatchContext(self.match_context_string) delimited_data_model_element = DelimitedDataModelElement('id', b'f') match_element = delimited_data_model_element.get_match_element( 'match', match_context) self.assertEqual(match_element, None)
def test2delimeter_string(self): """In this test case a whole string is searched for in the match_data.""" match_context = MatchContext(self.match_context_string) delimited_data_model_element = DelimitedDataModelElement('id', b' is') match_element = delimited_data_model_element.get_match_element( 'match', match_context) self.assertEqual(match_element.get_match_string(), b'this')
def test3delimeter_none_empty_or_not_printable(self): """In this test case all not allowed values are tested.""" match_context = MatchContext(self.match_context_string) delimited_data_model_element = DelimitedDataModelElement('id', b'') match_element = delimited_data_model_element.get_match_element( 'match', match_context) self.assertEqual(match_element, None) match_context = MatchContext(self.match_context_string) delimited_data_model_element = DelimitedDataModelElement('id', None) self.assertRaises(TypeError, delimited_data_model_element.get_match_element, 'match', match_context) match_context = MatchContext(self.match_context_string) delimited_data_model_element = DelimitedDataModelElement('id', b'\x01') match_element = delimited_data_model_element.get_match_element( 'match', match_context) self.assertEqual(match_element, None)
def test4get_match_element_single_char_no_match(self): """A single character is used as delimiter and not matched.""" data = b"this is a match context.\n" for char in "bdfgjklpqruvwyz": delimited_data_model_element = DelimitedDataModelElement( self.id_, char.encode()) match_context = DummyMatchContext(data) match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context)
def test5delimiter_string(self): """In this test case a whole string is searched for in the match_data and it is not consumed (consume_delimiter=False).""" data = b"this is a match context.\n" value = b"this" match_context = DummyMatchContext(data) delimited_data_model_element = DelimitedDataModelElement( self.id_, b" is") match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_, self.path, value, value, None) value = b"th" match_context = DummyMatchContext(data) delimited_data_model_element = DelimitedDataModelElement( self.id_, b"is") match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_, self.path, value, value, None) value = b"this is a match " match_context = DummyMatchContext(data) delimited_data_model_element = DelimitedDataModelElement( self.id_, b"context.\n") match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_, self.path, value, value, None) value = b"t" match_context = DummyMatchContext(data) delimited_data_model_element = DelimitedDataModelElement( self.id_, b"his is a match context.\n") match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_, self.path, value, value, None)
def test10consume_delimiter_no_match(self): """In this test case check if the consume_delimiter parameter is working properly and does not match data.""" data = b"this is a match context.\n" for char in "bdfgjklpqruvwyz": delimited_data_model_element = DelimitedDataModelElement( self.id_, char.encode(), consume_delimiter=True) match_context = DummyMatchContext(data) match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) match_context = DummyMatchContext(data) delimited_data_model_element = DelimitedDataModelElement( self.id_, b"other data", consume_delimiter=True) match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) match_context = DummyMatchContext(data) delimited_data_model_element = DelimitedDataModelElement( self.id_, b"isa", consume_delimiter=True) match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) match_context = DummyMatchContext(data) delimited_data_model_element = DelimitedDataModelElement( self.id_, b"context\n", consume_delimiter=True) match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context) match_context = DummyMatchContext(data) delimited_data_model_element = DelimitedDataModelElement( self.id_, b"this is a match context.\n", consume_delimiter=True) match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_no_match_results(data, match_element, match_context)
def test3get_match_element_single_char(self): """A single character is used as delimiter and not consumed (consume_delimiter=False).""" data = b"this is a match context.\n" delimited_data_model_element = DelimitedDataModelElement( self.id_, b"a") value = b"this is " match_context = DummyMatchContext(data) match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_, self.path, value, value, None) delimited_data_model_element = DelimitedDataModelElement( self.id_, b"c") value = b"this is a mat" match_context = DummyMatchContext(data) match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_, self.path, value, value, None) delimited_data_model_element = DelimitedDataModelElement( self.id_, b"e") value = b"this is a match cont" match_context = DummyMatchContext(data) match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_, self.path, value, value, None) delimited_data_model_element = DelimitedDataModelElement( self.id_, b"\n") value = b"this is a match context." match_context = DummyMatchContext(data) match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_, self.path, value, value, None)
def test9consume_delimiter(self): """In this test case check if the consume_delimiter parameter is working properly.""" data = b"this is a match context.\n" delimited_data_model_element = DelimitedDataModelElement( self.id_, b"a", consume_delimiter=True) value = b"this is a" match_context = DummyMatchContext(data) match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_, self.path, value, value, None) delimited_data_model_element = DelimitedDataModelElement( self.id_, b"c", consume_delimiter=True) value = b"this is a matc" match_context = DummyMatchContext(data) match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_, self.path, value, value, None) delimited_data_model_element = DelimitedDataModelElement( self.id_, b"e", consume_delimiter=True) value = b"this is a match conte" match_context = DummyMatchContext(data) match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_, self.path, value, value, None) delimited_data_model_element = DelimitedDataModelElement( self.id_, b"\n", consume_delimiter=True) value = b"this is a match context.\n" match_context = DummyMatchContext(data) match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_, self.path, value, value, None) value = b"this is" match_context = DummyMatchContext(data) delimited_data_model_element = DelimitedDataModelElement( self.id_, b" is", consume_delimiter=True) match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_, self.path, value, value, None) value = b"this" match_context = DummyMatchContext(data) delimited_data_model_element = DelimitedDataModelElement( self.id_, b"is", consume_delimiter=True) match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_, self.path, value, value, None) value = b"this is a match context.\n" match_context = DummyMatchContext(data) delimited_data_model_element = DelimitedDataModelElement( self.id_, b"context.\n", consume_delimiter=True) match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_, self.path, value, value, None) value = b"this is a match context.\n" match_context = DummyMatchContext(data) delimited_data_model_element = DelimitedDataModelElement( self.id_, b"his is a match context.\n", consume_delimiter=True) match_element = delimited_data_model_element.get_match_element( self.path, match_context) self.compare_match_results(data, match_element, match_context, self.id_, self.path, value, value, None)