Skip to content

Commit

Permalink
Add coverage testing for parse node kinds. (#4436)
Browse files Browse the repository at this point in the history
This refactors the diagnostic kind coverage check into something that
also works for node kinds. Then, since this points out a few node kinds
that aren't having their parse verified, I'm adding minor tests for
those.
  • Loading branch information
jonmeow authored Oct 23, 2024
1 parent 03ddeb5 commit e58ce3e
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 145 deletions.
8 changes: 3 additions & 5 deletions toolchain/diagnostics/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,20 @@ manifest(
)

cc_test(
name = "emitted_diagnostics_test",
name = "coverage_test",
size = "small",
srcs = ["emitted_diagnostics_test.cpp"],
srcs = ["coverage_test.cpp"],
args = ["--testdata_manifest=$(location :all_testdata.txt)"],
data = [
":all_testdata.txt",
"//toolchain/testing:all_testdata",
],
deps = [
":diagnostic_kind",
"//common:set",
"//testing/base:gtest_main",
"//toolchain/testing:coverage_helper",
"@abseil-cpp//absl/flags:flag",
"@googletest//:gtest",
"@llvm-project//llvm:Support",
"@re2",
],
)

Expand Down
73 changes: 73 additions & 0 deletions toolchain/diagnostics/coverage_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <gtest/gtest.h>

#include "absl/flags/flag.h"
#include "toolchain/diagnostics/diagnostic_kind.h"
#include "toolchain/testing/coverage_helper.h"

ABSL_FLAG(std::string, testdata_manifest, "",
"A path to a file containing repo-relative names of test files.");

namespace Carbon {
namespace {

constexpr DiagnosticKind DiagnosticKinds[] = {
#define CARBON_DIAGNOSTIC_KIND(Name) DiagnosticKind::Name,
#include "toolchain/diagnostics/diagnostic_kind.def"
};

constexpr DiagnosticKind UntestedDiagnosticKinds[] = {
// These exist only for unit tests.
DiagnosticKind::TestDiagnostic,
DiagnosticKind::TestDiagnosticNote,

// These diagnose filesystem issues that are hard to unit test.
DiagnosticKind::ErrorReadingFile,
DiagnosticKind::ErrorStattingFile,
DiagnosticKind::FileTooLarge,

// Int literals are currently limited to i32. Once that's fixed, this
// should be tested.
DiagnosticKind::ArrayBoundTooLarge,

// This isn't feasible to test with a normal testcase, but is tested in
// lex/tokenized_buffer_test.cpp.
DiagnosticKind::TooManyTokens,

// TODO: Should look closer at these, but adding tests is a high risk of
// loss in merge conflicts due to the amount of tests being changed right
// now.
DiagnosticKind::ExternLibraryInImporter,
DiagnosticKind::ExternLibraryOnDefinition,
DiagnosticKind::HexadecimalEscapeMissingDigits,
DiagnosticKind::ImplOfUndefinedInterface,
DiagnosticKind::IncompleteTypeInFunctionParam,
DiagnosticKind::InvalidDigit,
DiagnosticKind::InvalidDigitSeparator,
DiagnosticKind::InvalidHorizontalWhitespaceInString,
DiagnosticKind::MismatchedIndentInString,
DiagnosticKind::ModifierPrivateNotAllowed,
DiagnosticKind::MultiLineStringWithDoubleQuotes,
DiagnosticKind::NameAmbiguousDueToExtend,
DiagnosticKind::TooManyDigits,
DiagnosticKind::UnaryOperatorRequiresWhitespace,
DiagnosticKind::UnicodeEscapeSurrogate,
DiagnosticKind::UnicodeEscapeTooLarge,
DiagnosticKind::UnknownBaseSpecifier,
DiagnosticKind::UnsupportedCRLineEnding,
DiagnosticKind::UnsupportedLFCRLineEnding,
};

// Looks for diagnostic kinds that aren't covered by a file_test.
TEST(Coverage, DiagnosticKind) {
Testing::TestKindCoverage(absl::GetFlag(FLAGS_testdata_manifest),
R"(^ *// CHECK:STDERR: .*\.carbon:.* \[(\w+)\]$)",
llvm::ArrayRef(DiagnosticKinds),
llvm::ArrayRef(UntestedDiagnosticKinds));
}

} // namespace
} // namespace Carbon
130 changes: 0 additions & 130 deletions toolchain/diagnostics/emitted_diagnostics_test.cpp

This file was deleted.

24 changes: 24 additions & 0 deletions toolchain/parse/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
load("//bazel/manifest:defs.bzl", "manifest")
load("//testing/fuzzing:rules.bzl", "cc_fuzz_test")

package(default_visibility = ["//visibility:public"])
Expand All @@ -12,6 +13,11 @@ filegroup(
data = glob(["testdata/**/*.carbon"]),
)

manifest(
name = "testdata.txt",
srcs = [":testdata"],
)

cc_library(
name = "node_kind",
srcs = ["node_kind.cpp"],
Expand Down Expand Up @@ -198,3 +204,21 @@ cc_test(
"@googletest//:gtest",
],
)

cc_test(
name = "coverage_test",
size = "small",
srcs = ["coverage_test.cpp"],
args = ["--testdata_manifest=$(location :testdata.txt)"],
data = [
":testdata",
":testdata.txt",
],
deps = [
":node_kind",
"//testing/base:gtest_main",
"//toolchain/testing:coverage_helper",
"@abseil-cpp//absl/flags:flag",
"@googletest//:gtest",
],
)
32 changes: 32 additions & 0 deletions toolchain/parse/coverage_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <gtest/gtest.h>

#include "absl/flags/flag.h"
#include "toolchain/parse/node_kind.h"
#include "toolchain/testing/coverage_helper.h"

ABSL_FLAG(std::string, testdata_manifest, "",
"A path to a file containing repo-relative names of test files.");

namespace Carbon::Parse {
namespace {

constexpr NodeKind NodeKinds[] = {
#define CARBON_PARSE_NODE_KIND(Name) NodeKind::Name,
#include "toolchain/parse/node_kind.def"
};

constexpr NodeKind UntestedNodeKinds[] = {NodeKind::Placeholder};

// Looks for node kinds that aren't covered by a file_test.
TEST(Coverage, NodeKind) {
Testing::TestKindCoverage(absl::GetFlag(FLAGS_testdata_manifest),
R"(kind: '(\w+)')", llvm::ArrayRef(NodeKinds),
llvm::ArrayRef(UntestedNodeKinds));
}

} // namespace
} // namespace Carbon::Parse
43 changes: 33 additions & 10 deletions toolchain/parse/testdata/operators/infix.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,42 @@
// TIP: To dump output, run:
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/parse/testdata/operators/infix.carbon

var n: i8 = n * n;
fn F() {
n * n;
n ^ n;
n >= n;
n >> n;
n / n;
}

// CHECK:STDOUT: - filename: infix.carbon
// CHECK:STDOUT: parse_tree: [
// CHECK:STDOUT: {kind: 'FileStart', text: ''},
// CHECK:STDOUT: {kind: 'VariableIntroducer', text: 'var'},
// CHECK:STDOUT: {kind: 'IdentifierName', text: 'n'},
// CHECK:STDOUT: {kind: 'IntTypeLiteral', text: 'i8'},
// CHECK:STDOUT: {kind: 'BindingPattern', text: ':', subtree_size: 3},
// CHECK:STDOUT: {kind: 'VariableInitializer', text: '='},
// CHECK:STDOUT: {kind: 'IdentifierNameExpr', text: 'n'},
// CHECK:STDOUT: {kind: 'IdentifierNameExpr', text: 'n'},
// CHECK:STDOUT: {kind: 'InfixOperatorStar', text: '*', subtree_size: 3},
// CHECK:STDOUT: {kind: 'VariableDecl', text: ';', subtree_size: 9},
// CHECK:STDOUT: {kind: 'FunctionIntroducer', text: 'fn'},
// CHECK:STDOUT: {kind: 'IdentifierName', text: 'F'},
// CHECK:STDOUT: {kind: 'TuplePatternStart', text: '('},
// CHECK:STDOUT: {kind: 'TuplePattern', text: ')', subtree_size: 2},
// CHECK:STDOUT: {kind: 'FunctionDefinitionStart', text: '{', subtree_size: 5},
// CHECK:STDOUT: {kind: 'IdentifierNameExpr', text: 'n'},
// CHECK:STDOUT: {kind: 'IdentifierNameExpr', text: 'n'},
// CHECK:STDOUT: {kind: 'InfixOperatorStar', text: '*', subtree_size: 3},
// CHECK:STDOUT: {kind: 'ExprStatement', text: ';', subtree_size: 4},
// CHECK:STDOUT: {kind: 'IdentifierNameExpr', text: 'n'},
// CHECK:STDOUT: {kind: 'IdentifierNameExpr', text: 'n'},
// CHECK:STDOUT: {kind: 'InfixOperatorCaret', text: '^', subtree_size: 3},
// CHECK:STDOUT: {kind: 'ExprStatement', text: ';', subtree_size: 4},
// CHECK:STDOUT: {kind: 'IdentifierNameExpr', text: 'n'},
// CHECK:STDOUT: {kind: 'IdentifierNameExpr', text: 'n'},
// CHECK:STDOUT: {kind: 'InfixOperatorGreaterEqual', text: '>=', subtree_size: 3},
// CHECK:STDOUT: {kind: 'ExprStatement', text: ';', subtree_size: 4},
// CHECK:STDOUT: {kind: 'IdentifierNameExpr', text: 'n'},
// CHECK:STDOUT: {kind: 'IdentifierNameExpr', text: 'n'},
// CHECK:STDOUT: {kind: 'InfixOperatorGreaterGreater', text: '>>', subtree_size: 3},
// CHECK:STDOUT: {kind: 'ExprStatement', text: ';', subtree_size: 4},
// CHECK:STDOUT: {kind: 'IdentifierNameExpr', text: 'n'},
// CHECK:STDOUT: {kind: 'IdentifierNameExpr', text: 'n'},
// CHECK:STDOUT: {kind: 'InfixOperatorSlash', text: '/', subtree_size: 3},
// CHECK:STDOUT: {kind: 'ExprStatement', text: ';', subtree_size: 4},
// CHECK:STDOUT: {kind: 'FunctionDefinition', text: '}', subtree_size: 26},
// CHECK:STDOUT: {kind: 'FileEnd', text: ''},
// CHECK:STDOUT: ]
12 changes: 12 additions & 0 deletions toolchain/testing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ cc_library(
],
)

cc_library(
name = "coverage_helper",
testonly = 1,
hdrs = ["coverage_helper.h"],
deps = [
"//common:set",
"@googletest//:gtest",
"@llvm-project//llvm:Support",
"@re2",
],
)

file_test(
name = "file_test",
size = "small",
Expand Down
Loading

0 comments on commit e58ce3e

Please sign in to comment.