Repository: jerryscript-project/jerryscript
File: tests/unit-core/test-snapshot.c (C)

Code before:

    jerry_value_t parse_result = jerry_parse (code_to_snapshot, sizeof (code_to_snapshot) - 1, NULL);
    TEST_ASSERT (!jerry_value_is_exception (parse_result));

    jerry_value_t generate_result =
      jerry_generate_snapshot (parse_result, JERRY_SNAPSHOT_SAVE_STATIC, snapshot_buffer, SNAPSHOT_BUFFER_SIZE);
    jerry_value_free (parse_result);

    TEST_ASSERT (!jerry_value_is_exception (generate_result) && jerry_value_is_number (generate_result));

    size_t snapshot_size = (size_t) jerry_value_as_number (generate_result);
    jerry_value_free (generate_result);

    /* Static snapshots are not supported by default. */
    jerry_value_t exec_result = jerry_exec_snapshot (snapshot_buffer, snapshot_size, 0, 0, NULL);
    TEST_ASSERT (jerry_value_is_exception (exec_result));
    jerry_value_free (exec_result);

    jerry_cleanup ();

    test_exec_snapshot (snapshot_buffer, snapshot_size, JERRY_SNAPSHOT_EXEC_ALLOW_STATIC);

    /* Merge snapshot */
    static uint32_t snapshot_buffer_0[SNAPSHOT_BUFFER_SIZE];
    static uint32_t snapshot_buffer_1[SNAPSHOT_BUFFER_SIZE];
    size_t snapshot_sizes[2];
    static uint32_t merged_snapshot_buffer[SNAPSHOT_BUFFER_SIZE];

    const jerry_char_t code_to_snapshot1[] = "var a = 'hello'; 123";

    jerry_init (JERRY_INIT_EMPTY);

    parse_result = jerry_parse (code_to_snapshot1, sizeof (code_to_snapshot1) - 1, NULL);
    TEST_ASSERT (!jerry_value_is_exception (parse_result));

    generate_result = jerry_generate_snapshot (parse_result, 0, snapshot_buffer_0, SNAPSHOT_BUFFER_SIZE);
    jerry_value_free (parse_result);

    TEST_ASSERT (!jerry_value_is_exception (generate_result) && jerry_value_is_number (generate_result));

    snapshot_sizes[0] = (size_t) jerry_value_as_number (generate_result);
    jerry_value_free (generate_result);

    jerry_cleanup ();

    const jerry_char_t code_to_snapshot2[] = "var b = 'hello'; 456";

    jerry_init (JERRY_INIT_EMPTY);

    parse_result = jerry_parse (code_to_snapshot2, sizeof (code_to_snapshot2) - 1, NULL);
    TEST_ASSERT (!jerry_value_is_exception (parse_result));

Review comment (refactor):
I think that instead of merging these two blocks, which are actually two separate test cases that happen to be guarded by the same condition, all test cases of `main` should be organized into separate test functions. 

interestingly, at the end of `main`, there are already calls to three test cases organized into functions (`test_function_snapshot`, `test_function_arguments_snapshot`, `test_snapshot_with_user`). but for some reason, the first three test cases are still big blocks with main (prefixed with comments `/* Static snapshot */`, `/* Merge snapshot */`, and `/* Save literals */`). let's refactor them.


Code after:
  {
    return;
  }

  const jerry_init_flag_t flags = JERRY_INIT_EMPTY;
  static uint32_t function_snapshot_buffer[SNAPSHOT_BUFFER_SIZE];

  const jerry_char_t code_to_snapshot[] = "return a + b";

  jerry_init (flags);

  jerry_parse_options_t parse_options;
  parse_options.options = JERRY_PARSE_HAS_ARGUMENT_LIST;
  parse_options.argument_list = jerry_string_sz ("a, b");

  jerry_value_t parse_result = jerry_parse (code_to_snapshot, sizeof (code_to_snapshot) - 1, &parse_options);
  TEST_ASSERT (!jerry_value_is_exception (parse_result));

  jerry_value_t generate_result =
    jerry_generate_snapshot (parse_result, 0, function_snapshot_buffer, SNAPSHOT_BUFFER_SIZE);
  jerry_value_free (parse_result);
  jerry_value_free (parse_options.argument_list);

  TEST_ASSERT (!jerry_value_is_exception (generate_result) && jerry_value_is_number (generate_result));

  size_t function_snapshot_size = (size_t) jerry_value_as_number (generate_result);
  jerry_value_free (generate_result);

  jerry_cleanup ();

  jerry_init (flags);

  jerry_value_t function_obj = jerry_exec_snapshot (function_snapshot_buffer,
                                                    function_snapshot_size,
                                                    0,
                                                    JERRY_SNAPSHOT_EXEC_LOAD_AS_FUNCTION,
                                                    NULL);

  TEST_ASSERT (!jerry_value_is_exception (function_obj));
  TEST_ASSERT (jerry_value_is_function (function_obj));

  jerry_value_t this_val = jerry_undefined ();
  jerry_value_t args[2];
  args[0] = jerry_number (1.0);
  args[1] = jerry_number (2.0);

  jerry_value_t res = jerry_call (function_obj, this_val, args, 2);

  TEST_ASSERT (!jerry_value_is_exception (res));
  TEST_ASSERT (jerry_value_is_number (res));
  double num = jerry_value_as_number (res);