File: test_utils.py

package info (click to toggle)
grass 7.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 135,976 kB
  • ctags: 44,148
  • sloc: ansic: 410,300; python: 166,939; cpp: 34,819; sh: 9,358; makefile: 6,618; xml: 3,551; sql: 769; lex: 519; yacc: 450; asm: 387; perl: 282; sed: 17; objc: 7
file content (66 lines) | stat: -rw-r--r-- 1,627 bytes parent folder | download
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
# -*- coding: utf-8 -*-
import os

from grass.gunittest.case import TestCase
from grass.gunittest.main import test

from grass.script import utils


class EnvironChange(TestCase):
    env = dict()
    NOT_FOUND = 'Not found!'

    def setUp(self):
        self.original_env = {k: os.environ.get(k, self.NOT_FOUND)
                             for k in self.env.keys()}
        for k, v in self.env.items():
                os.environ[k] = v

    def tearDown(self):
        for k, v in self.env.items():
                oval = self.original_env[k]
                if oval == self.NOT_FOUND:
                    os.environ.pop(k)
                else:
                    os.environ[k] = oval


class LcAllC(EnvironChange):
    env = dict(LC_ALL='C')


class TestEncode(TestCase):
    """Tests function `encode` that convert value to bytes."""

    def test_bytes(self):
        self.assertEqual(b'text', utils.encode(b'text'))

    def test_unicode(self):
        self.assertEqual(b'text', utils.encode(u'text'))

    def test_bytes_grabage_in_out(self):
        """If the input is bytes we should not touch it for encoding"""
        self.assertEqual(b'Příšerný kůň', utils.encode(b'Příšerný kůň'))


class TestDecode(TestCase):
    """Tests function `encode` that convert value to unicode."""

    def test_bytes(self):
        self.assertEqual(u'text', utils.decode(b'text'))

    def test_unicode(self):
        self.assertEqual(u'text', utils.decode(u'text'))


class TestEncodeLcAllC(TestEncode, LcAllC):
    pass


class TestDecodeLcAllC(TestDecode, LcAllC):
    pass


if __name__ == '__main__':
    test()