Build tool
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BXParser.t.cc 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include <initializer_list>
  2. #include <unordered_set>
  3. #include "BXParser.h"
  4. #include "catch.hpp"
  5. static BXVariables parse(const char *str) {
  6. bufio::ISStream stream(str);
  7. BXParser parser(stream);
  8. BXVariables vars;
  9. parser.parse(vars);
  10. return vars;
  11. }
  12. std::string stringify(std::vector<std::string> &vec) {
  13. if (vec.size() == 0) {
  14. return "(empty)";
  15. }
  16. std::string s;
  17. for (auto &val: vec) {
  18. if (s.size() == 0) {
  19. s += '\'' + val + '\'';
  20. } else {
  21. s += " '" + val + '\'';
  22. }
  23. }
  24. return s;
  25. }
  26. static void expecteq(
  27. const char *str,
  28. const std::initializer_list<std::pair<
  29. std::string, std::vector<std::string>>> list) {
  30. INFO("Source: " << str);
  31. bufio::ISStream stream(str);
  32. BXParser parser(stream);
  33. BXVariables vars;
  34. parser.parse(vars);
  35. std::unordered_set<std::string> keyset;
  36. for (auto &expected: list) {
  37. keyset.insert(expected.first);
  38. auto it = vars.find(expected.first);
  39. if (it == vars.end()) {
  40. FAIL("Missing key '" << expected.first << "'!");
  41. }
  42. if (it->second != expected.second) {
  43. INFO("Value: " << stringify(it->second));
  44. FAIL("Value of key '" << expected.first << "' doesn't match!");
  45. }
  46. }
  47. for (auto &pair: vars) {
  48. if (keyset.find(pair.first) == keyset.end()) {
  49. FAIL("Unexpected key '" << pair.first << "'!");
  50. }
  51. }
  52. }
  53. TEST_CASE("BXParser parsing", "[BXParser][parse]") {
  54. expecteq("hello := 10 world := 20", {
  55. { "hello", { "10" } },
  56. { "world", { "20" } },
  57. });
  58. expecteq("foo := hello world bar := 10 20 30 baz := 0", {
  59. { "foo", { "hello", "world" } },
  60. { "bar", { "10", "20", "30" } },
  61. { "baz", { "0" } },
  62. });
  63. }
  64. TEST_CASE("BXParser parsing variables", "[BXParser][parse]") {
  65. expecteq("foo := \"hello world\" bar := $foo", {
  66. { "foo", { "hello world" } },
  67. { "bar", { "hello world" } },
  68. });
  69. expecteq("foo := hello world bar := hey $foo what's up", {
  70. { "foo", { "hello", "world" } },
  71. { "bar", { "hey", "hello", "world", "what's", "up" } },
  72. });
  73. }
  74. TEST_CASE("BXParser append/prepend", "[BXParser][parse]") {
  75. expecteq("foo := 10 foo += 20 foo += 30", {
  76. { "foo", { "10", "20", "30" } },
  77. });
  78. expecteq("foo := 10 foo =+ 20 foo =+ 30", {
  79. { "foo", { "30", "20", "10" } },
  80. });
  81. }
  82. TEST_CASE("BXParser newline separation", "[BXParser][parse]") {
  83. expecteq(
  84. "foo := 10 20\n"
  85. "bar := 30 40\n", {
  86. { "foo", { "10", "20" } },
  87. { "bar", { "30", "40" } },
  88. });
  89. }