UNPKG

signet

Version:
355 lines (334 loc) 15.6 kB
<html> <head> <title>Signet -- Mochadoc Test Documentation</title> <link rel="stylesheet" href="../assets/doc-style.css" media="screen"> <link rel="stylesheet" href="../assets/github-gist.css" media="screen"> </head> <body> <header> <a href="../index.html"><span id="library-name">Signet</span> <span id="subtitle">Mochadoc-Generated Test Documents</span></a> </header> <div class="content"> <h1>Signet Types</h1> <div> <a href="#" class="collapse-all collapse-link">Collapse All</a> </div> <ul> <li class="describe-item"> <h3>Preregistered Types</h3> <div><a href="#" class="describe-link collapse-link"></a></div> <ul class="describe-collapsible shown"> <ul> <li> <strong>should automatically register the * type</strong> </li> <li>File location: <span class="filename">./test/types.test.js</span></li> <li class="code-sample-wrapper"> <div class="code-expand"> <a href="#" class="collapse-link"></a> </div> <pre class="code-sample"> <code> function () { assert.equal(signet.isTypeOf(&#x27;*&#x27;)(&#x27;foo&#x27;), true); } </code> </pre> </li> </ul> <ul> <li> <strong>should have all core JS types preregistered</strong> </li> <li>File location: <span class="filename">./test/types.test.js</span></li> <li class="code-sample-wrapper"> <div class="code-expand"> <a href="#" class="collapse-link"></a> </div> <pre class="code-sample"> <code> function () { assert.equal(signet.isTypeOf(&#x27;boolean&#x27;)(false), true); assert.equal(signet.isTypeOf(&#x27;function&#x27;)(addBuilder()), true); assert.equal(signet.isTypeOf(&#x27;number&#x27;)(17), true); assert.equal(signet.isTypeOf(&#x27;object&#x27;)({}), true); assert.equal(signet.isTypeOf(&#x27;string&#x27;)(&#x27;foo&#x27;), true); assert.equal(signet.isTypeOf(&#x27;symbol&#x27;)(Symbol()), true); assert.equal(signet.isTypeOf(&#x27;undefined&#x27;)(undefined), true); } </code> </pre> </li> </ul> <ul> <li> <strong>should have common subtypes preregistered</strong> </li> <li>File location: <span class="filename">./test/types.test.js</span></li> <li class="code-sample-wrapper"> <div class="code-expand"> <a href="#" class="collapse-link"></a> </div> <pre class="code-sample"> <code> function () { assert.equal(signet.isTypeOf(&#x27;null&#x27;)(null), true); assert.equal(signet.isTypeOf(&#x27;array&#x27;)([]), true); assert.equal(signet.isTypeOf(&#x27;array&lt;*&gt;&#x27;)([1, 2, &#x27;foo&#x27;]), true); assert.equal(signet.isTypeOf(&#x27;array&lt;int&gt;&#x27;)([1, 2, &#x27;foo&#x27;]), false); assert.equal(signet.isTypeOf(&#x27;int&#x27;)(5), true); assert.equal(signet.isTypeOf(&#x27;int&#x27;)(5.3), false); } </code> </pre> </li> </ul> <ul> <li> <strong>should preregister algebraic types</strong> </li> <li>File location: <span class="filename">./test/types.test.js</span></li> <li class="code-sample-wrapper"> <div class="code-expand"> <a href="#" class="collapse-link"></a> </div> <pre class="code-sample"> <code> function () { assert.equal(signet.isTypeOf(&#x27;tuple&lt;int; formattedString&lt;^\\d+(\\%;)?\\D*$&gt;; boolean&gt;&#x27;)([123, &#x27;1234;foo&#x27;, false]), true); assert.equal(signet.isTypeOf(&#x27;tuple&lt;int; formattedString&lt;^\\d+(\\%;)?\\D*$&gt;; boolean&gt;&#x27;)([123, &#x27;1234;33&#x27;, false]), false); assert.equal(signet.isTypeOf(&#x27;tuple&lt;int; formattedString&lt;^\\d+(\\%;)?\\D*$&gt;; boolean&gt;&#x27;)([123, &#x27;1234;foo&#x27;, false, &#x27;hooray!&#x27;]), false); assert.equal(signet.isTypeOf(&#x27;variant&lt;int; string&gt;&#x27;)(10), true); assert.equal(signet.isTypeOf(&#x27;variant&lt;int; string&gt;&#x27;)(&#x27;I am a string&#x27;), true); assert.equal(signet.isTypeOf(&#x27;variant&lt;int; string&gt;&#x27;)(null), false); assert.equal(signet.isTypeOf(&#x27;taggedUnion&lt;int; string&gt;&#x27;)(null), false); } </code> </pre> </li> </ul> <ul> <li> <strong>should support an unordered product type</strong> </li> <li>File location: <span class="filename">./test/types.test.js</span></li> <li class="code-sample-wrapper"> <div class="code-expand"> <a href="#" class="collapse-link"></a> </div> <pre class="code-sample"> <code> function () { var isUnorderedProduct &#x3D; signet.isTypeOf(&#x27;unorderedProduct&lt;number; int; object; array; string&gt;&#x27;); assert.equal(isUnorderedProduct([1, 2, 3, 4]), false); //too short assert.equal(isUnorderedProduct([1, 2, 3, 4, 5, 6]), false); //too long assert.equal(isUnorderedProduct([2.5, &#x27;foo&#x27;, {}, 1.7, []]), false); //bad type assert.equal(isUnorderedProduct([1, 2.5, &#x27;foo&#x27;, [], {}]), true); assert.equal(isUnorderedProduct([2.5, &#x27;foo&#x27;, {}, 1, []]), true); } </code> </pre> </li> </ul> <ul> <li> <strong>should have a not operator to describe exclusive types</strong> </li> <li>File location: <span class="filename">./test/types.test.js</span></li> <li class="code-sample-wrapper"> <div class="code-expand"> <a href="#" class="collapse-link"></a> </div> <pre class="code-sample"> <code> function () { assert.equal(signet.isTypeOf(&#x27;not&lt;null&gt;&#x27;)(&#x27;foo&#x27;), true); assert.equal(signet.isTypeOf(&#x27;not&lt;null&gt;&#x27;)(null), false); } </code> </pre> </li> </ul> <ul> <li> <strong>should support a composition operator</strong> </li> <li>File location: <span class="filename">./test/types.test.js</span></li> <li class="code-sample-wrapper"> <div class="code-expand"> <a href="#" class="collapse-link"></a> </div> <pre class="code-sample"> <code> function () { assert.equal(signet.isTypeOf(&#x27;composite&lt;not&lt;null&gt;, object&gt;&#x27;)({}), true); assert.equal(signet.isTypeOf(&#x27;composite&lt;not&lt;null&gt;, object&gt;&#x27;)(undefined), false); } </code> </pre> </li> </ul> <ul> <li> <strong>should verify type types against existing known types</strong> </li> <li>File location: <span class="filename">./test/types.test.js</span></li> <li class="code-sample-wrapper"> <div class="code-expand"> <a href="#" class="collapse-link"></a> </div> <pre class="code-sample"> <code> function () { assert.equal(signet.isTypeOf(&#x27;type&#x27;)(function () { }), true); assert.equal(signet.isTypeOf(&#x27;type&#x27;)(&#x27;variant&#x27;), true); assert.equal(signet.isTypeOf(&#x27;type&#x27;)(&#x27;badType&#x27;), false); } </code> </pre> </li> </ul> <ul> <li> <strong>should preregister sequence types</strong> </li> <li>File location: <span class="filename">./test/types.test.js</span></li> <li class="code-sample-wrapper"> <div class="code-expand"> <a href="#" class="collapse-link"></a> </div> <pre class="code-sample"> <code> function () { assert.equal(signet.isTypeOf(&#x27;sequence&lt;int&gt;&#x27;)([1, 2, 3, 4]), true); assert.equal(signet.isTypeOf(&#x27;sequence&lt;int&gt;&#x27;)([1, 2, 3.5, 4]), false); assert.throws(signet.isTypeOf(&#x27;sequence&lt;boolean&gt;&#x27;).bind(null, []), &#x27;A sequence may only be comprised of numbers, strings or their subtypes.&#x27;); assert.equal(signet.isTypeOf(&#x27;monotoneSequence&lt;number&gt;&#x27;)([1, 2.5, 3, 4.7]), true); assert.equal(signet.isTypeOf(&#x27;monotoneSequence&lt;string&gt;&#x27;)([&#x27;d&#x27;, &#x27;c&#x27;, &#x27;b&#x27;, &#x27;a&#x27;]), true); assert.equal(signet.isTypeOf(&#x27;monotoneSequence&lt;int&gt;&#x27;)([1]), true); assert.equal(signet.isTypeOf(&#x27;monotoneSequence&lt;int&gt;&#x27;)([1, 2, -1, 5]), false); assert.equal(signet.isTypeOf(&#x27;increasingSequence&lt;number&gt;&#x27;)([1, 2.5, 3, 4.7]), true, &#x27;Not an increasing sequence of int&#x27;); assert.equal(signet.isTypeOf(&#x27;increasingSequence&lt;string&gt;&#x27;)([&#x27;d&#x27;, &#x27;c&#x27;, &#x27;b&#x27;, &#x27;a&#x27;]), false, &#x27;Is an increasing sequence of string&#x27;); assert.equal(signet.isTypeOf(&#x27;increasingSequence&lt;int&gt;&#x27;)([1]), true, &#x27;Not an increasing sequence of one value&#x27;); assert.equal(signet.isTypeOf(&#x27;increasingSequence&lt;int&gt;&#x27;)([1, 2, -1, 5]), false, &#x27;Is an increasing sequence of int with a negative&#x27;); assert.equal(signet.isTypeOf(&#x27;decreasingSequence&lt;number&gt;&#x27;)([1, 2.5, 3, 4.7]), false, &#x27;Not an increasing sequence of int&#x27;); assert.equal(signet.isTypeOf(&#x27;decreasingSequence&lt;string&gt;&#x27;)([&#x27;d&#x27;, &#x27;c&#x27;, &#x27;b&#x27;, &#x27;a&#x27;]), true, &#x27;Is an increasing sequence of string&#x27;); assert.equal(signet.isTypeOf(&#x27;decreasingSequence&lt;int&gt;&#x27;)([1]), true, &#x27;Not an increasing sequence of one value&#x27;); assert.equal(signet.isTypeOf(&#x27;decreasingSequence&lt;int&gt;&#x27;)([1, 2, -1, 5]), false, &#x27;Is an increasing sequence of int with a negative&#x27;); } </code> </pre> </li> </ul> <ul> <li> <strong>should have registered bounded types</strong> </li> <li>File location: <span class="filename">./test/types.test.js</span></li> <li class="code-sample-wrapper"> <div class="code-expand"> <a href="#" class="collapse-link"></a> </div> <pre class="code-sample"> <code> function () { assert.equal(signet.isTypeOf(&#x27;bounded&lt;int, 1, 5&gt;&#x27;)(3), true); assert.equal(signet.isTypeOf(&#x27;bounded&lt;number, 1, 5&gt;&#x27;)(5.1), false); assert.equal(signet.isTypeOf(&#x27;bounded&lt;int, 1, 5&gt;&#x27;)(0), false); assert.equal(signet.isTypeOf(&#x27;leftBounded&lt;number, 0&gt;&#x27;)(0), true); assert.equal(signet.isTypeOf(&#x27;leftBounded&lt;number, 0&gt;&#x27;)(1), true); assert.equal(signet.isTypeOf(&#x27;leftBounded&lt;number, 0&gt;&#x27;)(-1), false); assert.equal(signet.isTypeOf(&#x27;rightBounded&lt;number, 0&gt;&#x27;)(0), true); assert.equal(signet.isTypeOf(&#x27;rightBounded&lt;number, 0&gt;&#x27;)(-1), true); assert.equal(signet.isTypeOf(&#x27;rightBounded&lt;number, 0&gt;&#x27;)(1), false); assert.equal(signet.isTypeOf(&#x27;rightBounded&lt;int, 5&gt;&#x27;)(1.3), false); assert.equal(signet.isTypeOf(&#x27;boundedInt&lt;1; 5&gt;&#x27;)(3), true); assert.equal(signet.isTypeOf(&#x27;boundedInt&lt;1; 5&gt;&#x27;)(3.1), false); assert.equal(signet.isTypeOf(&#x27;boundedInt&lt;1; 5&gt;&#x27;)(6), false); assert.equal(signet.isTypeOf(&#x27;boundedInt&lt;1; 5&gt;&#x27;)(0), false); assert.equal(signet.isTypeOf(&#x27;leftBoundedInt&lt;0&gt;&#x27;)(0), true); assert.equal(signet.isTypeOf(&#x27;leftBoundedInt&lt;0&gt;&#x27;)(1), true); assert.equal(signet.isTypeOf(&#x27;leftBoundedInt&lt;0&gt;&#x27;)(-1), false); assert.equal(signet.isTypeOf(&#x27;leftBoundedInt&lt;0&gt;&#x27;)(), false); assert.equal(signet.isTypeOf(&#x27;rightBoundedInt&lt;0&gt;&#x27;)(0), true); assert.equal(signet.isTypeOf(&#x27;rightBoundedInt&lt;0&gt;&#x27;)(-1), true); assert.equal(signet.isTypeOf(&#x27;rightBoundedInt&lt;0&gt;&#x27;)(1), false); assert.equal(signet.isTypeOf(&#x27;boundedString&lt;2; 15&gt;&#x27;)(&#x27;hello&#x27;), true); assert.equal(signet.isTypeOf(&#x27;boundedString&lt;2; 15&gt;&#x27;)(&#x27;&#x27;), false); assert.equal(signet.isTypeOf(&#x27;boundedString&lt;2; 15&gt;&#x27;)(&#x27;this is a long string which should fail&#x27;), false); } </code> </pre> </li> </ul> <ul> <li> <strong>should support formatted strings</strong> </li> <li>File location: <span class="filename">./test/types.test.js</span></li> <li class="code-sample-wrapper"> <div class="code-expand"> <a href="#" class="collapse-link"></a> </div> <pre class="code-sample"> <code> function () { assert.equal(signet.isTypeOf(&#x27;formattedString&lt;^\\d+(\\%;)?\\d*$&gt;&#x27;)(&#x27;123;45&#x27;), true); assert.equal(signet.isTypeOf(&#x27;formattedString&lt;^\\d+(\\%;)?\\d*$&gt;&#x27;)(&#x27;Not numbers&#x27;), false); assert.doesNotThrow(signet.isTypeOf.bind(null, &#x27;formattedString&lt;:&gt;&#x27;)) } </code> </pre> </li> </ul> <ul> <li> <strong>should verify enforced functions</strong> </li> <li>File location: <span class="filename">./test/types.test.js</span></li> <li class="code-sample-wrapper"> <div class="code-expand"> <a href="#" class="collapse-link"></a> </div> <pre class="code-sample"> <code> function () { const goodEnforcedFunction &#x3D; signet.enforce(&#x27;* &#x3D;&gt; *&#x27;, () &#x3D;&gt; null); const badEnforcedFunction &#x3D; signet.enforce(&#x27;* &#x3D;&gt; null&#x27;, () &#x3D;&gt; null); assert.equal(signet.isTypeOf(&#x27;enforcedFunction&lt;* &#x3D;&gt; *&gt;&#x27;)(goodEnforcedFunction), true); assert.equal(signet.isTypeOf(&#x27;enforcedFunction&lt;* &#x3D;&gt; *&gt;&#x27;)(badEnforcedFunction), false); assert.equal(signet.isTypeOf(&#x27;enforcedFunction&lt;* &#x3D;&gt; *&gt;&#x27;)(() &#x3D;&gt; null), false); } </code> </pre> </li> </ul> <ul> <li> <strong>should pre-register signet type aliases</strong> </li> <li>File location: <span class="filename">./test/types.test.js</span></li> <li class="code-sample-wrapper"> <div class="code-expand"> <a href="#" class="collapse-link"></a> </div> <pre class="code-sample"> <code> function () { assert.equal(signet.isTypeOf(&#x27;void&#x27;)(undefined), true); assert.equal(signet.isTypeOf(&#x27;any&#x27;)(&#x27;anything&#x27;), true); } </code> </pre> </li> </ul> </ul> </li> </ul> </div> <script src="../assets/code-collapse.js"></script> <script src="../assets/highlight.pack.js"></script> <script>hljs.initHighlightingOnLoad();</script> </body> </html>