File: test_parsing_basic_types.py

package info (click to toggle)
svgwrite 1.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 2,092 kB
  • sloc: python: 12,603; makefile: 117; sh: 5
file content (116 lines) | stat: -rw-r--r-- 6,116 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python
#coding:utf-8
# Author:  mozman --<mozman@gmx.at>
# Purpose: test parsing basic types
# Created: 17.10.2010
# Copyright (C) 2010, Manfred Moitzi
# License: MIT License

import sys
import unittest
import pyparsing as pp

from svgwrite.data.svgparser import exponent, fractional_constant, \
     scientific_constant, number

class TestBasicTypes(unittest.TestCase):
    def is_valid(self, parser, value):
        try:
            parser.parseString(value, parseAll=True)
            return True
        except pp.ParseException:
            return False

    def test_exponent(self):
        self.assertTrue(self.is_valid(exponent, "e1"))
        self.assertTrue(self.is_valid(exponent, "E1"))
        self.assertTrue(self.is_valid(exponent, "e10"))
        self.assertTrue(self.is_valid(exponent, "E10"))
        self.assertTrue(self.is_valid(exponent, "e-1"))
        self.assertTrue(self.is_valid(exponent, "E-1"))
        self.assertTrue(self.is_valid(exponent, "e-10"))
        self.assertTrue(self.is_valid(exponent, "E-10"))
        self.assertTrue(self.is_valid(exponent, "e+1"))
        self.assertTrue(self.is_valid(exponent, "E+1"))
        self.assertTrue(self.is_valid(exponent, "e+10"))
        self.assertTrue(self.is_valid(exponent, "E+10"))

    def test_exponent_error(self):
        self.assertFalse(self.is_valid(exponent, "e"))
        self.assertFalse(self.is_valid(exponent, "e1."))
        self.assertFalse(self.is_valid(exponent, "e1.0"))

    def test_fractional_constant(self):
        self.assertTrue(self.is_valid(fractional_constant, "1."))
        self.assertTrue(self.is_valid(fractional_constant, "1.0"))
        self.assertTrue(self.is_valid(fractional_constant, ".1"))

    def test_fractional_constant_error(self):
        self.assertFalse(self.is_valid(fractional_constant, "1"), "missing '.' is not valid")
        self.assertFalse(self.is_valid(fractional_constant, "-1.0"), "a sign is not valid")
        self.assertFalse(self.is_valid(fractional_constant, "+1.0"), "a sign is not valid")
        self.assertFalse(self.is_valid(fractional_constant, "1.0.0"), "two or more '.' are not valid")
        self.assertFalse(self.is_valid(fractional_constant, "1..0"), "two or more '.' are not valid")
        self.assertFalse(self.is_valid(fractional_constant, "."), "only a '.' are not valid")
        self.assertFalse(self.is_valid(fractional_constant, "-."), "only a '.' are not valid")
        self.assertFalse(self.is_valid(fractional_constant, "+."), "only a '.' are not valid")
        self.assertFalse(self.is_valid(fractional_constant, "1,0"), "',' is not valid comma")

    def test_scientific_constant(self):
        self.assertTrue(self.is_valid(scientific_constant, "3.1415"))
        self.assertTrue(self.is_valid(scientific_constant, "3.1415e10"))
        self.assertTrue(self.is_valid(scientific_constant, "3.1415e-10"))
        self.assertTrue(self.is_valid(scientific_constant, "3.1415e+10"))

    def test_scientific_constant_error(self):
        self.assertFalse(self.is_valid(scientific_constant, "-3.1415"), "leading sign is not valid")
        self.assertFalse(self.is_valid(scientific_constant, "+3.1415"), "leading sign is not valid")
        self.assertFalse(self.is_valid(scientific_constant, "1.0.0e10"), "two or more '.' are not valid")
        self.assertFalse(self.is_valid(scientific_constant, "1..0e10"), "two or more '.' are not valid")
        self.assertFalse(self.is_valid(scientific_constant, "."), "only a '.' are not valid")
        self.assertFalse(self.is_valid(scientific_constant, "-."), "only a '.' are not valid")
        self.assertFalse(self.is_valid(scientific_constant, "+."), "only a '.' are not valid")

    def test_number(self):
        self.assertTrue(self.is_valid(number, "3.1415"))
        self.assertTrue(self.is_valid(number, "3."))
        self.assertTrue(self.is_valid(number, "3.e10"))
        self.assertTrue(self.is_valid(number, ".1415"))
        self.assertTrue(self.is_valid(number, "-.1415"))
        self.assertTrue(self.is_valid(number, ".1415e10"))
        self.assertTrue(self.is_valid(number, "-.1415e10"))
        self.assertTrue(self.is_valid(number, "-3.1415"))
        self.assertTrue(self.is_valid(number, "+3.1415"))

        self.assertTrue(self.is_valid(number, "3.1415e10"))
        self.assertTrue(self.is_valid(number, "-3.1415e10"))
        self.assertTrue(self.is_valid(number, "-3.e10"))
        self.assertTrue(self.is_valid(number, "-.1415e10"))
        self.assertTrue(self.is_valid(number, "+3.1415e10"))
        self.assertTrue(self.is_valid(number, "+.1415e10"))

        self.assertTrue(self.is_valid(number, "3.1415e-10"))
        self.assertTrue(self.is_valid(number, "-3.1415e-10"))
        self.assertTrue(self.is_valid(number, "+3.1415e-10"))
        self.assertTrue(self.is_valid(number, "3.1415e+10"))
        self.assertTrue(self.is_valid(number, "-3.1415e+10"))
        self.assertTrue(self.is_valid(number, "+3.1415e+10"))

        self.assertTrue(self.is_valid(number, "31415e+10"))
        self.assertTrue(self.is_valid(number, "-31415e+10"))
        self.assertTrue(self.is_valid(number, "+31415e+10"))

    def test_number_error(self):
        self.assertFalse(self.is_valid(number, "1.0.0e10"), "two or more '.' are not valid")
        self.assertFalse(self.is_valid(number, "1..0e10"), "two or more '.' are not valid")
        self.assertFalse(self.is_valid(number, "."), "only a '.' are not valid")
        self.assertFalse(self.is_valid(number, "-."), "only a '.' are not valid")
        self.assertFalse(self.is_valid(number, "+."), "only a '.' are not valid")
        self.assertFalse(self.is_valid(number, "1.0e1-0"), "exponent 'e1-0' is not valid")
        self.assertFalse(self.is_valid(number, "1.0e1+0"), "exponent 'e1+0' is not valid")
        self.assertFalse(self.is_valid(number, "1.0e1.0"), "exponent 'e1.0' is not valid")
        self.assertFalse(self.is_valid(number, "1.0e"), "only 'e' is not valid")
        self.assertFalse(self.is_valid(number, "e10"), "only an exponent is not valid")

if __name__=='__main__':
    unittest.main()