-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_op_gstate.py
127 lines (121 loc) · 5.5 KB
/
test_op_gstate.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
"""Tests of graphics state operators for Stilted."""
import pytest
from error import StiltedError
from evaluate import evaluate
from test_helpers import compare_stacks
@pytest.mark.parametrize(
"text, stack",
[
# currentgray / setgray
("currentgray", [0.0]),
(".75 setgray currentgray", [.75]),
(".75 setgray gsave .5 setgray currentgray grestore currentgray", [.5, .75]),
("-.1 setgray currentgray", [0.0]),
("1.1 setgray currentgray", [1.0]),
(".25 setgray currentrgbcolor", [.25, .25, .25]),
(".25 setgray currentcmykcolor", [0.0, 0.0, 0.0, .75]),
# currentcmykcolor / setcmykcolor
("0 0 0 .25 setcmykcolor currentcmykcolor", [0.0, 0.0, 0.0, .25]),
("0 0 0 .25 setcmykcolor currentgray", [.75]),
("0 0 0 1 setcmykcolor currentgray", [0.0]),
("0 setgray currentcmykcolor", [0.0, 0.0, 0.0, 1.0]),
("1 0 0 0 setcmykcolor currentrgbcolor", [0.0, 1.0, 1.0]),
("0 .5 .5 0 setcmykcolor currentrgbcolor", [1.0, 0.5, 0.5]),
# currentdash / setdash
("currentdash", "[] 0.0"),
("[1 2 3] 11.5 setdash currentdash", "[1 2 3] 11.5"),
# currentflat / setflat
("currentflat", [0.1]),
("2.25 setflat currentflat", [2.25]),
# currenthsbcolor / sethsbcolor
(".1 .2 .3 setrgbcolor currenthsbcolor", [.5833333333333, .666666666, .3]),
(".1 .2 .3 sethsbcolor currenthsbcolor", [.1, .2, .3]),
(".1 .2 .3 sethsbcolor currentrgbcolor", [.3, .276, .24]),
# currentlinecap / setlinecap
("currentlinecap", [0]),
("1 setlinecap currentlinecap", [1]),
("1 setlinecap gsave 2 setlinecap currentlinecap grestore currentlinecap", [2, 1]),
# currentlinejoin / setlinejoin
("currentlinejoin", [0]),
("1 setlinejoin currentlinejoin", [1]),
("1 setlinejoin gsave 2 setlinejoin currentlinejoin grestore currentlinejoin", [2, 1]),
# currentlinewidth / setlinewidth
("currentlinewidth", [1.0]),
("3.5 setlinewidth currentlinewidth", [3.5]),
# currentmiterlimit / setmiterlimit
("currentmiterlimit", [10.0]),
("17.5 setmiterlimit currentmiterlimit", [17.5]),
("17.5 setmiterlimit gsave 35.25 setmiterlimit currentmiterlimit grestore currentmiterlimit", [35.25, 17.5]),
# currentrgbcolor / setrgbcolor
("currentrgbcolor", [0.0, 0.0, 0.0]),
(".1 .2 .3 setrgbcolor currentrgbcolor", [.1, .2, .3]),
(".1 .2 .3 setrgbcolor currentgray", [.181]),
(".1 .2 .3 setrgbcolor gsave .5 setgray currentrgbcolor grestore currentrgbcolor", [.5, .5, .5, .1, .2, .3]),
("-.5 .5 .5 setrgbcolor currentrgbcolor", [0., .5, .5]),
("1.5 .5 .5 setrgbcolor currentrgbcolor", [1., .5, .5]),
(".5 -.5 .5 setrgbcolor currentrgbcolor", [.5, 0., .5]),
(".5 1.5 .5 setrgbcolor currentrgbcolor", [.5, 1., .5]),
(".5 .5 -.5 setrgbcolor currentrgbcolor", [.5, .5, 0.]),
(".5 .5 1.5 setrgbcolor currentrgbcolor", [.5, .5, 1.]),
# grestore
("grestore grestore grestore", []),
# grestoreall
("grestoreall grestoreall grestoreall", []),
("101 202 moveto save pop 3 4 moveto gsave 5 6 moveto gsave grestoreall currentpoint", [101.0, 202.0]),
# gsave/grestore
("101 202 moveto gsave 303 404 moveto grestore currentpoint", [101.0, 202.0]),
("1 2 moveto gsave 3 4 moveto save pop 5 6 moveto grestore grestore grestore currentpoint", [3.0, 4.0]),
("1 2 moveto gsave 3 4 moveto save 5 6 moveto gsave 7 8 moveto gsave restore currentpoint", [3.0, 4.0]),
("2.5 setlinewidth gsave 3.5 setlinewidth grestore currentlinewidth", [2.5]),
("[1 2 3] 3.5 setdash gsave [4 5] 1 setdash grestore currentdash", "[1 2 3] 3.5"),
],
)
def test_evaluate(text, stack):
compare_stacks(evaluate(text).ostack, stack)
@pytest.mark.parametrize(
"text, error",
[
# gsave
("gsave 10 10 moveto grestore currentpoint", "nocurrentpoint"),
# setdash
("setdash", "stackunderflow"),
("1 setdash", "stackunderflow"),
("1 1 setdash", "typecheck"),
("[1] (a) setdash", "typecheck"),
("[1 (a)] 1 setdash", "typecheck"),
# setgray
("setgray", "stackunderflow"),
("(a) setgray", "typecheck"),
# sethsbcolor
("sethsbcolor", "stackunderflow"),
(".5 sethsbcolor", "stackunderflow"),
(".5 .5 sethsbcolor", "stackunderflow"),
("(a) .5 .5 sethsbcolor", "typecheck"),
(".5 (a) .5 sethsbcolor", "typecheck"),
(".5 .5 (a) sethsbcolor", "typecheck"),
# setlinecap
("setlinecap", "stackunderflow"),
("(a) setlinecap", "typecheck"),
("10 setlinecap", "rangecheck"),
# setlinejoin
("setlinejoin", "stackunderflow"),
("(a) setlinejoin", "typecheck"),
("10 setlinejoin", "rangecheck"),
# setlinewidth
("setlinewidth", "stackunderflow"),
("(a) setlinewidth", "typecheck"),
# setmiterlimit
("setmiterlimit", "stackunderflow"),
("(a) setmiterlimit", "typecheck"),
# setrgbcolor
("setrgbcolor", "stackunderflow"),
(".5 setrgbcolor", "stackunderflow"),
(".5 .5 setrgbcolor", "stackunderflow"),
("(a) .5 .5 setrgbcolor", "typecheck"),
(".5 (a) .5 setrgbcolor", "typecheck"),
(".5 .5 (a) setrgbcolor", "typecheck"),
],
)
def test_evaluate_error(text, error):
with pytest.raises(StiltedError, match=error):
evaluate(text)