Skip to content

Commit

Permalink
libnixf: fix string attr & add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc committed Jan 23, 2024
1 parent 4d6abe9 commit 345f48d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
3 changes: 1 addition & 2 deletions libnixf/src/Parse/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,6 @@ class Parser {
return std::make_shared<AttrName>(std::move(ID), Tok.range());
}
case tok_dquote: {
consume();
std::shared_ptr<ExprString> String = parseString(/*IsIndented=*/false);
return std::make_shared<AttrName>(std::move(String), Tok.range());
}
Expand Down Expand Up @@ -447,7 +446,7 @@ class Parser {
// expected "{" for attrset
assert(LastToken && "LastToken should be set after valid rec");
Diagnostic &D = Diags.emplace_back(Diagnostic::DK_Expected,
RangeTy(LastToken->range()));
RangeTy(LastToken->end()));
D << std::string(tok::spelling(tok_l_curly));
D.fix("insert {").edit(TextEdit::mkInsertion(LastToken->end(), "{"));
}
Expand Down
51 changes: 51 additions & 0 deletions libnixf/test/Parse/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,4 +495,55 @@ TEST(Parser, AttrsOrID) {
ASSERT_EQ(D.kind(), Diagnostic::DK_OrIdentifier);
}

TEST(Parser, AttrsOKSpecialAttr) {
auto Src = R"({ a.b."foo".${"bar"} = 1; })"sv;

std::vector<Diagnostic> Diags;
auto AST = nixf::parse(Src, Diags);

ASSERT_TRUE(AST);
ASSERT_EQ(AST->kind(), Node::NK_ExprAttrs);
ASSERT_TRUE(AST->range().begin().isAt(0, 0, 0));
ASSERT_TRUE(AST->range().end().isAt(0, 27, 27));

ASSERT_EQ(Diags.size(), 0);
}

TEST(Parser, AttrsExtraDot) {
auto Src = R"({ a.b. = 1; })"sv;

std::vector<Diagnostic> Diags;
auto AST = nixf::parse(Src, Diags);

ASSERT_TRUE(AST);
ASSERT_EQ(AST->kind(), Node::NK_ExprAttrs);
ASSERT_TRUE(AST->range().begin().isAt(0, 0, 0));
ASSERT_TRUE(AST->range().end().isAt(0, 13, 13));

ASSERT_EQ(Diags.size(), 1);
auto &D = Diags[0];
ASSERT_TRUE(D.range().begin().isAt(0, 5, 5));
ASSERT_TRUE(D.range().end().isAt(0, 6, 6));
ASSERT_EQ(D.kind(), Diagnostic::DK_AttrPathExtraDot);

// Check that the note is correct.
ASSERT_EQ(D.notes().size(), 0);

// Check fix-it hints.
ASSERT_EQ(D.fixes().size(), 2);
ASSERT_EQ(D.fixes()[0].edits().size(), 1);
ASSERT_EQ(D.fixes()[0].message(), "remove extra .");
const auto &F = D.fixes()[0].edits()[0];
ASSERT_TRUE(F.oldRange().begin().isAt(0, 5, 5));
ASSERT_TRUE(F.oldRange().end().isAt(0, 6, 6));
ASSERT_EQ(F.newText(), "");

ASSERT_EQ(D.fixes()[1].edits().size(), 1);
ASSERT_EQ(D.fixes()[1].message(), "insert dummy attrname");
const auto &F2 = D.fixes()[1].edits()[0];
ASSERT_TRUE(F2.oldRange().begin().isAt(0, 6, 6));
ASSERT_TRUE(F2.oldRange().end().isAt(0, 6, 6));
ASSERT_EQ(F2.newText(), "\"dummy\"");
}

} // namespace

0 comments on commit 345f48d

Please sign in to comment.