[YoutubeDL] Fix typo in string negation implementation and add more tests (closes #18961)

master
Sergey M․ 5 years ago
parent 435e382423
commit e118a8794f
No known key found for this signature in database
GPG Key ID: 2C393E0F18A9236D

@ -242,6 +242,7 @@ class TestFormatSelection(unittest.TestCase):
def test_format_selection_string_ops(self): def test_format_selection_string_ops(self):
formats = [ formats = [
{'format_id': 'abc-cba', 'ext': 'mp4', 'url': TEST_URL}, {'format_id': 'abc-cba', 'ext': 'mp4', 'url': TEST_URL},
{'format_id': 'zxc-cxz', 'ext': 'webm', 'url': TEST_URL},
] ]
info_dict = _make_result(formats) info_dict = _make_result(formats)
@ -253,6 +254,11 @@ class TestFormatSelection(unittest.TestCase):
# does not equal (!=) # does not equal (!=)
ydl = YDL({'format': '[format_id!=abc-cba]'}) ydl = YDL({'format': '[format_id!=abc-cba]'})
ydl.process_ie_result(info_dict.copy())
downloaded = ydl.downloaded_info_dicts[0]
self.assertEqual(downloaded['format_id'], 'zxc-cxz')
ydl = YDL({'format': '[format_id!=abc-cba][format_id!=zxc-cxz]'})
self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy()) self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy())
# starts with (^=) # starts with (^=)
@ -262,7 +268,12 @@ class TestFormatSelection(unittest.TestCase):
self.assertEqual(downloaded['format_id'], 'abc-cba') self.assertEqual(downloaded['format_id'], 'abc-cba')
# does not start with (!^=) # does not start with (!^=)
ydl = YDL({'format': '[format_id!^=abc-cba]'}) ydl = YDL({'format': '[format_id!^=abc]'})
ydl.process_ie_result(info_dict.copy())
downloaded = ydl.downloaded_info_dicts[0]
self.assertEqual(downloaded['format_id'], 'zxc-cxz')
ydl = YDL({'format': '[format_id!^=abc][format_id!^=zxc]'})
self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy()) self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy())
# ends with ($=) # ends with ($=)
@ -272,16 +283,29 @@ class TestFormatSelection(unittest.TestCase):
self.assertEqual(downloaded['format_id'], 'abc-cba') self.assertEqual(downloaded['format_id'], 'abc-cba')
# does not end with (!$=) # does not end with (!$=)
ydl = YDL({'format': '[format_id!$=abc-cba]'}) ydl = YDL({'format': '[format_id!$=cba]'})
ydl.process_ie_result(info_dict.copy())
downloaded = ydl.downloaded_info_dicts[0]
self.assertEqual(downloaded['format_id'], 'zxc-cxz')
ydl = YDL({'format': '[format_id!$=cba][format_id!$=cxz]'})
self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy()) self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy())
# contains (*=) # contains (*=)
ydl = YDL({'format': '[format_id*=-]'}) ydl = YDL({'format': '[format_id*=bc-cb]'})
ydl.process_ie_result(info_dict.copy()) ydl.process_ie_result(info_dict.copy())
downloaded = ydl.downloaded_info_dicts[0] downloaded = ydl.downloaded_info_dicts[0]
self.assertEqual(downloaded['format_id'], 'abc-cba') self.assertEqual(downloaded['format_id'], 'abc-cba')
# does not contain (!*=) # does not contain (!*=)
ydl = YDL({'format': '[format_id!*=bc-cb]'})
ydl.process_ie_result(info_dict.copy())
downloaded = ydl.downloaded_info_dicts[0]
self.assertEqual(downloaded['format_id'], 'zxc-cxz')
ydl = YDL({'format': '[format_id!*=abc][format_id!*=zxc]'})
self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy())
ydl = YDL({'format': '[format_id!*=-]'}) ydl = YDL({'format': '[format_id!*=-]'})
self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy()) self.assertRaises(ExtractorError, ydl.process_ie_result, info_dict.copy())

@ -1078,7 +1078,7 @@ class YoutubeDL(object):
comparison_value = m.group('value') comparison_value = m.group('value')
str_op = STR_OPERATORS[m.group('op')] str_op = STR_OPERATORS[m.group('op')]
if m.group('negation'): if m.group('negation'):
op = lambda attr, value: not str_op op = lambda attr, value: not str_op(attr, value)
else: else:
op = str_op op = str_op

Loading…
Cancel
Save