mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
TODOs in the test for known issues. I may switch the approach to getting the variable type (on examination, this isn't working quite as well as I'd thought) but for now I think it's okay. I had an earlier approach though that may work better overall -- I'd been thinking this would work better, but as you can see in the null check for type information, I think I missed a key point. Anyways, what'd really been vexing me was `int i, j` which I think I handle passably well now. There's obviously room for improvement, but given I've been going at this for a couple days now, I thought it best to checkpoint where I was. This also includes some related framework changes to fix bumps I was running into. Overall the tool should operate a bit more smoothly with these changes. There are still issues with overlapping replacements, but I think it's primarily with range-based for loops which I just need to take some time to fix.
126 lines
3.2 KiB
C++
126 lines
3.2 KiB
C++
// 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 "migrate_cpp/cpp_refactoring/var_decl.h"
|
|
|
|
#include "migrate_cpp/cpp_refactoring/matcher_test_base.h"
|
|
|
|
namespace Carbon {
|
|
namespace {
|
|
|
|
class VarDeclTest : public MatcherTestBase {
|
|
protected:
|
|
VarDeclTest() : var_decl(replacements, &finder) {}
|
|
|
|
Carbon::VarDecl var_decl;
|
|
};
|
|
|
|
TEST_F(VarDeclTest, Declaration) {
|
|
constexpr char Before[] = "int i;";
|
|
constexpr char After[] = "var i: int;";
|
|
ExpectReplacement(Before, After);
|
|
}
|
|
|
|
TEST_F(VarDeclTest, DeclarationArray) {
|
|
constexpr char Before[] = "int i[4];";
|
|
constexpr char After[] = "var i: int [4];";
|
|
ExpectReplacement(Before, After);
|
|
}
|
|
|
|
TEST_F(VarDeclTest, DeclarationComma) {
|
|
// TODO: Maybe replace the comma with a `;`.
|
|
constexpr char Before[] = "int i, j;";
|
|
constexpr char After[] = "var i: int, var j: int;";
|
|
ExpectReplacement(Before, After);
|
|
}
|
|
|
|
TEST_F(VarDeclTest, Assignment) {
|
|
constexpr char Before[] = "int i = 0;";
|
|
// TODO: Include init.
|
|
constexpr char After[] = "var i: int;";
|
|
ExpectReplacement(Before, After);
|
|
}
|
|
|
|
TEST_F(VarDeclTest, Auto) {
|
|
constexpr char Before[] = "auto i = 0;";
|
|
// TODO: Keep auto.
|
|
constexpr char After[] = "var i: int;";
|
|
ExpectReplacement(Before, After);
|
|
}
|
|
|
|
TEST_F(VarDeclTest, Const) {
|
|
// TODO: Include init, have `const` indicate `let`.
|
|
constexpr char Before[] = "const int i = 0;";
|
|
constexpr char After[] = "var i: const int;";
|
|
ExpectReplacement(Before, After);
|
|
}
|
|
|
|
TEST_F(VarDeclTest, Params) {
|
|
constexpr char Before[] = "auto Foo(int i) -> int;";
|
|
constexpr char After[] = "auto Foo(i: int) -> int;";
|
|
ExpectReplacement(Before, After);
|
|
}
|
|
|
|
TEST_F(VarDeclTest, ParamsDefault) {
|
|
// TODO: Include init.
|
|
constexpr char Before[] = "auto Foo(int i = 0) -> int;";
|
|
constexpr char After[] = "auto Foo(i: int) -> int;";
|
|
ExpectReplacement(Before, After);
|
|
}
|
|
|
|
TEST_F(VarDeclTest, ParamsConst) {
|
|
constexpr char Before[] = "auto Foo(const int i) -> int;";
|
|
constexpr char After[] = "auto Foo(i: const int) -> int;";
|
|
ExpectReplacement(Before, After);
|
|
}
|
|
|
|
TEST_F(VarDeclTest, ParamStruct) {
|
|
// This is to ensure the 'struct' keyword doesn't get added to the call type.
|
|
constexpr char Before[] = R"cpp(
|
|
struct Circle {};
|
|
auto Draw(int times, const Circle& circle) -> bool;
|
|
)cpp";
|
|
constexpr char After[] = R"(
|
|
struct Circle {};
|
|
auto Draw(times: int, circle: const Circle &) -> bool;
|
|
)";
|
|
ExpectReplacement(Before, After);
|
|
}
|
|
|
|
TEST_F(VarDeclTest, Member) {
|
|
// TODO: Handle member variables.
|
|
constexpr char Before[] = R"cpp(
|
|
struct Circle {
|
|
Circle() : x(0), y(0), radius(1) {}
|
|
|
|
int x;
|
|
int y;
|
|
int radius;
|
|
};
|
|
)cpp";
|
|
ExpectReplacement(Before, Before);
|
|
}
|
|
|
|
TEST_F(VarDeclTest, RangeFor) {
|
|
// TODO: Handle range based for loops.
|
|
constexpr char Before[] = R"cpp(
|
|
void Foo() {
|
|
int items[] = {1};
|
|
for (int i : items) {
|
|
}
|
|
}
|
|
)cpp";
|
|
constexpr char After[] = R"(
|
|
void Foo() {
|
|
var items: int [1];
|
|
for (int i var __begin1: int * var __range1: int (&)[1]) {
|
|
}
|
|
}
|
|
)";
|
|
ExpectReplacement(Before, After);
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace Carbon
|