def test_delims(self): args = _parse_args('-w --include-chars !abcdefghi'.split()) self.assertEqual(set(ch for ch in args.include_chars), set(ch for ch in "!abcdefghi")) self.assertEqual(set(ch for ch in args.exclude_chars), set()) self.assertTrue(args.word) self.assertEqual(args.word_level, 1) for ch in '!abcdefghi': self.assertTrue(ch in args.delimset) args = _parse_args(['--word', '--exclude-chars', "./, \n\ta"]) self.assertEqual(set(ch for ch in args.exclude_chars), set(ch for ch in "./, \n\ta")) self.assertEqual(set(ch for ch in args.include_chars), set()) self.assertTrue(args.word) self.assertEqual(args.word_level, 1) for ch in "./, \n\ta": self.assertFalse(ch in args.delimset) args = _parse_args( '--include-chars 012345678 --exclude-chars $%@!# -w 4'.split()) self.assertEqual(set(ch for ch in args.exclude_chars), set(ch for ch in "$%@!#")) self.assertEqual(set(ch for ch in args.include_chars), set(str(i) for i in range(9))) self.assertTrue(args.word) self.assertEqual(args.word_level, 4) for i in range(9): self.assertTrue(str(i) in args.delimset) for ch in "$%@!#": self.assertFalse(ch in args.delimset)
def test_delims(self): args = _parse_args('-w --include-chars !abcdefghi'.split()) self.assertEqual(set(ch for ch in args.include_chars), set(ch for ch in "!abcdefghi")) self.assertEqual(set(ch for ch in args.exclude_chars), set()) self.assertTrue(args.word) self.assertEqual(args.word_level, 1) for ch in '!abcdefghi': self.assertTrue(ch in args.delimset) args = _parse_args(['--word', '--exclude-chars', "./, \n\ta"]) self.assertEqual(set(ch for ch in args.exclude_chars), set(ch for ch in "./, \n\ta")) self.assertEqual(set(ch for ch in args.include_chars), set()) self.assertTrue(args.word) self.assertEqual(args.word_level, 1) for ch in "./, \n\ta": self.assertFalse(ch in args.delimset) args = _parse_args('--include-chars 012345678 --exclude-chars $%@!# -w 4'.split()) self.assertEqual(set(ch for ch in args.exclude_chars), set(ch for ch in "$%@!#")) self.assertEqual(set(ch for ch in args.include_chars), set(str(i) for i in range(9))) self.assertTrue(args.word) self.assertEqual(args.word_level, 4) for i in range(9): self.assertTrue(str(i) in args.delimset) for ch in "$%@!#": self.assertFalse(ch in args.delimset)
def test_wc(self): args = _parse_args('-w -c'.split()) self.assertTrue(args.word) self.assertEqual(args.word_level, 1) self.assertTrue(args.character) self.assertEqual(args.character_level, 1) self.assertFalse(args.ignore_lines) args = _parse_args('-c --word'.split()) self.assertTrue(args.word) self.assertEqual(args.word_level, 1) self.assertTrue(args.character) self.assertEqual(args.character_level, 1) self.assertFalse(args.ignore_lines) args = _parse_args('--word -c 10 --ignore-lines'.split()) self.assertTrue(args.word) self.assertEqual(args.word_level, 1) self.assertTrue(args.character) self.assertEqual(args.character_level, 10) self.assertTrue(args.ignore_lines) args = _parse_args('--ignore-lines -c 3 --word 10'.split()) self.assertTrue(args.word) self.assertEqual(args.word_level, 10) self.assertTrue(args.character) self.assertEqual(args.character_level, 3) self.assertTrue(args.ignore_lines)
def test_no_args(self): args = _parse_args([]) self.assertTrue(args.character) self.assertEqual(args.character_level, 1) self.assertFalse(args.word) self.assertFalse(args.byte) self.assertIs(args.input, sys.stdin) self.assertIs(args.output, sys.stdout) self.assertEqual(args.include_chars, '') self.assertEqual(args.exclude_chars, '')
def test_bytes(self): args = _parse_args(['-b']) self.assertTrue(args.byte) self.assertEqual(args.byte_level, 1) self.assertFalse(args.character) self.assertFalse(args.word) self.assertIs(args.input, sys.stdin.buffer) self.assertIs(args.output, sys.stdout.buffer) args = _parse_args(['--byte']) self.assertTrue(args.byte) self.assertEqual(args.byte_level, 1) self.assertFalse(args.character) self.assertFalse(args.word) args = _parse_args('--byte 4'.split()) self.assertTrue(args.byte) self.assertEqual(args.byte_level, 4) self.assertFalse(args.character) self.assertFalse(args.word)
def test_character_args(self): args = _parse_args(['-c']) self.assertTrue(args.character) self.assertEqual(args.character_level, 1) self.assertFalse(args.word) self.assertFalse(args.byte) self.assertIs(args.input, sys.stdin) self.assertIs(args.output, sys.stdout) args = _parse_args(['--character']) self.assertTrue(args.character) self.assertEqual(args.character_level, 1) self.assertFalse(args.word) self.assertFalse(args.byte) args = _parse_args(['-c 24']) self.assertTrue(args.character) self.assertEqual(args.character_level, 24) self.assertFalse(args.word) self.assertFalse(args.byte)
def test_word_args(self): args = _parse_args(['-w']) self.assertFalse(args.character) self.assertTrue(args.word) self.assertEqual(args.word_level, 1) self.assertFalse(args.byte) self.assertFalse(args.ignore_lines) self.assertIs(args.input, sys.stdin) self.assertIs(args.output, sys.stdout) args = _parse_args(['--word']) self.assertFalse(args.character) self.assertTrue(args.word) self.assertEqual(args.word_level, 1) self.assertFalse(args.byte) args = _parse_args('--word 3'.split()) self.assertFalse(args.character) self.assertTrue(args.word) self.assertEqual(args.word_level, 3) self.assertFalse(args.byte)
def test_io(self): import tempfile input_file = tempfile.NamedTemporaryFile(delete=False) input_file.write(b'testing testing') input_file.close() args = _parse_args([input_file.name]) self.assertEqual(args.input.read(), 'testing testing') args.input.close() self.assertIs(args.output, sys.stdout) output_file = tempfile.NamedTemporaryFile(delete=False) args = _parse_args(['--output', output_file.name]) args.output.write('abc def') args.output.close() self.assertEqual(output_file.read(), b'abc def') output_file.close() self.assertIs(args.input, sys.stdin) input_file = tempfile.NamedTemporaryFile(delete=False) input_file.write(b'this') input_file.close() output_file = tempfile.NamedTemporaryFile(delete=False) args = _parse_args([input_file.name, '--output', output_file.name]) self.assertEqual(args.input.read(), 'this') args.input.close() args.output.write('that') args.output.close() self.assertEqual(output_file.read(), b'that') output_file.close() same_file = tempfile.NamedTemporaryFile(delete=False) args = _parse_args(['--output', same_file.name, same_file.name]) args.output.write('goodbye') args.output.close() self.assertEqual(same_file.read(), b'goodbye') same_file.write(b'hello') same_file.close() self.assertEqual(args.input.read(), 'goodbyehello') args.input.close()
def _parse_err(cls, arg_str): with cls._nostderr(): _parse_args(arg_str.split())