2008-06-02 17:41:35 -04:00
|
|
|
#include "assert.h"
|
|
|
|
#include <iostream>
|
|
|
|
|
2010-04-02 11:35:36 -04:00
|
|
|
#include "xpath.h"
|
2008-06-02 17:41:35 -04:00
|
|
|
#include "pbd/xml++.h"
|
2013-07-11 10:59:30 -04:00
|
|
|
#include "pbd/file_utils.h"
|
|
|
|
|
|
|
|
#include "test_common.h"
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2010-04-02 11:35:36 -04:00
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION (XPathTest);
|
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
using namespace std;
|
2013-07-11 10:59:30 -04:00
|
|
|
using namespace PBD;
|
2010-04-02 10:52:55 -04:00
|
|
|
|
2010-04-02 11:35:36 -04:00
|
|
|
void
|
|
|
|
XPathTest::testMisc ()
|
2008-06-02 17:41:35 -04:00
|
|
|
{
|
2012-05-14 13:18:48 -04:00
|
|
|
// cout << "Test 1: RosegardenPatchFile.xml: Find all banks in the file" << endl;
|
2013-07-11 10:59:30 -04:00
|
|
|
|
|
|
|
std::string testdata_path;
|
|
|
|
CPPUNIT_ASSERT (find_file_in_search_path (test_search_path (), "RosegardenPatchFile.xml", testdata_path));
|
|
|
|
|
|
|
|
XMLTree doc(testdata_path);
|
2008-06-02 17:41:35 -04:00
|
|
|
// "//bank" gives as last element an empty element libxml bug????
|
2012-03-20 21:07:05 -04:00
|
|
|
boost::shared_ptr<XMLSharedNodeList> result = doc.find("//bank[@name]");
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2012-05-14 13:18:48 -04:00
|
|
|
// cout << "Found " << result->size() << " banks" << endl;
|
2008-06-02 17:41:35 -04:00
|
|
|
assert(result->size() == 8);
|
2011-01-19 17:05:38 -05:00
|
|
|
// int counter = 1;
|
2008-06-02 17:41:35 -04:00
|
|
|
for(XMLSharedNodeList::const_iterator i = result->begin(); i != result->end(); ++i) {
|
|
|
|
assert((*i)->name() == "bank");
|
|
|
|
assert((*i)->property("name"));
|
2010-04-02 11:35:36 -04:00
|
|
|
// cout << "Found bank number " << counter++ << " with name: " << (*i)->property("name")->value() << endl;
|
2008-06-02 17:41:35 -04:00
|
|
|
for(XMLNodeList::const_iterator j = (*i)->children().begin(); j != (*i)->children().end(); ++j) {
|
2010-04-02 11:35:36 -04:00
|
|
|
// cout << "\t found program " << (*j)->property("id")->value() <<
|
|
|
|
// " with name: " << (*j)->property("name")->value() << endl;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-14 13:18:48 -04:00
|
|
|
// cout << endl << endl << "Test 2: RosegardenPatchFile.xml: Find all programs whose program name contains 'Latin'" << endl;
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2012-03-20 21:07:05 -04:00
|
|
|
result = doc.find("/rosegarden-data/studio/device/bank/program[contains(@name, 'Latin')]");
|
2008-06-02 17:41:35 -04:00
|
|
|
assert(result->size() == 5);
|
|
|
|
|
|
|
|
for(XMLSharedNodeList::const_iterator i = result->begin(); i != result->end(); ++i) {
|
2010-04-02 11:35:36 -04:00
|
|
|
// cout << "\t found program " << (*i)->property("id")->value() <<
|
|
|
|
// " with name: " << (*i)->property("name")->value() << endl;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
2010-04-02 11:35:36 -04:00
|
|
|
// cout << endl << endl << "Test 3: TestSession.ardour: find all Sources where captured-for contains the string 'Guitar'" << endl;
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
// We have to allocate a new document here, or we get segfaults
|
2013-07-11 10:59:30 -04:00
|
|
|
std::string testsession_path;
|
|
|
|
CPPUNIT_ASSERT (find_file_in_search_path (test_search_path (), "TestSession.ardour", testsession_path));
|
|
|
|
|
|
|
|
XMLTree doc2(testsession_path);
|
2012-03-20 21:07:05 -04:00
|
|
|
result = doc2.find("/Session/Sources/Source[contains(@captured-for, 'Guitar')]");
|
2008-06-02 17:41:35 -04:00
|
|
|
assert(result->size() == 16);
|
|
|
|
|
|
|
|
for(XMLSharedNodeList::const_iterator i = result->begin(); i != result->end(); ++i) {
|
2010-04-02 11:35:36 -04:00
|
|
|
// cout << "\t found source '" << (*i)->property("name")->value() <<
|
|
|
|
// "' with id: " << (*i)->property("id")->value() << endl;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
2012-05-14 13:18:48 -04:00
|
|
|
// cout << endl << endl << "Test 4: TestSession.ardour: Find all elements with an 'id' and 'name' attribute" << endl;
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2012-03-20 21:07:05 -04:00
|
|
|
result = doc2.find("//*[@id and @name]");
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
for(XMLSharedNodeList::const_iterator i = result->begin(); i != result->end(); ++i) {
|
|
|
|
assert((*i)->property("id"));
|
|
|
|
assert((*i)->property("name"));
|
2010-04-02 11:35:36 -04:00
|
|
|
// cout << "\t found element '" << (*i)->name() <<
|
|
|
|
// "' with id: " << (*i)->property("id")->value() <<
|
|
|
|
// "' and name: " << (*i)->property("name")->value() << endl;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
2012-05-14 13:18:48 -04:00
|
|
|
// cout << endl << endl << "Test 5: ProtoolsPatchFile.midnam: Get Banks and Patches for 'Name Set 1'" << endl;
|
2008-06-02 17:41:35 -04:00
|
|
|
|
2013-07-11 10:59:30 -04:00
|
|
|
std::string testmidnam_path;
|
|
|
|
CPPUNIT_ASSERT (find_file_in_search_path (test_search_path (), "ProtoolsPatchFile.midnam", testmidnam_path));
|
|
|
|
|
2008-06-02 17:41:35 -04:00
|
|
|
// We have to allocate a new document here, or we get segfaults
|
2013-07-11 10:59:30 -04:00
|
|
|
XMLTree doc3(testmidnam_path);
|
2012-03-20 21:07:05 -04:00
|
|
|
result = doc3.find("/MIDINameDocument/MasterDeviceNames/ChannelNameSet[@Name='Name Set 1']/PatchBank");
|
2008-06-02 17:41:35 -04:00
|
|
|
assert(result->size() == 16);
|
|
|
|
|
|
|
|
for(XMLSharedNodeList::const_iterator i = result->begin(); i != result->end(); ++i) {
|
2010-04-02 11:35:36 -04:00
|
|
|
// cout << "\t found Patchbank " << (*i)->property("Name")->value() << endl;
|
2012-03-20 21:07:05 -04:00
|
|
|
boost::shared_ptr<XMLSharedNodeList> patches = doc3.find ("//Patch[@Name]", i->get());
|
2008-06-02 17:41:35 -04:00
|
|
|
for(XMLSharedNodeList::const_iterator p = patches->begin(); p != patches->end(); ++p) {
|
2010-04-02 11:35:36 -04:00
|
|
|
// cout << "\t\t found patch number " << (*p)->property("Number")->value()
|
|
|
|
// << " with name: " << (*p)->property("Name")->value() << endl;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-14 13:18:48 -04:00
|
|
|
// cout << endl << endl << "Test 5: ProtoolsPatchFile.midnam: Find attribute nodes" << endl;
|
2012-03-20 21:07:05 -04:00
|
|
|
result = doc3.find("//@Value");
|
2008-06-02 17:41:35 -04:00
|
|
|
|
|
|
|
for(XMLSharedNodeList::const_iterator i = result->begin(); i != result->end(); ++i) {
|
|
|
|
boost::shared_ptr<XMLNode> node = (*i);
|
2010-04-02 11:35:36 -04:00
|
|
|
// cout << "\t found attribute node: " << node->name()
|
|
|
|
// << " value: " << node->attribute_value() << endl;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
2012-05-14 13:18:48 -04:00
|
|
|
// cout << endl << endl << "Test 6: ProtoolsPatchFile.midnam: Find available channels on 'Name Set 1'" << endl;
|
2012-03-20 21:07:05 -04:00
|
|
|
result = doc3.find(
|
2008-06-02 17:41:35 -04:00
|
|
|
"//ChannelNameSet[@Name = 'Name Set 1']//AvailableChannel[@Available = 'true']/@Channel");
|
|
|
|
|
|
|
|
assert(result->size() == 15);
|
|
|
|
for(XMLSharedNodeList::const_iterator i = result->begin(); i != result->end(); ++i) {
|
|
|
|
boost::shared_ptr<XMLNode> node = (*i);
|
2010-04-02 11:35:36 -04:00
|
|
|
// cout << "\t found available Channel: " << node->name()
|
|
|
|
// << " value: " << node->attribute_value() << endl;
|
2008-06-02 17:41:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|