Skip to content

Commit 8088094

Browse files
authored
Merge pull request #24 from bldl/testing-section
Testing section
2 parents 9055302 + 5722f8a commit 8088094

File tree

2 files changed

+96
-44
lines changed

2 files changed

+96
-44
lines changed

upsert-tutorial/index.html

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,59 +1478,69 @@ <h2>Testing with Test262</h2>
14781478
<div class="content-body">
14791479
<h3 id="writing-tests-for-test262">Writing tests for <a href="https://github.com/tc39/test262" target="_blank">Test262</a></h3>
14801480
<p> When it comes to testing implementations, there are some guidelines to follow.
1481-
The official guidelines state that an acceptable test in Test262 is the following:</p>
1482-
<pre><code>Any test that exercises observable grammar or semantics, originating with citable,
1483-
ormative text in the latest draft of the ECMAScript® Language Specification,
1484-
the ECMAScript® Internationalization API Specification, the The JSON Data Interchange Syntax,
1485-
a Stage 3 proposal or a Pull Request which makes a normative change to any of those specifications.
1486-
</code></pre>
1487-
<p> The key point for this, is that we can write tests for any observable grammar or semantic from our specification.</p>
1488-
<p> First we need to identify the so-called testable lines in our specification.
1489-
One way to think about it, is when the behaviour of the specification is observable to the user, it is testable.</p>
1490-
<p> An example of easily testable line in our specification is:
1491-
<code>2. Perform ? RequireInternalSlot(M, [[MapData]])</code></p>
1492-
<p> Recall that this line, among other things, checks if <code>this</code> is an Object, therefore we can test it by trying it on non-objects.
1493-
Primitive types in JavaScript™ are not considered objects.
1494-
So an example of the tests you can write for this, could look like this:</p>
1481+
The <a href="https://github.com/tc39/test262/blob/main/CONTRIBUTING.md#acceptable-tests">official guidelines</a> state that an acceptable test in Test262 is the following:</p>
1482+
<blockquote>
1483+
<p><em>Any test that exercises observable grammar or semantics, originating with citable, normative text in the latest draft of the ECMAScript® Language Specification, the ECMAScript® Internationalization API Specification, the The JSON Data Interchange Syntax, a Stage 3 proposal or a Pull Request which makes a normative change to any of those specifications.</em></p>
1484+
</blockquote>
1485+
<p> The key point for this is that we should write tests for any observable grammar or semantic from our specification.</p>
1486+
<p> First, we need to identify the so-called <em>testable lines</em> in our specification.
1487+
One way to think about it is this: whenever the behaviour of the specification is observable to the user, then it is testable.</p>
1488+
<p> An example of an easily testable line in our specification is:</p>
1489+
<pre><code class="language-lua">2. Perform ? RequireInternalSlot(M, [[MapData]]).
1490+
</code></pre>
1491+
<p> Recall that this line, among other things, checks whether <code>this</code> is an <code>Object</code>. Therefore, we can test it by trying it on non-objects, for example, on <a href="https://262.ecma-international.org/#sec-primitive-value">values of primitive types</a>.
1492+
Here is an example of a test where we assert that calling the <code>upsert</code> method on <code>false</code> with arguments <code>1</code> and <code>1</code> will throw <code>TypeError</code> exception:</p>
14951493
<pre><code class="language-js">var m = new Map();
14961494

14971495
assert.throws(TypeError, function () {
14981496
m.upsert.call(false, 1, 1);
14991497
});
15001498
</code></pre>
1501-
<p> The <code>assert</code> is part of the Test262 suite, here we assert that a TypeError is thrown.</p>
1502-
<p> You can find the rest of the functions for assert <a href="https://github.com/tc39/test262/blob/main/CONTRIBUTING.md#test-environment" target="_blank">here</a>.</p>
1503-
<h3 id="more-than-just-testing">More than just testing</h3>
1504-
<p> Additional to the tests, there is a strict guide for documentation for the test.</p>
1505-
<p> You start the test by declaring the copyright, here you just fill in the year and your name:</p>
1499+
<p> The <code>assert</code> method is a part of the Test262 suite. You can find the rest of the functions for assert <a href="https://github.com/tc39/test262/blob/main/CONTRIBUTING.md#test-environment" target="_blank">here</a>.</p>
1500+
<h3 id="more-than-just-testing">More Than Just Testing</h3>
1501+
<p> Test262 test should be documented in a <a href="https://github.com/tc39/test262/blob/main/CONTRIBUTING.md#test-case-style">certain manner</a>. </p>
1502+
<p> We start with stating the copyright:</p>
15061503
<pre><code>// Copyright (C) *Year *Name. All rights reserved.
15071504
// This code is governed by the BSD license found in the LICENSE file.
15081505
</code></pre>
1509-
<p> The rest of the information is enclosed in a YAML string and has specified values to simplify parsing.
1510-
All the info is inside the YAML tags <code>/*---</code> and <code>---*/</code>.</p>
1511-
<p> We start with the required key <code>esid</code>, which is the ECMAScript® identifier.
1512-
This doesn&#39;t apply to us yet, as the proposal hasn&#39;t gotten one, therefore we will use <code>pending</code>.</p>
1506+
<p> The rest of the test meta-information is enclosed in a YAML string and mentions specific fields (&quot;keys), some of which we explain in the table below.
1507+
The complete list of possible keys can be found <a href="https://github.com/tc39/test262/blob/main/CONTRIBUTING.md#frontmatter" target="_blank">here</a>.</p>
1508+
<table class="styled-table">
1509+
<thead>
1510+
<tr>
1511+
<th>field</th>
1512+
<th>explanation</th>
1513+
</tr>
1514+
</thead>
1515+
<tbody><tr>
1516+
<td><code>esid</code></td>
1517+
<td>reference to specification section, e.g. <code>sec-well-known-symbols</code></td>
1518+
</tr>
1519+
<tr>
1520+
<td><code>description</code></td>
1521+
<td>short one-line description stating the purpose of the test, e.g., &quot;Throws a TypeError if <code>this</code> is not an object&quot;</td>
1522+
</tr>
1523+
<tr>
1524+
<td><code>info</code></td>
1525+
<td>verbose description of the test mentioning, e.g., what does the method look like, which line of code are we testing, etc.</td>
1526+
</tr>
1527+
</tbody></table>
1528+
<p> For our <code>upsert</code> specification, the required key <code>esid</code> is not applicable yet, as (at the time of writing) the proposal hasn&#39;t gotten one. Therefore, we will use <code>pending</code>.</p>
15131529
<pre><code>esid: pending
15141530
</code></pre>
1515-
<p> Next comes the description which is the other required key.
1516-
The description should be short and on one line regarding the purpose of this testcase.
1517-
In our case, it will look something like: </p>
1531+
<p> Next comes the <code>description</code> key. In our case, it will look like this: </p>
15181532
<pre><code>description: &gt;
15191533
Throws a TypeError if &#39;this&#39; is not an object
15201534
</code></pre>
1521-
<p> Although not required, we should fill out the <code>info</code> key as well.
1522-
Some points that are beneficial here are:
1523-
- What does the method look like?
1524-
- Which line of code are we testing?</p>
1535+
<p> Although not required, we fill out the <code>info</code> key as well:</p>
15251536
<pre><code>info: |
15261537
Map.upsert( key , value )
15271538

1528-
1. Let M be the this value
1529-
2. Perform ? RequireInternalSlot(M, [[MapData]])
1539+
1. Let M be the this value.
1540+
2. Perform ? RequireInternalSlot(M, [[MapData]]).
15301541
...
15311542
</code></pre>
1532-
<p> There are many other keys we can look at, but if you want to learn more about them, check out this <a href="https://github.com/tc39/test262/blob/main/CONTRIBUTING.md#frontmatter" target="_blank">link.</a></p>
1533-
<p> Our full test should now look something like this:</p>
1543+
<p> Our full test should now look as follows:</p>
15341544
<pre><code class="language-js">// Copyright (C) 2024 Sune Eriksson Lianes. All rights reserved.
15351545
// This code is governed by the BSD license found in the LICENSE file.
15361546
/*---
@@ -1550,8 +1560,8 @@ <h3 id="more-than-just-testing">More than just testing</h3>
15501560
m.upsert.call(false, 1, 1);
15511561
});
15521562
</code></pre>
1553-
<h3 id="fill-in-test-cases">Fill in test cases</h3>
1554-
<p> We can now fill in with other test cases (non-objects):</p>
1563+
<h3 id="filling-in-test-cases">Filling in Test Cases</h3>
1564+
<p> We can now fill in other test cases related to calling <code>upsert</code> on non-objects:</p>
15551565
<pre><code class="language-js">// Copyright (C) 2024 Sune Eriksson Lianes. All rights reserved.
15561566
// This code is governed by the BSD license found in the LICENSE file.
15571567
/*---
@@ -1592,12 +1602,14 @@ <h3 id="fill-in-test-cases">Fill in test cases</h3>
15921602
m.upsert.call(Symbol(), 1, 1);
15931603
});
15941604
</code></pre>
1595-
<p> You can take a look at other tests written in the test262 folder or try to write some tests yourself.</p>
1596-
<h3 id="running-tests-in-spidermonkey">Running tests in SpiderMonkey</h3>
1597-
<p> To add the test you simply create the file in <code>mozilla-unified/js/src/tests/test262/built-ins/Map/</code>.
1598-
Preferably creating a folder for the proposal as well.</p>
1599-
<p> When this is done, you can run the tests with <code>./mach jstests built-ins/Map</code>, or be even more specific if you have created a folder.</p>
1600-
<p> You will then see something like this, depending on how many tests are run:</p>
1605+
<p> You can take a look at other tests in the <a href="/test262"><code>test262</code></a> folder or try to write some tests yourself.</p>
1606+
<h3 id="running-tests-in-spidermonkey">Running Tests in SpiderMonkey</h3>
1607+
<p> To add a test, we create a file with the test in <code>mozilla-unified/js/src/tests/test262/built-ins/Map/</code>.
1608+
It makes sense to create a folder for all the tests related to the proposal.</p>
1609+
<p> We can run the tests by executing</p>
1610+
<pre><code class="language-sh">./mach jstests built-ins/Map
1611+
</code></pre>
1612+
<p> Depending on how many tests are run, the output of this command will be similar to the following:</p>
16011613
<pre><code class="language-sh">[1|0|0|0] 12% =====&gt;
16021614
[2|0|0|0] 25% ============&gt;
16031615
[3|0|0|0] 37% ===================&gt;
@@ -1609,7 +1621,7 @@ <h3 id="running-tests-in-spidermonkey">Running tests in SpiderMonkey</h3>
16091621
[8|0|0|0] 100% ======================================================&gt;|
16101622
0.4s
16111623
</code></pre>
1612-
<p> A general tip for testing is looking at how similar lines are tested in other implementations.</p>
1624+
<p> As with the implementation itself, a general tip when writing Test262 tests is to look at how similar lines in other specifications are tested.</p>
16131625

16141626
</div>
16151627
</section>

upsert-tutorial/style.css

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ pre {
122122
overflow-x: auto; /* Horizontal scroll for long lines */
123123
font-family: Consolas, "Courier New", monospace; /* Monospace font */
124124
font-size: 0.9em; /* Slightly smaller font size for a code look */
125+
margin: 10px 10px 15px;
125126
}
126127

127128
/* Tab Container Styles */
@@ -196,4 +197,43 @@ pre {
196197
margin: 0 auto 1rem;
197198
width: 90%;
198199
max-width: 50rem;
199-
}
200+
}
201+
202+
/* Table styling */
203+
.styled-table {
204+
width: 100%;
205+
border-collapse: collapse;
206+
margin: 25px 0;
207+
font-size: 18px;
208+
text-align: left;
209+
box-shadow: 0 0 20px rgba(0, 0, 0, 0.15);
210+
}
211+
212+
.styled-table thead tr {
213+
background-color: #009879;
214+
color: #ffffff;
215+
text-align: left;
216+
font-weight: bold;
217+
}
218+
219+
.styled-table th, .styled-table td {
220+
padding: 12px 15px;
221+
border: 1px solid #dddddd;
222+
}
223+
224+
.styled-table tbody tr {
225+
border-bottom: 1px solid #dddddd;
226+
}
227+
228+
.styled-table tbody tr:nth-of-type(even) {
229+
background-color: #f3f3f3;
230+
}
231+
232+
.styled-table tbody tr:last-of-type {
233+
border-bottom: 2px solid #009879;
234+
}
235+
236+
.styled-table tbody tr:hover {
237+
background-color: #f1f1f1;
238+
cursor: pointer;
239+
}

0 commit comments

Comments
 (0)