Repository: jerryscript-project/jerryscript
File: tests/unit-ext/test-ext-arg.c (C)

Code before:
  return jerry_create_undefined ();
} /* test_validator2_handler */

/**
 * The handler should have following arguments:
 *   arg1: Bool. It is an optional argument.
 *
 */
static jerry_value_t
test_validator3_handler (const jerry_value_t func_obj_val __attribute__((unused)), /**< function object */
                         const jerry_value_t this_val, /**< this value */
                         const jerry_value_t args_p[], /**< arguments list */
                         const jerry_length_t args_cnt) /**< arguments length */
{

  bool arg1 = false;

  jerryx_arg_t mapping[] =
  {
    /* ignore this */
    jerryx_arg_ignore (),
    /* 1th argument should be boolean, and it is optional */
    jerryx_arg_boolean (&arg1, JERRYX_ARG_COERCE, JERRYX_ARG_OPTIONAL),
  };

  jerry_value_t is_ok = jerryx_arg_transform_this_and_args (this_val,
                                                            args_p,
                                                            args_cnt,
                                                            mapping,
                                                            ARRAY_SIZE (mapping));

  if (validator3_count == 0)
  {
    TEST_ASSERT (!jerry_value_is_error (is_ok));
    TEST_ASSERT (arg1);
  }
  else if (validator3_count == 1)
  {
    TEST_ASSERT (!jerry_value_is_error (is_ok));
    /* arg1 must be unchanged */
    TEST_ASSERT (!arg1);
  }

  jerry_release_value (is_ok);
  validator3_count++;

  return jerry_create_undefined ();
} /* test_validator3_handler */

/**
 * Calling jerryx_arg_transform_object_properties directly.

Review comment (suggestion):
AFAIK, there should be multiple optional args, to see if multiple optional arguments can be handled.

Code after:
  validator2_count++;

  return jerry_create_undefined ();
} /* test_validator2_handler */

/**
 * The handler should have following arguments:
 *   arg1: Bool. It is an optional argument.
 *
 */
static jerry_value_t
test_validator3_handler (const jerry_value_t func_obj_val __attribute__((unused)), /**< function object */
                         const jerry_value_t this_val, /**< this value */
                         const jerry_value_t args_p[], /**< arguments list */
                         const jerry_length_t args_cnt) /**< arguments length */
{

  bool arg1 = false;
  bool arg2 = false;

  jerryx_arg_t mapping[] =
  {
    /* ignore this */
    jerryx_arg_ignore (),
    /* 1th argument should be boolean, and it is optional */
    jerryx_arg_boolean (&arg1, JERRYX_ARG_COERCE, JERRYX_ARG_OPTIONAL),
    /* 2nd argument should be boolean, and it is optional */
    jerryx_arg_boolean (&arg2, JERRYX_ARG_COERCE, JERRYX_ARG_OPTIONAL),
  };

  jerry_value_t is_ok = jerryx_arg_transform_this_and_args (this_val,
                                                            args_p,
                                                            args_cnt,
                                                            mapping,
                                                            ARRAY_SIZE (mapping));

  if (validator3_count == 0)
  {
    TEST_ASSERT (!jerry_value_is_error (is_ok));
    TEST_ASSERT (arg1);
    TEST_ASSERT (arg2);
  }
  else if (validator3_count == 1)
  {
    TEST_ASSERT (!jerry_value_is_error (is_ok));
    TEST_ASSERT (arg1);
    /* arg2 must be unchanged */
    TEST_ASSERT (!arg2);
  }
  else if (validator3_count == 2)
  {