Skip to content

Commit

Permalink
libnixf: fixup test cases for line-column-offset ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
inclyc committed Jan 14, 2024
1 parent ca187d1 commit 3f18ed1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 29 deletions.
10 changes: 5 additions & 5 deletions libnixf/lib/Parse/test/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ TEST_F(LexerTest, Trivia1) {
Lexer Lexer(Src, Diag);
auto P = Lexer.lex();
ASSERT_EQ(P.kind(), tok_int);
// ASSERT_EQ(P.view(), "3");
ASSERT_EQ(P.view(), "3");
ASSERT_TRUE(Diag.diags().empty());
}

Expand All @@ -72,7 +72,7 @@ TEST_F(LexerTest, TriviaLComment) {
Diag);
auto P = Lexer.lex();
ASSERT_EQ(P.kind(), tok_int);
// ASSERT_EQ(P.view(), "3");
ASSERT_EQ(P.view(), "3");
ASSERT_TRUE(Diag.diags().empty());
}

Expand All @@ -83,7 +83,7 @@ aaa
Lexer Lexer(Src, Diag);
auto P = Lexer.lex();
ASSERT_EQ(P.kind(), tok_eof);
// ASSERT_EQ(P.view(), "");
ASSERT_EQ(P.view(), "");
ASSERT_TRUE(Diag.diags().empty());
}

Expand All @@ -94,7 +94,7 @@ aaa
Lexer Lexer(Src, Diag);
auto P = Lexer.lex();
ASSERT_EQ(P.kind(), tok_eof);
// ASSERT_EQ(P.view(), "");
ASSERT_EQ(P.view(), "");
ASSERT_TRUE(!Diag.diags().empty());

ASSERT_EQ(std::string(Diag.diags()[0]->message()), "unterminated /* comment");
Expand All @@ -106,7 +106,7 @@ TEST_F(LexerTest, FloatLeadingZero) {
Lexer Lexer("00.33", Diag);
auto P = Lexer.lex();
ASSERT_EQ(P.kind(), tok_float);
// ASSERT_EQ(P.view(), "00.33");
ASSERT_EQ(P.view(), "00.33");
ASSERT_FALSE(Diag.diags().empty());
ASSERT_EQ(std::string(Diag.diags()[0]->message()),
"float begins with extra zeros `{}` is nixf extension");
Expand Down
53 changes: 29 additions & 24 deletions libnixf/lib/Parse/test/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "nixf/Basic/Diagnostic.h"
#include "nixf/Basic/DiagnosticEngine.h"
#include "nixf/Basic/Range.h"
#include "nixf/Parse/Nodes.h"
#include "nixf/Parse/Parser.h"

Expand Down Expand Up @@ -29,7 +30,8 @@ TEST(Parser, Float) {
ASSERT_EQ(Expr->kind(), Node::NK_ExprFloat);
ASSERT_EQ(static_cast<ExprFloat *>(Expr.get())->value(), 1.0);
ASSERT_EQ(Diags.diags().size(), 0);
// ASSERT_EQ(Expr->range().view(), Src);
ASSERT_EQ(Expr->range().begin(), Point(0, 0, 0));
ASSERT_EQ(Expr->range().end(), Point(0, 3, 3));
}

TEST(Parser, FloatLeading) {
Expand All @@ -39,13 +41,12 @@ TEST(Parser, FloatLeading) {
ASSERT_TRUE(Expr);
ASSERT_EQ(Expr->kind(), Node::NK_ExprFloat);
ASSERT_EQ(static_cast<ExprFloat *>(Expr.get())->value(), 1.0);
// ASSERT_EQ(Expr->range().view(), Src);

// Check the diagnostic.
ASSERT_EQ(Diags.diags().size(), 1);
auto &D = Diags.diags()[0];
// ASSERT_EQ(D->range().Begin, Src.begin() + 0);
// ASSERT_EQ(D->range().End, Src.begin() + 2);
ASSERT_EQ(Expr->range().begin(), Point(0, 0, 0));
ASSERT_EQ(Expr->range().end(), Point(0, 4, 4));
ASSERT_EQ(D->kind(), Diagnostic::DK_FloatLeadingZero);
ASSERT_EQ(D->args().size(), 1);
ASSERT_EQ(D->args()[0], "01");
Expand All @@ -59,7 +60,8 @@ TEST(Parser, FloatLeading00) {
ASSERT_EQ(Expr->kind(), Node::NK_ExprFloat);
ASSERT_EQ(static_cast<ExprFloat *>(Expr.get())->value(), 0.5);
ASSERT_EQ(Diags.diags().size(), 1);
// ASSERT_EQ(Expr->range().view(), Src);
ASSERT_EQ(Expr->range().begin(), Point(0, 0, 0));
ASSERT_EQ(Expr->range().end(), Point(0, 4, 4));
}

TEST(Parser, StringSimple) {
Expand All @@ -69,10 +71,12 @@ TEST(Parser, StringSimple) {
ASSERT_TRUE(Expr);
ASSERT_EQ(Expr->kind(), Node::NK_ExprString);
auto Parts = static_cast<ExprString *>(Expr.get())->parts();
// ASSERT_EQ(Parts->range().view(), "aaa");
ASSERT_EQ(Parts->range().begin(), Point(0, 1, 1));
ASSERT_EQ(Parts->range().end(), Point(0, 4, 4));
ASSERT_EQ(Parts->fragments().size(), 1);
ASSERT_EQ(Diags.diags().size(), 0);
// ASSERT_EQ(Expr->range().view(), Src);
ASSERT_EQ(Expr->range().begin(), Point(0, 0, 0));
ASSERT_EQ(Expr->range().end(), Point(0, 5, 5));
}

TEST(Parser, StringMissingDQuote) {
Expand All @@ -82,35 +86,34 @@ TEST(Parser, StringMissingDQuote) {
ASSERT_TRUE(Expr);
ASSERT_EQ(Expr->kind(), Node::NK_ExprString);
auto Parts = static_cast<ExprString *>(Expr.get())->parts();
// ASSERT_EQ(Parts->range().view(), "aaa");
ASSERT_EQ(Parts->range().begin(), Point(0, 1, 1));
ASSERT_EQ(Parts->range().end(), Point(0, 4, 4));
ASSERT_EQ(Parts->fragments().size(), 1);

// Check the diagnostic.
ASSERT_EQ(Diags.diags().size(), 1);
auto &D = Diags.diags()[0];
// ASSERT_EQ(D->range().Begin, Src.begin() + 4);
// ASSERT_EQ(D->range().End, Src.begin() + 4);
ASSERT_EQ(Expr->range().begin(), Point(0, 0, 0));
ASSERT_EQ(Expr->range().end(), Point(0, 4, 4));
ASSERT_EQ(D->kind(), Diagnostic::DK_Expected);
ASSERT_EQ(D->args().size(), 1);
ASSERT_EQ(D->args()[0], "\"");

// Check the note.
ASSERT_EQ(D->notes().size(), 1);
auto &N = D->notes()[0];
// ASSERT_EQ(N->range().Begin, Src.begin() + 0);
// ASSERT_EQ(N->range().End, Src.begin() + 1);
ASSERT_EQ(N->range().begin(), Point(0, 0, 0));
ASSERT_EQ(N->range().end(), Point(0, 1, 1));
ASSERT_EQ(N->kind(), Note::NK_ToMachThis);
ASSERT_EQ(N->args().size(), 1);
ASSERT_EQ(N->args()[0], "\"");

// Check fix-it hints.
ASSERT_EQ(D->fixes().size(), 1);
const auto &F = D->fixes()[0];
// ASSERT_EQ(F.oldRange().Begin, Src.begin() + 4);
// ASSERT_EQ(F.oldRange().End, Src.begin() + 4);
ASSERT_EQ(F.oldRange().begin(), Point(0, 4, 4));
ASSERT_EQ(F.oldRange().end(), Point(0, 4, 4));
ASSERT_EQ(F.newText(), "\"");

// ASSERT_EQ(Expr->range().view(), Src);
}

TEST(Parser, StringInterpolation) {
Expand All @@ -120,15 +123,17 @@ TEST(Parser, StringInterpolation) {
ASSERT_TRUE(Expr);
ASSERT_EQ(Expr->kind(), Node::NK_ExprString);
auto Parts = static_cast<ExprString *>(Expr.get())->parts();
// ASSERT_EQ(Parts->range().view(), "aaa ${1} bbb");
ASSERT_EQ(Parts->range().begin(), Point(0, 1, 1));
ASSERT_EQ(Parts->range().end(), Point(0, 13, 13));
ASSERT_EQ(Parts->fragments().size(), 3);

ASSERT_EQ(Parts->fragments()[0].kind(), InterpolablePart::SPK_Escaped);
ASSERT_EQ(Parts->fragments()[1].kind(), InterpolablePart::SPK_Interpolation);
ASSERT_EQ(Parts->fragments()[2].kind(), InterpolablePart::SPK_Escaped);

ASSERT_EQ(Diags.diags().size(), 0);
// ASSERT_EQ(Expr->range().view(), Src);
ASSERT_EQ(Expr->range().begin(), Point(0, 0, 0));
ASSERT_EQ(Expr->range().end(), Point(0, 14, 14));
}

TEST(Parser, IndentedString) {
Expand All @@ -151,26 +156,26 @@ TEST(Parser, IndentedString) {
// Check the diagnostic.
ASSERT_EQ(Diags.diags().size(), 1);
auto &D = Diags.diags()[0];
// ASSERT_EQ(D->range().Begin, Src.begin() + 39);
// ASSERT_EQ(D->range().End, Src.begin() + 39);
ASSERT_EQ(D->range().begin(), Point(7, 3, 39));
ASSERT_EQ(D->range().end(), Point(7, 3, 39));
ASSERT_EQ(D->kind(), Diagnostic::DK_Expected);
ASSERT_EQ(D->args().size(), 1);
ASSERT_EQ(D->args()[0], "''");

// Check the note.
ASSERT_EQ(D->notes().size(), 1);
auto &N = D->notes()[0];
// ASSERT_EQ(N->range().Begin, Src.begin() + 0);
// ASSERT_EQ(N->range().End, Src.begin() + 2);
ASSERT_EQ(N->range().begin(), Point(0, 0, 0));
ASSERT_EQ(N->range().end(), Point(0, 2, 2));
ASSERT_EQ(N->kind(), Note::NK_ToMachThis);
ASSERT_EQ(N->args().size(), 1);
ASSERT_EQ(N->args()[0], "''");

// Check fix-it hints.
ASSERT_EQ(D->fixes().size(), 1);
const auto &F = D->fixes()[0];
// ASSERT_EQ(F.oldRange().Begin, Src.begin() + 39);
// ASSERT_EQ(F.oldRange().End, Src.begin() + 39);
ASSERT_EQ(F.oldRange().begin(), Point(7, 3, 39));
ASSERT_EQ(F.oldRange().end(), Point(7, 3, 39));
ASSERT_EQ(F.newText(), "''");
}

Expand Down

0 comments on commit 3f18ed1

Please sign in to comment.