1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-23 15:39:49 -05:00
denoland-deno/src/mock_runtime_test.cc

115 lines
3.1 KiB
C++
Raw Normal View History

2018-06-11 17:01:35 +02:00
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
// All rights reserved. MIT License.
#include "testing/gtest/include/gtest/gtest.h"
#include "deno.h"
2018-06-11 17:01:35 +02:00
TEST(MockRuntimeTest, InitializesCorrectly) {
Deno* d = deno_new(nullptr, nullptr);
2018-06-11 20:49:57 +02:00
EXPECT_TRUE(deno_execute(d, "a.js", "1 + 2"));
2018-06-11 22:36:14 +02:00
deno_delete(d);
2018-06-11 17:01:35 +02:00
}
TEST(MockRuntimeTest, CanCallFunction) {
Deno* d = deno_new(nullptr, nullptr);
EXPECT_TRUE(deno_execute(d, "a.js",
"if (CanCallFunction() != 'foo') throw Error();"));
2018-06-11 22:36:14 +02:00
deno_delete(d);
2018-06-11 17:01:35 +02:00
}
TEST(MockRuntimeTest, ErrorsCorrectly) {
Deno* d = deno_new(nullptr, nullptr);
2018-06-11 20:49:57 +02:00
EXPECT_FALSE(deno_execute(d, "a.js", "throw Error()"));
2018-06-11 22:36:14 +02:00
deno_delete(d);
2018-06-11 17:01:35 +02:00
}
2018-06-11 22:19:34 +02:00
deno_buf strbuf(const char* str) { return deno_buf{str, strlen(str)}; }
2018-06-11 18:17:28 +02:00
TEST(MockRuntimeTest, SendSuccess) {
Deno* d = deno_new(nullptr, nullptr);
EXPECT_TRUE(deno_execute(d, "a.js", "SendSuccess()"));
EXPECT_TRUE(deno_send(d, strbuf("abc")));
2018-06-11 22:36:14 +02:00
deno_delete(d);
2018-06-11 18:17:28 +02:00
}
TEST(MockRuntimeTest, SendByteLength) {
Deno* d = deno_new(nullptr, nullptr);
EXPECT_TRUE(deno_execute(d, "a.js", "SendByteLength()"));
2018-06-11 19:18:53 +02:00
// We pub the wrong sized message, it should throw.
EXPECT_FALSE(deno_send(d, strbuf("abcd")));
2018-06-11 22:36:14 +02:00
deno_delete(d);
2018-06-11 18:17:28 +02:00
}
TEST(MockRuntimeTest, SendNoCallback) {
Deno* d = deno_new(nullptr, nullptr);
// We didn't call deno.recv() in JS, should fail.
EXPECT_FALSE(deno_send(d, strbuf("abc")));
2018-06-11 22:36:14 +02:00
deno_delete(d);
2018-06-11 18:17:28 +02:00
}
TEST(MockRuntimeTest, RecvReturnEmpty) {
2018-06-11 20:18:56 +02:00
static int count = 0;
Deno* d = deno_new(nullptr, [](auto _, auto buf) {
2018-06-11 20:18:56 +02:00
count++;
EXPECT_EQ(static_cast<size_t>(3), buf.len);
2018-06-11 22:19:34 +02:00
EXPECT_EQ(buf.data[0], 'a');
EXPECT_EQ(buf.data[1], 'b');
EXPECT_EQ(buf.data[2], 'c');
2018-06-11 20:18:56 +02:00
});
EXPECT_TRUE(deno_execute(d, "a.js", "RecvReturnEmpty()"));
2018-06-11 20:18:56 +02:00
EXPECT_EQ(count, 2);
2018-06-11 22:36:14 +02:00
deno_delete(d);
2018-06-11 20:18:56 +02:00
}
TEST(MockRuntimeTest, RecvReturnBar) {
2018-06-11 20:18:56 +02:00
static int count = 0;
Deno* d = deno_new(nullptr, [](auto deno, auto buf) {
2018-06-11 20:18:56 +02:00
count++;
EXPECT_EQ(static_cast<size_t>(3), buf.len);
2018-06-11 22:19:34 +02:00
EXPECT_EQ(buf.data[0], 'a');
EXPECT_EQ(buf.data[1], 'b');
EXPECT_EQ(buf.data[2], 'c');
deno_set_response(deno, strbuf("bar"));
2018-06-11 20:18:56 +02:00
});
EXPECT_TRUE(deno_execute(d, "a.js", "RecvReturnBar()"));
2018-06-11 20:18:56 +02:00
EXPECT_EQ(count, 1);
2018-06-11 22:36:14 +02:00
deno_delete(d);
2018-06-11 20:18:56 +02:00
}
TEST(MockRuntimeTest, DoubleRecvFails) {
Deno* d = deno_new(nullptr, nullptr);
EXPECT_FALSE(deno_execute(d, "a.js", "DoubleRecvFails()"));
2018-06-11 22:51:11 +02:00
deno_delete(d);
}
2018-06-12 06:36:01 +02:00
TEST(MockRuntimeTest, TypedArraySnapshots) {
Deno* d = deno_new(nullptr, nullptr);
2018-06-12 06:36:01 +02:00
EXPECT_TRUE(deno_execute(d, "a.js", "TypedArraySnapshots()"));
deno_delete(d);
}
2018-06-18 15:55:36 +02:00
TEST(MockRuntimeTest, SnapshotBug) {
Deno* d = deno_new(nullptr, nullptr);
EXPECT_TRUE(deno_execute(d, "a.js", "SnapshotBug()"));
deno_delete(d);
}
TEST(MockRuntimeTest, ErrorHandling) {
static int count = 0;
Deno* d = deno_new(nullptr, [](auto deno, auto buf) {
count++;
EXPECT_EQ(static_cast<size_t>(1), buf.len);
EXPECT_EQ(buf.data[0], 42);
});
EXPECT_FALSE(deno_execute(d, "a.js", "ErrorHandling()"));
EXPECT_EQ(count, 1);
deno_delete(d);
}
2018-06-11 17:01:35 +02:00
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
deno_init();
deno_set_flags(&argc, argv);
2018-06-11 17:01:35 +02:00
return RUN_ALL_TESTS();
}