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"
|
|
|
|
|
2018-07-06 15:19:19 +08:00
|
|
|
#include "deno.h"
|
2018-06-11 17:01:35 +02:00
|
|
|
|
|
|
|
TEST(MockRuntimeTest, InitializesCorrectly) {
|
2018-06-13 19:38:22 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-06-11 21:30:58 +02:00
|
|
|
TEST(MockRuntimeTest, CanCallFunction) {
|
2018-06-13 19:38:22 +02:00
|
|
|
Deno* d = deno_new(nullptr, nullptr);
|
2018-06-11 21:30:58 +02:00
|
|
|
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) {
|
2018-06-13 19:38:22 +02:00
|
|
|
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
|
|
|
|
2018-07-01 18:07:12 +02:00
|
|
|
TEST(MockRuntimeTest, SendSuccess) {
|
2018-06-13 19:38:22 +02:00
|
|
|
Deno* d = deno_new(nullptr, nullptr);
|
2018-07-01 18:07:12 +02:00
|
|
|
EXPECT_TRUE(deno_execute(d, "a.js", "SendSuccess()"));
|
2018-07-06 16:26:34 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-07-01 18:07:12 +02:00
|
|
|
TEST(MockRuntimeTest, SendByteLength) {
|
2018-06-13 19:38:22 +02:00
|
|
|
Deno* d = deno_new(nullptr, nullptr);
|
2018-07-01 18:07:12 +02:00
|
|
|
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.
|
2018-07-06 16:26:34 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-07-01 18:07:12 +02:00
|
|
|
TEST(MockRuntimeTest, SendNoCallback) {
|
2018-06-13 19:38:22 +02:00
|
|
|
Deno* d = deno_new(nullptr, nullptr);
|
2018-07-01 18:07:12 +02:00
|
|
|
// We didn't call deno.recv() in JS, should fail.
|
2018-07-06 16:26:34 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-07-01 18:07:12 +02:00
|
|
|
TEST(MockRuntimeTest, RecvReturnEmpty) {
|
2018-06-11 20:18:56 +02:00
|
|
|
static int count = 0;
|
2018-07-06 16:26:34 -04:00
|
|
|
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
|
|
|
});
|
2018-07-01 18:07:12 +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
|
|
|
}
|
|
|
|
|
2018-07-01 18:07:12 +02:00
|
|
|
TEST(MockRuntimeTest, RecvReturnBar) {
|
2018-06-11 20:18:56 +02:00
|
|
|
static int count = 0;
|
2018-07-06 16:26:34 -04:00
|
|
|
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');
|
2018-06-13 19:38:22 +02:00
|
|
|
deno_set_response(deno, strbuf("bar"));
|
2018-06-11 20:18:56 +02:00
|
|
|
});
|
2018-07-01 18:07:12 +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
|
|
|
}
|
|
|
|
|
2018-07-01 18:07:12 +02:00
|
|
|
TEST(MockRuntimeTest, DoubleRecvFails) {
|
2018-06-13 19:38:22 +02:00
|
|
|
Deno* d = deno_new(nullptr, nullptr);
|
2018-07-01 18:07:12 +02:00
|
|
|
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) {
|
2018-06-13 19:38:22 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-06-22 14:57:49 +02:00
|
|
|
TEST(MockRuntimeTest, ErrorHandling) {
|
|
|
|
static int count = 0;
|
2018-07-06 16:26:34 -04:00
|
|
|
Deno* d = deno_new(nullptr, [](auto deno, auto buf) {
|
2018-06-22 14:57:49 +02:00
|
|
|
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();
|
2018-06-18 16:02:08 +02:00
|
|
|
deno_set_flags(&argc, argv);
|
2018-06-11 17:01:35 +02:00
|
|
|
return RUN_ALL_TESTS();
|
|
|
|
}
|