summaryrefslogtreecommitdiff
path: root/src/mock_runtime_test.cc
blob: 67b097e8d3422202bd6a2c86d3c3bc0dad77a897 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2018 Ryan Dahl <ry@tinyclouds.org>
// All rights reserved. MIT License.
#include "testing/gtest/include/gtest/gtest.h"

#include "deno.h"

TEST(MockRuntimeTest, InitializesCorrectly) {
  Deno* d = deno_new(nullptr, nullptr);
  EXPECT_TRUE(deno_execute(d, "a.js", "1 + 2"));
  deno_delete(d);
}

TEST(MockRuntimeTest, CanCallFunction) {
  Deno* d = deno_new(nullptr, nullptr);
  EXPECT_TRUE(deno_execute(d, "a.js",
                           "if (CanCallFunction() != 'foo') throw Error();"));
  deno_delete(d);
}

TEST(MockRuntimeTest, ErrorsCorrectly) {
  Deno* d = deno_new(nullptr, nullptr);
  EXPECT_FALSE(deno_execute(d, "a.js", "throw Error()"));
  deno_delete(d);
}

deno_buf strbuf(const char* str) { return deno_buf{str, strlen(str)}; }

TEST(MockRuntimeTest, SendSuccess) {
  Deno* d = deno_new(nullptr, nullptr);
  EXPECT_TRUE(deno_execute(d, "a.js", "SendSuccess()"));
  EXPECT_TRUE(deno_send(d, strbuf("abc")));
  deno_delete(d);
}

TEST(MockRuntimeTest, SendByteLength) {
  Deno* d = deno_new(nullptr, nullptr);
  EXPECT_TRUE(deno_execute(d, "a.js", "SendByteLength()"));
  // We pub the wrong sized message, it should throw.
  EXPECT_FALSE(deno_send(d, strbuf("abcd")));
  deno_delete(d);
}

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")));
  deno_delete(d);
}

TEST(MockRuntimeTest, RecvReturnEmpty) {
  static int count = 0;
  Deno* d = deno_new(nullptr, [](auto _, auto buf) {
    count++;
    EXPECT_EQ(static_cast<size_t>(3), buf.len);
    EXPECT_EQ(buf.data[0], 'a');
    EXPECT_EQ(buf.data[1], 'b');
    EXPECT_EQ(buf.data[2], 'c');
  });
  EXPECT_TRUE(deno_execute(d, "a.js", "RecvReturnEmpty()"));
  EXPECT_EQ(count, 2);
  deno_delete(d);
}

TEST(MockRuntimeTest, RecvReturnBar) {
  static int count = 0;
  Deno* d = deno_new(nullptr, [](auto deno, auto buf) {
    count++;
    EXPECT_EQ(static_cast<size_t>(3), buf.len);
    EXPECT_EQ(buf.data[0], 'a');
    EXPECT_EQ(buf.data[1], 'b');
    EXPECT_EQ(buf.data[2], 'c');
    deno_set_response(deno, strbuf("bar"));
  });
  EXPECT_TRUE(deno_execute(d, "a.js", "RecvReturnBar()"));
  EXPECT_EQ(count, 1);
  deno_delete(d);
}

TEST(MockRuntimeTest, DoubleRecvFails) {
  Deno* d = deno_new(nullptr, nullptr);
  EXPECT_FALSE(deno_execute(d, "a.js", "DoubleRecvFails()"));
  deno_delete(d);
}

TEST(MockRuntimeTest, TypedArraySnapshots) {
  Deno* d = deno_new(nullptr, nullptr);
  EXPECT_TRUE(deno_execute(d, "a.js", "TypedArraySnapshots()"));
  deno_delete(d);
}

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);
}

int main(int argc, char** argv) {
  testing::InitGoogleTest(&argc, argv);
  deno_init();
  deno_set_flags(&argc, argv);
  return RUN_ALL_TESTS();
}