summaryrefslogtreecommitdiff
path: root/core/libdeno/test.cc
diff options
context:
space:
mode:
Diffstat (limited to 'core/libdeno/test.cc')
-rw-r--r--core/libdeno/test.cc46
1 files changed, 31 insertions, 15 deletions
diff --git a/core/libdeno/test.cc b/core/libdeno/test.cc
index 3d022c904..0bfe374ef 100644
--- a/core/libdeno/test.cc
+++ b/core/libdeno/test.cc
@@ -1,28 +1,44 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
#include "test.h"
+#include <fstream>
#include <string>
-#include "file_util.h"
+#include "internal.h"
deno_snapshot snapshot = {nullptr, 0};
-int main(int argc, char** argv) {
- // Locate the snapshot.
- std::string exe_path;
- if (!deno::ExePath(&exe_path)) {
- std::cerr << "deno::ExePath() failed" << std::endl;
- return 1;
+bool ReadFileToString(const char* fn, std::string* contents) {
+ std::ifstream file(fn, std::ios::binary);
+ if (file.fail()) {
+ return false;
}
- std::string snapshot_path = deno::Dirname(exe_path) + SNAPSHOT_PATH;
+ contents->assign(std::istreambuf_iterator<char>{file}, {});
+ return !file.fail();
+}
+
+int main(int argc, char** argv) {
+ // All of the JS code in libdeno_test.js is tested after being snapshotted.
+ // We create that snapshot now at runtime, rather than at compile time to
+ // simplify the build process. So we load and execute the libdeno_test.js
+ // file, without running any of the tests and store the result in the global
+ // "snapshot" variable, which will be used later in the tests.
+ std::string js_fn = JS_PATH;
+ std::string js_source;
+ CHECK(ReadFileToString(js_fn.c_str(), &js_source));
+
+ deno_init();
+ deno_config config = {1, deno::empty_snapshot, deno::empty_buf, nullptr,
+ nullptr};
+ Deno* d = deno_new(config);
- // Load the snapshot.
- std::string contents;
- if (!deno::ReadFileToString(snapshot_path.c_str(), &contents)) {
- std::cerr << "Failed to read snapshot from " << snapshot_path << std::endl;
+ deno_execute(d, nullptr, js_fn.c_str(), js_source.c_str());
+ if (deno_last_exception(d) != nullptr) {
+ std::cerr << "Snapshot Exception " << std::endl;
+ std::cerr << deno_last_exception(d) << std::endl;
+ deno_delete(d);
return 1;
}
- snapshot.data_ptr =
- reinterpret_cast<uint8_t*>(const_cast<char*>(contents.c_str()));
- snapshot.data_len = contents.size();
+
+ snapshot = deno_snapshot_new(d);
testing::InitGoogleTest(&argc, argv);
deno_init();