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

91 lines
2.5 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 "include/deno.h"
TEST(MockRuntimeTest, InitializesCorrectly) {
Deno* d = deno_new(NULL, NULL);
2018-06-11 20:49:57 +02:00
EXPECT_TRUE(deno_execute(d, "a.js", "1 + 2"));
deno_dispose(d);
2018-06-11 17:01:35 +02:00
}
TEST(MockRuntimeTest, CanCallFoo) {
Deno* d = deno_new(NULL, NULL);
2018-06-11 20:49:57 +02:00
EXPECT_TRUE(deno_execute(d, "a.js", "if (foo() != 'foo') throw Error();"));
deno_dispose(d);
2018-06-11 17:01:35 +02:00
}
TEST(MockRuntimeTest, ErrorsCorrectly) {
Deno* d = deno_new(NULL, NULL);
2018-06-11 20:49:57 +02:00
EXPECT_FALSE(deno_execute(d, "a.js", "throw Error()"));
deno_dispose(d);
2018-06-11 17:01:35 +02:00
}
2018-06-11 18:17:28 +02:00
deno_buf strbuf(const char* str) {
void* d = reinterpret_cast<void*>(const_cast<char*>(str));
return deno_buf{d, strlen(str)};
}
2018-06-11 19:18:53 +02:00
TEST(MockRuntimeTest, PubSuccess) {
2018-06-11 18:17:28 +02:00
Deno* d = deno_new(NULL, NULL);
2018-06-11 20:49:57 +02:00
EXPECT_TRUE(deno_execute(d, "a.js", "subabc();"));
2018-06-11 19:18:53 +02:00
EXPECT_TRUE(deno_pub(d, strbuf("abc")));
deno_dispose(d);
2018-06-11 18:17:28 +02:00
}
2018-06-11 19:18:53 +02:00
TEST(MockRuntimeTest, PubByteLength) {
2018-06-11 18:17:28 +02:00
Deno* d = deno_new(NULL, NULL);
2018-06-11 20:49:57 +02:00
EXPECT_TRUE(deno_execute(d, "a.js", "subabc();"));
2018-06-11 19:18:53 +02:00
// We pub the wrong sized message, it should throw.
EXPECT_FALSE(deno_pub(d, strbuf("abcd")));
deno_dispose(d);
2018-06-11 18:17:28 +02:00
}
2018-06-11 19:18:53 +02:00
TEST(MockRuntimeTest, PubNoCallback) {
2018-06-11 18:17:28 +02:00
Deno* d = deno_new(NULL, NULL);
2018-06-11 19:18:53 +02:00
// We didn't call deno_sub(), pubing should fail.
EXPECT_FALSE(deno_pub(d, strbuf("abc")));
deno_dispose(d);
2018-06-11 18:17:28 +02:00
}
2018-06-11 20:18:56 +02:00
TEST(MockRuntimeTest, SubReturnEmpty) {
static int count = 0;
Deno* d = deno_new(NULL, [](Deno* _, deno_buf buf) {
count++;
EXPECT_EQ(static_cast<size_t>(3), buf.len);
// TODO(ry) buf.data should just be a char*.
char* data = reinterpret_cast<char*>(buf.data);
EXPECT_EQ(data[0], 'a');
EXPECT_EQ(data[1], 'b');
EXPECT_EQ(data[2], 'c');
return deno_buf{nullptr, 0};
});
2018-06-11 20:49:57 +02:00
EXPECT_TRUE(deno_execute(d, "a.js", "pubReturnEmpty()"));
2018-06-11 20:18:56 +02:00
EXPECT_EQ(count, 2);
deno_dispose(d);
}
TEST(MockRuntimeTest, SubReturnBar) {
static int count = 0;
Deno* d = deno_new(NULL, [](Deno* _, deno_buf buf) {
count++;
EXPECT_EQ(static_cast<size_t>(3), buf.len);
// TODO(ry) buf.data should just be a char*.
char* data = reinterpret_cast<char*>(buf.data);
EXPECT_EQ(data[0], 'a');
EXPECT_EQ(data[1], 'b');
EXPECT_EQ(data[2], 'c');
return strbuf("bar");
});
2018-06-11 20:49:57 +02:00
EXPECT_TRUE(deno_execute(d, "a.js", "pubReturnBar()"));
2018-06-11 20:18:56 +02:00
EXPECT_EQ(count, 1);
deno_dispose(d);
}
2018-06-11 17:01:35 +02:00
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
deno_init();
return RUN_ALL_TESTS();
}