Add minimal test for UniformTree

master
Jos van den Oever 2017-08-20 17:15:27 +02:00
parent 9afd214f77
commit 4724f2c62f
11 changed files with 646 additions and 2 deletions

View File

@ -1332,7 +1332,7 @@ void writeRustImplementationObject(QTextStream& r, const Object& o) {
const QString lc(snakeCase(p.name));
r << QString(" %1: %2,\n").arg(lc, rustType(p));
}
if (o.type == ObjectType::List) {
if (o.type != ObjectType::Object) {
r << QString(" list: Vec<%1Item>,\n").arg(o.name);
}
r << "}\n\n";
@ -1348,7 +1348,7 @@ void writeRustImplementationObject(QTextStream& r, const Object& o) {
const QString lc(snakeCase(p.name));
r << QString(" %1: %2,\n").arg(lc, rustTypeInit(p));
}
if (o.type == ObjectType::List) {
if (o.type != ObjectType::Object) {
r << QString(" list: vec![%1Item::default(); 10],\n")
.arg(o.name);
}

View File

@ -62,3 +62,4 @@ endfunction(rust_test)
rust_test(test_object rust_object)
rust_test(test_object_types rust_object_types)
rust_test(test_list rust_list)
rust_test(test_tree rust_tree)

View File

@ -0,0 +1,10 @@
[package]
name = "rust_tree"
version = "1.0.0"
[dependencies]
libc = "*"
[lib]
name = "rust"
crate-type = ["staticlib"]

View File

@ -0,0 +1,47 @@
#![allow(unused_imports)]
#![allow(unused_variables)]
#![allow(dead_code)]
use libc::c_int;
use libc::c_uint;
use types::*;
use interface::*;
#[derive (Default, Clone)]
struct PersonsItem {
user_name: String,
}
pub struct Persons {
emit: PersonsEmitter,
model: PersonsUniformTree,
list: Vec<PersonsItem>,
}
impl PersonsTrait for Persons {
fn create(emit: PersonsEmitter, model: PersonsUniformTree) -> Persons {
Persons {
emit: emit,
model: model,
list: vec![PersonsItem::default(); 10],
}
}
fn emit(&self) -> &PersonsEmitter {
&self.emit
}
fn row_count(&self, row: c_int, parent: usize) -> c_int {
self.list.len() as c_int
}
fn user_name(&self, row: c_int, parent: usize) -> String {
self.list[row as usize].user_name.clone()
}
fn set_user_name(&mut self, row: c_int, parent: usize, v: String) -> bool {
self.list[row as usize].user_name = v;
true
}
fn index(&self, row: c_int, parent: usize) -> usize {
0
}
fn parent(&self, parent: usize) -> QModelIndex {
QModelIndex::create(0, 0)
}
}

View File

@ -0,0 +1,152 @@
/* generated by rust_qt_binding_generator */
#![allow(unknown_lints)]
#![allow(mutex_atomic, needless_pass_by_value)]
#![allow(unused_imports)]
use libc::{c_int, c_uint, c_void};
use types::*;
use std::sync::{Arc, Mutex};
use std::ptr::null;
use implementation::*;
pub struct PersonsQObject {}
#[derive (Clone)]
pub struct PersonsEmitter {
qobject: Arc<Mutex<*const PersonsQObject>>,
new_data_ready: fn(*const PersonsQObject, row: c_int, parent: usize),
}
unsafe impl Send for PersonsEmitter {}
impl PersonsEmitter {
fn clear(&self) {
*self.qobject.lock().unwrap() = null();
}
pub fn new_data_ready(&self, row: c_int, parent: usize) {
let ptr = *self.qobject.lock().unwrap();
if !ptr.is_null() {
(self.new_data_ready)(ptr, row, parent);
}
}
}
pub struct PersonsUniformTree {
qobject: *const PersonsQObject,
begin_reset_model: fn(*const PersonsQObject),
end_reset_model: fn(*const PersonsQObject),
begin_insert_rows: fn(*const PersonsQObject,row: c_int, parent: usize, c_int, c_int),
end_insert_rows: fn(*const PersonsQObject),
begin_remove_rows: fn(*const PersonsQObject,row: c_int, parent: usize, c_int, c_int),
end_remove_rows: fn(*const PersonsQObject),
}
impl PersonsUniformTree {
pub fn begin_reset_model(&self) {
(self.begin_reset_model)(self.qobject);
}
pub fn end_reset_model(&self) {
(self.end_reset_model)(self.qobject);
}
pub fn begin_insert_rows(&self,row: c_int, parent: usize, first: c_int, last: c_int) {
(self.begin_insert_rows)(self.qobject,row, parent, first, last);
}
pub fn end_insert_rows(&self) {
(self.end_insert_rows)(self.qobject);
}
pub fn begin_remove_rows(&self,row: c_int, parent: usize, first: c_int, last: c_int) {
(self.begin_remove_rows)(self.qobject,row, parent, first, last);
}
pub fn end_remove_rows(&self) {
(self.end_remove_rows)(self.qobject);
}
}
pub trait PersonsTrait {
fn create(emit: PersonsEmitter, model: PersonsUniformTree) -> Self;
fn emit(&self) -> &PersonsEmitter;
fn row_count(&self, row: c_int, parent: usize) -> c_int;
fn can_fetch_more(&self, c_int, usize) -> bool { false }
fn fetch_more(&mut self, c_int, usize) {}
fn sort(&mut self, c_int, SortOrder) {}
fn user_name(&self, row: c_int, parent: usize) -> String;
fn set_user_name(&mut self, row: c_int, parent: usize, String) -> bool;
fn index(&self, row: c_int, parent: usize) -> usize;
fn parent(&self, parent: usize) -> QModelIndex;
}
#[no_mangle]
pub extern "C" fn persons_new(qobject: *const PersonsQObject,
new_data_ready: fn(*const PersonsQObject, row: c_int, parent: usize),
begin_reset_model: fn(*const PersonsQObject),
end_reset_model: fn(*const PersonsQObject),
begin_insert_rows: fn(*const PersonsQObject,row: c_int, parent: usize,
c_int,
c_int),
end_insert_rows: fn(*const PersonsQObject),
begin_remove_rows: fn(*const PersonsQObject,row: c_int, parent: usize,
c_int,
c_int),
end_remove_rows: fn(*const PersonsQObject))
-> *mut Persons {
let emit = PersonsEmitter {
qobject: Arc::new(Mutex::new(qobject)),
new_data_ready: new_data_ready,
};
let model = PersonsUniformTree {
qobject: qobject,
begin_reset_model: begin_reset_model,
end_reset_model: end_reset_model,
begin_insert_rows: begin_insert_rows,
end_insert_rows: end_insert_rows,
begin_remove_rows: begin_remove_rows,
end_remove_rows: end_remove_rows,
};
let d = Persons::create(emit, model);
Box::into_raw(Box::new(d))
}
#[no_mangle]
pub unsafe extern "C" fn persons_free(ptr: *mut Persons) {
Box::from_raw(ptr).emit().clear();
}
#[no_mangle]
pub unsafe extern "C" fn persons_row_count(ptr: *const Persons, row: c_int, parent: usize) -> c_int {
(&*ptr).row_count(row, parent)
}
#[no_mangle]
pub unsafe extern "C" fn persons_can_fetch_more(ptr: *const Persons, row: c_int, parent: usize) -> bool {
(&*ptr).can_fetch_more(row, parent)
}
#[no_mangle]
pub unsafe extern "C" fn persons_fetch_more(ptr: *mut Persons, row: c_int, parent: usize) {
(&mut *ptr).fetch_more(row, parent)
}
#[no_mangle]
pub unsafe extern "C" fn persons_sort(ptr: *mut Persons, column: c_int, order: SortOrder) {
(&mut *ptr).sort(column, order)
}
#[no_mangle]
pub unsafe extern "C" fn persons_data_user_name(ptr: *const Persons,
row: c_int, parent: usize,
d: *mut c_void,
set: fn(*mut c_void, QString)) {
let data = (&*ptr).user_name(row, parent);
set(d, QString::from(&data));
}
#[no_mangle]
pub unsafe extern "C" fn persons_set_data_user_name(ptr: *mut Persons, row: c_int, parent: usize, v: QStringIn) -> bool {
(&mut *ptr).set_user_name(row, parent, v.convert())
}
#[no_mangle]
pub unsafe extern "C" fn persons_index(ptr: *const Persons, row: c_int, parent: usize) -> usize {
(&*ptr).index(row, parent)
}
#[no_mangle]
pub unsafe extern "C" fn persons_parent(ptr: *const Persons, parent: usize) -> QModelIndex {
(&*ptr).parent(parent)
}

View File

@ -0,0 +1,5 @@
extern crate libc;
mod types;
pub mod interface;
mod implementation;

View File

@ -0,0 +1,104 @@
/* generated by rust_qt_binding_generator */
#![allow(dead_code)]
use std::slice;
use libc::{c_int, uint8_t, uint16_t};
#[repr(C)]
pub struct COption<T> {
data: T,
some: bool,
}
impl<T> From<Option<T>> for COption<T> where T: Default {
fn from(t: Option<T>) -> COption <T> {
if let Some(v) = t {
COption {
data: v,
some: true
}
} else {
COption {
data: T::default(),
some: false
}
}
}
}
#[repr(C)]
pub struct QString {
data: *const uint8_t,
len: c_int,
}
#[repr(C)]
pub struct QStringIn {
data: *const uint16_t,
len: c_int,
}
impl QStringIn {
pub fn convert(&self) -> String {
let data = unsafe { slice::from_raw_parts(self.data, self.len as usize) };
String::from_utf16_lossy(data)
}
}
impl<'a> From<&'a String> for QString {
fn from(string: &'a String) -> QString {
QString {
len: string.len() as c_int,
data: string.as_ptr(),
}
}
}
#[repr(C)]
pub struct QByteArray {
data: *const uint8_t,
len: c_int,
}
impl QByteArray {
pub fn convert(&self) -> Vec<u8> {
let data = unsafe { slice::from_raw_parts(self.data, self.len as usize) };
Vec::from(data)
}
}
impl<'a> From<&'a Vec<u8>> for QByteArray {
fn from(value: &'a Vec<u8>) -> QByteArray {
QByteArray {
len: value.len() as c_int,
data: value.as_ptr(),
}
}
}
#[repr(C)]
pub struct QModelIndex {
row: c_int,
internal_id: usize,
}
impl QModelIndex {
pub fn invalid() -> QModelIndex {
QModelIndex {
row: -1,
internal_id: 0,
}
}
pub fn create(row: c_int, id: usize) -> QModelIndex {
QModelIndex {
row: row,
internal_id: id,
}
}
}
#[repr(C)]
pub enum SortOrder {
Ascending = 0,
Descending = 1
}

49
tests/test_tree.cpp Normal file
View File

@ -0,0 +1,49 @@
#include "test_list_rust.h"
#include <QTest>
#include <QSignalSpy>
class TestRustTree : public QObject
{
Q_OBJECT
private slots:
void testConstructor();
void testStringGetter();
void testStringSetter();
};
void TestRustTree::testConstructor()
{
Persons persons;
}
void TestRustTree::testStringGetter()
{
Persons persons;
QCOMPARE(persons.rowCount(), 10);
QVariant value = persons.data(persons.index(0,0));
// value should be empty string in default implementation
QVERIFY(value.isValid());
QCOMPARE(value.type(), QVariant::String);
QCOMPARE(value.toString(), QString());
}
void TestRustTree::testStringSetter()
{
// GIVEN
Persons persons;
QSignalSpy spy(&persons, &Persons::dataChanged);
// WHEN
const QModelIndex index(persons.index(0,0));
const bool set = persons.setData(index, "Konqi");
// THEN
QVERIFY(set);
QVERIFY(spy.isValid());
QCOMPARE(spy.count(), 1);
QVariant value = persons.data(persons.index(0,0));
QCOMPARE(value.toString(), QString("Konqi"));
}
QTEST_MAIN(TestRustTree)
#include "test_tree.moc"

23
tests/test_tree.json Normal file
View File

@ -0,0 +1,23 @@
{
"cppFile": "test_tree_rust.cpp",
"rust": {
"dir": "rust_tree",
"interfaceModule": "interface",
"implementationModule": "implementation",
"typesModule": "types"
},
"objects": [{
"name": "Persons",
"type": "UniformTree",
"roles": [{
"name": "userName",
"value": "Qt::DisplayRole",
"type": "QString"
}, {
"name": "userName",
"value": "Qt::EditRole",
"type": "QString",
"write": true
}]
}]
}

216
tests/test_tree_rust.cpp Normal file
View File

@ -0,0 +1,216 @@
/* generated by rust_qt_binding_generator */
#include "test_tree_rust.h"
namespace {
template <typename T>
struct option {
private:
T value;
bool some;
public:
operator QVariant() const {
if (some) {
return QVariant(value);
}
return QVariant();
}
};
struct qbytearray_t {
private:
const char* data;
int len;
public:
qbytearray_t(const QByteArray& v):
data(v.data()),
len(v.size()) {
}
operator QByteArray() const {
return QByteArray(data, len);
}
};
struct qstring_t {
private:
const void* data;
int len;
public:
qstring_t(const QString& v):
data(static_cast<const void*>(v.utf16())),
len(v.size()) {
}
operator QString() const {
return QString::fromUtf8(static_cast<const char*>(data), len);
}
};
struct qmodelindex_t {
int row;
quintptr id;
};
}
typedef void (*qstring_set)(QString*, qstring_t*);
void set_qstring(QString* v, qstring_t* val) {
*v = *val;
}
typedef void (*qbytearray_set)(QByteArray*, qbytearray_t*);
void set_qbytearray(QByteArray* v, qbytearray_t* val) {
*v = *val;
}
extern "C" {
Persons::Private* persons_new(Persons*,
void (*)(const Persons*, int, quintptr),
void (*)(Persons*),
void (*)(Persons*),
void (*)(Persons*, int, quintptr, int, int),
void (*)(Persons*),
void (*)(Persons*, int, quintptr, int, int),
void (*)(Persons*));
void persons_free(Persons::Private*);
};
Persons::Persons(QObject *parent):
QAbstractItemModel(parent),
d(persons_new(this,
[](const Persons* o, int row, quintptr id) {
emit o->newDataReady(o->createIndex(row, 0, id));
},
[](Persons* o) {
o->beginResetModel();
},
[](Persons* o) {
o->endResetModel();
},
[](Persons* o, int row, quintptr id, int first, int last) {
o->beginInsertRows(o->createIndex(row, 0, id), first, last);
},
[](Persons* o) {
o->endInsertRows();
},
[](Persons* o, int row, quintptr id, int first, int last) {
o->beginRemoveRows(o->createIndex(row, 0, id), first, last);
},
[](Persons* o) {
o->endRemoveRows();
}
)) {
connect(this, &Persons::newDataReady, this, [this](const QModelIndex& i) {
fetchMore(i);
}, Qt::QueuedConnection);
}
Persons::~Persons() {
persons_free(d);
}
extern "C" {
void persons_data_user_name(const Persons::Private*, int, quintptr, QString*, qstring_set);
bool persons_set_data_user_name(Persons::Private*, int, quintptr, qstring_t);
void persons_sort(Persons::Private*, int column, Qt::SortOrder order = Qt::AscendingOrder);
int persons_row_count(const Persons::Private*, int, quintptr);
bool persons_can_fetch_more(const Persons::Private*, int, quintptr);
void persons_fetch_more(Persons::Private*, int, quintptr);
quintptr persons_index(const Persons::Private*, int, quintptr);
qmodelindex_t persons_parent(const Persons::Private*, quintptr);
}
int Persons::columnCount(const QModelIndex &) const
{
return 1;
}
bool Persons::hasChildren(const QModelIndex &parent) const
{
return rowCount(parent) > 0;
}
int Persons::rowCount(const QModelIndex &parent) const
{
if (parent.isValid() && parent.column() != 0) {
return 0;
}
return persons_row_count(d, parent.row(), parent.internalId());
}
QModelIndex Persons::index(int row, int column, const QModelIndex &parent) const
{
if (row < 0 || column < 0 || column >= 1) {
return QModelIndex();
}
if (parent.isValid() && parent.column() != 0) {
return QModelIndex();
}
const quintptr id = persons_index(d, parent.row(), parent.internalId());
return createIndex(row, column, id);
}
QModelIndex Persons::parent(const QModelIndex &index) const
{
if (!index.isValid()) {
return QModelIndex();
}
const qmodelindex_t parent = persons_parent(d, index.internalId());
return parent.id ?createIndex(parent.row, 0, parent.id) :QModelIndex();
}
bool Persons::canFetchMore(const QModelIndex &parent) const
{
if (parent.isValid() && parent.column() != 0) {
return false;
}
return persons_can_fetch_more(d, parent.row(), parent.internalId());
}
void Persons::fetchMore(const QModelIndex &parent)
{
persons_fetch_more(d, parent.row(), parent.internalId());
}
void Persons::sort(int column, Qt::SortOrder order)
{
persons_sort(d, column, order);
}
Qt::ItemFlags Persons::flags(const QModelIndex &i) const
{
auto flags = QAbstractItemModel::flags(i);
if (i.column() == 0) {
flags |= Qt::ItemIsEditable;
}
return flags;
}
QVariant Persons::data(const QModelIndex &index, int role) const
{
QVariant v;
QString s;
QByteArray b;
switch (index.column()) {
case 0:
switch (role) {
case Qt::DisplayRole:
persons_data_user_name(d, index.row(), index.internalId(), &s, set_qstring);
if (!s.isNull()) v.setValue<QString>(s);
break;
case Qt::EditRole:
persons_data_user_name(d, index.row(), index.internalId(), &s, set_qstring);
if (!s.isNull()) v.setValue<QString>(s);
break;
}
break;
}
return v;
}
QHash<int, QByteArray> Persons::roleNames() const {
QHash<int, QByteArray> names;
names.insert(Qt::DisplayRole, "userName");
return names;
}
bool Persons::setData(const QModelIndex &index, const QVariant &value, int role)
{
bool set = false;
if (index.column() == 0) {
if (role == Qt::EditRole) {
set = persons_set_data_user_name(d, index.row(), index.internalId(), value.value<QString>());
}
}
if (set) {
emit dataChanged(index, index, QVector<int>() << role);
}
return set;
}

37
tests/test_tree_rust.h Normal file
View File

@ -0,0 +1,37 @@
/* generated by rust_qt_binding_generator */
#ifndef TEST_TREE_RUST_H
#define TEST_TREE_RUST_H
#include <QObject>
#include <QAbstractItemModel>
class Persons : public QAbstractItemModel
{
Q_OBJECT
public:
class Private;
private:
Private * const d;
public:
explicit Persons(QObject *parent = nullptr);
~Persons();
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
QModelIndex parent(const QModelIndex &index) const override;
bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
bool canFetchMore(const QModelIndex &parent) const override;
void fetchMore(const QModelIndex &parent) override;
Qt::ItemFlags flags(const QModelIndex &index) const override;
void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
QHash<int, QByteArray> roleNames() const override;
signals:
// new data is ready to be made available to the model with fetchMore()
void newDataReady(const QModelIndex &parent) const;
signals:
private:
};
#endif // TEST_TREE_RUST_H