TEST(TestMeiElement, TestPrintElement) { MeiElement *m = new MeiElement("mei"); MeiElement *music = new MeiElement("music"); MeiElement *body = new MeiElement("body"); MeiElement *staff = new MeiElement("staff"); Note *note = new Note(); Note *note2 = new Note(); MeiDocument *doc = new MeiDocument(); doc->setRootElement(m); m->addChild(music); music->addChild(body); body->addChild(staff); staff->addChild(note); staff->addChild(note2); note->m_NoteVis.setHeadshape("diamond"); note->m_Pitch.setPname("c"); note2->m_Pitch.setPname("d"); m->printElement(); /* The printElement method does not return anything, so the value of it can't really be tested. This test simply ensures that it is capable of being called without failing. So we just assert that everything's OK here if we haven't segfaulted by now. */ ASSERT_TRUE(true); }
TEST(TestMeiDocument, RootElement) { MeiDocument *doc = new MeiDocument(); Mei *m = new Mei(); doc->setRootElement(m); ASSERT_EQ(m, doc->getRootElement()); }
TEST(TestMeiElement, GetPositionInDocument) { MeiElement* m = new MeiElement("mei"); MeiElement *m1 = new MeiElement("music"); string musicId = m1->getId(); MeiElement *b1 = new MeiElement("body"); MeiElement *s1 = new MeiElement("staff"); MeiElement *n1 = new MeiElement("note"); string nId = n1->getId(); MeiElement *n2 = new MeiElement("note"); MeiElement *n3 = new MeiElement("note"); MeiElement *n4 = new MeiElement("note"); string n4Id = n4->getId(); m->addChild(m1); m1->addChild(b1); b1->addChild(s1); s1->addChild(n1); s1->addChild(n2); s1->addChild(n3); MeiDocument* doc = new MeiDocument(); doc->setRootElement(m); ASSERT_EQ(4, n1->getPositionInDocument()); ASSERT_EQ(-1, n4->getPositionInDocument()); }
TEST(TestMeiDocument, SetsCorrectMeiVersion) { MeiDocument *doc = new MeiDocument(); ASSERT_EQ(*--MEI_VERSION.end(), doc->getVersion()); MeiDocument *doc2 = new MeiDocument("2012"); ASSERT_EQ("2012", doc2->getVersion()); }
TEST(CmnModuleTest, TestTieMembership) { MeiDocument* doc = createMeiDocument(); MeiElement* layer = doc->getElementById("id-layer"); MeiElement* measure = doc->getElementById("id-measure"); Note* n1 = new Note(); Note* n2 = new Note(); Note* n3 = new Note(); Note* n4 = new Note(); layer->addChild(n1); layer->addChild(n2); layer->addChild(n3); layer->addChild(n4); Tie* t1 = new Tie(); measure->addChild(t1); t1->m_Startid.setStartid(n1->getId()); t1->m_Startendid.setEndid(n4->getId()); t1->m_Staffident.setStaff("staffname"); vector<MeiElement*> members = t1->getMembers(); ASSERT_EQ(4, members.size()); }
TEST(TestMeiElement, TestSetDocument) { MeiElement *m = new MeiElement("mei"); MeiDocument *doc = new MeiDocument(); ASSERT_THROW(m->setDocument(doc), mei::DocumentRootNotSetException); doc->setRootElement(m); ASSERT_EQ(doc->getRootElement(), m); }
TEST(TestMeiDocument, SetsDefaultNamespace) { MeiDocument *doc = new MeiDocument(); MeiElement *root = new MeiElement("mei"); doc->setRootElement(root); ASSERT_TRUE(root->hasAttribute("xmlns")); ASSERT_EQ(root->getAttribute("xmlns")->getValue(), MEI_NS); }
void XmlImportImpl::init() { // get mei version from document xmlAttrPtr meiversAttr = xmlHasProp(this->rootXmlNode, (const xmlChar*)"meiversion"); string meiVersion = string((const char*)meiversAttr->children->content); MeiDocument *doc = new MeiDocument(meiVersion); this->meiDocument = doc; this->rootMeiElement = this->xmlNodeToMeiElement(this->rootXmlNode); doc->setRootElement(this->rootMeiElement); }
bool MusMeiOutput::ExportFile( ) { if ( m_doc->GetMeiDocument() == NULL) { wxLogError("Unable to export a new document to MEI. Please import first"); return false; } MeiDocument *doc = m_doc->GetMeiDocument(); string fname = m_filename.mb_str(); doc->setDocName(fname); doc->WriteToXml(doc); return true; }
bool MusMeiInput::ImportFile( ) { string encoding = "utf-8"; string fname = m_filename.mb_str(); MeiDocument *doc = MeiDocument::ReadFromXml(fname, encoding); m_doc->SetMeiDocument(doc); // When importing MEI, the first step is to load one single page and to load everything in one system MusPage *page = new MusPage(); page->defin = 20; //m_doc->m_pages.Add( page ); // ax2 // To simplify accessibility, we keep a pointer on that pages //m_page = & m_doc->m_pages[0]; // ax2 MeiElement *root_element = doc->getRootElement(); if (!ReadElement(root_element)) { wxLogError(_("An error occurred while importing '%s'"), m_doc->m_fname.c_str() ); } m_doc->CheckIntegrity(); // adjust the page size - this is very temporary, we need something better // we also have to see what to do if we have a scoredef // for now, it just adjust the page size for the content (GetMaxXY) wxArrayPtrVoid params; // tableau de pointeurs pour parametres MusStaffFunctor getMaxXY( &MusLaidOutStaff::GetMaxXY ); wxArrayPtrVoid staffParams; // idem for staff functor int max_x = -1; int max_y = 0; staffParams.Add( &max_x ); staffParams.Add( &max_y ); m_page->Process( &getMaxXY, staffParams ); // change the staff width // m_page->lrg_lign = max_x / 10; // ax2 // add the space at the bottom - so far in max_y we have the top corner of the last staff /* if (m_page->m_staves.GetCount() > 0 ) { max_y += max((unsigned short)(&m_page->m_staves.Last())->portNbLine,(&m_page->m_staves.Last())->ecart); } */ // ax2 max_y *= m_page->defin; // transform this into coord. // update the page size m_doc->m_parameters.pageFormatHor = max_x / 10 + (2 * m_doc->m_parameters.MargeGAUCHEIMPAIRE); m_doc->m_parameters.pageFormatVer = max_y / 10 + (2 * m_doc->m_parameters.MargeSOMMET); return true; }
TEST(TestMeiElement, TestLookBack) { MeiElement *m = new MeiElement("mei"); MeiElement *music = new MeiElement("music"); MeiElement *body = new MeiElement("body"); MeiElement *staff = new MeiElement("staff"); MeiElement *note = new MeiElement("note"); MeiDocument *doc = new MeiDocument(); doc->setRootElement(m); m->addChild(music); music->addChild(body); body->addChild(staff); staff->addChild(note); ASSERT_EQ(music->lookBack("mei"), m); ASSERT_EQ(staff->lookBack("mei"), m); }
TEST(TestMeiElement, TestRemoveManyChildren) { MeiDocument *doc = new MeiDocument(); MeiElement *mei = new MeiElement("mei"); MeiElement *sd1 = new MeiElement("staffDef"); MeiElement *sd2 = new MeiElement("staffDef"); MeiElement *sd3 = new MeiElement("staffDef"); MeiElement *sd4 = new MeiElement("staffDef"); MeiElement *sd5 = new MeiElement("staffDef"); MeiElement *sd6 = new MeiElement("staffDef"); doc->setRootElement(mei); MeiElement *music = new mei::MeiElement("music"); MeiElement *scoreDef = new mei::MeiElement("scoreDef"); mei->addChild(music); music->addChild(scoreDef); scoreDef->addChild(sd1); scoreDef->addChild(sd2); scoreDef->addChild(sd3); scoreDef->addChild(sd4); scoreDef->addChild(sd5); scoreDef->addChild(sd6); sd1->addAttribute("n", "1"); sd2->addAttribute("n", "2"); sd3->addAttribute("n", "3"); sd4->addAttribute("n", "4"); sd5->addAttribute("n", "5"); sd6->addAttribute("n", "6"); vector<MeiElement*> scoreDefs = doc->getRootElement()->getDescendantsByName("scoreDef"); vector<MeiElement*> staffDefs = doc->getRootElement()->getDescendantsByName("staffDef"); for (vector<MeiElement*>::iterator iter = staffDefs.begin(); iter != staffDefs.end(); ++iter) { if ((*iter)->getAttribute("n")->getValue() == "3" || (*iter)->getAttribute("n")->getValue() == "6") { (*iter)->getParent()->removeChild((*iter)); } } ASSERT_EQ(4, music->getDescendantsByName("staffDef").size()); }
TEST(TestMeiDocument, ElementsByName) { Mei *mei = new Mei(); Music *mus = new Music(); Body *body = new Body(); Staff *staff = new Staff(); Staff *s2 = new Staff(); Note *n1 = new Note(); string wantedId = n1->getId(); Note *n2 = new Note(); Note *n3 = new Note(); Note *n4 = new Note(); mei->addChild(mus); mus->addChild(body); body->addChild(staff); body->addChild(s2); staff->addChild(n1); staff->addChild(n2); staff->addChild(n3); s2->addChild(n4); MeiDocument *doc = new MeiDocument(); doc->setRootElement(mei); std::vector<MeiElement*> notes = doc->getElementsByName("note"); ASSERT_EQ(4, notes.size()); std::vector<MeiElement*> rests = doc->getElementsByName("rest"); ASSERT_EQ(0, rests.size()); // After adding the root element, making a new element works Note *n5 = new Note(); staff->addChild(n5); std::vector<MeiElement*> notes_new = doc->getElementsByName("note"); ASSERT_EQ(5, notes_new.size()); }
TEST(TestMeiDocument, GetNamespace) { MeiDocument *doc = new MeiDocument(); MeiNamespace *ns = new MeiNamespace("prefix", "http://example.com/ns"); ASSERT_EQ("http://www.music-encoding.org/ns/mei", doc->getNamespaces().at(0)->getHref()); doc->addNamespace(ns); ASSERT_EQ(2, doc->getNamespaces().size()); ASSERT_EQ(ns, doc->getNamespace("http://example.com/ns")); ASSERT_TRUE(doc->hasNamespace("http://www.music-encoding.org/ns/mei")); ASSERT_FALSE(doc->hasNamespace("http://www.mcgill.ca")); }
TEST(TestMeiDocument, DocumentPointers) { Mei *mei = new Mei(); Music *mus = new Music(); Body *body = new Body(); Staff *staff = new Staff(); // If an element is added as a child and neither element is attached to a document, nothing happens. ASSERT_EQ(NULL, mei->getDocument()); mei->addChild(mus); ASSERT_EQ(NULL, mus->getDocument()); MeiDocument *doc = new MeiDocument(); // add root to document, all elements should have their document pointer updated mus->addChild(body); doc->setRootElement(mei); ASSERT_EQ(doc, mei->getDocument()); ASSERT_EQ(doc, mus->getDocument()); ASSERT_EQ(doc, body->getDocument()); // add another element as a child, child element should be linked to the same document body->addChild(staff); ASSERT_EQ(doc, staff->getDocument()); }
// after adding a root to a document, you can find an element TEST(TestMeiDocument, ElementById) { Mei *mei = new Mei(); Music *mus = new Music(); Body *body = new Body(); Staff *staff = new Staff(); Staff *s2 = new Staff(); Note *n1 = new Note(); string wantedId = n1->getId(); Note *n2 = new Note(); Note *n3 = new Note(); Note *n4 = new Note(); MeiDocument *doc = new MeiDocument(); ASSERT_EQ(NULL, doc->getElementById(wantedId)); mei->addChild(mus); mus->addChild(body); body->addChild(staff); body->addChild(s2); staff->addChild(n1); staff->addChild(n2); staff->addChild(n3); s2->addChild(n4); doc->setRootElement(mei); ASSERT_EQ(n1, doc->getElementById(wantedId)); ASSERT_EQ(NULL, doc->getElementById("some-unknown-id")); // After adding the root element, making a new element works Note *n5 = new Note(); string newid = n5->getId(); s2->addChild(n5); ASSERT_EQ(n5, doc->getElementById(newid)); // removing the element from the document, clear from document map s2->removeChild(n5); ASSERT_EQ(NULL, doc->getElementById(newid)); }
TEST(TestMeiDocument, FlattenedDocTree) { Mei *mei = new Mei(); Music *mus = new Music(); Body *body = new Body(); Staff *staff = new Staff(); Staff *s2 = new Staff(); Note *n1 = new Note(); Note *n2 = new Note(); Note *n3 = new Note(); MeiDocument *doc = new MeiDocument(); mei->addChild(mus); // empty since mei not added as document root yet ASSERT_TRUE(doc->getFlattenedTree().empty()); doc->setRootElement(mei); ASSERT_EQ(2, doc->getFlattenedTree().size()); mus->addChild(body); body->addChild(staff); body->addChild(s2); staff->addChild(n1); staff->addChild(n2); s2->addChild(n3); doc->lookBack(n2, "mei"); ASSERT_EQ(8, doc->getFlattenedTree().size()); staff->removeChild(n2); ASSERT_EQ(7, doc->getFlattenedTree().size()); ASSERT_EQ(s2, doc->getFlattenedTree()[5]); staff->removeChildrenWithName("note"); ASSERT_EQ(6, doc->getFlattenedTree().size()); body->deleteAllChildren(); ASSERT_EQ(3, doc->getFlattenedTree().size()); std::vector<MeiElement*> newChildren; Staff *newStaff1 = new Staff(); Staff *newStaff2 = new Staff(); newChildren.push_back(newStaff1); newChildren.push_back(newStaff2); body->setChildren(newChildren); ASSERT_EQ(5, doc->getFlattenedTree().size()); // check contents MeiElement* elements[] = { mei, mus, body, newStaff1, newStaff2 }; std::vector<MeiElement*> rightOrder (elements, elements + sizeof(elements) / sizeof(MeiElement)); for (int i = 0; i < rightOrder.size(); i++) { // check don't overshoot memory allocation ASSERT_LT(i, doc->getFlattenedTree().size()); ASSERT_EQ(rightOrder[i], doc->getFlattenedTree()[i]); } }