Skip to content

Commit

Permalink
Add additional tests to cover untested functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
virtuald committed Aug 28, 2023
1 parent b202dc0 commit b82e4d4
Show file tree
Hide file tree
Showing 14 changed files with 211 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tests/cpp/gen/ft/buffers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---

classes:
Buffers:
methods:
set_buffer:
buffers:
- { type: in, src: data, len: len }
get_buffer2:
buffers:
- { type: out, src: data, len: len }
get_buffer1:
buffers:
- { type: out, src: data, len: len }

v_set_buffer:
buffers:
- { type: in, src: data, len: len }
v_get_buffer2:
buffers:
- { type: out, src: data, len: len }
v_get_buffer1:
buffers:
- { type: out, src: data, len: len }
9 changes: 9 additions & 0 deletions tests/cpp/gen/ft/ignore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ classes:
Param1:
ignore: true
methods:
ClassWithIgnored:
cpp_code: |
[](py::args) {
return std::make_shared<ClassWithIgnored>(1);
}
keepalive: []
param_override:
y:
ignore: true
fnIgnore:
ignore: true
fnIgnoredParam:
Expand Down
7 changes: 7 additions & 0 deletions tests/cpp/gen/ft/inline_code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@

classes:
InlineCode:
constants:
- NS::inner::KONSTANT
enums:
MyE:
inline_code: |
.value("Value2", (InlineCode::MyE)2)
methods:
get2:
cpp_code_with_constant:
cpp_code: |
[](InlineCode *self) {
return KONSTANT;
}
inline_code: |
// you can even start with a comment
.def("get4", [](InlineCode *self) {
Expand Down
5 changes: 5 additions & 0 deletions tests/cpp/gen/ft/virtual_xform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ functions:
classes:
VBase:
methods:
different_cpp_and_py:
cpp_code: |
[](VBase * self, int x) {
return x + 2;
}
pure_io:
param_override:
ss:
Expand Down
2 changes: 2 additions & 0 deletions tests/cpp/pyproject.toml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ generate = [
{ abstract = "abstract.h" },
{ base_qualname = "base_qualname.h" },
{ base_qualname_hidden = "base_qualname_hidden.h" },
{ buffers = "buffers.h" },
{ custom_type_caster = "custom_type_caster.h" },
{ defaults = "defaults.h" },
{ docstrings = "docstrings.h" },
Expand All @@ -56,6 +57,7 @@ generate = [
{ inline_code = "inline_code.h" },
{ lifetime = "lifetime.h" },
{ nested = "nested.h" },
{ operators = "operators.h" },
{ overloads = "overloads.h" },
{ parameters = "parameters.h" },
{ refqual = "refqual.h" },
Expand Down
6 changes: 6 additions & 0 deletions tests/cpp/rpytest/ft/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# autogenerated by 'robotpy-build create-imports rpytest.ft rpytest.ft._rpytest_ft'
from ._rpytest_ft import (
Abstract,
Buffers,
ClassWithFields,
ClassWithIgnored,
ClassWithTrampoline,
Expand All @@ -19,6 +20,7 @@
GEnum,
GEnumMath,
HasFactory,
HasOperator,
IBase,
IChild,
IFinal,
Expand Down Expand Up @@ -80,6 +82,7 @@
VChild,
VirtualComma,
checkConvertRpyintToInt,
check_different_cpp_and_py,
check_impure_io,
check_pure_io,
convertRpyintToInt,
Expand Down Expand Up @@ -107,6 +110,7 @@

__all__ = [
"Abstract",
"Buffers",
"ClassWithFields",
"ClassWithIgnored",
"ClassWithTrampoline",
Expand All @@ -123,6 +127,7 @@
"GEnum",
"GEnumMath",
"HasFactory",
"HasOperator",
"IBase",
"IChild",
"IFinal",
Expand Down Expand Up @@ -184,6 +189,7 @@
"VChild",
"VirtualComma",
"checkConvertRpyintToInt",
"check_different_cpp_and_py",
"check_impure_io",
"check_pure_io",
"convertRpyintToInt",
Expand Down
54 changes: 54 additions & 0 deletions tests/cpp/rpytest/ft/include/buffers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma once

#include <cstring>
#include <vector>

class Buffers {
public:

// in
void set_buffer(const uint8_t *data, size_t len) {
m_buf.resize(len);
memcpy(m_buf.data(), data, len);
}

// out
// - data is bytes
// - len is input_size and output size
void get_buffer2(uint8_t *data, size_t *len) {
*len = get_buffer1(data, *len);
}

// out
// - data is bytes
// - len is input size
// - return value is output size
size_t get_buffer1(uint8_t *data, size_t len) {
size_t rlen = len < m_buf.size() ? len : m_buf.size();
if (rlen) {
memcpy(data, m_buf.data(), rlen);
}
return rlen;
}

//
// virtual functions -- trampolines are disabled but normal function
// calls work
//

virtual void v_set_buffer(const uint8_t *data, size_t len) {
set_buffer(data, len);
}

virtual void v_get_buffer2(uint8_t *data, size_t *len) {
get_buffer2(data, len);
}

virtual size_t v_get_buffer1(uint8_t *data, size_t len) {
return get_buffer1(data, len);
}

private:

std::vector<uint8_t> m_buf;
};
3 changes: 3 additions & 0 deletions tests/cpp/rpytest/ft/include/ignore.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ struct IgnoredClassWithEnum {

struct ClassWithIgnored {

// constructor with ignored param
ClassWithIgnored(int y) {}

// class function
int fnIgnore() { return 0x2; }

Expand Down
6 changes: 6 additions & 0 deletions tests/cpp/rpytest/ft/include/inline_code.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@

#pragma once

namespace NS::inner {
static constexpr auto KONSTANT = 4;
}

class InlineCode {
public:
enum MyE {
Value1 = 1
};

int get2() const { return 2; }

int cpp_code_with_constant() { return 3; }
};
14 changes: 14 additions & 0 deletions tests/cpp/rpytest/ft/include/operators.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

class HasOperator {
public:
HasOperator() : m_i(0) {}
HasOperator(int i) : m_i(i) {}

bool operator==(const HasOperator &o) const {
return m_i == o.m_i;
}

private:
int m_i;
};
5 changes: 5 additions & 0 deletions tests/cpp/rpytest/ft/include/overloads.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ struct OverloadedObject
return o;
}

// This shows rtnType is inconsistent in CppHeaderParser
const OverloadedObject& overloaded() {
return *this;
}

constexpr int overloaded_constexpr(int a, int b) {
return a + b;
}
Expand Down
9 changes: 9 additions & 0 deletions tests/cpp/rpytest/ft/include/virtual_comma.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@

struct VirtualComma
{
struct RVal {};

virtual ~VirtualComma() {}

virtual std::pair<int, int> getTwoTwo()
{
return std::pair<int, int>{1, 2};
}

// ensures that RVal is recognized as VirtualComma::RVal
virtual RVal getRval() {
return RVal{};
}

int getRval(int) { return 1; }
};
10 changes: 10 additions & 0 deletions tests/cpp/rpytest/ft/include/virtual_xform.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

struct VBase
{
// overridden in yml but doesn't need vcheck
virtual int different_cpp_and_py(int x) {
return x + 1;
}

virtual void pure_io(std::stringstream &ss) = 0;
virtual void impure_io(std::stringstream &ss)
{
Expand Down Expand Up @@ -39,3 +44,8 @@ std::string check_impure_io(VBase *base)
base->impure_io(ss);
return ss.str();
}

int check_different_cpp_and_py(VBase *base, int x)
{
return base->different_cpp_and_py(x);
}
57 changes: 57 additions & 0 deletions tests/test_ft_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,41 @@ def test_good_private_abstract():
assert ft.PrivateAbstract.getPrivateOverride(m) == 0x3


#
# buffers.h
#


def test_buffers():
o = ft.Buffers()
o.set_buffer(b"12345")

b = bytearray(4)
l = o.get_buffer1(b)
assert b == b"1234"
assert l == 4

b = bytearray(4)
l = o.get_buffer2(b)
assert b == b"1234"
assert l == 4


def test_buffers_v():
o = ft.Buffers()
o.v_set_buffer(b"12345")

b = bytearray(4)
l = o.v_get_buffer1(b)
assert b == b"1234"
assert l == 4

b = bytearray(4)
l = o.v_get_buffer2(b)
assert b == b"1234"
assert l == 4


#
# factory.h
#
Expand All @@ -101,6 +136,25 @@ def test_inline_code():
assert o.get4() == 4


def test_cpp_code_with_constant():
o = ft.InlineCode()
assert o.cpp_code_with_constant() == 4


#
# operators.h
#


def test_operators_eq():
o1 = ft.HasOperator(1)
o1a = ft.HasOperator(1)
o2 = ft.HasOperator(2)

assert o1 == o1a
assert not (o1 == o2)


#
# static_only.h
#
Expand Down Expand Up @@ -136,6 +190,9 @@ def test_virtual_xform():
assert base.impure_io() == "py vbase impure + c++ vbase impure"
assert ft.check_impure_io(base) == "c++ vbase impure"

assert base.different_cpp_and_py(1) == 3
assert ft.check_different_cpp_and_py(base, 1) == 2

class PyChild(ft.VBase):
def pure_io(self) -> str:
return "pychild pure"
Expand Down

0 comments on commit b82e4d4

Please sign in to comment.