Add a C++ wrapper around TC FFI

This uses CMake to build a simple Rust library (in `src/tc/rust`) that
just re-exports everything from the `taskchampion-lib` crate.

The C++ wrappers then wrap this into C++ objects with proper lifecycle
maintenance, in the `tc` namespace.

The C++ wrappers are incomplete, and missing methods are tagged with
"TODO".  These will be added as needed.
This commit is contained in:
Dustin J. Mitchell 2022-03-12 22:07:42 +00:00 committed by Tomas Babej
parent fd03169314
commit 8c30400af3
32 changed files with 1760 additions and 30 deletions

1
test/.gitignore vendored
View file

@ -34,6 +34,7 @@ variant_partial.t
variant_subtract.t
variant_xor.t
view.t
tc.t
tw-2689.t
json_test

View file

@ -14,9 +14,10 @@ include_directories (${CMAKE_SOURCE_DIR}
${CMAKE_SOURCE_DIR}/src/columns
${CMAKE_SOURCE_DIR}/src/libshared/src
${CMAKE_SOURCE_DIR}/test
${CMAKE_SOURCE_DIR}/rust/lib
${TASK_INCLUDE_DIRS})
set (test_SRCS col.t dom.t eval.t lexer.t t.t tw-2689.t tdb2.t util.t variant_add.t variant_and.t variant_cast.t variant_divide.t variant_equal.t variant_exp.t variant_gt.t variant_gte.t variant_inequal.t variant_lt.t variant_lte.t variant_match.t variant_math.t variant_modulo.t variant_multiply.t variant_nomatch.t variant_not.t variant_or.t variant_partial.t variant_subtract.t variant_xor.t view.t)
set (test_SRCS col.t dom.t eval.t lexer.t t.t tw-2689.t tdb2.t tc.t util.t variant_add.t variant_and.t variant_cast.t variant_divide.t variant_equal.t variant_exp.t variant_gt.t variant_gte.t variant_inequal.t variant_lt.t variant_lte.t variant_match.t variant_math.t variant_modulo.t variant_multiply.t variant_nomatch.t variant_not.t variant_or.t variant_partial.t variant_subtract.t variant_xor.t view.t)
add_custom_target (test ./run_all --verbose
DEPENDS ${test_SRCS} task_executable
@ -27,7 +28,7 @@ add_custom_target (build_tests DEPENDS ${test_SRCS}
foreach (src_FILE ${test_SRCS})
add_executable (${src_FILE} "${src_FILE}.cpp" test.cpp)
target_link_libraries (${src_FILE} task commands columns libshared task commands columns libshared task commands columns libshared ${TASK_LIBRARIES})
target_link_libraries (${src_FILE} task tc commands columns libshared task tc tc-rust commands columns libshared task commands columns libshared ${TASK_LIBRARIES})
endforeach (src_FILE)
configure_file(run_all run_all COPYONLY)

111
test/tc.t.cpp Normal file
View file

@ -0,0 +1,111 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2022, Dustin J. Mitchell
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// https://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////
#include <cmake.h>
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include "test.h"
#include "tc/Replica.h"
#include "tc/WorkingSet.h"
#include "tc/Task.h"
#include "tc/util.h"
////////////////////////////////////////////////////////////////////////////////
int main (int, char**)
{
UnitTest t (21);
// This function contains unit tests for the various bits of the wrappers for
// taskchampion-lib (that is, for `src/tc/*.cpp`).
//// util
{
auto s1 = std::string ("a\0string!");
auto stc = tc::string2tc (s1);
auto s2 = tc::tc2string (stc);
t.is (s1, s2, "round-trip to tc string and back (containing an embedded NUL)");
}
{
auto s1 = std::string ("62123ec9-c443-4f7e-919a-35362a8bef8d");
auto tcuuid = tc::uuid2tc (s1);
auto s2 = tc::tc2uuid (tcuuid);
t.is(s1, s2, "round-trip to TCUuid and back");
}
//// Replica
auto rep = tc::Replica ();
t.pass ("replica constructed");
auto maybe_task = rep.get_task("24478a28-4609-4257-bc19-44ec51391431");
t.notok(maybe_task.has_value(), "task with fixed uuid does not exist");
auto task = rep.new_task (tc::Status::Pending, "a test");
t.pass ("new task constructed");
t.is (task.get_description (), std::string ("a test"), "task description round-trip");
t.is (task.get_status (), tc::Status::Pending, "task status round-trip");
auto uuid = task.get_uuid();
auto maybe_task2 = rep.get_task (uuid);
t.ok(maybe_task2.has_value(), "task lookup by uuid finds task");
t.is ((*maybe_task2).get_description (), std::string ("a test"), "task description round-trip");
rep.rebuild_working_set ();
t.pass ("rebuild_working_set");
auto tasks = rep.all_tasks ();
t.is ((int)tasks.size(), 1, "all_tasks returns one task");
//// Task
task = std::move(tasks[0]);
t.is (task.get_uuid(), uuid, "returned task has correct uuid");
t.is (task.get_status(), tc::Status::Pending, "returned task is pending");
auto map = task.get_taskmap ();
t.is (map["description"], "a test", "task description in taskmap");
t.is (task.get_description(), "a test", "returned task has correct description");
//// WorkingSet
auto ws = rep.working_set ();
t.is (ws.len (), (size_t)1, "WorkingSet::len");
t.is (ws.largest_index (), (size_t)1, "WorkingSet::largest_index");
t.is (ws.by_index (1).value(), uuid, "WorkingSet::by_index");
t.is (ws.by_index (2).has_value(), false, "WorkingSet::by_index for unknown index");
t.is (ws.by_uuid (uuid).value (), (size_t)1, "WorkingSet::by_uuid");
t.is (ws.by_uuid ("3e18a306-e3a8-4a53-a85c-fa7c057759a2").has_value (), false, "WorkingSet::by_uuid for unknown uuid");
return 0;
}
////////////////////////////////////////////////////////////////////////////////