1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
import unittest
from argparse import ArgumentTypeError
from streamlink.utils.args import (
boolean, comma_list, comma_list_filter, filesize, keyvalue, num
)
class TestUtilsArgs(unittest.TestCase):
def test_boolean_true(self):
self.assertEqual(boolean('1'), True)
self.assertEqual(boolean('on'), True)
self.assertEqual(boolean('true'), True)
self.assertEqual(boolean('yes'), True)
self.assertEqual(boolean('Yes'), True)
def test_boolean_false(self):
self.assertEqual(boolean('0'), False)
self.assertEqual(boolean('false'), False)
self.assertEqual(boolean('no'), False)
self.assertEqual(boolean('No'), False)
self.assertEqual(boolean('off'), False)
def test_boolean_error(self):
with self.assertRaises(ArgumentTypeError):
boolean('yesno')
with self.assertRaises(ArgumentTypeError):
boolean('FOO')
with self.assertRaises(ArgumentTypeError):
boolean('2')
def test_comma_list(self):
# (values, result)
test_data = [
('foo.bar,example.com', ['foo.bar', 'example.com']),
('/var/run/foo,/var/run/bar', ['/var/run/foo', '/var/run/bar']),
('foo bar,24', ['foo bar', '24']),
('hls', ['hls']),
]
for _v, _r in test_data:
self.assertEqual(comma_list(_v), _r)
def test_comma_list_filter(self):
# (acceptable, values, result)
test_data = [
(['foo', 'bar', 'com'], 'foo,bar,example.com', ['foo', 'bar']),
(['/var/run/foo', 'FO'], '/var/run/foo,/var/run/bar',
['/var/run/foo']),
(['hls', 'hls5', 'dash'], 'hls,hls5', ['hls', 'hls5']),
(['EU', 'RU'], 'DE,FR,RU,US', ['RU']),
]
for _a, _v, _r in test_data:
func = comma_list_filter(_a)
self.assertEqual(func(_v), _r)
def test_filesize(self):
self.assertEqual(filesize('2000'), 2000)
self.assertEqual(filesize('11KB'), 1024 * 11)
self.assertEqual(filesize('12MB'), 1024 * 1024 * 12)
self.assertEqual(filesize('1KB'), 1024)
self.assertEqual(filesize('1MB'), 1024 * 1024)
self.assertEqual(filesize('2KB'), 1024 * 2)
def test_filesize_error(self):
with self.assertRaises(ValueError):
filesize('FOO')
with self.assertRaises(ValueError):
filesize('0.00000')
def test_keyvalue(self):
# (value, result)
test_data = [
('X-Forwarded-For=127.0.0.1', ('X-Forwarded-For', '127.0.0.1')),
('Referer=https://foo.bar', ('Referer', 'https://foo.bar')),
(
'User-Agent=Mozilla/5.0 (X11; Linux x86_64; rv:60.0)'
' Gecko/20100101 Firefox/60.0',
('User-Agent', 'Mozilla/5.0 (X11; Linux x86_64; rv:60.0) '
'Gecko/20100101 Firefox/60.0')
),
('domain=example.com', ('domain', 'example.com')),
]
for _v, _r in test_data:
self.assertEqual(keyvalue(_v), _r)
def test_keyvalue_error(self):
with self.assertRaises(ValueError):
keyvalue('127.0.0.1')
def test_num(self):
# (value, func, result)
test_data = [
('33', num(int, 5, 120), 33),
('234', num(int, min=10), 234),
('50.222', num(float, 10, 120), 50.222),
]
for _v, _f, _r in test_data:
self.assertEqual(_f(_v), _r)
def test_num_error(self):
with self.assertRaises(ArgumentTypeError):
func = num(int, 5, 10)
func('3')
with self.assertRaises(ArgumentTypeError):
func = num(int, max=11)
func('12')
with self.assertRaises(ArgumentTypeError):
func = num(int, min=15)
func('8')
with self.assertRaises(ArgumentTypeError):
func = num(float, 10, 20)
func('40.222')
|