AuthoringJson.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include <catch2/catch_test_macros.hpp>
  2. #include "WallpaperEngine/Assets/AssetLocator.h"
  3. #include "WallpaperEngine/Data/JSON.h"
  4. #include "WallpaperEngine/Data/Model/Material.h"
  5. #include "WallpaperEngine/Data/Model/Project.h"
  6. #include "WallpaperEngine/Data/Model/Wallpaper.h"
  7. #include "WallpaperEngine/Data/Parsers/MaterialParser.h"
  8. #include "WallpaperEngine/FileSystem/Container.h"
  9. using WallpaperEngine::Data::JSON::JSON;
  10. TEST_CASE ("Asset JSON takes comments and trailing commas and leaves strings alone") {
  11. const auto value = JSON::parseAsset (R"({
  12. // a line comment
  13. "url": "https://example.test/a//b/*c*/",
  14. "escaped": "quote \" // still text",
  15. "slashes": "\\/* not a comment */",
  16. "commas": "[1,] {\"a\":1,}",
  17. /* a block
  18. comment */
  19. "items": [1, /* between */ 2,],
  20. "nested": { "a": [ { "b": 1, }, ], },
  21. })");
  22. CHECK (value["url"] == "https://example.test/a//b/*c*/");
  23. CHECK (value["escaped"] == "quote \" // still text");
  24. CHECK (value["slashes"] == "\\/* not a comment */");
  25. CHECK (value["commas"] == "[1,] {\"a\":1,}");
  26. REQUIRE (value["items"].size () == 2);
  27. CHECK (value["items"][1] == 2);
  28. CHECK (value["nested"]["a"][0]["b"] == 1);
  29. }
  30. TEST_CASE ("A trailing comma followed by a comment is still dropped") {
  31. const auto value = JSON::parseAsset ("{\n \"a\": 1, // the last one\n \"b\": [2, /* end */ ],\n /* x */\n}");
  32. CHECK (value["a"] == 1);
  33. CHECK (value["b"].size () == 1);
  34. }
  35. TEST_CASE ("Broken asset JSON still fails with the position") {
  36. try {
  37. (void)JSON::parseAsset ("{\n \"items\": [1,,]\n}");
  38. FAIL ("a doubled comma parsed");
  39. } catch (const nlohmann::json::parse_error& e) {
  40. CHECK (std::string (e.what ()).find ("line 2") != std::string::npos);
  41. }
  42. CHECK_THROWS (JSON::parseAsset ("{\n /* never closed"));
  43. CHECK_THROWS (JSON::parseAsset ("{\"value\": 1e10000}"));
  44. CHECK_THROWS (JSON::parseAsset ("{\"unterminated\": \"text}"));
  45. }
  46. namespace {
  47. WallpaperEngine::Data::Model::Project projectWith (const std::string& file, const std::string& content) {
  48. auto container = std::make_unique<WallpaperEngine::FileSystem::Container> ();
  49. container->getVFS ().add (file, content);
  50. WallpaperEngine::Data::Model::Project project {};
  51. project.assetLocator = std::make_unique<WallpaperEngine::Assets::AssetLocator> (std::move (container));
  52. return project;
  53. }
  54. } // namespace
  55. TEST_CASE ("Materials load from asset JSON, a broken one becomes WE's error pass") {
  56. using WallpaperEngine::Data::Parsers::MaterialParser;
  57. const auto project = projectWith ("materials/test.json", R"({
  58. "passes": [ { "shader": "genericimage2", "combos": { "TEST": 2, }, }, ],
  59. })");
  60. const auto material = MaterialParser::load (project, "materials/test.json");
  61. REQUIRE (material->passes.size () == 1);
  62. CHECK (material->passes[0]->shader == "genericimage2");
  63. CHECK (material->passes[0]->combos.at ("TEST") == 2);
  64. // sub_1401515B0: an unparsable material becomes one "error" shader pass
  65. const auto broken = projectWith ("materials/broken.json", "{\n \"passes\": [1,,]\n}");
  66. const auto fallback = MaterialParser::load (broken, "materials/broken.json");
  67. REQUIRE (fallback->passes.size () == 1);
  68. CHECK (fallback->passes[0]->shader == "error");
  69. }