Skip to content

Commit e41ea55

Browse files
authored
Merge pull request #379 from camerenisonfire/master
Convert TDD Learn page to use ES6. resolves #315
2 parents 06f2670 + 43d9a3a commit e41ea55

File tree

14 files changed

+249
-211
lines changed

14 files changed

+249
-211
lines changed

dist/es/learn/coordinate-system-and-shapes.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ <h2>Formas Primitivas</h2>
191191
}
192192
</script>
193193

194-
<p>Una vez que nos hemos familiarizado con el concepto de dibujar un rectángulo, una <a href="/reference/#/p5/ellipse">ellipse()</a> es muy sencilla de dibujar. De hecho es idéntica al <a href="/reference/#/p5/rect">rect()</a>> con la diferencia de que la elipse se dibuja donde la caja que contiene al rectángulo debiera estar. El modo por defecto para la <a href="/reference/#/p5/ellipse">ellipse()</a> es <a href="/reference/#/p5/CENTER">CENTER</a>, en vez de <a href="/reference/#/p5/CORNER">CORNER</a>.</p>
194+
<p>Una vez que nos hemos familiarizado con el concepto de dibujar un rectángulo, una <a href="/reference/#/p5/ellipse">ellipse()</a> es muy sencilla de dibujar. De hecho es idéntica al <a href="/reference/#/p5/rect">rect()</a> con la diferencia de que la elipse se dibuja donde la caja que contiene al rectángulo debiera estar. El modo por defecto para la <a href="/reference/#/p5/ellipse">ellipse()</a> es <a href="/reference/#/p5/CENTER">CENTER</a>, en vez de <a href="/reference/#/p5/CORNER">CORNER</a>.</p>
195195
<script type="text/p5" data-autoplay>
196196
function setup(){
197197
createCanvas(100, 100);

dist/es/learn/tdd.html

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -212,9 +212,9 @@ <h2>A sketch without tests</h2>
212212
// fillColor will be the color of the rectangle.
213213
// colorIncreaser will become an instance of our ColorIncreaser class.
214214

215-
var colorValueIncrease = 1;
216-
var fillColor
217-
var colorIncreaser
215+
let colorValueIncrease = 1;
216+
let fillColor;
217+
let colorIncreaser;
218218

219219
function setup() {
220220
createCanvas(500, 500);
@@ -249,7 +249,7 @@ <h2>A sketch without tests</h2>
249249
</xmp></code></pre>
250250
</li>
251251
<li>
252-
Open the index.html file in your browser (you don’t need to run a local server in this case). You should see a black box become red and then black again. This is the basis of looping over all the RGB colors. The problem you face is that there are 256 values for red, green, and blue, which means there are over 16 million combinations of colors! You can make this faster. However, making this faster will cause flashing lights which might give some people headaches or seizures. Please only increase this value if you do not have issues with flashing lights. The rest of the tutorial should be fine. In the sketch.js file change the<pre><code class="language-javascript"><xmp>var colorValueIncrease = 33;</xmp></code></pre> and reload the page. However this still doesn’t guarantee this is working properly. You don’t have time to watch all the colors change nor can you tell yourself if it is iterating through the millions of colors. Change the colorValueIncrease variable back to 1 before you move on.
252+
Open the index.html file in your browser (you don’t need to run a local server in this case). You should see a black box become red and then black again. This is the basis of looping over all the RGB colors. The problem you face is that there are 256 values for red, green, and blue, which means there are over 16 million combinations of colors! You can make this faster. However, making this faster will cause flashing lights which might give some people headaches or seizures. Please only increase this value if you do not have issues with flashing lights. The rest of the tutorial should be fine. In the sketch.js file change the<pre><code class="language-javascript"><xmp>let colorValueIncrease = 33;</xmp></code></pre> and reload the page. However this still doesn’t guarantee this is working properly. You don’t have time to watch all the colors change nor can you tell yourself if it is iterating through the millions of colors. Change the colorValueIncrease variable back to 1 before you move on.
253253
</li>
254254
<li>
255255
At 30 draw loops per second it would take over six days to loop through all of the colors. You certainly don’t have time to do that once, and would never be able to check and see if it worked every time you updated our code. What if you accidentally change something in our draw loop that breaks the code right before install? You wouldn’t know until it was live and everyone saw your sketch break.
@@ -264,13 +264,14 @@ <h2>Your first unit tests</h2>
264264
<p>
265265
In the folder called color_unit_test that you already created, make a new folder called test. Open the test folder and make a file called test.js. Type this code into test.js
266266
<pre><code class="language-javascript"><xmp>
267+
'use strict';
267268
// Import the expect library. This is what allows us to check our code.
268269
// You can check out the full documentation at http://chaijs.com/api/bdd/
269-
var expect = require('chai').expect;
270+
const expect = require('chai').expect;
270271

271272

272273
// Create the variable you are going to test
273-
var p5js = 'awesome';
274+
let p5js = 'awesome';
274275

275276

276277
// describe lets you comment on what this block of code is for.
@@ -309,11 +310,11 @@ <h2>Your first unit tests</h2>
309310
</p>
310311
<p>
311312
Let's look at what happens when tests fail. Change line 6 in test.js from:
312-
<pre><code class="language-javascript"><xmp>var p5js = 'awesome';</xmp></code></pre>
313+
<pre><code class="language-javascript"><xmp>let p5js = 'awesome';</xmp></code></pre>
313314
</p>
314315
<p>
315316
to
316-
<pre><code class="language-javascript"><xmp>var p5js = 42;</xmp></code></pre>
317+
<pre><code class="language-javascript"><xmp>let p5js = 42;</xmp></code></pre>
317318
</p>
318319
<p>
319320
Save test.js and go back to your terminal and run the mocha command again. You should see something like:
@@ -358,13 +359,15 @@ <h2>Test Driven Development</h2>
358359
<p>
359360
Delete everything in test.js and replace it with this
360361
<pre><code class="language-javascript">
362+
'use strict';
361363
// Import the expect library. This is what allows us to check our code.
362364
// You can check out the full documentation at http://chaijs.com/api/bdd/
363-
var expect = require('chai').expect;
365+
const expect = require('chai').expect;
364366
// Import our colorIncreaser class.
365-
var ColorIncreaser = require('../sketch').ColorIncreaser;
367+
const ColorIncreaser = require('../sketch');
366368

367369
describe('ColorIncreaser tests', function() {
370+
let colorIncreaser;
368371

369372
// beforeEach is a special function that is similar to the setup function in
370373
// p5.js. The major difference it that this function runs before each it()
@@ -386,18 +389,20 @@ <h2>Test Driven Development</h2>
386389

387390
Go to the bottom of your sketch.js file, below the closing bracket } for the draw function, and add this:
388391
<pre><code class="language-javascript">
389-
function ColorIncreaser() {
390-
// Stores a value and a color and allows you to increase the color
391-
// by that value.
392+
class ColorIncreaser {
393+
constructor() {
394+
// Stores a value and a color and allows you to increase the color
395+
// by that value.
396+
}
392397
}
393398

394-
module.exports.ColorIncreaser = ColorIncreaser;
399+
module.exports = ColorIncreaser;
395400
</code></pre>
396401
</p>
397402
<p>
398-
Save sketch.js. The function line will be our color increasing object. The module.export line is what allows our test.js file to import our ColorIncreaser with the line you already added that looks like this:
403+
Save sketch.js. The function line will be our color increasing object. The module.exports line is what allows our test.js file to import our ColorIncreaser with the line you already added that looks like this:
399404
<pre><code class="language-javascript">
400-
var ColorIncreaser = require('../sketch').ColorIncreaser;
405+
const ColorIncreaser = require('../sketch');
401406
</code></pre>
402407
</p>
403408
<p>
@@ -406,7 +411,7 @@ <h2>Test Driven Development</h2>
406411
Now get your ColorIncreaser function to actually do something. Start by storing the colorValueIncrease as a variable in your class. Before you change the code in sketch.js you have to write your tests. Change the beforeEach function in test.js to look like this
407412
<pre><code class="language-javascript">
408413
beforeEach(function() {
409-
var colorValueIncrease = 1;
414+
let colorValueIncrease = 1;
410415
colorIncreaser = new ColorIncreaser(colorValueIncrease);
411416
});
412417
</code></pre>
@@ -429,10 +434,12 @@ <h2>Test Driven Development</h2>
429434
</code></pre>
430435
and change the ColorIncreaser function to look like this
431436
<pre><code class="language-javascript">
432-
function ColorIncreaser(colorValueIncrease) {
433-
// Stores a value and a color and allows you to increase the color
434-
// by that value.
435-
this.colorValueIncrease = colorValueIncrease
437+
class ColorIncreaser() {
438+
constructor(colorValueIncrease) {
439+
// Stores a value and a color and allows you to increase the color
440+
// by that value.
441+
this.colorValueIncrease = colorValueIncrease
442+
}
436443
}
437444
</code></pre>
438445
Let’s run the mocha test again. It should pass!
@@ -443,7 +450,7 @@ <h2>Testing when your object is composed of other objects</h2>
443450
Now you need to add an instance of the p5 color class to our ColorIncreaser. However, for your tests, you don’t want to use an actual instance of the color() class because you don’t want to test external dependencies given by another library.You just want to make sure that your increment function works. So you are going to create what is called a mock of the p5 color class so you can test without worrying about the implementation of code you didn’t write.
444451
</p>
445452
<p>
446-
The original way of incrementing colors just used the color.levels and changed the red, green and blue values at index [0], [1], and [2]. You can see this in the draw function in sketch.js. The implementation of color just stores those values in a javascript array so you can mock it out very easily. Put this code in test.js below the var ColorIncreaser = require… line and above the describe line
453+
The original way of incrementing colors just used the color.levels and changed the red, green and blue values at index [0], [1], and [2]. You can see this in the draw function in sketch.js. The implementation of color just stores those values in a javascript array so you can mock it out very easily. Put this code in test.js below the const ColorIncreaser = require… line and above the describe line
447454
<pre><code class="language-javascript">
448455
function mockColor(red, green, blue, alpha) {
449456
// Mock of the color class from p5
@@ -459,8 +466,8 @@ <h2>Testing when your object is composed of other objects</h2>
459466
Update your beforeEach() function
460467
<pre><code class="language-javascript">
461468
beforeEach(function() {
462-
var colorValueIncrease = 1;
463-
var fillColor = new mockColor(0, 0, 0, 255);
469+
let colorValueIncrease = 1;
470+
let fillColor = new mockColor(0, 0, 0, 255);
464471
colorIncreaser = new ColorIncreaser(colorValueIncrease, fillColor);
465472
});
466473
</code></pre>
@@ -488,11 +495,13 @@ <h2>Testing when your object is composed of other objects</h2>
488495
<p>
489496
and update the ColorValueIncreaser function
490497
<pre><code class="language-javascript">
491-
function ColorIncreaser(colorValueIncrease, fillColor) {
492-
// Stores a value and a color and allows you to increase the color
493-
// by that value.
494-
this.colorValueIncrease = colorValueIncrease
495-
this.fillColor = fillColor;
498+
class ColorIncreaser {
499+
constructor(colorValueIncrease, fillColor) {
500+
// Stores a value and a color and allows you to increase the color
501+
// by that value.
502+
this.colorValueIncrease = colorValueIncrease
503+
this.fillColor = fillColor;
504+
}
496505
}
497506
</code></pre>
498507
</p>
@@ -512,7 +521,7 @@ <h2>Testing class methods</h2>
512521
<pre><code class="language-javascript">
513522
it('should have rgb values 255, 0, 0 after calling increaseFillColor 255 times', function(done) {
514523
//it is 256^1 - 1 because it starts with the color black
515-
for (var count = 0; count < 255; count += 1) {
524+
for (let count = 0; count < 255; count += 1) {
516525
colorIncreaser.increaseFillColor()
517526
}
518527
expect(colorIncreaser.fillColor.levels[0]).to.equal(255)
@@ -524,7 +533,7 @@ <h2>Testing class methods</h2>
524533

525534
it('should have rgb values 255, 255, 0 after calling increaseFillColor 65535 times', function(done) {
526535
//it is 256^2 - 1 because it starts with the color black
527-
for (var count = 0; count < 65535; count += 1) {
536+
for (let count = 0; count < 65535; count += 1) {
528537
colorIncreaser.increaseFillColor()
529538
}
530539
expect(colorIncreaser.fillColor.levels[0]).to.equal(255)
@@ -536,7 +545,7 @@ <h2>Testing class methods</h2>
536545

537546
it('should have rgb values 255, 255, 255 after calling increaseFillColor 16777215 times', function(done) {
538547
//it is 256^3 - 1 because it starts with the color black
539-
for (var count = 0; count < 16777215; count += 1) {
548+
for (let count = 0; count < 16777215; count += 1) {
540549
colorIncreaser.increaseFillColor()
541550
}
542551
expect(colorIncreaser.fillColor.levels[0]).to.equal(255)
@@ -547,9 +556,9 @@ <h2>Testing class methods</h2>
547556
</code></pre>
548557
</p>
549558
<p>
550-
And when you run mocha the tests will fail because that function does not exist! Let’s add a skeleton function in sketch.js between your ColorIncreaserFunction and your module export
559+
And when you run mocha the tests will fail because that function does not exist! Let’s add a skeleton function in sketch.js inside the ColorIncreaser class below the constructor function.
551560
<pre><code class="language-javascript">
552-
ColorIncreaser.prototype.increaseFillColor = function() {
561+
increaseFillColor() {
553562
// increase the first color channel by one. If that channel
554563
// is now > 255 then increment the next color channel. Repeat for second
555564
// and third channel
@@ -559,7 +568,7 @@ <h2>Testing class methods</h2>
559568
<p>
560569
Save sketch.js. Now when you run mocha the tests fail for a different reason. It finds the function, but it doesn’t do what you expect it to. Let’s change the function in sketch.js to look like what is inside the draw function.
561570
<pre><code class="language-javascript">
562-
ColorIncreaser.prototype.increaseFillColor = function() {
571+
increaseFillColor() {
563572
// increase the first color channel by one. If that channel
564573
// is now > 255 then increment the next color channel. Repeat for second
565574
// and third channel

dist/learn/coordinate-system-and-shapes.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ <h2>Simple Shapes</h2>
191191
}
192192
</script>
193193

194-
<p>Once we have become comfortable with the concept of drawing a rectangle, an <a href="/reference/#/p5/ellipse">ellipse()</a> is a snap. In fact, it is identical to <a href="/reference/#/p5/rect">rect()</a>> with the difference being that an ellipse is drawn where the bounding box of the rectangle would be. The default mode for <a href="/reference/#/p5/ellipse">ellipse()</a> is <a href="/reference/#/p5/CENTER">CENTER</a>, rather than <a href="/reference/#/p5/CORNER">CORNER</a>.</p>
194+
<p>Once we have become comfortable with the concept of drawing a rectangle, an <a href="/reference/#/p5/ellipse">ellipse()</a> is a snap. In fact, it is identical to <a href="/reference/#/p5/rect">rect()</a> with the difference being that an ellipse is drawn where the bounding box of the rectangle would be. The default mode for <a href="/reference/#/p5/ellipse">ellipse()</a> is <a href="/reference/#/p5/CENTER">CENTER</a>, rather than <a href="/reference/#/p5/CORNER">CORNER</a>.</p>
195195
<script type="text/p5" data-autoplay>
196196
function setup(){
197197
createCanvas(100, 100);

0 commit comments

Comments
 (0)