1# encoding=UTF-8
2
3# Copyright © 2015-2017 Jakub Wilk <jwilk@jwilk.net>
4#
5# This file is part of pdf2djvu.
6#
7# pdf2djvu is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License version 2 as
9# published by the Free Software Foundation.
10#
11# pdf2djvu is distributed in the hope that it will be useful, but
12# WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14# General Public License for more details.
15
16import re
17
18from tools import (
19    case,
20)
21
22class test(case):
23
24    def test_no_title(self):
25        def t(*args):
26            self.pdf2djvu(*args).assert_()
27            r = self.ls()
28            r.assert_(stdout=re.compile(
29                r'\n'
30                r'\s*1\s+P\s+\d+\s+[\w.]+\n'
31                r'\s*2\s+P\s+\d+\s+[\w.]+\n'
32            ))
33        t('--page-title-template', '')
34        t('--no-page-titles')
35
36    def test_ascii(self):
37        template = '#{page}'
38        self.pdf2djvu('--page-title-template', template).assert_()
39        r = self.ls()
40        r.assert_(stdout=re.compile(
41            r'\n'
42            r'\s*1\s+P\s+\d+\s+[\w.]+\s+T=#1\n'
43            r'\s*2\s+P\s+\d+\s+[\w.]+\s+T=#2\n'
44        ))
45
46    def test_utf8(self):
47        self.require_feature('POSIX')
48        template = '№{page}'
49        self.pdf2djvu('--page-title-template', template, encoding='UTF-8').assert_()
50        r = self.ls()
51        r.assert_(stdout=re.compile(
52            r'\n'
53            r'\s*1\s+P\s+\d+\s+[\w.]+\s+T=№1\n'
54            r'\s*2\s+P\s+\d+\s+[\w.]+\s+T=№2\n'
55        ))
56
57    def test_bad_encoding(self):
58        self.require_feature('POSIX')
59        template = '{page}\xBA'
60        r = self.pdf2djvu('--page-title-template', template, encoding='UTF-8')
61        r.assert_(stderr=re.compile('^Unable to convert page title to UTF-8:'), rc=1)
62
63    def test_iso8859_1(self):
64        template = '{page}\xBA'
65        self.pdf2djvu('--page-title-template', template, encoding='ISO8859-1').assert_()
66        r = self.ls()
67        r.assert_(stdout=re.compile(
68            r'\n'
69            r'\s*1\s+P\s+\d+\s+[\w.]+\s+T=1º\n'
70            r'\s*2\s+P\s+\d+\s+[\w.]+\s+T=2º\n'
71        ))
72
73# vim:ts=4 sts=4 sw=4 et
74