Skip to content

Commit

Permalink
DynMathObject: get_name(): return non-empty name in more cases
Browse files Browse the repository at this point in the history
when the equation is syntactically valid but has errors
of other kinds (e.g. undefined variable or function)
  • Loading branch information
AdelKS committed Jul 27, 2024
1 parent ae1c45b commit f51179f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
3 changes: 2 additions & 1 deletion include/zecalculator/math_objects/decl/dyn_math_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ class DynMathObject: public tl::expected<MathObjectsVariant<type>, Error>
requires (std::is_convertible_v<DBL, double> and ...)
tl::expected<double, Error> evaluate(DBL... val) const;

/// @brief returns the name of the object, if it's valid, otherwise empty string
/// @brief returns the name of the object
/// @note returns non-empty string only if this instance contains a syntactically valid equation or object
std::string_view get_name() const;

/// @brief gets the value of the expected (the variant) as a specific alternative
Expand Down
4 changes: 3 additions & 1 deletion include/zecalculator/math_objects/impl/dyn_math_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,9 @@ bool DynMathObject<type>::holds() const
template <parsing::Type type>
std::string_view DynMathObject<type>::get_name() const
{
if (bool(*this))
if (opt_eq_object)
return opt_eq_object->name;
else if (bool(*this))
return std::visit([](const auto& val){ return val.get_name(); }, **this);
else return std::string_view();
}
Expand Down
12 changes: 12 additions & 0 deletions test/function_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,16 @@ int main()

} | std::tuple<FAST_TEST, RPN_TEST>{};

"name of function in error state"_test = []<class StructType>()
{
constexpr parsing::Type type = std::is_same_v<StructType, FAST_TEST> ? parsing::Type::FAST : parsing::Type::RPN;

MathWorld<type> world;

auto& f = world.new_object() = "f( x) = cos(x) * g(X)";

expect(f.get_name() == "f");

} | std::tuple<FAST_TEST, RPN_TEST>{};

}

0 comments on commit f51179f

Please sign in to comment.