test_registry.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <string>
  4. #include "model/control_registry.h"
  5. static std::string load_file(const char* path) {
  6. FILE* fp = fopen(path, "rb");
  7. if (!fp) { fprintf(stderr, "Cannot open %s\n", path); return {}; }
  8. fseek(fp, 0, SEEK_END);
  9. long sz = ftell(fp);
  10. fseek(fp, 0, SEEK_SET);
  11. std::string buf(sz, '\0');
  12. fread(buf.data(), 1, sz, fp);
  13. fclose(fp);
  14. return buf;
  15. }
  16. int main() {
  17. uidef::ControlRegistry reg;
  18. const char* files[] = {
  19. "assets/controls/abstract-controls.xml",
  20. "assets/controls/winform-controls.xml",
  21. "assets/controls/avalonia-controls.xml",
  22. "assets/controls/webawesome-controls.xml",
  23. };
  24. for (auto f : files) {
  25. std::string data = load_file(f);
  26. if (data.empty()) { fprintf(stderr, "FAIL: cannot load %s\n", f); return 1; }
  27. if (!reg.load_control_set(data)) { fprintf(stderr, "FAIL: parse error %s\n", f); return 1; }
  28. printf("Loaded %s -> %zu controls total\n", f, reg.control_count());
  29. }
  30. reg.resolve_inheritance();
  31. printf("\n=== After inheritance resolution: %zu controls ===\n", reg.control_count());
  32. auto check = [&](const char* name) {
  33. auto* def = reg.find(name);
  34. if (!def) { printf(" NOT FOUND: %s\n", name); return; }
  35. printf(" %s: type=%s cat=%s inherits=%s attrs=%zu events=%zu container=%d\n",
  36. def->name.c_str(), def->type.c_str(), def->category.c_str(),
  37. def->inherits.c_str(), def->attributes.size(), def->events.size(),
  38. def->is_container);
  39. };
  40. check("CommonControl");
  41. check("Label");
  42. check("TextBox");
  43. check("Panel");
  44. check("wa-button");
  45. check("AvaloniaTextBox");
  46. check("ListView");
  47. check("StackPanel");
  48. printf("\n=== Categories ===\n");
  49. for (auto& [cat, names] : reg.by_category()) {
  50. printf(" %s: %zu controls\n", cat.c_str(), names.size());
  51. }
  52. printf("\n=== Structures ===\n");
  53. for (auto& s : reg.structures()) {
  54. printf(" %s (type=%s)\n", s.name.c_str(), s.type.c_str());
  55. }
  56. // Verify inheritance resolution
  57. printf("\n=== Inheritance verification ===\n");
  58. auto* label = reg.find("Label");
  59. if (label) {
  60. bool has_visible = false, has_text = false, has_width = false;
  61. for (auto& a : label->attributes) {
  62. if (a.name == "visible") has_visible = true;
  63. if (a.name == "text") has_text = true;
  64. if (a.name == "width") has_width = true;
  65. }
  66. printf(" Label has 'visible' (from CommonControl): %s\n", has_visible ? "YES" : "NO");
  67. printf(" Label has 'text' (own): %s\n", has_text ? "YES" : "NO");
  68. printf(" Label has 'width' (from CommonControl): %s\n", has_width ? "YES" : "NO");
  69. if (!has_visible || !has_text || !has_width) {
  70. printf(" FAIL: inheritance not resolved correctly!\n");
  71. return 1;
  72. }
  73. }
  74. auto* wa_button = reg.find("wa-button");
  75. if (wa_button) {
  76. bool has_variant = false, has_text = false, has_visible = false;
  77. for (auto& a : wa_button->attributes) {
  78. if (a.name == "variant") has_variant = true;
  79. if (a.name == "text") has_text = true;
  80. if (a.name == "visible") has_visible = true;
  81. }
  82. printf(" wa-button has 'variant' (own): %s\n", has_variant ? "YES" : "NO");
  83. printf(" wa-button has 'text' (from Button): %s\n", has_text ? "YES" : "NO");
  84. printf(" wa-button has 'visible' (from CommonControl): %s\n", has_visible ? "YES" : "NO");
  85. if (!has_variant || !has_text || !has_visible) {
  86. printf(" FAIL: deep inheritance not resolved correctly!\n");
  87. return 1;
  88. }
  89. }
  90. printf("\nSUCCESS\n");
  91. return 0;
  92. }