Przeglądaj źródła

Test for MXL reading working

Andrea Condoluci 9 lat temu
rodzic
commit
c7c0da3e9e

+ 18 - 20
src/Common/FileIO/Mxl.ts

@@ -1,31 +1,29 @@
 import { IXmlElement } from "./Xml";
 import JSZip = require("jszip");
 
-export function extractSheetFromMxl(data: string): any {
+export function extractSheetFromMxl(data: string, onFullfilled: any, onRejected: any): void {
   "use strict";
-  // let buf = Buffer.concat(data);
   let zip: any = new JSZip();
-
-  return zip.loadAsync(data).then((_: any) => {
-    return zip.file("META-INF/container.xml").async("string");
-  }).then((content: string) => {
-    let parser: DOMParser = new DOMParser();
-    let doc: Document = parser.parseFromString(content, "text/xml");
-    console.log(content);
-    // doc.Root.Element("rootfiles").Element("rootfile").Attribute("full-path").Value;
-    let rootFile: string = doc.getElementsByTagName("rootfile")[0].getAttribute("full-path");
-    console.log("success..", rootFile);
-    return zip.file(rootFile).async("string");
-  }).then(
+  zip.loadAsync(data).then(
+    (_: any) => {
+      return zip.file("META-INF/container.xml").async("string");
+    },
+    onRejected
+  ).then(
+    (content: string) => {
+      let parser: DOMParser = new DOMParser();
+      let doc: Document = parser.parseFromString(content, "text/xml");
+      // doc.Root.Element("rootfiles").Element("rootfile").Attribute("full-path").Value;
+      let rootFile: string = doc.getElementsByTagName("rootfile")[0].getAttribute("full-path");
+      return zip.file(rootFile).async("string");
+    },
+    onRejected
+  ).then(
     (content: string) => {
-      console.log("success...", content);
       let parser: DOMParser = new DOMParser();
       let doc: Document = parser.parseFromString(content, "text/xml");
-      console.log("success...", doc);
-      return new IXmlElement(doc.documentElement);
+      onFullfilled(new IXmlElement(doc.documentElement));
     },
-    (reason: any) => {
-      chai.assert.fail(0, 1, reason.message);
-    }
+    onRejected
   );
 }

+ 1 - 1
src/MusicalScore/ScoreIO/InstrumentReader.ts

@@ -707,7 +707,7 @@ export class InstrumentReader {
 
   private saveAbstractInstructionList(numberOfStaves: number, beginOfMeasure: boolean): void {
     // FIXME TODO
-    Logging.debug("saveAbstractInstructionList still to implement! See InstrumentReader.ts");
+    // Logging.debug("saveAbstractInstructionList still to implement! See InstrumentReader.ts");
   }
 
   /*private saveAbstractInstructionList(numberOfStaves: number, beginOfMeasure: boolean): void {

+ 32 - 21
test/Common/FileIO/Mxl.ts

@@ -1,34 +1,45 @@
 import { IXmlElement } from "../../../src/Common/FileIO/Xml";
 import { extractSheetFromMxl } from "../../../src/Common/FileIO/Mxl.ts";
 
-describe("MXL Tests", () => {
-  // Initialize variables
-  let path: string = "test/data/MozartTrio.mxl";
-  // let score: IXmlElement;
 
+describe("MXL Tests", () => {
+  // Load the mxl file
   function getSheet(filename: string): string {
-    console.log(((window as any).__mxl__));
     return ((window as any).__mxl__)[filename];
   }
 
-  before((): void => {
-      // Load the xml file
-      let mxl: string = getSheet(path);
+  // Generates a test for a mxl file name
+  function testFile(scoreName: string): void {
+    it(scoreName, (done: MochaDone) => {
+      // Load the xml file content
+      let mxl: string = getSheet("test/data/" + scoreName + ".mxl");
       chai.expect(mxl).to.not.be.undefined;
-      extractSheetFromMxl(mxl).then(
-        (elem: IXmlElement) => {
-          console.log("success!", elem);
+      // Extract XML from MXL
+      // Warning, the sheet is loaded asynchronously,
+      // (with Promises), thus we need a little hack to
+      // make Mocha work asynch. with "done()"
+      extractSheetFromMxl(
+        mxl,
+        (score: IXmlElement) => {
+          try {
+            chai.expect(score).to.not.be.undefined;
+            chai.expect(score.name).to.equal("score-partwise");
+          } catch (e) {
+            return done(e);
+          }
+          done();
         },
         (reason: any) => {
-          chai.assert.fail(0, 1, reason.message);
+          done(reason.message);
         }
-      );
-      // score = new IXmlElement(doc.getElementsByTagName("score-partwise")[0]);
-      // // chai.expect(score).to.not.be.undefined;
-      // sheet = reader.createMusicSheet(score, path);
-  });
-  it("Success", (done: MochaDone) => {
-    chai.expect(extractSheetFromMxl).to.equal(extractSheetFromMxl);
-    done();
-  });
+      )
+    });
+  }
+
+  // Test all the following mxl files:
+  let scores: string[] = ["MozartTrio"];
+  for (let score of scores) {
+    testFile(score);
+  }
+
 });