processing 0.5.29 → 0.5.31

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,9 +6,7 @@ class TestGraphicsContext < Test::Unit::TestCase
6
6
  P = Processing
7
7
  G = P::Graphics
8
8
 
9
- def graphics(w = 10, h = 10)
10
- G.new w, h
11
- end
9
+ THRESHOLD_TO_BE_FIXED = 0.0
12
10
 
13
11
  def test_color()
14
12
  g = graphics
@@ -57,8 +55,29 @@ class TestGraphicsContext < Test::Unit::TestCase
57
55
  assert_raise {g.blendMode LEFT}
58
56
  end
59
57
 
58
+ def test_default_background_color()
59
+ assert_p5_draw '', default_header: '', threshold: THRESHOLD_TO_BE_FIXED
60
+ end
61
+
62
+ def test_default_fill_color()
63
+ assert_p5_draw <<~END, default_header: nil
64
+ background 100
65
+ noStroke
66
+ rect 100, 100, 500, 500
67
+ END
68
+ end
69
+
70
+ def test_default_stroke_color()
71
+ assert_p5_draw <<~END, default_header: nil
72
+ background 100
73
+ noFill
74
+ strokeWeight 50
75
+ line 100, 100, 500, 500
76
+ END
77
+ end
78
+
60
79
  def test_clear()
61
- colors = -> g {get_pixels(g).map(&:to_a).flatten.uniq}
80
+ colors = -> g {get_pixels(g).uniq}
62
81
 
63
82
  g = graphics
64
83
  assert_equal [0], colors[g]
@@ -70,6 +89,385 @@ class TestGraphicsContext < Test::Unit::TestCase
70
89
  assert_equal [0], colors[g]
71
90
  end
72
91
 
92
+ def test_point()
93
+ src = 'strokeWeight 300; point 500, 600'
94
+ assert_p5_fill src
95
+ assert_p5_stroke src, threshold: THRESHOLD_TO_BE_FIXED
96
+ assert_p5_fill_stroke src, threshold: THRESHOLD_TO_BE_FIXED
97
+ end
98
+
99
+ def test_line()
100
+ src = 'strokeWeight 100; line 100, 200, 500, 600'
101
+ assert_p5_fill src
102
+ assert_p5_stroke src
103
+ assert_p5_fill_stroke src
104
+ end
105
+
106
+ def test_rect()
107
+ src = 'rect 100, 200, 300, 400'
108
+ assert_p5_fill src
109
+ assert_p5_stroke src
110
+ assert_p5_fill_stroke src
111
+ end
112
+
113
+ def test_rect_with_rectMode()
114
+ assert_p5_draw 'rectMode CORNER; rect 100, 200, 300, 400'
115
+ assert_p5_draw 'rectMode CORNERS; rect 100, 200, 300, 500'
116
+ assert_p5_draw 'rectMode CENTER; rect 400, 500, 300, 400'
117
+ assert_p5_draw 'rectMode RADIUS; rect 400, 500, 300, 400'
118
+ end
119
+
120
+ def test_ellipse()
121
+ src = 'ellipse 500, 600, 300, 400'
122
+ assert_p5_fill src
123
+ assert_p5_stroke src
124
+ assert_p5_fill_stroke src
125
+ end
126
+
127
+ def test_ellipse_with_ellipseMode()
128
+ assert_p5_draw 'ellipseMode CORNER; ellipse 100, 200, 300, 400'
129
+ assert_p5_draw 'ellipseMode CORNERS; ellipse 100, 200, 300, 500'
130
+ assert_p5_draw 'ellipseMode CENTER; ellipse 400, 500, 300, 400'
131
+ assert_p5_draw 'ellipseMode RADIUS; ellipse 400, 500, 300, 400'
132
+ end
133
+
134
+ def test_circle()
135
+ src = 'circle 500, 600, 300'
136
+ assert_p5_fill src
137
+ assert_p5_stroke src
138
+ assert_p5_fill_stroke src
139
+ end
140
+
141
+ def test_circle_with_ellipseMode()
142
+ assert_p5_draw 'ellipseMode CORNER; circle 100, 200, 300'
143
+ assert_p5_draw 'ellipseMode CORNERS; circle 100, 200, 300'
144
+ assert_p5_draw 'ellipseMode CENTER; circle 400, 500, 300'
145
+ assert_p5_draw 'ellipseMode RADIUS; circle 400, 500, 300'
146
+ end
147
+
148
+ def test_arc()
149
+ src = 'arc 500, 600, 300, 400, PI * 0.25, PI * 0.75'
150
+ assert_p5_fill src, threshold: THRESHOLD_TO_BE_FIXED
151
+ assert_p5_stroke src, threshold: THRESHOLD_TO_BE_FIXED
152
+ assert_p5_fill_stroke src, threshold: THRESHOLD_TO_BE_FIXED
153
+ end
154
+
155
+ def test_arc_with_ellipseMode()
156
+ assert_p5_draw 'ellipseMode CORNER; arc 100, 200, 300, 400, 0, PI * 0.75',
157
+ threshold: THRESHOLD_TO_BE_FIXED
158
+ assert_p5_draw 'ellipseMode CORNERS; arc 100, 200, 300, 500, 0, PI * 0.75',
159
+ threshold: THRESHOLD_TO_BE_FIXED
160
+ assert_p5_draw 'ellipseMode CENTER; arc 400, 500, 300, 400, 0, PI * 0.75',
161
+ threshold: THRESHOLD_TO_BE_FIXED
162
+ assert_p5_draw 'ellipseMode RADIUS; arc 400, 500, 300, 400, 0, PI * 0.75',
163
+ threshold: THRESHOLD_TO_BE_FIXED
164
+ end
165
+
166
+ def test_square()
167
+ src = 'square 500, 600, 300'
168
+ assert_p5_fill src
169
+ assert_p5_stroke src
170
+ assert_p5_fill_stroke src
171
+ end
172
+
173
+ def test_triangle()
174
+ src = 'triangle 100,100, 100,500, 500,200'
175
+ assert_p5_fill src
176
+ assert_p5_stroke src
177
+ assert_p5_fill_stroke src
178
+ end
179
+
180
+ def test_quad()
181
+ src = 'quad 100,100, 100,500, 500,500, 600,100'
182
+ assert_p5_fill src
183
+ assert_p5_stroke src
184
+ assert_p5_fill_stroke src
185
+ end
186
+
187
+ def test_curve()
188
+ src = 'curve 100,100, 100,500, 500,500, 600,100'
189
+ assert_p5_fill src, threshold: 0
190
+ assert_p5_stroke src, threshold: 0
191
+ assert_p5_fill_stroke src, threshold: 0
192
+ end
193
+
194
+ def test_bezier()
195
+ src = 'curve 100,100, 100,500, 500,500, 600,100'
196
+ assert_p5_fill src, threshold: 0
197
+ assert_p5_stroke src, threshold: 0
198
+ assert_p5_fill_stroke src, threshold: 0
199
+ end
200
+
201
+ def test_shape_with_shapeMode_corner()
202
+ header = 'noStroke'
203
+
204
+ assert_equal_draw header, <<~EXPECTED, <<~ACTUAL
205
+ rect 100, 200, 300, 400
206
+ EXPECTED
207
+ shapeMode CORNER
208
+ shape createShape(RECT, 100, 200, 300, 400)
209
+ ACTUAL
210
+
211
+ assert_equal_draw header, <<~EXPECTED, <<~ACTUAL
212
+ rect 100, 200, 300, 400
213
+ EXPECTED
214
+ shapeMode CORNER
215
+ shape createShape(RECT, 0, 0, 300, 400), 100, 200
216
+ ACTUAL
217
+
218
+ assert_equal_draw header, <<~EXPECTED, <<~ACTUAL
219
+ rect 100, 200, 300, 400
220
+ EXPECTED
221
+ shapeMode CORNER
222
+ shape createShape(RECT, 0, 0, 500, 600), 100, 200, 300, 400
223
+ ACTUAL
224
+ end
225
+
226
+ def test_shape_with_shapeMode_corners()
227
+ header = 'noStroke'
228
+
229
+ assert_equal_draw header, <<~EXPECTED, <<~ACTUAL
230
+ rect 100, 200, 300, 400
231
+ EXPECTED
232
+ shapeMode CORNERS
233
+ shape createShape(RECT, 100, 200, 300, 400)
234
+ ACTUAL
235
+
236
+ assert_equal_draw header, <<~EXPECTED, <<~ACTUAL
237
+ rect 100, 200, 200, 200
238
+ EXPECTED
239
+ shapeMode CORNERS
240
+ shape createShape(RECT, 0, 0, 300, 400), 100, 200
241
+ ACTUAL
242
+
243
+ assert_equal_draw header, <<~EXPECTED, <<~ACTUAL
244
+ rect 100, 200, 200, 200
245
+ EXPECTED
246
+ shapeMode CORNERS
247
+ shape createShape(RECT, 0, 0, 500, 600), 100, 200, 300, 400
248
+ ACTUAL
249
+ end
250
+
251
+ def test_shape_with_shapeMode_center()
252
+ header = 'noStroke'
253
+
254
+ assert_equal_draw header, <<~EXPECTED, <<~ACTUAL
255
+ rect 400, 400, 200, 400
256
+ EXPECTED
257
+ shapeMode CENTER
258
+ shape createShape(RECT, 500, 600, 200, 400)
259
+ ACTUAL
260
+
261
+ assert_equal_draw header, <<~EXPECTED, <<~ACTUAL
262
+ rect 400, 400, 200, 400
263
+ EXPECTED
264
+ shapeMode CENTER
265
+ shape createShape(RECT, 0, 0, 200, 400), 500, 600
266
+ ACTUAL
267
+
268
+ assert_equal_draw header, <<~EXPECTED, <<~ACTUAL
269
+ rect 400, 400, 200, 400
270
+ EXPECTED
271
+ shapeMode CENTER
272
+ shape createShape(RECT, 0, 0, 700, 800), 500, 600, 200, 400
273
+ ACTUAL
274
+ end
275
+
276
+ def test_shape_with_groups()
277
+ assert_equal_draw <<~EXPECTED, <<~ACTUAL
278
+ rect 100, 100, 300, 200
279
+ rect 200, 200, 300, 200
280
+ EXPECTED
281
+ group = createShape GROUP
282
+ group.addChild createShape(RECT, 100, 100, 300, 200)
283
+ group.addChild createShape(RECT, 200, 200, 300, 200)
284
+ shape group
285
+ ACTUAL
286
+
287
+ assert_equal_draw <<~EXPECTED, <<~ACTUAL
288
+ rect 100, 100, 300, 200
289
+ rect 200, 200, 300, 200
290
+ EXPECTED
291
+ group1 = createShape GROUP
292
+ group2 = createShape GROUP
293
+ group1.addChild createShape(RECT, 100, 100, 300, 200)
294
+ group1.addChild group2
295
+ group2.addChild createShape(RECT, 200, 200, 300, 200)
296
+ shape group1
297
+ ACTUAL
298
+ end
299
+
300
+ def test_shape_with_non_groups()
301
+ assert_equal_draw '', <<~ACTUAL
302
+ s = createShape
303
+ s.addChild createShape(RECT, 100, 100, 300, 200)
304
+ s.addChild createShape(RECT, 200, 200, 300, 200)
305
+ shape s
306
+ ACTUAL
307
+ end
308
+
309
+ def test_beginShape_points()
310
+ src = <<~END
311
+ strokeWeight 200
312
+ beginShape POINTS
313
+ vertex 200, 300
314
+ vertex 500, 600
315
+ END
316
+ assert_p5_fill src, 'endShape'
317
+ assert_p5_stroke src, 'endShape', threshold: THRESHOLD_TO_BE_FIXED
318
+ assert_p5_fill_stroke src, 'endShape', threshold: THRESHOLD_TO_BE_FIXED
319
+ end
320
+
321
+ def test_beginShape_lines()
322
+ src = <<~END
323
+ strokeWeight 200
324
+ beginShape LINES
325
+ vertex 100, 200
326
+ vertex 300, 400
327
+ vertex 500, 600
328
+ vertex 700, 800
329
+ END
330
+ assert_p5_fill src, 'endShape'
331
+ assert_p5_stroke src, 'endShape'
332
+ assert_p5_fill_stroke src, 'endShape'
333
+ assert_p5_fill src, 'endShape CLOSE'
334
+ assert_p5_stroke src, 'endShape CLOSE'
335
+ assert_p5_fill_stroke src, 'endShape CLOSE'
336
+ end
337
+
338
+ def test_beginShape_triangles()
339
+ src = <<~END
340
+ beginShape TRIANGLES
341
+ vertex 100, 100
342
+ vertex 100, 500
343
+ vertex 400, 200
344
+ vertex 500, 100
345
+ vertex 500, 500
346
+ vertex 900, 200
347
+ END
348
+ assert_p5_fill src, 'endShape'
349
+ assert_p5_stroke src, 'endShape'
350
+ assert_p5_fill_stroke src, 'endShape'
351
+ assert_p5_fill src, 'endShape CLOSE'
352
+ assert_p5_stroke src, 'endShape CLOSE'
353
+ assert_p5_fill_stroke src, 'endShape CLOSE'
354
+ end
355
+
356
+ def test_beginShape_triangle_fan()
357
+ src = <<~END
358
+ beginShape TRIANGLE_FAN
359
+ vertex 100, 100
360
+ vertex 100, 500
361
+ vertex 200, 600
362
+ vertex 300, 500
363
+ vertex 400, 600
364
+ vertex 500, 200
365
+ vertex 400, 100
366
+ END
367
+ assert_p5_fill src, 'endShape'
368
+ assert_p5_stroke src, 'endShape', threshold: 0.98
369
+ assert_p5_fill_stroke src, 'endShape', threshold: 0.98
370
+ assert_p5_fill src, 'endShape CLOSE'
371
+ assert_p5_stroke src, 'endShape CLOSE', threshold: 0.98
372
+ assert_p5_fill_stroke src, 'endShape CLOSE', threshold: 0.98
373
+ end
374
+
375
+ def test_beginShape_triangle_strip()
376
+ src = <<~END
377
+ beginShape TRIANGLE_STRIP
378
+ vertex 100, 100
379
+ vertex 100, 900
380
+ vertex 500, 200
381
+ vertex 500, 800
382
+ vertex 900, 100
383
+ vertex 900, 900
384
+ END
385
+ assert_p5_fill src, 'endShape'
386
+ assert_p5_stroke src, 'endShape'
387
+ assert_p5_fill_stroke src, 'endShape'
388
+ assert_p5_fill src, 'endShape CLOSE', threshold: THRESHOLD_TO_BE_FIXED
389
+ assert_p5_stroke src, 'endShape CLOSE', threshold: THRESHOLD_TO_BE_FIXED
390
+ assert_p5_fill_stroke src, 'endShape CLOSE', threshold: THRESHOLD_TO_BE_FIXED
391
+ end
392
+
393
+ def test_beginShape_quads()
394
+ src = <<~END
395
+ beginShape QUADS
396
+ vertex 100, 100
397
+ vertex 200, 500
398
+ vertex 300, 400
399
+ vertex 400, 200
400
+ vertex 500, 100
401
+ vertex 600, 500
402
+ vertex 700, 400
403
+ vertex 800, 200
404
+ END
405
+ assert_p5_fill src, 'endShape'
406
+ assert_p5_stroke src, 'endShape'
407
+ assert_p5_fill_stroke src, 'endShape'
408
+ assert_p5_fill src, 'endShape CLOSE'
409
+ assert_p5_stroke src, 'endShape CLOSE'
410
+ assert_p5_fill_stroke src, 'endShape CLOSE'
411
+ end
412
+
413
+ def test_beginShape_quad_strip()
414
+ src = <<~END
415
+ beginShape QUAD_STRIP
416
+ vertex 100, 100
417
+ vertex 100, 500
418
+ vertex 500, 200
419
+ vertex 500, 400
420
+ vertex 900, 100
421
+ vertex 900, 500
422
+ END
423
+ assert_p5_fill src, 'endShape'
424
+ assert_p5_stroke src, 'endShape'
425
+ assert_p5_fill_stroke src, 'endShape'
426
+ assert_p5_fill src, 'endShape CLOSE', threshold: THRESHOLD_TO_BE_FIXED
427
+ assert_p5_stroke src, 'endShape CLOSE', threshold: THRESHOLD_TO_BE_FIXED
428
+ assert_p5_fill_stroke src, 'endShape CLOSE', threshold: THRESHOLD_TO_BE_FIXED
429
+ end
430
+
431
+ def test_beginShape_tess()
432
+ src = <<~END
433
+ beginShape TESS
434
+ vertex 100, 100
435
+ vertex 100, 500
436
+ vertex 500, 500
437
+ vertex 500, 400
438
+ vertex 300, 400
439
+ vertex 300, 300
440
+ vertex 500, 300
441
+ vertex 500, 100
442
+ END
443
+ assert_p5_fill src, 'endShape'
444
+ assert_p5_stroke src, 'endShape'
445
+ assert_p5_fill_stroke src, 'endShape'
446
+ assert_p5_fill src, 'endShape CLOSE'
447
+ assert_p5_stroke src, 'endShape CLOSE'
448
+ assert_p5_fill_stroke src, 'endShape CLOSE'
449
+ end
450
+
451
+ def test_beginShape_polygon()
452
+ src = <<~END
453
+ beginShape
454
+ vertex 100, 100
455
+ vertex 100, 500
456
+ vertex 500, 500
457
+ vertex 500, 400
458
+ vertex 300, 400
459
+ vertex 300, 300
460
+ vertex 500, 300
461
+ vertex 500, 100
462
+ END
463
+ assert_p5_fill src, 'endShape'
464
+ assert_p5_stroke src, 'endShape'
465
+ assert_p5_fill_stroke src, 'endShape'
466
+ assert_p5_fill src, 'endShape CLOSE'
467
+ assert_p5_stroke src, 'endShape CLOSE'
468
+ assert_p5_fill_stroke src, 'endShape CLOSE'
469
+ end
470
+
73
471
  def test_lerp()
74
472
  g = graphics
75
473
 
@@ -93,4 +491,52 @@ class TestGraphicsContext < Test::Unit::TestCase
93
491
  assert_equal c[ 70, 80, 90], g.lerpColor(c[10, 20, 30], c[50, 60, 70], 1.5)
94
492
  end
95
493
 
96
- end# TestGraphics
494
+ def test_createShape_line()
495
+ assert_equal_draw <<~EXPECTED, <<~ACTUAL
496
+ line 100, 200, 800, 900
497
+ EXPECTED
498
+ shape createShape(LINE, 100, 200, 800, 900)
499
+ ACTUAL
500
+ end
501
+
502
+ def test_createShape_rect()
503
+ assert_equal_draw <<~EXPECTED, <<~ACTUAL
504
+ rect 100, 200, 500, 600
505
+ EXPECTED
506
+ shape createShape(RECT, 100, 200, 500, 600)
507
+ ACTUAL
508
+ end
509
+
510
+ def test_createShape_ellipse()
511
+ assert_equal_draw <<~EXPECTED, <<~ACTUAL
512
+ ellipse 500, 600, 300, 400
513
+ EXPECTED
514
+ shape createShape(ELLIPSE, 500, 600, 300, 400)
515
+ ACTUAL
516
+ end
517
+
518
+ def test_createShape_arc()
519
+ assert_equal_draw <<~EXPECTED, <<~ACTUAL
520
+ arc 500, 600, 300, 400, 0, PI / 2
521
+ EXPECTED
522
+ shape createShape(ARC, 500, 600, 300, 400, 0, PI / 2)
523
+ ACTUAL
524
+ end
525
+
526
+ def test_createShape_triangle()
527
+ assert_equal_draw <<~EXPECTED, <<~ACTUAL
528
+ triangle 100,100, 100,500, 400,200
529
+ EXPECTED
530
+ shape createShape(TRIANGLE, 100,100, 100,500, 400,200)
531
+ ACTUAL
532
+ end
533
+
534
+ def test_createShape_quad()
535
+ assert_equal_draw <<~EXPECTED, <<~ACTUAL
536
+ quad 100,100, 100,500, 500,500, 600,100
537
+ EXPECTED
538
+ shape createShape(QUAD, 100,100, 100,500, 500,500, 600,100)
539
+ ACTUAL
540
+ end
541
+
542
+ end# TestGraphicsContext