2018-06-11 11:01:35 -04:00
|
|
|
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
|
|
|
|
// All rights reserved. MIT License.
|
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
|
|
|
|
#include "include/deno.h"
|
|
|
|
|
|
|
|
TEST(MockRuntimeTest, InitializesCorrectly) {
|
2018-06-13 13:38:22 -04:00
|
|
|
Deno* d = deno_new(nullptr, nullptr);
|
2018-06-11 14:49:57 -04:00
|
|
|
EXPECT_TRUE(deno_execute(d, "a.js", "1 + 2"));
|
2018-06-11 16:36:14 -04:00
|
|
|
deno_delete(d);
|
2018-06-11 11:01:35 -04:00
|
|
|
}
|
|
|
|
|
2018-06-11 15:30:58 -04:00
|
|
|
TEST(MockRuntimeTest, CanCallFunction) {
|
2018-06-13 13:38:22 -04:00
|
|
|
Deno* d = deno_new(nullptr, nullptr);
|
2018-06-11 15:30:58 -04:00
|
|
|
EXPECT_TRUE(deno_execute(d, "a.js",
|
|
|
|
"if (CanCallFunction() != 'foo') throw Error();"));
|
2018-06-11 16:36:14 -04:00
|
|
|
deno_delete(d);
|
2018-06-11 11:01:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MockRuntimeTest, ErrorsCorrectly) {
|
2018-06-13 13:38:22 -04:00
|
|
|
Deno* d = deno_new(nullptr, nullptr);
|
2018-06-11 14:49:57 -04:00
|
|
|
EXPECT_FALSE(deno_execute(d, "a.js", "throw Error()"));
|
2018-06-11 16:36:14 -04:00
|
|
|
deno_delete(d);
|
2018-06-11 11:01:35 -04:00
|
|
|
}
|
|
|
|
|
2018-06-11 16:19:34 -04:00
|
|
|
deno_buf strbuf(const char* str) { return deno_buf{str, strlen(str)}; }
|
2018-06-11 12:17:28 -04:00
|
|
|
|
2018-06-11 13:18:53 -04:00
|
|
|
TEST(MockRuntimeTest, PubSuccess) {
|
2018-06-13 13:38:22 -04:00
|
|
|
Deno* d = deno_new(nullptr, nullptr);
|
2018-06-11 15:30:58 -04:00
|
|
|
EXPECT_TRUE(deno_execute(d, "a.js", "PubSuccess()"));
|
2018-06-11 15:57:25 -04:00
|
|
|
EXPECT_TRUE(deno_pub(d, "PubSuccess", strbuf("abc")));
|
2018-06-11 16:36:14 -04:00
|
|
|
deno_delete(d);
|
2018-06-11 12:17:28 -04:00
|
|
|
}
|
|
|
|
|
2018-06-11 13:18:53 -04:00
|
|
|
TEST(MockRuntimeTest, PubByteLength) {
|
2018-06-13 13:38:22 -04:00
|
|
|
Deno* d = deno_new(nullptr, nullptr);
|
2018-06-11 15:30:58 -04:00
|
|
|
EXPECT_TRUE(deno_execute(d, "a.js", "PubByteLength()"));
|
2018-06-11 13:18:53 -04:00
|
|
|
// We pub the wrong sized message, it should throw.
|
2018-06-11 15:57:25 -04:00
|
|
|
EXPECT_FALSE(deno_pub(d, "PubByteLength", strbuf("abcd")));
|
2018-06-11 16:36:14 -04:00
|
|
|
deno_delete(d);
|
2018-06-11 12:17:28 -04:00
|
|
|
}
|
|
|
|
|
2018-06-11 13:18:53 -04:00
|
|
|
TEST(MockRuntimeTest, PubNoCallback) {
|
2018-06-13 13:38:22 -04:00
|
|
|
Deno* d = deno_new(nullptr, nullptr);
|
2018-06-11 13:18:53 -04:00
|
|
|
// We didn't call deno_sub(), pubing should fail.
|
2018-06-11 15:57:25 -04:00
|
|
|
EXPECT_FALSE(deno_pub(d, "PubNoCallback", strbuf("abc")));
|
2018-06-11 16:36:14 -04:00
|
|
|
deno_delete(d);
|
2018-06-11 12:17:28 -04:00
|
|
|
}
|
|
|
|
|
2018-06-11 14:18:56 -04:00
|
|
|
TEST(MockRuntimeTest, SubReturnEmpty) {
|
|
|
|
static int count = 0;
|
2018-06-13 13:38:22 -04:00
|
|
|
Deno* d = deno_new(nullptr, [](auto _, auto channel, auto buf) {
|
2018-06-11 14:18:56 -04:00
|
|
|
count++;
|
2018-06-11 15:57:25 -04:00
|
|
|
EXPECT_STREQ(channel, "SubReturnEmpty");
|
2018-06-11 14:18:56 -04:00
|
|
|
EXPECT_EQ(static_cast<size_t>(3), buf.len);
|
2018-06-11 16:19:34 -04:00
|
|
|
EXPECT_EQ(buf.data[0], 'a');
|
|
|
|
EXPECT_EQ(buf.data[1], 'b');
|
|
|
|
EXPECT_EQ(buf.data[2], 'c');
|
2018-06-11 14:18:56 -04:00
|
|
|
});
|
2018-06-11 15:30:58 -04:00
|
|
|
EXPECT_TRUE(deno_execute(d, "a.js", "SubReturnEmpty()"));
|
2018-06-11 14:18:56 -04:00
|
|
|
EXPECT_EQ(count, 2);
|
2018-06-11 16:36:14 -04:00
|
|
|
deno_delete(d);
|
2018-06-11 14:18:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(MockRuntimeTest, SubReturnBar) {
|
|
|
|
static int count = 0;
|
2018-06-13 13:38:22 -04:00
|
|
|
Deno* d = deno_new(nullptr, [](auto deno, auto channel, auto buf) {
|
2018-06-11 14:18:56 -04:00
|
|
|
count++;
|
2018-06-11 15:57:25 -04:00
|
|
|
EXPECT_STREQ(channel, "SubReturnBar");
|
2018-06-11 14:18:56 -04:00
|
|
|
EXPECT_EQ(static_cast<size_t>(3), buf.len);
|
2018-06-11 16:19:34 -04:00
|
|
|
EXPECT_EQ(buf.data[0], 'a');
|
|
|
|
EXPECT_EQ(buf.data[1], 'b');
|
|
|
|
EXPECT_EQ(buf.data[2], 'c');
|
2018-06-13 13:38:22 -04:00
|
|
|
deno_set_response(deno, strbuf("bar"));
|
2018-06-11 14:18:56 -04:00
|
|
|
});
|
2018-06-11 15:30:58 -04:00
|
|
|
EXPECT_TRUE(deno_execute(d, "a.js", "SubReturnBar()"));
|
2018-06-11 14:18:56 -04:00
|
|
|
EXPECT_EQ(count, 1);
|
2018-06-11 16:36:14 -04:00
|
|
|
deno_delete(d);
|
2018-06-11 14:18:56 -04:00
|
|
|
}
|
|
|
|
|
2018-06-11 16:51:11 -04:00
|
|
|
TEST(MockRuntimeTest, DoubleSubFails) {
|
2018-06-13 13:38:22 -04:00
|
|
|
Deno* d = deno_new(nullptr, nullptr);
|
2018-06-11 16:51:11 -04:00
|
|
|
EXPECT_FALSE(deno_execute(d, "a.js", "DoubleSubFails()"));
|
|
|
|
deno_delete(d);
|
|
|
|
}
|
|
|
|
|
2018-06-12 00:36:01 -04:00
|
|
|
TEST(MockRuntimeTest, TypedArraySnapshots) {
|
2018-06-13 13:38:22 -04:00
|
|
|
Deno* d = deno_new(nullptr, nullptr);
|
2018-06-12 00:36:01 -04:00
|
|
|
EXPECT_TRUE(deno_execute(d, "a.js", "TypedArraySnapshots()"));
|
|
|
|
deno_delete(d);
|
|
|
|
}
|
|
|
|
|
2018-06-18 09:55:36 -04:00
|
|
|
TEST(MockRuntimeTest, SnapshotBug) {
|
|
|
|
Deno* d = deno_new(nullptr, nullptr);
|
|
|
|
EXPECT_TRUE(deno_execute(d, "a.js", "SnapshotBug()"));
|
|
|
|
deno_delete(d);
|
|
|
|
}
|
|
|
|
|
2018-06-22 08:57:49 -04:00
|
|
|
TEST(MockRuntimeTest, ErrorHandling) {
|
|
|
|
static int count = 0;
|
|
|
|
Deno* d = deno_new(nullptr, [](auto deno, auto channel, auto buf) {
|
|
|
|
count++;
|
|
|
|
EXPECT_STREQ(channel, "ErrorHandling");
|
|
|
|
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 11:01:35 -04:00
|
|
|
int main(int argc, char** argv) {
|
|
|
|
testing::InitGoogleTest(&argc, argv);
|
|
|
|
deno_init();
|
2018-06-18 10:02:08 -04:00
|
|
|
deno_set_flags(&argc, argv);
|
2018-06-11 11:01:35 -04:00
|
|
|
return RUN_ALL_TESTS();
|
|
|
|
}
|