15062c61325f24f1cc30803fc5deed3a08b4aa37
[ardour.git] / libs / pbd / test / xpath.cc
1 #include "assert.h"
2 #include <iostream>
3
4 #include "xpath.h"
5 #include "pbd/xml++.h"
6
7 CPPUNIT_TEST_SUITE_REGISTRATION (XPathTest);
8
9 using namespace std;
10
11 static string const prefix = "../libs/pbd/test/";
12
13 void
14 XPathTest::testMisc ()
15 {
16         cout << "Test 1: RosegardenPatchFile.xml: Find all banks in the file" << endl;
17         XMLTree  doc(prefix + "RosegardenPatchFile.xml");
18         // "//bank" gives as last element an empty element libxml bug????
19         boost::shared_ptr<XMLSharedNodeList> result = doc.find("//bank[@name]");
20         
21         cout << "Found " << result->size() << " banks" << endl;
22         assert(result->size() == 8);
23 //      int counter = 1;
24         for(XMLSharedNodeList::const_iterator i = result->begin(); i != result->end(); ++i) {
25                 assert((*i)->name() == "bank");
26                 assert((*i)->property("name"));
27 //              cout << "Found bank number " << counter++ << " with name: " << (*i)->property("name")->value() << endl;
28                 for(XMLNodeList::const_iterator j = (*i)->children().begin(); j != (*i)->children().end(); ++j) {
29 //                      cout << "\t found program " << (*j)->property("id")->value() << 
30 //                              " with name: " << (*j)->property("name")->value() << endl;
31                 }
32         }
33         
34         cout << endl << endl << "Test 2: RosegardenPatchFile.xml: Find all programs whose program name contains 'Latin'" << endl;
35         
36         result = doc.find("/rosegarden-data/studio/device/bank/program[contains(@name, 'Latin')]");
37         assert(result->size() == 5);
38         
39         for(XMLSharedNodeList::const_iterator i = result->begin(); i != result->end(); ++i) {
40 //              cout << "\t found program " << (*i)->property("id")->value() << 
41 //                      " with name: " << (*i)->property("name")->value() << endl;
42         }
43
44 //      cout << endl << endl << "Test 3: TestSession.ardour: find all Sources where captured-for contains the string 'Guitar'" << endl;
45         
46         // We have to allocate a new document here, or we get segfaults
47         XMLTree doc2(prefix + "TestSession.ardour");
48         result = doc2.find("/Session/Sources/Source[contains(@captured-for, 'Guitar')]");
49         assert(result->size() == 16);
50         
51         for(XMLSharedNodeList::const_iterator i = result->begin(); i != result->end(); ++i) {
52 //              cout << "\t found source '" << (*i)->property("name")->value() << 
53 //                      "' with id: " << (*i)->property("id")->value() << endl;
54         }
55         
56         cout << endl << endl << "Test 4: TestSession.ardour: Find all elements with an 'id' and 'name' attribute" << endl;
57         
58         result = doc2.find("//*[@id and @name]");
59         
60         for(XMLSharedNodeList::const_iterator i = result->begin(); i != result->end(); ++i) {
61                 assert((*i)->property("id"));
62                 assert((*i)->property("name"));
63 //              cout << "\t found element '" << (*i)->name() << 
64 //                      "' with id: "  << (*i)->property("id")->value() << 
65 //                              "' and name: " << (*i)->property("name")->value() << endl;
66         }
67         
68         cout << endl << endl << "Test 5: ProtoolsPatchFile.midnam: Get Banks and Patches for 'Name Set 1'" << endl;
69         
70         // We have to allocate a new document here, or we get segfaults
71         XMLTree doc3(prefix + "ProtoolsPatchFile.midnam");
72         result = doc3.find("/MIDINameDocument/MasterDeviceNames/ChannelNameSet[@Name='Name Set 1']/PatchBank");
73         assert(result->size() == 16);
74         
75         for(XMLSharedNodeList::const_iterator i = result->begin(); i != result->end(); ++i) {
76 //              cout << "\t found Patchbank " << (*i)->property("Name")->value() << endl;
77                 boost::shared_ptr<XMLSharedNodeList> patches = doc3.find ("//Patch[@Name]", i->get());
78                 for(XMLSharedNodeList::const_iterator p = patches->begin(); p != patches->end(); ++p) {
79 //                      cout << "\t\t found patch number " << (*p)->property("Number")->value() 
80 //                           << " with name: " << (*p)->property("Name")->value()  << endl;
81                 }
82         }
83
84         cout << endl << endl << "Test 5: ProtoolsPatchFile.midnam: Find attribute nodes" << endl;
85         result = doc3.find("//@Value");
86         
87         for(XMLSharedNodeList::const_iterator i = result->begin(); i != result->end(); ++i) {
88                 boost::shared_ptr<XMLNode> node = (*i);
89 //              cout << "\t found attribute node: " << node->name()  
90 //                   << " value: " << node->attribute_value() << endl;
91         }       
92         
93         cout << endl << endl << "Test 6: ProtoolsPatchFile.midnam: Find available channels on 'Name Set 1'" << endl;
94         result = doc3.find(
95                 "//ChannelNameSet[@Name = 'Name Set 1']//AvailableChannel[@Available = 'true']/@Channel");
96         
97         assert(result->size() == 15);
98         for(XMLSharedNodeList::const_iterator i = result->begin(); i != result->end(); ++i) {
99                 boost::shared_ptr<XMLNode> node = (*i);
100 //              cout << "\t found available Channel: " << node->name()  
101 //                   << " value: " << node->attribute_value() << endl;
102         }       
103         
104 }