-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
138 lines (114 loc) · 5.8 KB
/
tests.py
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from cssmediaquery import match, parse
import unittest
class MediaQueryParserAndMatcherTest(unittest.TestCase):
def test_screen(self):
self.assertEqual(parse('screen'), [{
"inverse": False,
"type": 'screen',
"expressions": []
}])
def test_not_screen(self):
self.assertEqual(parse('not screen'), [{
"inverse": True,
"type": 'screen',
"expressions": []
}])
def test_retina_media_query_list(self):
query = 'only screen and (-webkit-min-device-pixel-ratio: 2),\n' + \
'only screen and (min--moz-device-pixel-ratio: 2),\n' + \
'only screen and (-o-min-device-pixel-ratio: 2/1),\n' + \
'only screen and (min-device-pixel-ratio: 2),\n' + \
'only screen and (min-resolution: 192dpi),\n' + \
'only screen and (min-resolution: 2dppx)'
parsed = parse(query)
self.assertTrue(isinstance(parsed, list))
self.assertEqual(len(parsed), 6)
self.assertEqual(parsed[0]["expressions"][0]["feature"],
"-webkit-min-device-pixel-ratio")
self.assertEqual(parsed[1]["expressions"][0]["modifier"],
"min")
def test_media_query_invalid(self):
queries = ["some crap", "48em", "screen and crap",
"screen and (48em)", "screen and (foo:)", "()",
"(foo) (bar)", "(foo:) and (bar)"]
for query in queries:
try:
parse(query)
except Exception as e:
msg = "Invalid CSS media query: {}".format(query)
self.assertEqual(str(e), msg)
else:
self.assertTrue(False)
def test_orientation_match(self):
self.assertTrue(match("(orientation: portrait)", {
"orientation": "portrait"
}))
self.assertFalse(match("(orientation: landscape)", {
"orientation": "portrait"
}))
def test_scan_match(self):
self.assertTrue(match("(scan: progressive)", {
"scan": "progressive"
}))
self.assertFalse(match("(scan: progressive)", {
"scan": "interlace"
}))
def test_width_match(self):
self.assertTrue(match("(width: 800px)", {"width": "800"}))
self.assertFalse(match("(width: 800px)", {"width": "810"}))
def test_width_range_match(self):
self.assertTrue(match("(min-width: 48em)", {"width": "80em"}))
self.assertFalse(match("(min-width: 48em)", {"width": "20em"}))
self.assertFalse(match("(min-width: 48em)", {"resolution": "72"}))
def test_different_unit_match(self):
self.assertTrue(match("(min-width: 500px)", {"width": "48em"}))
self.assertTrue(match("(min-width: 500px)", {"width": "48rem"}))
self.assertTrue(match("(max-height: 1000px)", {"height": "20cm"}))
self.assertFalse(match("(max-height: 1000px)", {"height": "850pt"}))
self.assertTrue(match("(max-height: 1000px)", {"height": "60pc"}))
def test_resolution_match(self):
self.assertTrue(match("(resolution: 50dpi)", {"resolution": "50"}))
self.assertTrue(match('(min-resolution: 50dpi)', {"resolution": "72"}))
self.assertFalse(match('(min-resolution: 72dpi)', {"width": "300"}))
self.assertFalse(match('(min-resolution: 72dpi)', {"width": "75dpcm"}))
self.assertTrue(match("(resolution: 192dpi)", {"resolution": "2dppx"}))
def test_aspect_ratio_match(self):
self.assertTrue(match('(min-aspect-ratio: 4/3)',
{'aspect-ratio': '16 / 9'}))
self.assertFalse(match('(max-aspect-ratio: 4/3)',
{'aspect-ratio': '16/9'}))
self.assertFalse(match('(max-aspect-ratio: 72dpi)', {"width": "300"}))
self.assertFalse(match('(min-aspect-ratio: 2560/1440)',
{'aspect-ratio': 4 / 3}))
def test_not_query_match(self):
self.assertFalse(match('not screen and (color)',
{"type": 'screen', "color": "1"}))
self.assertTrue(match('not screen and (color), screen and (min-height: 48em)',
{"type": 'screen', "height": "1000"}))
self.assertTrue(match('not screen and (color), screen and (min-height: 48em)',
{"type": 'screen', "height": "1000"}))
def test_type_match(self):
self.assertTrue(match('screen', {'type': 'screen'}))
self.assertFalse(match('screen and (color:1)',
{'type': 'tv', 'color': '1'}))
self.assertFalse(match('(min-width: 500px)', {'type': 'screen'}))
def test_not_match(self):
self.assertTrue(match('not screen and (color), screen and (min-height: 48em)',
{'type': 'screen', 'height': '1000'}))
self.assertFalse(match('not all and (min-width: 48em)',
{'type': 'all', 'width': '1000'}))
def test_media_query_combinations(self):
self.assertTrue(match('screen and (min-width: 767px)',
{'type': 'screen', 'width': '980'}))
self.assertTrue(match('screen and (min-width: 767px) and (max-width: 979px)',
{'type': 'screen', 'width': '800'}))
self.assertTrue(match('screen and (color)',
{'type': 'screen', 'color': '1'}))
self.assertTrue(match('screen and (min-width: 767px), screen and (color)',
{'type': 'screen', 'color': '1'}))
self.assertFalse(match('screen and (max-width: 1200px), handheld and (monochrome)',
{'type': 'screen', 'monochrome': '0'}))
if __name__ == '__main__':
unittest.main()