Repository: denoland/deno
File: libdeno/modules_test.cc (C++)

Code before:
TEST(ModulesTest, ImportMetaUrl) {
  exec_count = 0;  // Reset
  Deno* d = deno_new(deno_config{0, empty, empty, recv_cb});
  EXPECT_EQ(0, exec_count);

  static deno_mod a =
      deno_mod_new(d, true, "a.js",
                   "if ('a.js' != import.meta.url) throw 'hmm'\n"
                   "libdeno.send(new Uint8Array([4]));");
  EXPECT_NE(a, 0);
  EXPECT_EQ(nullptr, deno_last_exception(d));

  deno_mod_instantiate(d, d, a, nullptr);
  EXPECT_EQ(nullptr, deno_last_exception(d));
  EXPECT_EQ(0, exec_count);

  deno_mod_evaluate(d, d, a);
  EXPECT_EQ(nullptr, deno_last_exception(d));
  EXPECT_EQ(1, exec_count);
}

TEST(ModulesTest, ImportMetaMain) {
  Deno* d = deno_new(deno_config{0, empty, empty, recv_cb});

  const char* a_src = "if (!import.meta.main) throw 'hmm'";
  static deno_mod a = deno_mod_new(d, true, "a.js", a_src);
  EXPECT_NE(a, 0);
  EXPECT_EQ(nullptr, deno_last_exception(d));

  deno_mod_instantiate(d, d, a, nullptr);
  EXPECT_EQ(nullptr, deno_last_exception(d));

  deno_mod_evaluate(d, d, a);
  EXPECT_EQ(nullptr, deno_last_exception(d));

  const char* b_src = "if (import.meta.main) throw 'hmm'";
  static deno_mod b = deno_mod_new(d, false, "b.js", b_src);
  EXPECT_NE(b, 0);
  EXPECT_EQ(nullptr, deno_last_exception(d));

  deno_mod_instantiate(d, d, b, nullptr);
  EXPECT_EQ(nullptr, deno_last_exception(d));

  deno_mod_evaluate(d, d, b);
  EXPECT_EQ(nullptr, deno_last_exception(d));

  deno_delete(d);
}


Review comment (suggestion):
rename `a_src` to `throw_not_main_src` 
rename `a` to `throw_not_main`

rename `b_src` to `throw_main_src` 
rename `b` to `throw_main` 



Code after:
TEST(ModulesTest, ImportMetaUrl) {
  exec_count = 0;  // Reset
  Deno* d = deno_new(deno_config{0, empty, empty, recv_cb});
  EXPECT_EQ(0, exec_count);

  static deno_mod a =
      deno_mod_new(d, true, "a.js",
                   "if ('a.js' != import.meta.url) throw 'hmm'\n"
                   "libdeno.send(new Uint8Array([4]));");
  EXPECT_NE(a, 0);
  EXPECT_EQ(nullptr, deno_last_exception(d));

  deno_mod_instantiate(d, d, a, nullptr);
  EXPECT_EQ(nullptr, deno_last_exception(d));
  EXPECT_EQ(0, exec_count);

  deno_mod_evaluate(d, d, a);
  EXPECT_EQ(nullptr, deno_last_exception(d));
  EXPECT_EQ(1, exec_count);
}

TEST(ModulesTest, ImportMetaMain) {
  Deno* d = deno_new(deno_config{0, empty, empty, recv_cb});

  const char* throw_not_main_src = "if (!import.meta.main) throw 'err'";
  static deno_mod throw_not_main =
      deno_mod_new(d, true, "a.js", throw_not_main_src);
  EXPECT_NE(throw_not_main, 0);
  EXPECT_EQ(nullptr, deno_last_exception(d));

  deno_mod_instantiate(d, d, throw_not_main, nullptr);
  EXPECT_EQ(nullptr, deno_last_exception(d));

  deno_mod_evaluate(d, d, throw_not_main);
  EXPECT_EQ(nullptr, deno_last_exception(d));

  const char* throw_main_src = "if (import.meta.main) throw 'err'";
  static deno_mod throw_main = deno_mod_new(d, false, "b.js", throw_main_src);
  EXPECT_NE(throw_main, 0);
  EXPECT_EQ(nullptr, deno_last_exception(d));

  deno_mod_instantiate(d, d, throw_main, nullptr);
  EXPECT_EQ(nullptr, deno_last_exception(d));

  deno_mod_evaluate(d, d, throw_main);
  EXPECT_EQ(nullptr, deno_last_exception(d));

  deno_delete(d);
}