Skip to content

Commit

Permalink
Move tests to the namespace of the code under test (#3244)
Browse files Browse the repository at this point in the history
Rationale: this convention avoids forcing closely-related code to be far
apart in the namespace hierarchy, and vice versa. By the same token, it
makes the namespace hierarchy more consistent with the directory
hierarchy.
  • Loading branch information
geoffromer authored Sep 18, 2023
1 parent d6f689b commit e3d3122
Show file tree
Hide file tree
Showing 28 changed files with 155 additions and 162 deletions.
4 changes: 2 additions & 2 deletions common/check_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include <gtest/gtest.h>

namespace Carbon::Testing {
namespace Carbon {
namespace {

TEST(CheckTest, CheckTrue) { CARBON_CHECK(true); }
Expand Down Expand Up @@ -59,4 +59,4 @@ TEST(ErrorTest, FatalNoReturnRequired) {
}

} // namespace
} // namespace Carbon::Testing
} // namespace Carbon
175 changes: 81 additions & 94 deletions common/command_line_test.cpp

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions common/enum_base_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "testing/base/test_raw_ostream.h"

namespace Carbon::Testing {
namespace Carbon {
namespace {

// These are directly in the Carbon namespace because the defines require it.
Expand Down Expand Up @@ -45,7 +45,7 @@ TEST(EnumBaseTest, NamesAndConstants) {
}

TEST(EnumBaseTest, Printing) {
TestRawOstream stream;
Testing::TestRawOstream stream;

TestKind kind = TestKind::Beep;
stream << kind << " " << TestKind::Beep;
Expand Down Expand Up @@ -109,4 +109,4 @@ TEST(EnumBaseTest, IntConversion) {
}

} // namespace
} // namespace Carbon::Testing
} // namespace Carbon
6 changes: 3 additions & 3 deletions common/error_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "testing/base/test_raw_ostream.h"

namespace Carbon::Testing {
namespace Carbon {
namespace {

TEST(ErrorTest, Error) {
Expand Down Expand Up @@ -107,10 +107,10 @@ TEST(ErrorTest, ErrorBuilderOperatorImplicitCast) {

TEST(ErrorTest, StreamError) {
Error result = ErrorBuilder("TestFunc") << "msg";
TestRawOstream result_stream;
Testing::TestRawOstream result_stream;
result_stream << result;
EXPECT_EQ(result_stream.TakeStr(), "TestFunc: msg");
}

} // namespace
} // namespace Carbon::Testing
} // namespace Carbon
4 changes: 2 additions & 2 deletions common/indirect_value_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include <string>

namespace Carbon::Testing {
namespace Carbon {
namespace {

TEST(IndirectValueTest, ConstAccess) {
Expand Down Expand Up @@ -126,4 +126,4 @@ TEST(IndirectValueTest, IncompleteType) {
}

} // namespace
} // namespace Carbon::Testing
} // namespace Carbon
4 changes: 2 additions & 2 deletions common/string_helpers_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
using ::testing::Eq;
using ::testing::Optional;

namespace Carbon::Testing {
namespace Carbon {
namespace {

TEST(UnescapeStringLiteral, Valid) {
Expand Down Expand Up @@ -230,4 +230,4 @@ TEST(ParseBlockStringLiteral, OkMultipleSlashes) {
}

} // namespace
} // namespace Carbon::Testing
} // namespace Carbon
4 changes: 2 additions & 2 deletions docs/project/cpp_style_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ these.
namespaces. `static` minimizes the context necessary to notice the internal
linkage of a definition.
- Anonymous namespaces are still necessary for classes and enums.
- Tests are an exception and should typically be wrapped with
`namespace Carbon::Testing { namespace { ... } }` to keep everything
- Tests are an exception and should typically be wrapped in an anonymous
namespace under the namespace of the code under test, to keep everything
internal.
### Copyable and movable types
Expand Down
4 changes: 2 additions & 2 deletions explorer/ast/ast_test_matchers_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "explorer/ast/statement.h"
#include "explorer/base/arena.h"

namespace Carbon::Testing {
namespace Carbon {
namespace {

using ::testing::_;
Expand Down Expand Up @@ -159,4 +159,4 @@ TEST(ASTDeclarationsTest, BasicUsage) {
}

} // namespace
} // namespace Carbon::Testing
} // namespace Carbon
4 changes: 2 additions & 2 deletions explorer/ast/element_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "explorer/base/arena.h"
#include "llvm/Support/Casting.h"

namespace Carbon::Testing {
namespace Carbon {
namespace {

static auto FakeSourceLoc(int line_num) -> SourceLocation {
Expand Down Expand Up @@ -93,4 +93,4 @@ TEST_F(ElementTest, BaseElementIsNamed) {
EXPECT_FALSE(element.IsNamed("anything"));
}
} // namespace
} // namespace Carbon::Testing
} // namespace Carbon
4 changes: 2 additions & 2 deletions explorer/ast/expression_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "explorer/base/arena.h"
#include "llvm/Support/Casting.h"

namespace Carbon::Testing {
namespace Carbon {
namespace {

using llvm::cast;
Expand Down Expand Up @@ -135,4 +135,4 @@ TEST_F(ExpressionTest, BinaryAsTuple) {
}

} // namespace
} // namespace Carbon::Testing
} // namespace Carbon
4 changes: 2 additions & 2 deletions explorer/ast/pattern_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "explorer/base/arena.h"
#include "llvm/Support/Casting.h"

namespace Carbon::Testing {
namespace Carbon {
namespace {

using llvm::cast;
Expand Down Expand Up @@ -129,4 +129,4 @@ TEST_F(PatternTest, BinaryAsTuplePattern) {
}

} // namespace
} // namespace Carbon::Testing
} // namespace Carbon
6 changes: 3 additions & 3 deletions explorer/base/error_builders_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
#include "explorer/base/source_location.h"
#include "testing/base/test_raw_ostream.h"

namespace Carbon::Testing {
namespace Carbon {
namespace {

TEST(ErrorBuildersTest, ProgramError) {
Error err = ProgramError(SourceLocation("x", 1, FileKind::Main)) << "test";
EXPECT_EQ(err.location(), "x:1");
EXPECT_EQ(err.message(), "test");

TestRawOstream out;
Testing::TestRawOstream out;
out << err;
EXPECT_EQ(out.TakeStr(), "x:1: test");
}

} // namespace
} // namespace Carbon::Testing
} // namespace Carbon
4 changes: 2 additions & 2 deletions explorer/base/set_file_context_raii_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "explorer/base/trace_stream.h"

namespace Carbon::Testing {
namespace Carbon {
namespace {

TEST(SetFileContextRaiiTest, UpdateFileContext) {
Expand All @@ -34,4 +34,4 @@ TEST(SetFileContextRaiiTest, UpdateFileContext) {
}

} // namespace
} // namespace Carbon::Testing
} // namespace Carbon
4 changes: 2 additions & 2 deletions explorer/base/set_program_phase_raii_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "explorer/base/trace_stream.h"

namespace Carbon::Testing {
namespace Carbon {
namespace {

TEST(SetProgramPhaseRaiiTest, Simple) {
Expand All @@ -33,4 +33,4 @@ TEST(SetProgramPhaseRaiiTest, UpdatePhase) {
}

} // namespace
} // namespace Carbon::Testing
} // namespace Carbon
4 changes: 2 additions & 2 deletions explorer/parse_and_execute/parse_and_execute_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>

namespace Carbon::Testing {
namespace Carbon {
namespace {

using ::testing::MatchesRegex;
Expand Down Expand Up @@ -53,4 +53,4 @@ TEST(ParseAndExecuteTest, Recursion) {
}

} // namespace
} // namespace Carbon::Testing
} // namespace Carbon
4 changes: 2 additions & 2 deletions explorer/syntax/parse_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include "explorer/base/arena.h"

namespace Carbon::Testing {
namespace Carbon {
namespace {

static constexpr std::string_view FileContents = R"(
Expand All @@ -31,4 +31,4 @@ TEST(ParseTest, ParseFromString) {
}

} // namespace
} // namespace Carbon::Testing
} // namespace Carbon
4 changes: 2 additions & 2 deletions explorer/syntax/unimplemented_example_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "explorer/syntax/parse.h"
#include "explorer/syntax/parse_test_matchers.h"

namespace Carbon::Testing {
namespace Carbon {
namespace {

using ::testing::ElementsAre;
Expand All @@ -34,4 +34,4 @@ TEST(UnimplementedExampleTest, VerifyPrecedence) {
}

} // namespace
} // namespace Carbon::Testing
} // namespace Carbon
6 changes: 4 additions & 2 deletions toolchain/diagnostics/diagnostic_emitter_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
#include "llvm/ADT/StringRef.h"
#include "toolchain/diagnostics/mocks.h"

namespace Carbon::Testing {
namespace Carbon {
namespace {

using ::Carbon::Testing::IsDiagnostic;

struct FakeDiagnosticLocationTranslator : DiagnosticLocationTranslator<int> {
auto GetLocation(int n) -> DiagnosticLocation override {
return {.line_number = 1, .column_number = n};
Expand Down Expand Up @@ -68,4 +70,4 @@ TEST_F(DiagnosticEmitterTest, EmitNote) {
}

} // namespace
} // namespace Carbon::Testing
} // namespace Carbon
5 changes: 3 additions & 2 deletions toolchain/diagnostics/sorting_diagnostic_consumer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
#include "toolchain/diagnostics/diagnostic_emitter.h"
#include "toolchain/diagnostics/mocks.h"

namespace Carbon::Testing {
namespace Carbon {
namespace {

using ::Carbon::Testing::IsDiagnostic;
using ::testing::InSequence;

CARBON_DIAGNOSTIC(TestDiagnostic, Error, "{0}", llvm::StringRef);
Expand Down Expand Up @@ -61,4 +62,4 @@ TEST(SortedDiagnosticEmitterTest, SortErrors) {
}

} // namespace
} // namespace Carbon::Testing
} // namespace Carbon
7 changes: 5 additions & 2 deletions toolchain/driver/driver_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
#include "testing/base/test_raw_ostream.h"
#include "toolchain/testing/yaml_test_helpers.h"

namespace Carbon::Testing {
namespace Carbon {
namespace {

using ::Carbon::Testing::TestRawOstream;
using ::testing::_;
using ::testing::ContainsRegex;
using ::testing::HasSubstr;
using ::testing::StrEq;

namespace Yaml = ::Carbon::Testing::Yaml;

// Reads a file to string.
// TODO: Extract this to a helper and share it with other tests.
static auto ReadFile(std::filesystem::path path) -> std::string {
Expand Down Expand Up @@ -189,4 +192,4 @@ TEST_F(DriverTest, FileOutput) {
}

} // namespace
} // namespace Carbon::Testing
} // namespace Carbon
5 changes: 2 additions & 3 deletions toolchain/lex/numeric_literal_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
#include "toolchain/diagnostics/diagnostic_emitter.h"
#include "toolchain/lex/test_helpers.h"

namespace Carbon::Testing {
namespace Carbon::Lex {
namespace {

using Lex::NumericLiteral;
using ::testing::_;
using ::testing::Field;
using ::testing::Matcher;
Expand Down Expand Up @@ -328,4 +327,4 @@ TEST_F(NumericLiteralTest, TooManyDigits) {
}

} // namespace
} // namespace Carbon::Testing
} // namespace Carbon::Lex
6 changes: 2 additions & 4 deletions toolchain/lex/string_literal_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
#include "toolchain/diagnostics/diagnostic_emitter.h"
#include "toolchain/lex/test_helpers.h"

namespace Carbon::Testing {
namespace Carbon::Lex {
namespace {

using Lex::StringLiteral;

class StringLiteralTest : public ::testing::Test {
protected:
StringLiteralTest() : error_tracker(ConsoleDiagnosticConsumer()) {}
Expand Down Expand Up @@ -347,4 +345,4 @@ TEST_F(StringLiteralTest, UnicodeTooManyDigits) {
}

} // namespace
} // namespace Carbon::Testing
} // namespace Carbon::Lex
5 changes: 2 additions & 3 deletions toolchain/lex/token_kind_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@

#include "llvm/ADT/StringRef.h"

namespace Carbon::Testing {
namespace Carbon::Lex {
namespace {

using Lex::TokenKind;
using ::testing::MatchesRegex;

// We restrict symbols to punctuation characters that are expected to be widely
Expand Down Expand Up @@ -85,4 +84,4 @@ TEST(TokenKindTest, SymbolsInDescendingLength) {
}

} // namespace
} // namespace Carbon::Testing
} // namespace Carbon::Lex
Loading

0 comments on commit e3d3122

Please sign in to comment.