From 0585ecc56b210e045e639c41474cc72c475c8b76 Mon Sep 17 00:00:00 2001
From: Niels Lohmann <mail@nlohmann.me>
Date: Fri, 19 Jun 2020 13:10:35 +0200
Subject: [PATCH] :white_check_mark: add tests for comment skipping

---
 include/nlohmann/detail/input/lexer.hpp |  8 ++++-
 single_include/nlohmann/json.hpp        |  8 ++++-
 test/src/unit-class_parser.cpp          | 45 ++++++++++++++++++++++++-
 3 files changed, 58 insertions(+), 3 deletions(-)

diff --git a/include/nlohmann/detail/input/lexer.hpp b/include/nlohmann/detail/input/lexer.hpp
index d5e243e6..9dba2972 100644
--- a/include/nlohmann/detail/input/lexer.hpp
+++ b/include/nlohmann/detail/input/lexer.hpp
@@ -1507,7 +1507,13 @@ scan_number_done:
                 error_message = "invalid comment";
                 return token_type::parse_error;
             }
-            get();
+
+            // skip following whitespace
+            do
+            {
+                get();
+            }
+            while (current == ' ' or current == '\t' or current == '\n' or current == '\r');
         }
 
         switch (current)
diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp
index 099fdd8e..c341d40d 100644
--- a/single_include/nlohmann/json.hpp
+++ b/single_include/nlohmann/json.hpp
@@ -9574,7 +9574,13 @@ scan_number_done:
                 error_message = "invalid comment";
                 return token_type::parse_error;
             }
-            get();
+
+            // skip following whitespace
+            do
+            {
+                get();
+            }
+            while (current == ' ' or current == '\t' or current == '\n' or current == '\r');
         }
 
         switch (current)
diff --git a/test/src/unit-class_parser.cpp b/test/src/unit-class_parser.cpp
index 1912094a..de5c9638 100644
--- a/test/src/unit-class_parser.cpp
+++ b/test/src/unit-class_parser.cpp
@@ -224,6 +224,7 @@ class SaxCountdown : public nlohmann::json::json_sax_t
 
 json parser_helper(const std::string& s);
 bool accept_helper(const std::string& s);
+void comments_helper(const std::string& s);
 
 json parser_helper(const std::string& s)
 {
@@ -241,6 +242,8 @@ json parser_helper(const std::string& s)
     json::sax_parse(s, &sdp);
     CHECK(j_sax == j);
 
+    comments_helper(s);
+
     return j;
 }
 
@@ -275,11 +278,51 @@ bool accept_helper(const std::string& s)
     // 6. check if this approach came to the same result
     CHECK(ok_noexcept == ok_noexcept_cb);
 
-    // 7. return result
+    // 7. check if comments are properly ignored
+    if (ok_accept)
+    {
+        comments_helper(s);
+    }
+
+    // 8. return result
     return ok_accept;
 }
+
+void comments_helper(const std::string& s)
+{
+    json _;
+
+    // parse/accept with default parser
+    CHECK_NOTHROW(_ = json::parse(s));
+    CHECK(json::accept(s));
+
+    // parse/accept while skipping comments
+    CHECK_NOTHROW(_ = json::parse(s, nullptr, false, true));
+    CHECK(json::accept(s, true));
+
+    std::vector<std::string> json_with_comments;
+
+    // start with a comment
+    json_with_comments.push_back(std::string("// this is a comment\n") + s);
+    json_with_comments.push_back(std::string("/* this is a comment */") + s);
+    // end with a comment
+    json_with_comments.push_back(s + "// this is a comment");
+    json_with_comments.push_back(s + "/* this is a comment */");
+
+    // check all strings
+    for (const auto& json_with_comment : json_with_comments)
+    {
+        CAPTURE(json_with_comment)
+        CHECK_THROWS_AS(_ = json::parse(json_with_comment), json::parse_error);
+        CHECK(not json::accept(json_with_comment));
+
+        CHECK_NOTHROW(_ = json::parse(json_with_comment, nullptr, true, true));
+        CHECK(json::accept(json_with_comment, true));
+    }
 }
 
+} // namespace
+
 TEST_CASE("parser class")
 {
     SECTION("parse")