<commit_before>#include "vast/util/json.h"

#include "framework/unit.h"

using namespace vast;
using namespace util;

SUITE("util")

TEST("JSON construction/assignment")
{
  CHECK(json{}.which() == json::type::null);
  CHECK(json{nil}.which() == json::type::null);

  CHECK(json{true}.which() == json::type::boolean);
  CHECK(json{false}.which() == json::type::boolean);

  CHECK(json{4.2}.which() == json::type::number);
  CHECK(json{42}.which() == json::type::number);
  CHECK(json{-1337}.which() == json::type::number);

  CHECK(json(std::string{"foo"}).which() == json::type::string);
  CHECK(json("foo").which() == json::type::string);

  CHECK(json{json::array{}}.which() == json::type::array);

  CHECK(json{json::object{}}.which() == json::type::object);

  json j;
  j = nil;
  CHECK(j.which() == json::type::null);

  j = true;
  CHECK(j.which() == json::type::boolean);

  j = 42;
  CHECK(j.which() == json::type::number);

  j = "foo";
  CHECK(j.which() == json::type::string);

  j = json::array{};
  CHECK(j.which() == json::type::array);

  j = json::object{};
  CHECK(j.which() == json::type::object);
}

TEST("JSON printing")
{
  std::string str;
  auto out = std::back_inserter(str);

  CHECK(print(json{}, out));
  CHECK(str == "null");
  str.clear();

  CHECK(print(json{true}, out));
  CHECK(str == "true");
  str.clear();

  CHECK(print(json{false}, out));
  CHECK(str == "false");
  str.clear();

  CHECK(print(json{42}, out));
  CHECK(str == "42");
  str.clear();

  CHECK(print(json{42.0}, out));
  CHECK(str == "42");
  str.clear();

  CHECK(print(json{4.2}, out));
  CHECK(str == "4.2");
  str.clear();

  CHECK(print(json{"foo"}, out));
  CHECK(str == "\"foo\"");
  str.clear();

  std::string foo{"foo"};
  CHECK(print(json{foo}, out));
  CHECK(str == "\"foo\"");
  str.clear();

  json::array a{42, -1337, "foo", nil, true};
  CHECK(print(json{a}, out));
  CHECK(str == "[42, -1337, \"foo\", null, true]");
  str.clear();

  json::object o;
  o["foo"] = 42;
  o["bar"] = nil;

  // We use a std::map, which orders the keys alphabetically.
  CHECK(print(json{std::move(o)}, out));
  CHECK(str == "{\"bar\": null, \"foo\": 42}");
  str.clear();

  o = {{"baz", 4.2}};
  CHECK(print(json{std::move(o)}, out));
  CHECK(str == "{\"baz\": 4.2}");
  str.clear();

  o = {
    {"baz", 4.2},
    {"x", a},
    {"inner", json::object{{"a", false}, {"b", 42}, {"c", a}}}
  };

  auto tree = R"json({
  "baz": 4.2,
  "inner": {
    "a": false,
    "b": 42,
    "c": [
      42,
      -1337,
      "foo",
      null,
      true
    ]
  },
  "x": [
    42,
    -1337,
    "foo",
    null,
    true
  ]
})json";

  std::cout << tree << std::endl;
  CHECK(to_string(o, true) == tree);<commit_msg>Remove debugging output.<commit_after>#include "vast/util/json.h"

#include "framework/unit.h"

using namespace vast;
using namespace util;

SUITE("util")

TEST("JSON construction/assignment")
{
  CHECK(json{}.which() == json::type::null);
  CHECK(json{nil}.which() == json::type::null);

  CHECK(json{true}.which() == json::type::boolean);
  CHECK(json{false}.which() == json::type::boolean);

  CHECK(json{4.2}.which() == json::type::number);
  CHECK(json{42}.which() == json::type::number);
  CHECK(json{-1337}.which() == json::type::number);

  CHECK(json(std::string{"foo"}).which() == json::type::string);
  CHECK(json("foo").which() == json::type::string);

  CHECK(json{json::array{}}.which() == json::type::array);

  CHECK(json{json::object{}}.which() == json::type::object);

  json j;
  j = nil;
  CHECK(j.which() == json::type::null);

  j = true;
  CHECK(j.which() == json::type::boolean);

  j = 42;
  CHECK(j.which() == json::type::number);

  j = "foo";
  CHECK(j.which() == json::type::string);

  j = json::array{};
  CHECK(j.which() == json::type::array);

  j = json::object{};
  CHECK(j.which() == json::type::object);
}

TEST("JSON printing")
{
  std::string str;
  auto out = std::back_inserter(str);

  CHECK(print(json{}, out));
  CHECK(str == "null");
  str.clear();

  CHECK(print(json{true}, out));
  CHECK(str == "true");
  str.clear();

  CHECK(print(json{false}, out));
  CHECK(str == "false");
  str.clear();

  CHECK(print(json{42}, out));
  CHECK(str == "42");
  str.clear();

  CHECK(print(json{42.0}, out));
  CHECK(str == "42");
  str.clear();

  CHECK(print(json{4.2}, out));
  CHECK(str == "4.2");
  str.clear();

  CHECK(print(json{"foo"}, out));
  CHECK(str == "\"foo\"");
  str.clear();

  std::string foo{"foo"};
  CHECK(print(json{foo}, out));
  CHECK(str == "\"foo\"");
  str.clear();

  json::array a{42, -1337, "foo", nil, true};
  CHECK(print(json{a}, out));
  CHECK(str == "[42, -1337, \"foo\", null, true]");
  str.clear();

  json::object o;
  o["foo"] = 42;
  o["bar"] = nil;

  // We use a std::map, which orders the keys alphabetically.
  CHECK(print(json{std::move(o)}, out));
  CHECK(str == "{\"bar\": null, \"foo\": 42}");
  str.clear();

  o = {{"baz", 4.2}};
  CHECK(print(json{std::move(o)}, out));
  CHECK(str == "{\"baz\": 4.2}");
  str.clear();

  o = {
    {"baz", 4.2},
    {"x", a},
    {"inner", json::object{{"a", false}, {"b", 42}, {"c", a}}}
  };

  auto tree = R"json({
  "baz": 4.2,
  "inner": {
    "a": false,
    "b": 42,
    "c": [
      42,
      -1337,
      "foo",
      null,
      true
    ]
  },
  "x": [
    42,
    -1337,
    "foo",
    null,
    true
  ]
})json";

  CHECK(to_string(o, true) == tree);
{'fraction_non_alphanumeric': 0.2694089308875301, 'fraction_numerical': 0.023346303501945526, 'mean_word_length': 3.706190061028771, 'pattern_counts': {'":': 18, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2324660', 'n_tokens_mistral': 2206, 'n_tokens_neox': 2064, 'n_words': 394}