From 75d54069438989cd2fdd54c42bf72f3a8eb10ed3 Mon Sep 17 00:00:00 2001 From: Jos van den Oever Date: Wed, 16 May 2018 22:38:30 +0200 Subject: [PATCH] Add more tests --- demo/src/Bindings.cpp | 8 ++--- src/cpp.cpp | 22 ++++++++------ src/rust.cpp | 12 ++++---- templates/qt_quick/rust/src/interface.rs | 6 ++-- templates/qt_widgets/rust/src/interface.rs | 6 ++-- tests/rust_functions/src/implementation.rs | 9 ++++++ tests/rust_functions/src/interface.rs | 35 ++++++++++++++++++---- tests/test_functions.cpp | 24 +++++++++++++++ tests/test_functions.json | 26 +++++++++++++++- tests/test_functions_rust.cpp | 18 +++++++++-- tests/test_functions_rust.h | 4 ++- tests/test_list_rust.cpp | 4 +-- tests/test_list_types_rust.cpp | 4 +-- 13 files changed, 140 insertions(+), 38 deletions(-) diff --git a/demo/src/Bindings.cpp b/demo/src/Bindings.cpp index 7d64851..3ddb32c 100644 --- a/demo/src/Bindings.cpp +++ b/demo/src/Bindings.cpp @@ -144,12 +144,12 @@ int FibonacciList::rowCount(const QModelIndex &parent) const return (parent.isValid()) ? 0 : fibonacci_list_row_count(m_d); } -bool FibonacciList::insertRows(int row, int count, const QModelIndex &parent) +bool FibonacciList::insertRows(int row, int count, const QModelIndex &) { return fibonacci_list_insert_rows(m_d, row, count); } -bool FibonacciList::removeRows(int row, int count, const QModelIndex &parent) +bool FibonacciList::removeRows(int row, int count, const QModelIndex &) { return fibonacci_list_remove_rows(m_d, row, count); } @@ -721,12 +721,12 @@ int TimeSeries::rowCount(const QModelIndex &parent) const return (parent.isValid()) ? 0 : time_series_row_count(m_d); } -bool TimeSeries::insertRows(int row, int count, const QModelIndex &parent) +bool TimeSeries::insertRows(int row, int count, const QModelIndex &) { return time_series_insert_rows(m_d, row, count); } -bool TimeSeries::removeRows(int row, int count, const QModelIndex &parent) +bool TimeSeries::removeRows(int row, int count, const QModelIndex &) { return time_series_remove_rows(m_d, row, count); } diff --git a/src/cpp.cpp b/src/cpp.cpp index 80a273b..b4349d2 100644 --- a/src/cpp.cpp +++ b/src/cpp.cpp @@ -289,12 +289,12 @@ int %1::rowCount(const QModelIndex &parent) const return (parent.isValid()) ? 0 : %2_row_count(m_d); } -bool %1::insertRows(int row, int count, const QModelIndex &parent) +bool %1::insertRows(int row, int count, const QModelIndex &) { return %2_insert_rows(m_d, row, count); } -bool %1::removeRows(int row, int count, const QModelIndex &parent) +bool %1::removeRows(int row, int count, const QModelIndex &) { return %2_remove_rows(m_d, row, count); } @@ -951,7 +951,6 @@ void writeCppObject(QTextStream& cpp, const Object& o, const Configuration& conf } else if (p.type.name == "QByteArray") { cpp << QString(" %1_set(m_d, v.data(), v.size());").arg(base) << endl; } else { - qWarning() << p.type.cppSetType; cpp << QString(" %1_set(m_d, v);").arg(base) << endl; } cpp << "}" << endl; @@ -961,19 +960,19 @@ void writeCppObject(QTextStream& cpp, const Object& o, const Configuration& conf for (const Function& f: o.functions) { const QString base = QString("%1_%2") .arg(lcname, snakeCase(f.name)); - cpp << QString("%1 %2::%3(") - .arg(f.type.name, o.name, f.name); + cpp << QString("%1 %2::%3(").arg(f.type.name, o.name, f.name); for (auto a = f.args.begin(); a < f.args.end(); a++) { cpp << QString("%1 %2%3").arg(a->type.cppSetType, a->name, a + 1 < f.args.end() ? ", " : ""); } - cpp << QString(")%1\n{\n") - .arg(f.mut ? "" : " const"); + cpp << QString(")%1\n{\n").arg(f.mut ? "" : " const"); QString argList; for (auto a = f.args.begin(); a < f.args.end(); a++) { if (a->type.name == "QString") { - argList.append(QString(", %2%3.utf16(), %2%3.size()").arg(a->name, a + 1 < f.args.end() ? ", " : "")); + argList.append(QString(", %1.utf16(), %1.size()").arg(a->name)); + } else if (a->type.name == "QByteArray") { + argList.append(QString(", %1.data(), %1.size()").arg(a->name)); } else { - argList.append(QString(", %2%3").arg(a->name, a + 1 < f.args.end() ? ", " : "")); + argList.append(QString(", %1").arg(a->name)); } } if (f.type.name == "QString") { @@ -981,6 +980,11 @@ void writeCppObject(QTextStream& cpp, const Object& o, const Configuration& conf cpp << QString(" %1(m_d%2, &s, set_qstring);") .arg(base, argList) << endl; cpp << " return s;" << endl; + } else if (f.type.name == "QByteArray") { + cpp << QString(" %1 s;").arg(f.type.name) << endl; + cpp << QString(" %1(m_d%2, &s, set_qbytearray);") + .arg(base, argList) << endl; + cpp << " return s;" << endl; } else { cpp << QString(" return %1(m_d%2);") .arg(base, argList) << endl; diff --git a/src/rust.cpp b/src/rust.cpp index 34185f7..75b233e 100644 --- a/src/rust.cpp +++ b/src/rust.cpp @@ -157,6 +157,8 @@ pub extern "C" fn %1_%2(ptr: *%3 %4)").arg(lcname, lc, f.mut ? "mut" : "const", r << ", "; if (a->type.name == "QString") { r << QString("%1_str: *const c_ushort, %1_len: c_int").arg(a->name); + } else if (a->type.name == "QByteArray") { + r << QString("%1_str: *const c_char, %1_len: c_int").arg(a->name); } else { r << a->name << ": " << a->type.rustType; } @@ -174,6 +176,7 @@ pub extern "C" fn %1_%2(ptr: *%3 %4)").arg(lcname, lc, f.mut ? "mut" : "const", r << " let mut " << a->name << " = String::new();\n"; r << QString(" set_string_from_utf16(&mut %1, %1_str, %1_len);\n").arg(a->name); } else if (a->type.name == "QByteArray") { + r << QString(" let %1 = unsafe { slice::from_raw_parts(%1_str as *const u8, %1_len as usize) };\n").arg(a->name); } } if (f.mut) { @@ -192,7 +195,7 @@ pub extern "C" fn %1_%2(ptr: *%3 %4)").arg(lcname, lc, f.mut ? "mut" : "const", if (f.type.isComplex()) { r << " let s: *const c_char = r.as_ptr() as (*const c_char);\n"; r << " set(d, s, r.len() as i32);\n"; - } else if (f.type.rustType != "()") { + } else { r << " r\n"; } r << "}\n"; @@ -346,12 +349,9 @@ pub trait %1Trait { const QString lc(snakeCase(f.name)); QString argList; if (f.args.size() > 0) { - argList.append(", "); for (auto a = f.args.begin(); a < f.args.end(); a++) { - argList.append( - QString("%1: %2%3") - .arg(a->name, a->type.rustType, a + 1 < f.args.end() ? ", " : "") - ); + auto t = a->type.name == "QByteArray" ?"&[u8]" :a->type.rustType; + argList.append(QString(", %1: %2").arg(a->name, t)); } } r << QString(" fn %1(&%2self%4) -> %3;\n") diff --git a/templates/qt_quick/rust/src/interface.rs b/templates/qt_quick/rust/src/interface.rs index da94277..1a8e317 100644 --- a/templates/qt_quick/rust/src/interface.rs +++ b/templates/qt_quick/rust/src/interface.rs @@ -1,7 +1,7 @@ /* generated by rust_qt_binding_generator */ #![allow(unknown_lints)] #![allow(mutex_atomic, needless_pass_by_value)] -use libc::{c_char, c_ushort, c_int, c_void, uint8_t, uint16_t}; +use libc::{c_char, c_ushort, c_int}; use std::slice; use std::char::decode_utf16; @@ -73,8 +73,8 @@ pub unsafe extern "C" fn simple_free(ptr: *mut Simple) { #[no_mangle] pub extern "C" fn simple_message_get( ptr: *const Simple, - p: *mut c_void, - set: fn(*mut c_void, *const c_char, c_int), + p: *mut QString, + set: fn(*mut QString, *const c_char, c_int), ) { let o = unsafe { &*ptr }; let v = o.message(); diff --git a/templates/qt_widgets/rust/src/interface.rs b/templates/qt_widgets/rust/src/interface.rs index da94277..1a8e317 100644 --- a/templates/qt_widgets/rust/src/interface.rs +++ b/templates/qt_widgets/rust/src/interface.rs @@ -1,7 +1,7 @@ /* generated by rust_qt_binding_generator */ #![allow(unknown_lints)] #![allow(mutex_atomic, needless_pass_by_value)] -use libc::{c_char, c_ushort, c_int, c_void, uint8_t, uint16_t}; +use libc::{c_char, c_ushort, c_int}; use std::slice; use std::char::decode_utf16; @@ -73,8 +73,8 @@ pub unsafe extern "C" fn simple_free(ptr: *mut Simple) { #[no_mangle] pub extern "C" fn simple_message_get( ptr: *const Simple, - p: *mut c_void, - set: fn(*mut c_void, *const c_char, c_int), + p: *mut QString, + set: fn(*mut QString, *const c_char, c_int), ) { let o = unsafe { &*ptr }; let v = o.message(); diff --git a/tests/rust_functions/src/implementation.rs b/tests/rust_functions/src/implementation.rs index cb1cfcc..043cc48 100644 --- a/tests/rust_functions/src/implementation.rs +++ b/tests/rust_functions/src/implementation.rs @@ -40,5 +40,14 @@ impl PersonTrait for Person { _ => count }) } + + fn quote(&self, prefix: String, suffix: String) -> String { + format!("{}{}{}", prefix, self.user_name, suffix) + } + fn quote_bytes(&self, prefix: &[u8], suffix: &[u8]) -> String { + let prefix = String::from_utf8_lossy(prefix); + let suffix = String::from_utf8_lossy(suffix); + format!("{}{}{}", prefix, self.user_name, suffix) + } } diff --git a/tests/rust_functions/src/interface.rs b/tests/rust_functions/src/interface.rs index f096b9c..364105f 100644 --- a/tests/rust_functions/src/interface.rs +++ b/tests/rust_functions/src/interface.rs @@ -51,7 +51,9 @@ pub trait PersonTrait { fn user_name(&self) -> &str; fn set_user_name(&mut self, value: String); fn double_name(&mut self) -> (); - fn greet(&self, Name: String) -> String; + fn greet(&self, name: String) -> String; + fn quote(&self, prefix: String, suffix: String) -> String; + fn quote_bytes(&self, prefix: &[u8], suffix: &[u8]) -> String; fn vowels_in_name(&self) -> u8; } @@ -97,14 +99,37 @@ pub extern "C" fn person_user_name_set(ptr: *mut Person, v: *const c_ushort, len pub extern "C" fn person_double_name(ptr: *mut Person) -> () { let o = unsafe { &mut *ptr }; let r = o.double_name(); + r } #[no_mangle] -pub extern "C" fn person_greet(ptr: *const Person, Name_str: *const c_ushort, Name_len: c_int, d: *mut QString, set: fn(*mut QString, str: *const c_char, len: c_int)) { - let mut Name = String::new(); - set_string_from_utf16(&mut Name, Name_str, Name_len); +pub extern "C" fn person_greet(ptr: *const Person, name_str: *const c_ushort, name_len: c_int, d: *mut QString, set: fn(*mut QString, str: *const c_char, len: c_int)) { + let mut name = String::new(); + set_string_from_utf16(&mut name, name_str, name_len); let o = unsafe { &*ptr }; - let r = o.greet(Name); + let r = o.greet(name); + let s: *const c_char = r.as_ptr() as (*const c_char); + set(d, s, r.len() as i32); +} + +#[no_mangle] +pub extern "C" fn person_quote(ptr: *const Person, prefix_str: *const c_ushort, prefix_len: c_int, suffix_str: *const c_ushort, suffix_len: c_int, d: *mut QString, set: fn(*mut QString, str: *const c_char, len: c_int)) { + let mut prefix = String::new(); + set_string_from_utf16(&mut prefix, prefix_str, prefix_len); + let mut suffix = String::new(); + set_string_from_utf16(&mut suffix, suffix_str, suffix_len); + let o = unsafe { &*ptr }; + let r = o.quote(prefix, suffix); + let s: *const c_char = r.as_ptr() as (*const c_char); + set(d, s, r.len() as i32); +} + +#[no_mangle] +pub extern "C" fn person_quote_bytes(ptr: *const Person, prefix_str: *const c_char, prefix_len: c_int, suffix_str: *const c_char, suffix_len: c_int, d: *mut QString, set: fn(*mut QString, str: *const c_char, len: c_int)) { + let prefix = unsafe { slice::from_raw_parts(prefix_str as *const u8, prefix_len as usize) }; + let suffix = unsafe { slice::from_raw_parts(suffix_str as *const u8, suffix_len as usize) }; + let o = unsafe { &*ptr }; + let r = o.quote_bytes(prefix, suffix); let s: *const c_char = r.as_ptr() as (*const c_char); set(d, s, r.len() as i32); } diff --git a/tests/test_functions.cpp b/tests/test_functions.cpp index d4a8bab..df6922c 100644 --- a/tests/test_functions.cpp +++ b/tests/test_functions.cpp @@ -30,6 +30,8 @@ private slots: void testStringFunction(); void testSimpleFunction(); void testVoidFunction(); + void testQuoteFunction(); + void testQuoteBytesFunction(); }; void TestRustObject::testConstructor() @@ -59,6 +61,28 @@ void TestRustObject::testVoidFunction() QCOMPARE(person.userName(), QString("KonqiKonqi")); } +void TestRustObject::testQuoteFunction() +{ + // GIVEN + Person person; + person.setUserName("Konqi"); + + // THEN + auto r = person.quote("<<", ">>"); + QCOMPARE(r, QString("<>")); +} + +void TestRustObject::testQuoteBytesFunction() +{ + // GIVEN + Person person; + person.setUserName("Konqi"); + + // THEN + auto r = person.quoteBytes("<<", ">>"); + QCOMPARE(r, QString("<>")); +} + void TestRustObject::testStringFunction() { // GIVEN diff --git a/tests/test_functions.json b/tests/test_functions.json index 7530807..0c6d42e 100644 --- a/tests/test_functions.json +++ b/tests/test_functions.json @@ -20,7 +20,7 @@ "mut": false, "arguments": [ { - "name": "Name", + "name": "name", "type": "QString" } ] @@ -33,6 +33,30 @@ "vowelsInName": { "return": "quint8", "arguments": [] + }, + "quote": { + "return": "QString", + "arguments": [ + { + "name": "prefix", + "type": "QString" + }, { + "name": "suffix", + "type": "QString" + } + ] + }, + "quoteBytes": { + "return": "QString", + "arguments": [ + { + "name": "prefix", + "type": "QByteArray" + }, { + "name": "suffix", + "type": "QByteArray" + } + ] } } } diff --git a/tests/test_functions_rust.cpp b/tests/test_functions_rust.cpp index 182da35..0fd3fe5 100644 --- a/tests/test_functions_rust.cpp +++ b/tests/test_functions_rust.cpp @@ -19,6 +19,8 @@ extern "C" { void person_user_name_set(Person::Private*, const ushort *str, int len); void person_double_name(Person::Private*); void person_greet(const Person::Private*, const ushort*, int, QString*, qstring_set); + void person_quote(const Person::Private*, const ushort*, int, const ushort*, int, QString*, qstring_set); + void person_quote_bytes(const Person::Private*, const char*, int, const char*, int, QString*, qstring_set); quint8 person_vowels_in_name(const Person::Private*); }; @@ -55,10 +57,22 @@ void Person::doubleName() { return person_double_name(m_d); } -QString Person::greet(const QString& Name) const +QString Person::greet(const QString& name) const { QString s; - person_greet(m_d, Name.utf16(), Name.size(), &s, set_qstring); + person_greet(m_d, name.utf16(), name.size(), &s, set_qstring); + return s; +} +QString Person::quote(const QString& prefix, const QString& suffix) const +{ + QString s; + person_quote(m_d, prefix.utf16(), prefix.size(), suffix.utf16(), suffix.size(), &s, set_qstring); + return s; +} +QString Person::quoteBytes(const QByteArray& prefix, const QByteArray& suffix) const +{ + QString s; + person_quote_bytes(m_d, prefix.data(), prefix.size(), suffix.data(), suffix.size(), &s, set_qstring); return s; } quint8 Person::vowelsInName() const diff --git a/tests/test_functions_rust.h b/tests/test_functions_rust.h index 9f1ee5a..f5a82e6 100644 --- a/tests/test_functions_rust.h +++ b/tests/test_functions_rust.h @@ -23,7 +23,9 @@ public: QString userName() const; void setUserName(const QString& v); Q_INVOKABLE void doubleName(); - Q_INVOKABLE QString greet(const QString& Name) const; + Q_INVOKABLE QString greet(const QString& name) const; + Q_INVOKABLE QString quote(const QString& prefix, const QString& suffix) const; + Q_INVOKABLE QString quoteBytes(const QByteArray& prefix, const QByteArray& suffix) const; Q_INVOKABLE quint8 vowelsInName() const; signals: void userNameChanged(); diff --git a/tests/test_list_rust.cpp b/tests/test_list_rust.cpp index a845be7..bb9491f 100644 --- a/tests/test_list_rust.cpp +++ b/tests/test_list_rust.cpp @@ -55,12 +55,12 @@ int Persons::rowCount(const QModelIndex &parent) const return (parent.isValid()) ? 0 : persons_row_count(m_d); } -bool Persons::insertRows(int row, int count, const QModelIndex &parent) +bool Persons::insertRows(int row, int count, const QModelIndex &) { return persons_insert_rows(m_d, row, count); } -bool Persons::removeRows(int row, int count, const QModelIndex &parent) +bool Persons::removeRows(int row, int count, const QModelIndex &) { return persons_remove_rows(m_d, row, count); } diff --git a/tests/test_list_types_rust.cpp b/tests/test_list_types_rust.cpp index 858609f..2290bdc 100644 --- a/tests/test_list_types_rust.cpp +++ b/tests/test_list_types_rust.cpp @@ -111,12 +111,12 @@ int List::rowCount(const QModelIndex &parent) const return (parent.isValid()) ? 0 : list_row_count(m_d); } -bool List::insertRows(int row, int count, const QModelIndex &parent) +bool List::insertRows(int row, int count, const QModelIndex &) { return list_insert_rows(m_d, row, count); } -bool List::removeRows(int row, int count, const QModelIndex &parent) +bool List::removeRows(int row, int count, const QModelIndex &) { return list_remove_rows(m_d, row, count); }