forked from lix-project/lix
add an impl of Expr::show for ExprInheritFrom that doesn't crash
ExprVar::show() assumes it has a name. dynamic inherits do not
necessarily (ever?) have a name.
Change-Id: If10893188e307431da17f0c1bd0787adc74f7141
This commit is contained in:
parent
e7517419a6
commit
fa98f7bb4e
|
@ -50,6 +50,16 @@ void ExprVar::show(const SymbolTable & symbols, std::ostream & str) const
|
||||||
str << symbols[name];
|
str << symbols[name];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ExprInheritFrom::show(SymbolTable const & symbols, std::ostream & str) const
|
||||||
|
{
|
||||||
|
if (name) {
|
||||||
|
str << symbols[name];
|
||||||
|
} else {
|
||||||
|
// We can't get at the actual dynamic expression from here.
|
||||||
|
str << "(dynamic inherit)";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void ExprSelect::show(const SymbolTable & symbols, std::ostream & str) const
|
void ExprSelect::show(const SymbolTable & symbols, std::ostream & str) const
|
||||||
{
|
{
|
||||||
str << "(";
|
str << "(";
|
||||||
|
|
|
@ -151,7 +151,8 @@ struct ExprInheritFrom : ExprVar
|
||||||
this->fromWith = nullptr;
|
this->fromWith = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env);
|
void show(SymbolTable const & symbols, std::ostream & str) const override;
|
||||||
|
void bindVars(EvalState & es, const std::shared_ptr<const StaticEnv> & env) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ExprSelect : Expr
|
struct ExprSelect : Expr
|
||||||
|
|
32
tests/unit/libexpr/expr-print.cc
Normal file
32
tests/unit/libexpr/expr-print.cc
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
#include <sstream>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include "tests/libexpr.hh"
|
||||||
|
|
||||||
|
#include "nixexpr.hh"
|
||||||
|
|
||||||
|
namespace nix
|
||||||
|
{
|
||||||
|
|
||||||
|
using namespace testing;
|
||||||
|
struct ExprPrintingTests : LibExprTest
|
||||||
|
{
|
||||||
|
void test(Expr const & expr, std::string_view expected)
|
||||||
|
{
|
||||||
|
std::stringstream out;
|
||||||
|
expr.show(state.symbols, out);
|
||||||
|
ASSERT_EQ(out.str(), expected);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_F(ExprPrintingTests, ExprInheritFrom)
|
||||||
|
{
|
||||||
|
// ExprInheritFrom has its own show() impl.
|
||||||
|
// If it uses its parent class's impl it will crash.
|
||||||
|
ExprInheritFrom const eInheritFrom(noPos, 0);
|
||||||
|
test(eInheritFrom, "(dynamic inherit)");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -185,6 +185,7 @@ libexpr_tests_sources = files(
|
||||||
'libexpr/primops.cc',
|
'libexpr/primops.cc',
|
||||||
'libexpr/search-path.cc',
|
'libexpr/search-path.cc',
|
||||||
'libexpr/trivial.cc',
|
'libexpr/trivial.cc',
|
||||||
|
'libexpr/expr-print.cc',
|
||||||
'libexpr/value/context.cc',
|
'libexpr/value/context.cc',
|
||||||
'libexpr/value/print.cc',
|
'libexpr/value/print.cc',
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue