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

Code before:
static jerry_value_t
create_object (const char *source_p) /**< source script */
{
  jerry_value_t result = jerry_eval ((const jerry_char_t *) source_p, strlen (source_p), 0);
  TEST_ASSERT (jerry_value_is_object (result));
  return result;
} /* create_object */

static void
compare_string (jerry_value_t value, /**< value to compare */
                const char *string_p) /**< expected value */
{
  jerry_char_t string_buffer[64];

  TEST_ASSERT (jerry_value_is_string (value));

  size_t size = strlen (string_p);
  TEST_ASSERT (size <= sizeof (string_buffer));
  TEST_ASSERT (size == jerry_get_string_size (value));

  jerry_string_to_utf8_char_buffer (value, string_buffer, (jerry_size_t) size);
  TEST_ASSERT (memcmp (string_p, string_buffer, size) == 0);
} /* compare_string */

int
main (void)
{
  TEST_INIT ();
  jerry_init (JERRY_INIT_EMPTY);

  jerry_value_t pp_string = jerry_create_string ((const jerry_char_t *) "pp");
  jerry_value_t qq_string = jerry_create_string ((const jerry_char_t *) "qq");
  jerry_value_t rr_string = jerry_create_string ((const jerry_char_t *) "rr");

  jerry_value_t object = create_object ("'use strict';\n"
                                        "({ pp:'A', get qq() { return 'B' } })");

  jerry_value_t result = jerry_get_own_property (object, pp_string, object, NULL);
  compare_string (result, "A");
  jerry_release_value (result);

  bool found = false;
  result = jerry_get_own_property (object, pp_string, object, &found);
  compare_string (result, "A");
  TEST_ASSERT (found);
  jerry_release_value (result);

  result = jerry_get_own_property (object, qq_string, object, NULL);
  compare_string (result, "B");
  jerry_release_value (result);


Review comment (suggestion):
Afaik we'll need a tests for:
* `obj_val` is not an object
* `receiver_val` is not an object
* `prop_name_val` is not a valid prop name
* Proxy which triggers an error

Code after:
static jerry_value_t
create_object (const char *source_p) /**< source script */
{
  jerry_value_t result = jerry_eval ((const jerry_char_t *) source_p, strlen (source_p), 0);
  TEST_ASSERT (jerry_value_is_object (result));
  return result;
} /* create_object */

static void
compare_string (jerry_value_t value, /**< value to compare */
                const char *string_p) /**< expected value */
{
  jerry_char_t string_buffer[64];

  TEST_ASSERT (jerry_value_is_string (value));

  size_t size = strlen (string_p);
  TEST_ASSERT (size <= sizeof (string_buffer));
  TEST_ASSERT (size == jerry_get_string_size (value));

  jerry_string_to_char_buffer (value, string_buffer, (jerry_size_t) size);
  TEST_ASSERT (memcmp (string_p, string_buffer, size) == 0);
} /* compare_string */

int
main (void)
{
  TEST_INIT ();
  jerry_init (JERRY_INIT_EMPTY);

  jerry_value_t pp_string = jerry_create_string ((const jerry_char_t *) "pp");
  jerry_value_t qq_string = jerry_create_string ((const jerry_char_t *) "qq");
  jerry_value_t rr_string = jerry_create_string ((const jerry_char_t *) "rr");

  jerry_value_t object = create_object ("'use strict';\n"
                                        "({ pp:'A', get qq() { return 'B' } })");

  jerry_value_t result = jerry_get_own_property (object, pp_string, object, NULL);
  compare_string (result, "A");
  jerry_release_value (result);

  bool found = false;
  result = jerry_get_own_property (object, pp_string, object, &found);
  compare_string (result, "A");
  TEST_ASSERT (found);
  jerry_release_value (result);

  result = jerry_get_own_property (object, qq_string, object, NULL);
  compare_string (result, "B");
  jerry_release_value (result);