diff --git a/libwccl/ops/vargetter.h b/libwccl/ops/vargetter.h new file mode 100644 index 0000000000000000000000000000000000000000..b277cc1bcbd052340299dde66243edb61b281558 --- /dev/null +++ b/libwccl/ops/vargetter.h @@ -0,0 +1,58 @@ +#ifndef LIBWCCL_OPS_VARGETTER_H +#define LIBWCCL_OPS_VARGETTER_H + +#include <libwccl/ops/functions.h> + +namespace Wccl { + + +/** + * Operator that returns value of a variable of given type T + */ +template<class T> +class VarGetter : public Function<T> { +public: + VarGetter(const VariableAccesor<T>& var_acc) + : var_acc_(var_acc) + { + } + + /** + * @returns Operator name for variable getter, we assume "$", although real + * string representation depends on given Value's var_repr function + */ + virtual const std::string raw_operator_name() const { + return "$"; + } + + /** + * @returns String representation of the variable getter which is + * equal to string representation of the underlying variable. + */ + virtual std::string to_raw_string() const { + return T::var_repr(var_acc_.get_name()); + } + + /** + * @returns String representation of the variable getter which is + * equal to string representation of the underlying variable. + */ + virtual std::string to_string(const Corpus2::Tagset&) const { + return T::var_repr(var_acc_.get_name()); + } +protected: + /** + * Return value held by the underlying variable. + */ + virtual BaseRetValPtr apply_internal(const FunExecContext& context) const { + return context.variables()->get_fast(var_acc_); + } + +private: + const VariableAccesor<T> var_acc_; +}; + + +} /* end ns Wccl */ + +#endif // LIBWCCL_OPS_VARGETTER_H diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 17bcf426da5f7b7aa833c0f347e07be3c169ee00..3cf261cc6696e48e319d75d9c444587bb6dde238 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -15,6 +15,7 @@ add_executable(tests strsetfunctions.cpp values.cpp varaccess.cpp + vargetter.cpp variables.cpp ) diff --git a/tests/vargetter.cpp b/tests/vargetter.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c2c4a8c0080131d1c710c2e0975ba60f4b0a48c3 --- /dev/null +++ b/tests/vargetter.cpp @@ -0,0 +1,89 @@ +#include <boost/test/unit_test.hpp> +#include <boost/bind.hpp> + +#include <libwccl/ops/vargetter.h> +#include <libwccl/values/bool.h> +#include <libwccl/values/tset.h> +#include <libwccl/values/strset.h> +#include <libwccl/ops/constant.h> + +using namespace Wccl; + +BOOST_AUTO_TEST_SUITE(vargetter) + +struct VarGetFix +{ + VarGetFix() + : sc(boost::make_shared<Corpus2::Sentence>()), + cx(sc, boost::make_shared<Variables>()), + tagset(), + true_value(true), + false_value(false), + true_constant(true_value), + false_constant(false_value) + { + cx.variables()->put("True_bool", true_value); + cx.variables()->put("False_bool", false_value); + } + SentenceContext sc; + FunExecContext cx; + Corpus2::Tagset tagset; + + Bool true_value; + Bool false_value; + Constant<Bool> true_constant; + Constant<Bool> false_constant; +}; + +BOOST_FIXTURE_TEST_CASE(bool_apply, VarGetFix) +{ + VariableAccesor<Bool> acc_t = cx.variables()->create_accesor<Bool>("True_bool"); + VarGetter<Bool> var_getter_t(acc_t); + BOOST_CHECK_EQUAL(true, var_getter_t.apply(cx)->get_value()); + VariableAccesor<Bool> acc_f = cx.variables()->create_accesor<Bool>("False_bool"); + VarGetter<Bool> var_getter_f(acc_f); + BOOST_CHECK_EQUAL(false, var_getter_f.apply(cx)->get_value()); +} + +BOOST_FIXTURE_TEST_CASE(bool_apply_acc_destruct, VarGetFix) +{ + boost::shared_ptr<VarGetter<Bool> > var_getter; + { + VariableAccesor<Bool> acc_t = cx.variables()->create_accesor<Bool>("True_bool"); + var_getter.reset(new VarGetter<Bool>(acc_t)); + } + BOOST_CHECK_EQUAL(true, var_getter->apply(cx)->get_value()); +} + +BOOST_FIXTURE_TEST_CASE(bool_mod, VarGetFix) +{ + VariableAccesor<Bool> acc_t = cx.variables()->create_accesor<Bool>("True_bool"); + VarGetter<Bool> var_getter_t(acc_t); + BOOST_CHECK_EQUAL(true, var_getter_t.apply(cx)->get_value()); + cx.variables()->get<Bool>("True_bool")->set_value(false); + BOOST_CHECK_EQUAL(false, var_getter_t.apply(cx)->get_value()); + cx.variables()->put("True_bool", Bool(true)); + BOOST_CHECK_EQUAL(true, var_getter_t.apply(cx)->get_value()); + cx.variables()->set("True_bool", false_value); + BOOST_CHECK_EQUAL(false, var_getter_t.apply(cx)->get_value()); + cx.variables()->set("True_bool", Bool(true)); + BOOST_CHECK_EQUAL(true, var_getter_t.apply(cx)->get_value()); +} + +//---- To String ----- + +BOOST_FIXTURE_TEST_CASE(bool_varget_to_string, VarGetFix) +{ + VariableAccesor<Bool> acc_t = cx.variables()->create_accesor<Bool>("True_bool"); + VarGetter<Bool> var_getter_t(acc_t); + BOOST_CHECK_EQUAL(Bool::var_repr("True_bool"), var_getter_t.to_string(tagset)); +} + +BOOST_FIXTURE_TEST_CASE(bool_varget_to_raw_string, VarGetFix) +{ + VariableAccesor<Bool> acc_t = cx.variables()->create_accesor<Bool>("True_bool"); + VarGetter<Bool> var_getter_t(acc_t); + BOOST_CHECK_EQUAL(Bool::var_repr("True_bool"), var_getter_t.to_raw_string()); +} + +BOOST_AUTO_TEST_SUITE_END()