1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-11 02:58:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/相等还是不相等 [to-be-or-not-to-be].html
2023-06-02 01:00:40 +08:00

33 lines
1.3 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>请你编写一个名为 <code>expect</code> 的函数,用于帮助开发人员测试他们的代码。它应该接受任何值 <code>val</code> 并返回一个包含以下两个函数的对象。</p>
<ul>
<li><code>toBe(val)</code> 接受另一个值并在两个值相等( <code>===</code> )时返回 <code>true</code> 。如果它们不相等,则应抛出错误 <code>"Not Equal"</code></li>
<li><code>notToBe(val)</code> 接受另一个值并在两个值不相等( <code>!==</code> )时返回 <code>true</code> 。如果它们相等,则应抛出错误 <code>"Equal"</code></li>
</ul>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<pre>
<b>输入:</b>func = () =&gt; expect(5).toBe(5)
<b>输出:</b>{"value": true}
<b>解释:</b>5 === 5 因此该表达式返回 true。
</pre>
<p><strong class="example">示例 2</strong></p>
<pre>
<b>输入:</b>func = () =&gt; expect(5).toBe(null)
<b>输出:</b>{"error": "Not Equal"}
<b>解释:</b>5 !== null 因此抛出错误 "Not Equal".
</pre>
<p><strong class="example">示例 3</strong></p>
<pre>
<b>输入:</b>func = () =&gt; expect(5).notToBe(null)
<b>输出:</b>{"value": true}
<b>解释:</b>5 !== null 因此该表达式返回 true.
</pre>