"Fossies" - the Fresh Open Source Software Archive

Member "apt-2.2.4/test/libapt/json_test.cc" (10 Jun 2021, 1675 Bytes) of package /linux/misc/apt-2.2.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #include <config.h>
    2 #include "../../apt-private/private-cachefile.cc"
    3 #include "../../apt-private/private-json-hooks.cc"
    4 #include <gtest/gtest.h>
    5 #include <string>
    6 
    7 TEST(JsonTest, JsonString)
    8 {
    9    std::ostringstream os;
   10 
   11    // Check for escaping backslash and quotation marks, and ensure that we do not change number formatting
   12    JsonWriter(os).value("H al\"l\\o").value(17);
   13 
   14    EXPECT_EQ("\"H al\\u0022l\\u005Co\"17", os.str());
   15 
   16    for (int i = 0; i <= 0x1F; i++)
   17    {
   18       os.str("");
   19 
   20       JsonWriter(os).encodeString(os, std::string("X") + char(i) + "Z");
   21 
   22       std::string exp;
   23       strprintf(exp, "\"X\\u%04XZ\"", i);
   24 
   25       EXPECT_EQ(exp, os.str());
   26    }
   27 }
   28 
   29 TEST(JsonTest, JsonObject)
   30 {
   31    std::ostringstream os;
   32 
   33    JsonWriter(os).beginObject().name("key").value("value").endObject();
   34 
   35    EXPECT_EQ("{\"key\":\"value\"}", os.str());
   36 }
   37 
   38 TEST(JsonTest, JsonArrayAndValues)
   39 {
   40    std::ostringstream os;
   41 
   42    JsonWriter(os).beginArray().value(0).value("value").value(1).value(true).endArray();
   43 
   44    EXPECT_EQ("[0,\"value\",1,true]", os.str());
   45 }
   46 TEST(JsonTest, JsonStackRegression)
   47 {
   48    std::ostringstream os;
   49 
   50    JsonWriter w(os);
   51 
   52    // Nest those things deeply such that we transition states:
   53    //    object -> array -> object; -> array -> object
   54    // Older versions never popped back and got stuck on array state.
   55    w.beginObject();
   56    w.name("a").beginArray().beginObject().endObject().endArray();
   57    w.name("b").beginArray().beginObject().endObject().endArray();
   58    w.endObject();
   59 
   60    EXPECT_EQ("{\"a\":[{}],\"b\":[{}]}", os.str());
   61 }
   62 TEST(JsonTest, JsonNull)
   63 {
   64    std::ostringstream os;
   65 
   66    JsonWriter(os).value(nullptr);
   67 
   68    EXPECT_EQ("null", os.str());
   69 }