XmlRpcRequestParserControllerTest.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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, downcast<Integer>(array->get(0))->i());
  68. }
  69. void XmlRpcRequestParserControllerTest::testPopArrayFrame_noValue()
  70. {
  71. XmlRpcRequestParserController controller;
  72. controller.setCurrentFrameValue(List::g());
  73. controller.pushFrame();
  74. controller.popArrayFrame();
  75. const List* array = downcast<List>(controller.getCurrentFrameValue());
  76. CPPUNIT_ASSERT(array->empty());
  77. }
  78. void XmlRpcRequestParserControllerTest::testPopArrayFrame_compound()
  79. {
  80. XmlRpcRequestParserController controller;
  81. // We are making following structs. [] = array, {key:value .. } = dict
  82. // [ { "uris":["http://example.org/aria2","http://aria2.sf.net/"],
  83. // "options":{ "timeout":120 } },
  84. // [ "jp","us" ] ]
  85. controller.setCurrentFrameValue(List::g());
  86. controller.pushFrame();
  87. controller.setCurrentFrameValue(Dict::g());
  88. controller.pushFrame();
  89. controller.setCurrentFrameName("uris");
  90. controller.setCurrentFrameValue(List::g());
  91. controller.pushFrame();
  92. controller.setCurrentFrameValue(String::g("http://example.org/aria2"));
  93. controller.popArrayFrame();
  94. controller.pushFrame();
  95. controller.setCurrentFrameValue(String::g("http://aria2.sf.net/"));
  96. controller.popArrayFrame();
  97. controller.popStructFrame();
  98. controller.pushFrame();
  99. controller.setCurrentFrameName("options");
  100. controller.setCurrentFrameValue(Dict::g());
  101. controller.pushFrame();
  102. controller.setCurrentFrameName("timeout");
  103. controller.setCurrentFrameValue(Integer::g(120));
  104. controller.popStructFrame();
  105. controller.popStructFrame();
  106. controller.popArrayFrame();
  107. controller.pushFrame();
  108. controller.setCurrentFrameValue(List::g());
  109. controller.pushFrame();
  110. controller.setCurrentFrameValue(String::g("jp"));
  111. controller.popArrayFrame();
  112. controller.pushFrame();
  113. controller.setCurrentFrameValue(String::g("us"));
  114. controller.popArrayFrame();
  115. controller.popArrayFrame();
  116. const List* result = downcast<List>(controller.getCurrentFrameValue());
  117. const Dict* dict = downcast<Dict>(result->get(0));
  118. const List* uris = downcast<List>(dict->get("uris"));
  119. const Dict* options = downcast<Dict>(dict->get("options"));
  120. const List* countryList = downcast<List>(result->get(1));
  121. CPPUNIT_ASSERT_EQUAL(std::string("http://aria2.sf.net/"),
  122. downcast<String>(uris->get(1))->s());
  123. CPPUNIT_ASSERT_EQUAL((Integer::ValueType)120,
  124. downcast<Integer>(options->get("timeout"))->i());
  125. CPPUNIT_ASSERT_EQUAL(std::string("jp"), downcast<String>(countryList->get(0))->s());
  126. }
  127. } // namespace rpc
  128. } // namespace aria2