XmlRpcRequestParserControllerTest.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include "XmlRpcRequestParserController.h"
  2. #include <cppunit/extensions/HelperMacros.h>
  3. namespace aria2 {
  4. namespace rpc {
  5. class XmlRpcRequestParserControllerTest : public CppUnit::TestFixture {
  6. CPPUNIT_TEST_SUITE(XmlRpcRequestParserControllerTest);
  7. CPPUNIT_TEST(testPopStructFrame);
  8. CPPUNIT_TEST(testPopStructFrame_noName);
  9. CPPUNIT_TEST(testPopStructFrame_noValue);
  10. CPPUNIT_TEST(testPopArrayFrame);
  11. CPPUNIT_TEST(testPopArrayFrame_noValue);
  12. CPPUNIT_TEST(testPopArrayFrame_compound);
  13. CPPUNIT_TEST_SUITE_END();
  14. public:
  15. void setUp() {}
  16. void tearDown() {}
  17. void testPopStructFrame();
  18. void testPopStructFrame_noName();
  19. void testPopStructFrame_noValue();
  20. void testPopArrayFrame();
  21. void testPopArrayFrame_noValue();
  22. void testPopArrayFrame_compound();
  23. };
  24. CPPUNIT_TEST_SUITE_REGISTRATION(XmlRpcRequestParserControllerTest);
  25. void XmlRpcRequestParserControllerTest::testPopStructFrame()
  26. {
  27. XmlRpcRequestParserController controller;
  28. controller.setCurrentFrameValue(Dict::g());
  29. controller.pushFrame();
  30. controller.setCurrentFrameValue(String::g("Hello, aria2"));
  31. controller.setCurrentFrameName("greeting");
  32. controller.popStructFrame();
  33. const Dict* structValue = downcast<Dict>(controller.getCurrentFrameValue());
  34. CPPUNIT_ASSERT_EQUAL((size_t)1, structValue->size());
  35. CPPUNIT_ASSERT_EQUAL(std::string("Hello, aria2"),
  36. downcast<String>(structValue->get("greeting"))->s());
  37. }
  38. void XmlRpcRequestParserControllerTest::testPopStructFrame_noName()
  39. {
  40. XmlRpcRequestParserController controller;
  41. controller.setCurrentFrameValue(Dict::g());
  42. controller.pushFrame();
  43. controller.setCurrentFrameValue(String::g("Hello, aria2"));
  44. controller.popStructFrame();
  45. const Dict* structValue = downcast<Dict>(controller.getCurrentFrameValue());
  46. CPPUNIT_ASSERT(structValue->empty());
  47. }
  48. void XmlRpcRequestParserControllerTest::testPopStructFrame_noValue()
  49. {
  50. XmlRpcRequestParserController controller;
  51. controller.setCurrentFrameValue(Dict::g());
  52. controller.pushFrame();
  53. controller.setCurrentFrameName("greeting");
  54. controller.popStructFrame();
  55. const Dict* structValue = downcast<Dict>(controller.getCurrentFrameValue());
  56. CPPUNIT_ASSERT(structValue->empty());
  57. }
  58. void XmlRpcRequestParserControllerTest::testPopArrayFrame()
  59. {
  60. XmlRpcRequestParserController controller;
  61. controller.setCurrentFrameValue(List::g());
  62. controller.pushFrame();
  63. controller.setCurrentFrameValue(Integer::g(100));
  64. controller.popArrayFrame();
  65. const List* array = downcast<List>(controller.getCurrentFrameValue());
  66. CPPUNIT_ASSERT_EQUAL((size_t)1, array->size());
  67. CPPUNIT_ASSERT_EQUAL((Integer::ValueType)100,
  68. downcast<Integer>(array->get(0))->i());
  69. }
  70. void XmlRpcRequestParserControllerTest::testPopArrayFrame_noValue()
  71. {
  72. XmlRpcRequestParserController controller;
  73. controller.setCurrentFrameValue(List::g());
  74. controller.pushFrame();
  75. controller.popArrayFrame();
  76. const List* array = downcast<List>(controller.getCurrentFrameValue());
  77. CPPUNIT_ASSERT(array->empty());
  78. }
  79. void XmlRpcRequestParserControllerTest::testPopArrayFrame_compound()
  80. {
  81. XmlRpcRequestParserController controller;
  82. // We are making following structs. [] = array, {key:value .. } = dict
  83. // [ { "uris":["http://example.org/aria2","http://aria2.sf.net/"],
  84. // "options":{ "timeout":120 } },
  85. // [ "jp","us" ] ]
  86. controller.setCurrentFrameValue(List::g());
  87. controller.pushFrame();
  88. controller.setCurrentFrameValue(Dict::g());
  89. controller.pushFrame();
  90. controller.setCurrentFrameName("uris");
  91. controller.setCurrentFrameValue(List::g());
  92. controller.pushFrame();
  93. controller.setCurrentFrameValue(String::g("http://example.org/aria2"));
  94. controller.popArrayFrame();
  95. controller.pushFrame();
  96. controller.setCurrentFrameValue(String::g("http://aria2.sf.net/"));
  97. controller.popArrayFrame();
  98. controller.popStructFrame();
  99. controller.pushFrame();
  100. controller.setCurrentFrameName("options");
  101. controller.setCurrentFrameValue(Dict::g());
  102. controller.pushFrame();
  103. controller.setCurrentFrameName("timeout");
  104. controller.setCurrentFrameValue(Integer::g(120));
  105. controller.popStructFrame();
  106. controller.popStructFrame();
  107. controller.popArrayFrame();
  108. controller.pushFrame();
  109. controller.setCurrentFrameValue(List::g());
  110. controller.pushFrame();
  111. controller.setCurrentFrameValue(String::g("jp"));
  112. controller.popArrayFrame();
  113. controller.pushFrame();
  114. controller.setCurrentFrameValue(String::g("us"));
  115. controller.popArrayFrame();
  116. controller.popArrayFrame();
  117. const List* result = downcast<List>(controller.getCurrentFrameValue());
  118. const Dict* dict = downcast<Dict>(result->get(0));
  119. const List* uris = downcast<List>(dict->get("uris"));
  120. const Dict* options = downcast<Dict>(dict->get("options"));
  121. const List* countryList = downcast<List>(result->get(1));
  122. CPPUNIT_ASSERT_EQUAL(std::string("http://aria2.sf.net/"),
  123. downcast<String>(uris->get(1))->s());
  124. CPPUNIT_ASSERT_EQUAL((Integer::ValueType)120,
  125. downcast<Integer>(options->get("timeout"))->i());
  126. CPPUNIT_ASSERT_EQUAL(std::string("jp"),
  127. downcast<String>(countryList->get(0))->s());
  128. }
  129. } // namespace rpc
  130. } // namespace aria2