File: api-test.cc

package info (click to toggle)
leatherman 1.4.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,944 kB
  • sloc: cpp: 14,555; python: 3,105; sh: 45; makefile: 9; ruby: 1
file content (206 lines) | stat: -rw-r--r-- 6,887 bytes parent folder | download | duplicates (4)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <catch.hpp>
#include <leatherman/ruby/api.hpp>
#include <limits>

using namespace std;
using namespace leatherman::ruby;

TEST_CASE("api::eval", "[ruby-api]") {
    SECTION("can load api and evaluate ruby code") {
        auto& ruby = api::instance();
        ruby.initialize();
        REQUIRE(ruby.initialized());

        REQUIRE(ruby.get_load_path().size() > 0u);

        REQUIRE(ruby.to_string(ruby.eval("'foo'")) == "foo");
    }
}

TEST_CASE("api::is_*", "[ruby-api]") {
    auto& ruby = api::instance();
    ruby.initialize();
    REQUIRE(ruby.initialized());

    SECTION("can correctly identify nil values") {
        REQUIRE(ruby.is_nil(ruby.nil_value()));
        REQUIRE_FALSE(ruby.is_nil(ruby.true_value()));
    }

    SECTION("can correctly identify true and false values") {
        REQUIRE(ruby.is_true(ruby.true_value()));
        REQUIRE_FALSE(ruby.is_true(ruby.false_value()));
        REQUIRE(ruby.is_false(ruby.false_value()));
        REQUIRE_FALSE(ruby.is_false(ruby.true_value()));
    }

    SECTION("can correctly identify strings") {
        REQUIRE(ruby.is_string(ruby.utf8_value("'I'm a string'")));
        REQUIRE_FALSE(ruby.is_string(ruby.true_value()));
    }

    SECTION("can correctly identify symbols") {
        REQUIRE(ruby.is_symbol(ruby.to_symbol("mysymbol")));
        REQUIRE_FALSE(ruby.is_symbol(ruby.false_value()));
    }

    SECTION("can correctly identify numbers") {
        REQUIRE(ruby.is_float(ruby.eval("1.5")));
        REQUIRE_FALSE(ruby.is_float(ruby.utf8_value("foo")));

        REQUIRE(ruby.is_integer(ruby.eval("2")));
        REQUIRE_FALSE(ruby.is_integer(ruby.eval("1.5")));
    }

    SECTION("can correctly identify hashes") {
        REQUIRE(ruby.is_hash(ruby.eval("{ 'red' => 2 }")));
        REQUIRE_FALSE(ruby.is_hash(ruby.utf8_value("foo")));
    }

    SECTION("can correctly identify type") {
        REQUIRE(ruby.is_a(ruby.eval("1"), ruby.eval("Integer")));
        REQUIRE_FALSE(ruby.is_a(ruby.eval("'1'"), ruby.eval("Integer")));
    }

    SECTION("can correctly identify arrays") {
        REQUIRE(ruby.is_array(ruby.eval("[1, 2, 3]")));
        REQUIRE_FALSE(ruby.is_array(ruby.false_value()));
    }
}

TEST_CASE("api::equals", "[ruby-api]") {
    auto& ruby = api::instance();
    ruby.initialize();
    REQUIRE(ruby.initialized());
    SECTION("can correctly test boolean values for equality") {
        REQUIRE(ruby.equals(ruby.true_value(), ruby.true_value()));
        REQUIRE_FALSE(ruby.equals(ruby.true_value(), ruby.false_value()));
    }

    SECTION("can correctly test strings for equality") {
        REQUIRE(ruby.equals(ruby.utf8_value("foo"), ruby.utf8_value("foo")));
        REQUIRE_FALSE(ruby.equals(ruby.utf8_value("foo"), ruby.utf8_value("bar")));
    }

    SECTION("can correctly test numbers for equality") {
        REQUIRE(ruby.equals(ruby.eval("1"), ruby.eval("1")));
        REQUIRE_FALSE(ruby.equals(ruby.eval("1"), ruby.eval("3")));
        REQUIRE(ruby.equals(ruby.eval("1.5"), ruby.eval("1.5")));
        REQUIRE_FALSE(ruby.equals(ruby.eval("1.5"), ruby.eval("1")));
    }

    SECTION("can correctly test Ruby hashes for equality") {
        REQUIRE(ruby.equals(ruby.eval("{ 'red' => 'blue' }"), ruby.eval("{ 'red' => 'blue' }")));
        REQUIRE_FALSE(ruby.equals(ruby.eval("{ 'red' => 'blue' }"), ruby.eval("{ 'red' => 'green' }")));
    }

    SECTION("can correctly test symbols for equality") {
        REQUIRE(ruby.equals(ruby.to_symbol("mysymbol"), ruby.eval(":mysymbol")));
        REQUIRE_FALSE(ruby.equals(ruby.to_symbol("mysymbol"), ruby.to_symbol("notmysymbol")));
    }
}

TEST_CASE("api::case_equals", "[ruby-api]") {
    auto& ruby = api::instance();
    ruby.initialize();
    REQUIRE(ruby.initialized());

    SECTION("can detect class membership") {
        REQUIRE(ruby.case_equals(ruby.eval("Integer"), ruby.eval("1")));
        REQUIRE_FALSE(ruby.case_equals(ruby.eval("String"), ruby.eval("4")));
    }
}

VALUE test_func(VALUE self) {
    auto& ruby = api::instance();
    ruby.initialize();
    return ruby.utf8_value("test function");
}

TEST_CASE("api::rb_define_singleton_method", "[ruby-api]") {
    SECTION("can define a new module with a new method") {
        auto& ruby = api::instance();
        ruby.initialize();
        REQUIRE(ruby.initialized());

        auto module = ruby.rb_define_module("Test");
        REQUIRE(module);
        ruby.rb_define_singleton_method(module, "test_func", RUBY_METHOD_FUNC(test_func), 0);
        REQUIRE(ruby.to_string(ruby.eval("Test.test_func")) == "test function");
    }
}

TEST_CASE("api::exception_to_string", "[ruby-api]") {
    auto& ruby = api::instance();
    ruby.initialize();
    REQUIRE(ruby.initialized());

    SECTION("can print exception details") {
        try {
            ruby.eval("raise 'test_exception'");
        } catch (runtime_error exc) {
            REQUIRE(string(exc.what()) == "test_exception");
        }
    }

    SECTION("can print exception details with stack trace") {
        ruby.include_stack_trace(true);
        try {
            ruby.eval("raise 'test_exception'");
        } catch (runtime_error exc) {
            REQUIRE(string(exc.what()).find("backtrace") != string::npos);
        }
    }
}

TEST_CASE("api::lookup", "[ruby-api]") {
    auto& ruby = api::instance();
    ruby.initialize();
    REQUIRE(ruby.initialized());

    SECTION("can find module by name") {
        auto foo_module = ruby.rb_define_module("Foo");
        ruby.rb_define_module_under(foo_module, "Bar");
        REQUIRE(ruby.to_string(ruby.lookup({ "Foo", "Bar" })) == "Foo::Bar");
    }
}

TEST_CASE("api::to_string", "[ruby-api]") {
    auto& ruby = api::instance();
    ruby.initialize();
    REQUIRE(ruby.initialized());

    SECTION("can normalize encodings") {
        string john {"J\xc3\xb6hn"};
        auto obj = ruby.utf8_value(john);
        auto encoded = ruby.rb_funcall(obj, ruby.rb_intern("encode"), 1, ruby.utf8_value("Windows-1252"));
        REQUIRE(ruby.to_string(encoded) == john);
    }
}

TEST_CASE("api::num2size_t", "[ruby-api]") {
    auto& ruby = api::instance();
    ruby.initialize();
    REQUIRE(ruby.initialized());

    SECTION("can convert Ruby number to size_t") {
        auto fixednum = ruby.eval("1");
        auto num = ruby.num2size_t(fixednum);
        REQUIRE(1u == num);
    }

    SECTION("can convert large Ruby number to size_t") {
        auto expected = numeric_limits<size_t>::max();
        auto largenum = ruby.eval(to_string(expected));
        auto num = ruby.num2size_t(largenum);
        REQUIRE(expected == num);
    }

#if 0
    // Can't use this test yet, because Ruby SIGSEGVs on calling rb_num2ull.
    SECTION("throws exception on Ruby numbers exceeding size_t") {
        auto largenum = ruby.eval("184467440737095516150");
        REQUIRE_THROWS_AS(ruby.num2size_t(largenum), runtime_error);
    }
#endif
}