Files
carbon-lang/testing/file_test/line.h
T
Jon Ross-Perkins 0847532edc Refactor file_test autoupdating into a class. (#3228)
I'm finding the current autoupdate difficult to reason about. What I'm
trying to do here is use the class to make it easier to add helper
functions.

For example, I merge the vector+cursor for stdout/stderr into an object,
passed to helpers together instead of as two parameters.
ShouldAddCheckLine can check against output_file_number_ without passing
that through a couple levels of function calls. In turn,
ShouldAddCheckLine is shared with the end-of-file logic instead of that
having its own comparison from what AddCheckLines does.

Also, I'm trying to get the pre-AUTOUPDATE edits in their own loop,
distinct from the main code. The class means AddRemappedNonCheckLine is
a helper function to share code, instead of a lambda (which I was
thinking would just confuse the flow further).

I'm also changing the input non_check_lines to a single vector to match
stdout/stderr. Because of the AUTOUPDATE + SPLIT lines, we're guaranteed
to have at least one line per file.

I realized file_offset_in_new_lines is redundant with output_line_number
so code now uses the latter (because it's older -- I think one's as good
as the other, otherwise).

Note, this change deliberately does not affect output. I'm only trying
to make it easier to read for the next changes, using the lack of change
in results as a good indicator that this is getting it right.
2023-09-13 23:43:24 +00:00

54 lines
1.5 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
#ifndef CARBON_TESTING_FILE_TEST_LINE_H_
#define CARBON_TESTING_FILE_TEST_LINE_H_
#include "common/ostream.h"
#include "llvm/ADT/StringRef.h"
namespace Carbon::Testing {
// Interface for lines.
class FileTestLineBase : public Printable<FileTestLineBase> {
public:
explicit FileTestLineBase(int file_number, int line_number)
: file_number_(file_number), line_number_(line_number) {}
virtual ~FileTestLineBase() {}
// Prints the autoupdated line.
virtual auto Print(llvm::raw_ostream& out) const -> void = 0;
virtual auto is_blank() const -> bool = 0;
auto file_number() const -> int { return file_number_; }
auto line_number() const -> int { return line_number_; }
private:
int file_number_;
int line_number_;
};
// A line in the original file test.
class FileTestLine : public FileTestLineBase {
public:
explicit FileTestLine(int file_number, int line_number, llvm::StringRef line)
: FileTestLineBase(file_number, line_number), line_(line) {}
auto Print(llvm::raw_ostream& out) const -> void override { out << line_; }
auto is_blank() const -> bool override { return line_.empty(); }
auto indent() const -> llvm::StringRef {
return line_.substr(0, line_.find_first_not_of(" \n"));
}
private:
llvm::StringRef line_;
};
} // namespace Carbon::Testing
#endif // CARBON_TESTING_FILE_TEST_LINE_H_