/* * Copyright (c) 2010-2016 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include #include #include #include using namespace Swift; class FormParserTest : public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(FormParserTest); CPPUNIT_TEST(testParse_FormInformation); CPPUNIT_TEST(testParse_FormLayout); CPPUNIT_TEST(testParse); CPPUNIT_TEST(testParse_FormItems); CPPUNIT_TEST_SUITE_END(); public: void testParse_FormInformation() { PayloadsParserTester parser; CPPUNIT_ASSERT(parser.parse( "" "Bot Configuration" "Hello!" "Fill out this form to configure your new bot!" "" )); Form* payload = dynamic_cast(parser.getPayload().get()); CPPUNIT_ASSERT(payload); CPPUNIT_ASSERT_EQUAL(std::string("Bot Configuration"), payload->getTitle()); CPPUNIT_ASSERT_EQUAL(std::string("Hello!\nFill out this form to configure your new bot!"), payload->getInstructions()); CPPUNIT_ASSERT_EQUAL(Form::SubmitType, payload->getType()); } void testParse_FormLayout() { PayloadsParserTester parser; // P1 = page one, S1 = section one, F1 = field one, T1 = text one CPPUNIT_ASSERT(parser.parse( "" "" "" "P1T1" "" "
" "P1S1T1" "" "
" "
" "" "
" "
" "
" "
" "
" "" "" "" "")); Form* payload = dynamic_cast(parser.getPayload().get()); CPPUNIT_ASSERT(payload); CPPUNIT_ASSERT_EQUAL(0, static_cast(payload->getFields().size())); // PAGE ONE - parsing of element types CPPUNIT_ASSERT_EQUAL(std::string("P1"), payload->getPages()[0]->getLabel()); CPPUNIT_ASSERT(payload->getPages()[0]->getReportedRefs()[0]); CPPUNIT_ASSERT_EQUAL(std::string("P1T1"), payload->getPages()[0]->getTextElements()[0]->getTextString()); CPPUNIT_ASSERT_EQUAL(std::string("P1F1"), payload->getPages()[0]->getFields()[0]->getName()); CPPUNIT_ASSERT_EQUAL(std::string("P1S1"), payload->getPages()[0]->getChildSections()[0]->getLabel()); CPPUNIT_ASSERT_EQUAL(std::string("P1S1T1"), payload->getPages()[0]->getChildSections()[0]->getTextElements()[0]->getTextString()); CPPUNIT_ASSERT_EQUAL(std::string("P1S1F1"), payload->getPages()[0]->getChildSections()[0]->getFields()[0]->getName()); // PAGE TWO - parsing of nested elements CPPUNIT_ASSERT_EQUAL(std::string("P2"), payload->getPages()[1]->getLabel()); CPPUNIT_ASSERT_EQUAL(std::string("P2S1"), payload->getPages()[1]->getChildSections()[0]->getLabel()); CPPUNIT_ASSERT_EQUAL(std::string("P2S2"), payload->getPages()[1]->getChildSections()[0]->getChildSections()[0]->getLabel()); CPPUNIT_ASSERT_EQUAL(std::string("P2S3"), payload->getPages()[1]->getChildSections()[0]->getChildSections()[0]->getChildSections()[0]->getLabel()); } void testParse() { PayloadsParserTester parser; CPPUNIT_ASSERT(parser.parse( "" "" "jabber:bot" "" "Section 1: Bot Info" "" "This is a bot.A quite good one actually" "" "" "1" "" "" "" "" "" "" "" "" "news" "search" "" "" "20" "" "" "" "" "" "" "" "" "Tell all your friends about your new bot!" "foo@bar.com" "baz@fum.org" "" "" "foo" "" "")); Form* payload = dynamic_cast(parser.getPayload().get()); CPPUNIT_ASSERT(payload); CPPUNIT_ASSERT_EQUAL(10, static_cast(payload->getFields().size())); CPPUNIT_ASSERT_EQUAL(std::string("jabber:bot"), payload->getFields()[0]->getValues()[0]); CPPUNIT_ASSERT_EQUAL(std::string("FORM_TYPE"), payload->getFields()[0]->getName()); CPPUNIT_ASSERT(!payload->getFields()[0]->getRequired()); CPPUNIT_ASSERT_EQUAL(std::string("Section 1: Bot Info"), payload->getFields()[1]->getValues()[0]); CPPUNIT_ASSERT_EQUAL(std::string("The name of your bot"), payload->getFields()[2]->getLabel()); CPPUNIT_ASSERT_EQUAL(std::string("This is a bot.\nA quite good one actually"), payload->getFields()[3]->getTextMultiValue()); CPPUNIT_ASSERT_EQUAL(true, payload->getFields()[4]->getBoolValue()); CPPUNIT_ASSERT(payload->getFields()[4]->getRequired()); CPPUNIT_ASSERT_EQUAL(std::string("1"), payload->getFields()[4]->getValues()[0]); CPPUNIT_ASSERT_EQUAL(2, static_cast(payload->getFields()[6]->getValues().size())); CPPUNIT_ASSERT_EQUAL(std::string("news"), payload->getFields()[6]->getValues()[0]); CPPUNIT_ASSERT_EQUAL(std::string("search"), payload->getFields()[6]->getValues()[1]); CPPUNIT_ASSERT_EQUAL(5, static_cast(payload->getFields()[6]->getOptions().size())); CPPUNIT_ASSERT_EQUAL(std::string("Contests"), payload->getFields()[6]->getOptions()[0].label); CPPUNIT_ASSERT_EQUAL(std::string("contests"), payload->getFields()[6]->getOptions()[0].value); CPPUNIT_ASSERT_EQUAL(std::string("News"), payload->getFields()[6]->getOptions()[1].label); CPPUNIT_ASSERT_EQUAL(std::string("news"), payload->getFields()[6]->getOptions()[1].value); CPPUNIT_ASSERT_EQUAL(std::string("20"), payload->getFields()[7]->getValues()[0]); CPPUNIT_ASSERT_EQUAL(JID("foo@bar.com"), payload->getFields()[8]->getJIDMultiValue(0)); CPPUNIT_ASSERT_EQUAL(JID("baz@fum.org"), payload->getFields()[8]->getJIDMultiValue(1)); CPPUNIT_ASSERT_EQUAL(std::string("Tell all your friends about your new bot!"), payload->getFields()[8]->getDescription()); CPPUNIT_ASSERT_EQUAL(std::string("foo"), payload->getFields()[9]->getValues()[0]); } void testParse_FormItems() { PayloadsParserTester parser; CPPUNIT_ASSERT(parser.parse( "" "" "jabber:iq:search" "" "" "" "" "" "" "" "" "Benvolio" "Montague" "benvolio@montague.net" "male" "" "" "Romeo" "Montague" "romeo@montague.net" "male" "" "")); Form* dataForm = dynamic_cast(parser.getPayload().get()); CPPUNIT_ASSERT(dataForm); Form::FormItem reported = dataForm->getReportedFields(); CPPUNIT_ASSERT_EQUAL(static_cast(4), reported.size()); std::vector items = dataForm->getItems(); CPPUNIT_ASSERT_EQUAL(static_cast(2), items.size()); Form::FormItem item = items[0]; CPPUNIT_ASSERT_EQUAL(static_cast(4), item.size()); CPPUNIT_ASSERT_EQUAL(std::string("Benvolio"), item[0]->getValues()[0]); CPPUNIT_ASSERT_EQUAL(std::string("first"), item[0]->getName()); CPPUNIT_ASSERT_EQUAL(std::string("Montague"), item[1]->getValues()[0]); CPPUNIT_ASSERT_EQUAL(std::string("last"), item[1]->getName()); std::shared_ptr jidField = item[2]; CPPUNIT_ASSERT_EQUAL(JID("benvolio@montague.net"), jidField->getJIDSingleValue()); CPPUNIT_ASSERT_EQUAL(std::string("jid"), item[2]->getName()); CPPUNIT_ASSERT_EQUAL(std::string("male"), item[3]->getValues()[0]); CPPUNIT_ASSERT_EQUAL(std::string("x-gender"), item[3]->getName()); item = items[1]; CPPUNIT_ASSERT_EQUAL(static_cast(4), item.size()); CPPUNIT_ASSERT_EQUAL(std::string("Romeo"), item[0]->getValues()[0]); CPPUNIT_ASSERT_EQUAL(std::string("first"), item[0]->getName()); CPPUNIT_ASSERT_EQUAL(std::string("Montague"), item[1]->getValues()[0]); CPPUNIT_ASSERT_EQUAL(std::string("last"), item[1]->getName()); jidField = item[2]; CPPUNIT_ASSERT(jidField); CPPUNIT_ASSERT_EQUAL(JID("romeo@montague.net"), jidField->getJIDSingleValue()); CPPUNIT_ASSERT_EQUAL(std::string("jid"), item[2]->getName()); CPPUNIT_ASSERT_EQUAL(std::string("male"), item[3]->getValues()[0]); CPPUNIT_ASSERT_EQUAL(std::string("x-gender"), item[3]->getName()); } }; CPPUNIT_TEST_SUITE_REGISTRATION(FormParserTest);