Add support for doctests (#1909)
Markdown files in the docs/ directory can now be annotated to turn
fenced C code blocks into unit tests. The recognized syntax is:
[doctest]: # (name="test.c", test="run")
```c
// unit test code
```
The commit also fixes the issues revealed during the initial
annotation.
JerryScript-DCO-1.0-Signed-off-by: Akos Kiss akiss@inf.u-szeged.hu
This commit is contained in:
+28
-11
@@ -4,12 +4,14 @@ This guide is intended to introduce you to JerryScript embedding API through cre
|
||||
|
||||
## Step 1. Execute JavaScript from your application
|
||||
|
||||
[doctest]: # ()
|
||||
|
||||
```c
|
||||
#include <string.h>
|
||||
#include "jerryscript.h"
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
main (void)
|
||||
{
|
||||
const jerry_char_t script[] = "var str = 'Hello, World!';";
|
||||
size_t script_size = strlen ((const char *) script);
|
||||
@@ -32,13 +34,15 @@ Here we perform the same actions, as `jerry_run_simple`, while splitting into se
|
||||
- engine cleanup
|
||||
|
||||
|
||||
[doctest]: # ()
|
||||
|
||||
```c
|
||||
#include <string.h>
|
||||
#include "jerryscript.h"
|
||||
#include "jerryscript-ext/handler.h"
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
main (void)
|
||||
{
|
||||
const jerry_char_t script[] = "print ('Hello, World!');";
|
||||
size_t script_size = strlen ((const char *) script);
|
||||
@@ -76,13 +80,15 @@ Our code is more complex now, but it introduces possibilities to interact with J
|
||||
|
||||
## Step 3. Execution in 'eval'-mode
|
||||
|
||||
[doctest]: # ()
|
||||
|
||||
```c
|
||||
#include <string.h>
|
||||
#include "jerryscript.h"
|
||||
#include "jerryscript-ext/handler.h"
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
main (void)
|
||||
{
|
||||
const jerry_char_t script_1[] = "var s = 'Hello, World!';";
|
||||
const jerry_char_t script_2[] = "print (s);";
|
||||
@@ -123,13 +129,16 @@ This way, we execute two independent script parts in one execution environment.
|
||||
|
||||
## Step 4. Interaction with JavaScript environment
|
||||
|
||||
[doctest]: # ()
|
||||
|
||||
```c
|
||||
#include <string.h>
|
||||
#include "jerryscript.h"
|
||||
#include "jerryscript-ext/handler.h"
|
||||
|
||||
int
|
||||
main (int argc, char *argv[]) {
|
||||
main (void)
|
||||
{
|
||||
const jerry_char_t str[] = "Hello, World!";
|
||||
const jerry_char_t script[] = "print (s);";
|
||||
|
||||
@@ -183,6 +192,8 @@ created by API functions has the error flag set.
|
||||
|
||||
The following example function will output a JavaScript value:
|
||||
|
||||
[doctest]: # (test="compile")
|
||||
|
||||
```c
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -252,6 +263,8 @@ Shell operation can be described with the following loop:
|
||||
- print result of eval;
|
||||
- loop.
|
||||
|
||||
[doctest]: # (test="compile")
|
||||
|
||||
```c
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -259,10 +272,10 @@ Shell operation can be described with the following loop:
|
||||
#include "jerryscript.h"
|
||||
#include "jerryscript-ext/handler.h"
|
||||
|
||||
static void print_value (const jerry_value_t);
|
||||
void print_value (const jerry_value_t);
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
main (void)
|
||||
{
|
||||
bool is_done = false;
|
||||
|
||||
@@ -275,7 +288,7 @@ main (int argc, char *argv[])
|
||||
|
||||
while (!is_done)
|
||||
{
|
||||
char cmd[256] = {};
|
||||
char cmd[256];
|
||||
char *cmd_tail = cmd;
|
||||
size_t len = 0;
|
||||
|
||||
@@ -316,7 +329,7 @@ main (int argc, char *argv[])
|
||||
{
|
||||
/* Evaluated JS code thrown an exception
|
||||
* and didn't handle it with try-catch-finally */
|
||||
printf ("Unhandled JS exception occured: ");
|
||||
printf ("Unhandled JS exception occurred: ");
|
||||
}
|
||||
|
||||
print_value (ret_val);
|
||||
@@ -336,6 +349,8 @@ The application inputs commands and evaluates them, one after another.
|
||||
|
||||
In this example we demonstrate how to use native function and structures in JavaScript.
|
||||
|
||||
[doctest]: # ()
|
||||
|
||||
```c
|
||||
#include <string.h>
|
||||
#include "jerryscript.h"
|
||||
@@ -359,7 +374,7 @@ get_msg_handler (const jerry_value_t func_value, /**< function object */
|
||||
} /* get_msg_handler */
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
main (void)
|
||||
{
|
||||
/* Initialize engine */
|
||||
jerry_init (JERRY_INIT_EMPTY);
|
||||
@@ -369,7 +384,7 @@ main (int argc, char *argv[])
|
||||
jerryx_handler_print);
|
||||
|
||||
/* Do something with the native object */
|
||||
my_struct.msg = "Hello World";
|
||||
my_struct.msg = "Hello, World!";
|
||||
|
||||
/* Create an empty JS object */
|
||||
jerry_value_t object = jerry_create_object ();
|
||||
@@ -427,6 +442,8 @@ Hello World
|
||||
|
||||
Here we create a JS Object with `jerry_eval`, then extend it with a native function. This function shows how to get a property value from the object and how to manipulate it.
|
||||
|
||||
[doctest]: # ()
|
||||
|
||||
```c
|
||||
#include <string.h>
|
||||
#include "jerryscript.h"
|
||||
@@ -466,7 +483,7 @@ add_handler (const jerry_value_t func_value, /**< function object */
|
||||
} /* add_handler */
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
main (void)
|
||||
{
|
||||
/* Initialize engine */
|
||||
jerry_init (JERRY_INIT_EMPTY);
|
||||
|
||||
Reference in New Issue
Block a user