Node. js getting started-13.api: use assert testing and Virtual Machine (VM)

Source: Internet
Author: User

The two modules to be introduced today are assert and VM. Next we will introduce them separately.

  Use assert for testing

Node, we can use the assert module to test the code. Equal () and notequal () are used to determine equality and inequality respectively. The first parameter is the expected value, the second parameter is the real value, and the third parameter is the exception information. The example is as follows:

var assert = require('assert');assert.equal(1, true, 'Truthy');assert.notEqual(1, true, 'Truthy');

If the test passes, no results are displayed. If the test fails, an exception is thrown. The running result is as follows:

Equal and notequal are equivalent to = and! =. When testing a Boolean value, a small vertex problem occurs. Values of false, 0, null (''), null, undefined, and Nan will return false values. Values of other values will return true, for example, non-empty strings 'false' and '0. Because they are only suitable for comparing some simple values, such as strings and numbers. Stringequal () and notstrictequal () are equivalent to ===and! =, You can make more precise judgments.

The OK () method is a simple method to compare the true value, so it uses = to compare whether the current value is true.

var assert = require('assert');
assert.ok('This is a string', 'Strings that are not empty are truthy');
assert.ok(0, 'Zero is not truthy');

Node provides the comparison methods for object objects: deepequal () and notdeepequal (). They compare objects using the following steps. If one step does not match, an exception is thrown: 1. use = comparison; 2. compare whether they are buffers. If yes, compare the length, and then compare each byte; 3. is compared with =; 4. finally, if the parameters are object objects, compare their property length with the property value. As you can see, the performance of these two methods may be worse, so they are used only when necessary.

The following methods are introduced: throws () and doesnotthrow (). They are used to determine whether an exception is thrown in a piece of code. Four types of parameters can be passed: returns a true or false comparison function, using RegEx. the regular expression, string, and typeof constructor of test.

var assert = require('assert');
assert.throws(function() {
  throw new Error("Seven Fingers. Ten is too mainstream.");
});
assert.doesNotThrow(function() {
  throw new Error("I lived in the ocean way before Nemo");
});

 

  VM (Virtual Machine)

The VM module can execute arbitrary code and return results. It has a series of features that allow you to switch the code execution context. It provides a Sandbox Model. The VM is similar to the eval () method, but provides more features and better API Management Code. There are two ways to run code using VM: 1. Use embedded code, similar to using eval; 2. Pass the pre-compiled code into the VM. Script object.

Embedded code:

var vm = require('vm');vm.runInThisContext("1+1");

Running result:

Next let's take a look at the use of VM. runinnewcontext (). The second parameter represents the context of the runtime environment.

var vm = require('vm');var context = { alphabet:"" };vm.runInNewContext("alphabet+='a'", context);vm.runInNewContext("alphabet+='b'", context);context

Running result:

The following describes how to use VM. createscript. First, prepare a file named example. js in the root directory of your node (generally 'C: \ Program Files \ nodejs'). The content is:

console.log(output);

Run the following code in the node repl command box:

var vm = require('vm');var fs = require('fs');var code = fs.readFileSync('example.js');code.toString();var script = vm.createScript(code);script.runInNewContext({output:"Kick Ass"});script.runInNewContext({"console":console,"output":"Kick Ass"});

Running result:

Using VM. script can run the same code repeatedly in different contexts.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.