File: test_formatters.py

package info (click to toggle)
python-pygal 3.0.5-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 636 kB
  • sloc: python: 7,359; makefile: 9
file content (86 lines) | stat: -rw-r--r-- 2,535 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -*- coding: utf-8 -*-
# This file is part of pygal
#
# A python svg graph plotting library
# Copyright © 2012-2016 Kozea
#
# This library is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with pygal. If not, see <http://www.gnu.org/licenses/>.
"""Test formatters"""

from pygal import formatters


def test_human_readable():
    """Test human_readable formatter"""
    f = formatters.human_readable

    assert f(1) == '1'
    assert f(1.) == '1'
    assert f(10) == '10'
    assert f(12.5) == '12.5'
    assert f(1000) == '1k'
    assert f(5000) == '5k'
    assert f(100000) == '100k'
    assert f(1253) == '1.253k'
    assert f(1250) == '1.25k'

    assert f(0.1) == '100m'
    assert f(0.01) == '10m'
    assert f(0.001) == '1m'
    assert f(0.002) == '2m'
    assert f(0.0025) == '2.5m'
    assert f(0.0001) == '100µ'
    assert f(0.000123) == '123µ'
    assert f(0.00001) == '10µ'
    assert f(0.000001) == '1µ'
    assert f(0.0000001) == '100n'
    assert f(0.0000000001) == '100p'

    assert f(0) == '0'
    assert f(0.) == '0'
    assert f(-1337) == '-1.337k'
    assert f(-.000000042) == '-42n'


def test_human_readable_custom():
    """Test human_readable formatter option"""
    f = formatters.HumanReadable()
    assert f(None) == '∅'
    f = formatters.HumanReadable(none_char='/')
    assert f(None) == '/'


def test_significant():
    """Test significant formatter"""
    f = formatters.significant
    assert f(1) == '1'
    assert f(1.) == '1'
    assert f(-1.) == '-1'
    assert f(10) == '10'
    assert f(10000000000) == '1e+10'
    assert f(100000000000) == '1e+11'
    assert f(120000000000) == '1.2e+11'

    assert f(.1) == '0.1'
    assert f(.01) == '0.01'
    assert f(.0000000001) == '1e-10'
    assert f(-.0000000001) == '-1e-10'
    assert f(.0000000001002) == '1.002e-10'

    assert f(.0000000001002) == '1.002e-10'

    assert f(.12345678912345) == '0.1234567891'
    assert f(.012345678912345) == '0.01234567891'

    assert f(12345678912345) == '1.234567891e+13'