/
test.hpp
117 lines (108 loc) · 2.76 KB
/
test.hpp
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
115
116
117
//
// Copyright (c) 2013-2016 Pavel Medvedev. All rights reserved.
//
// This file is part of v8pp (https://github.com/pmed/v8pp) project.
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <iosfwd>
#include <array>
#include <string>
#include <sstream>
#include <stdexcept>
#include "v8pp/context.hpp"
#include "v8pp/convert.hpp"
template<typename Char, typename Traits,
typename T, typename Alloc, typename ...Other,
template<typename, typename, typename ...> class Sequence>
std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os,
Sequence<T, Alloc, Other...> const& sequence)
{
os << '[';
bool first = true;
for (auto const& item : sequence)
{
if (!first) os << ", ";
os << item;
first = false;
}
os << ']';
return os;
}
template<typename Char, typename Traits, typename T, size_t N>
std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os, std::array<T, N> const& array)
{
os << '[';
bool first = true;
for (auto const& item : array)
{
if (!first) os << ", ";
os << item;
first = false;
}
os << ']';
return os;
}
template<typename Char, typename Traits,
typename Key, typename Value, typename Less, typename Alloc, typename ...Other,
template<typename, typename, typename, typename, typename ...> class Mapping>
std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os,
Mapping<Key, Value, Less, Alloc, Other...> const& mapping)
{
os << '{';
bool first = true;
for (auto const& item : mapping)
{
if (!first) os << ", ";
os << item.first << ": " <<item.second;
first = false;
}
os << '}';
return os;
}
inline void check(std::string msg, bool condition)
{
if (!condition)
{
std::stringstream ss;
ss << "check failed: " << msg;
throw std::runtime_error(ss.str());
}
}
template<typename T, typename U>
void check_eq(std::string msg, T actual, U expected)
{
if (actual != expected)
{
std::stringstream ss;
ss << msg << " expected: '" << expected << "' actual: '" << actual << "'";
check(ss.str(), false);
}
}
template<typename Ex, typename F>
void check_ex(std::string msg, F&& f)
{
try
{
f();
check(msg + " expected " + typeid(Ex).name() + " exception", false);
}
catch (Ex const&)
{
}
}
template<typename T>
T run_script(v8pp::context& context, std::string const& source)
{
v8::Isolate* isolate = context.isolate();
v8::HandleScope scope(isolate);
v8::TryCatch try_catch;
v8::Handle<v8::Value> result = context.run_script(source);
if (try_catch.HasCaught())
{
std::string const msg = v8pp::from_v8<std::string>(isolate, try_catch.Exception()->ToString());
throw std::runtime_error(msg);
}
return v8pp::from_v8<T>(isolate, result);
}