Skip to content

Code modernization (empty, for-range, auto) #787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from

Conversation

Nekto89
Copy link
Contributor

@Nekto89 Nekto89 commented Jun 4, 2018

Using empty() increases readability.

Nekto89 added 2 commits June 4, 2018 23:51
Use auto when declaring iterators
Use auto when initializing with a cast to avoid duplicating the type name
@Nekto89 Nekto89 changed the title Use empty() for emptiness checks. Code modernization (empty, for-range, auto) Jun 4, 2018
@BillyGoto
Copy link

BillyGoto commented Jun 4, 2018 via email

@@ -436,4 +436,16 @@ TestResult& checkStringEqual(TestResult& result,
return result;
}

void checkStringEmpty(TestResult& result,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of void, return a reference to result, to match checkStringEqual and allow for operator<< chaining.

@@ -239,6 +245,10 @@ TestResult& checkStringEqual(TestResult& result,
JsonTest::ToJsonString(actual), __FILE__, \
__LINE__, #expected " == " #actual)

/// \brief Asserts that string is empty.
#define JSONTEST_ASSERT_STRING_EMPTY(actual) \
JsonTest::checkStringEmpty(*result_, (actual), __FILE__, __LINE__, #actual);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No semicolon here.
These macros are supposed to fluently take trailing extra info added with operator<< on TestResult&.

fseek(file, 0, SEEK_SET);
JSONCPP_STRING text;
char* buffer = new char[size + 1];
auto* buffer = new char[size + 1];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The text string is sitting right here, willing to manage a memory block of char for us.
We can just use text's buffer space directly and skip buffer.

@@ -583,7 +583,7 @@ bool Reader::decodeNumber(Token& token, Value& decoded) {
Char c = *current++;
if (c < '0' || c > '9')
return decodeDouble(token, decoded);
Value::UInt digit(static_cast<Value::UInt>(c - '0'));
auto digit(static_cast<Value::UInt>(c - '0'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be better for me with an = initializer.
auto digit = ...

@@ -1579,7 +1576,7 @@ bool OurReader::decodeNumber(Token& token, Value& decoded) {
Char c = *current++;
if (c < '0' || c > '9')
return decodeDouble(token, decoded);
Value::UInt digit(static_cast<Value::UInt>(c - '0'));
auto digit(static_cast<Value::UInt>(c - '0'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auto var = initializer;

itError != errors_.end(); ++itError) {
const ErrorInfo& error = *itError;
Reader::StructuredError structured;
allErrors.reserve(errors_.size());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::vector<Reader::StructuredError> allErrors;
allErrors.reserve(errors_.size());
std::transform(errors_.begin(), errors_.end(), std::back_inserter(allErrors),
               [](const ErrorInfo& e) -> Reader::StructuredError {
                 return {e.token_.start_ - begin_,
                         e.token_.end_ - begin_,
                         e.message_};
               });
return allErrors;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO code without too much std boilerplate might be better
for (const ErrorInfo& e : errors_) {
allErrors.push_back(
{ e.token_.start_ - begin_, e.token_.end_ - begin_, e.message_ });
}

itError != errors_.end(); ++itError) {
const ErrorInfo& error = *itError;
OurReader::StructuredError structured;
allErrors.reserve(errors_.size());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as Reader:: code above.

@@ -107,7 +107,7 @@ static inline char* duplicateStringValue(const char* value, size_t length) {
if (length >= static_cast<size_t>(Value::maxInt))
length = Value::maxInt - 1;

char* newString = static_cast<char*>(malloc(length + 1));
auto* newString = static_cast<char*>(malloc(length + 1));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not much value here.

@@ -275,13 +273,12 @@ bool Runner::runAllTest(bool printSummary) const {
}
return true;
} else {
for (unsigned int index = 0; index < failures.size(); ++index) {
TestResult& result = failures[index];
for (const TestResult& result : failures) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're adding constness here, and printFailure sounds like it might be or soon become non-const. let's leave off the const.

Nekto89 added 2 commits June 5, 2018 23:17
Rewrite readInputTestFile
Small changes from pull request review
@cdunn2001
Copy link
Contributor

I'll let @BillyDonahue handle this.

And we probably cannot merge until TravisCI is fixed. (They seem to have dropped gcc-4.9. Not sure yet.)

@cdunn2001
Copy link
Contributor

TravisCI is fixed (with Meson for MSVC). This should be rebased to master. Then, if @BillyDonahue is fine with it, this can be merged.

@hjmjohnson
Copy link
Contributor

@Nekto89 Can you please rebase this work on top of the current master branch and address the issues. I'm making a push to remove the backlog of PR issues.

If you can no longer work on this PR, just let me know and I'll close the PR.

@hjmjohnson
Copy link
Contributor

@Nekto89 FYI: Superceeded by #865

@hjmjohnson hjmjohnson closed this Jan 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants