From b4cd839793a86871606b462e77bb779cbfb81d94 Mon Sep 17 00:00:00 2001
From: David Dorchies <david.dorchies@irstea.fr>
Date: Thu, 15 Mar 2018 21:46:16 +0100
Subject: [PATCH] Debug des tests ParallelStructure suite au commit 50ca78fc

---
 spec/mock_jasmine.ts                      | 23 ++++++++++++--
 spec/structure/parallel_structure.spec.ts | 38 ++++++++++++++++-------
 spec/structure/structure.spec.ts          |  1 +
 src/structure/structure.ts                | 32 ++++++++++++-------
 4 files changed, 68 insertions(+), 26 deletions(-)

diff --git a/spec/mock_jasmine.ts b/spec/mock_jasmine.ts
index 32baea85..fccc570b 100644
--- a/spec/mock_jasmine.ts
+++ b/spec/mock_jasmine.ts
@@ -71,7 +71,7 @@ class Expect {
     public toBe(expected: any) {
         const res = this.actual === expected;
         if (!res) {
-            console.error("Error : expected " + this.actual + " to be " + expected);
+            console.error("Expected " + this.actual + " to be " + expected);
         }
         return res;
     }
@@ -83,7 +83,7 @@ class Expect {
     public toEqual(expected: any) {
         const res = this.actual === expected;
         if (!res) {
-            console.warn("Warning : test 'to be equal to' not tested");
+            console.warn("Test 'to be equal to' not tested");
         }
         return res;
     }
@@ -117,6 +117,25 @@ class Expect {
             }
         }
     }
+
+    public toThrow(expected: any) {
+        let exception: Error;
+        if (typeof this.actual !== "function") {
+            throw new Error("Actual is not a function");
+        }
+        try {
+            this.actual();
+        } catch (e) {
+            exception = e;
+        }
+        if (exception) {
+            if (exception === undefined) {
+                console.error("Expected tested function to thow an error");
+            } else if (exception.message !== expected.message) {
+                console.error("Function throws " + exception.message + " and " + expected.message + " was expected");
+            }
+        }
+    }
 }
 
 /**
diff --git a/spec/structure/parallel_structure.spec.ts b/spec/structure/parallel_structure.spec.ts
index 74dce91f..980a852d 100644
--- a/spec/structure/parallel_structure.spec.ts
+++ b/spec/structure/parallel_structure.spec.ts
@@ -65,12 +65,13 @@ const ps2: ParallelStructure = new ParallelStructure(
 );
 
 // Ajout d'une structure de chaque type dans ParallelStructure
+const iLoiDebits: number[] = [];
 for (const s of EnumEx.getValues(StructureType)) {
     for (const i of EnumEx.getValues(LoiDebit)) {
         try {
             ps2.addStructure(CreateStructure(s, i));
-        }
-        catch (e) {
+            iLoiDebits.push(i);
+        } catch (e) {
             // Pour les combinaisons StructureType X LoiDebit impossibles rejetées par la Factory
         }
     }
@@ -82,22 +83,35 @@ ps2.prms.Q.v = ps2.Calc("Q").vCalc;
 describe("Class ParallelStructure: ", () => {
     for (let i = 0; i < ps2.structures.length; i++) {
         const st: Structure = ps2.structures[i];
-        describe(`this.structures[${i}]: Structure${LoiDebit[i]}: `, () => {
+        describe(`this.structures[${i}]: Structure${LoiDebit[iLoiDebits[i]]}: `, () => {
             // tslint:disable-next-line:forin
             for (const prm of st.prms) {
                 if (prm.calculability === ParamCalculability.DICHO &&
                     prm.symbol !== "Z1" && prm.symbol !== "Z2") {
-                    const ref: number = prm.v;
-                    const res: Result = ps2.Calc(i + "." + prm.symbol);
-                    prm.v = ref; // Go back to initial value for following tests
-                    if ((i === 2 || i === 4) && prm.symbol === "ZDV") {
-                        xit(`Calc(${prm.symbol}) should return ${ref}`, () => {
-                            checkResult(res, ref);
+
+                    if (prm.symbol === "W" && prm.v === Infinity) {
+                        // Le calcul de l'ouverture sur les seuils doit renvoyer une exception (cas impossible)
+                        it(`Calc(${prm.symbol}) should return exception`, () => {
+                            expect(
+                                () => { ps2.Calc(i + "." + prm.symbol); }
+                            ).toThrow(new Error("Structure:Calc : Calcul de W impossible sur un seuil"));
                         });
                     } else {
-                        it(`Calc(${prm.symbol}) should return ${ref}`, () => {
-                            checkResult(res, ref);
-                        });
+                        const ref: number = prm.v;
+                        const res: Result = ps2.Calc(i + "." + prm.symbol);
+                        if ((iLoiDebits[i] === 2 || iLoiDebits[i] === 4) && prm.symbol === "ZDV") {
+                            // Les lois CEM88D et CUNGE80 ne font pas intervenir ZDV dans le calcul d'un orifice noyé
+                            // TODO Il faudrait générer une erreur pour dire que le résultat est indéfini
+                            xit(`Calc(${prm.symbol}) should return undefined`, () => {
+                                checkResult(res, undefined);
+                            });
+                        } else {
+                            // On ne teste pas le calcul de l'ouverture sur les seuils
+                            it(`Calc(${prm.symbol}) should return ${ref}`, () => {
+                                checkResult(res, ref);
+                            });
+                        }
+                        prm.v = ref; // Go back to initial value for following tests
                     }
                 }
             }
diff --git a/spec/structure/structure.spec.ts b/spec/structure/structure.spec.ts
index f1c23d0a..4a4c22b8 100644
--- a/spec/structure/structure.spec.ts
+++ b/spec/structure/structure.spec.ts
@@ -67,6 +67,7 @@ describe("Class Structure: ", () => {
         });
         it("Q=0 => W=0", () => {
             structTest.prms.Q.v = 0;
+            structTest.prms.W.v = 1; // Sinon, W = Infinity => Structure = Seuil => Throw Error
             checkResult(structTest.Calc("W"), 0);
             expect(structTest.Calc("W").extraResults).toEqual(flagsNull);
             structTest.prms.Q.v = 1;
diff --git a/src/structure/structure.ts b/src/structure/structure.ts
index d55fc170..6a7a3ca3 100644
--- a/src/structure/structure.ts
+++ b/src/structure/structure.ts
@@ -67,11 +67,19 @@ export abstract class Structure extends Nub {
      * @param rPrec précision de calcul
      */
     public Calc(sVarCalc: string, rInit?: number, rPrec: number = 0.001): Result {
+        // Gestion de l'exception de calcul de W sur les seuils
+        if (rInit === undefined) {
+            rInit = this._prms.map[sVarCalc].v;
+        }
+        if (sVarCalc === "W" && rInit === Infinity) {
+            throw new Error("Structure:Calc : Calcul de W impossible sur un seuil");
+        }
+
         // Mise à jour de h1 et h2
         this.prms.update_h1h2();
 
         // Gestion du débit nul
-        const flagsNull = {Mode: StructureFlowMode.NULL, Regime: StructureFlowRegime.NULL};
+        const flagsNull = { Mode: StructureFlowMode.NULL, Regime: StructureFlowRegime.NULL };
         if (sVarCalc === "Q") {
             if (this.prms.h1.v === this.prms.h2.v || this.prms.W.v <= 0) {
                 return new Result(0, flagsNull);
@@ -79,13 +87,13 @@ export abstract class Structure extends Nub {
         } else if (this.prms.Q.v === 0) {
             // Débit nul <=> tirant d'eau amont = tirant d'eau aval ou tout autre paramètre nul
             switch (sVarCalc) {
-                case "Z1" :
-                return new Result(this.prms.h2.v, flagsNull);
-                case "Z2" :
-                return new Result(this.prms.h1.v, flagsNull);
-                default :
-                // Est-ce toujours vrai ? Nécessitera peut-être d'étendre la méthode
-                return new Result(0, flagsNull);
+                case "Z1":
+                    return new Result(this.prms.h2.v, flagsNull);
+                case "Z2":
+                    return new Result(this.prms.h1.v, flagsNull);
+                default:
+                    // Est-ce toujours vrai ? Nécessitera peut-être d'étendre la méthode
+                    return new Result(0, flagsNull);
             }
         } else if (this.prms.W.v === 0 && sVarCalc === "Z1") {
             return new Result(Infinity, flagsNull); // Si la vanne est fermée la cote amont est infinie
@@ -101,9 +109,9 @@ export abstract class Structure extends Nub {
                 let rPrm: number;
                 switch (sVarCalc) {
                     case "ZDV":
-                    rPrm = Infinity;
+                        rPrm = Infinity;
                     default:
-                    rPrm = 0;
+                        rPrm = 0;
                 }
                 // TODO Ajouter un message d'erreur
                 // "Les cotes et le débit ne sont pas cohérents => fermeture de l'ouvrage
@@ -146,7 +154,7 @@ export abstract class Structure extends Nub {
     /**
      * Give the flow mode : weir or orifice flow
      */
-    protected getFlowMode(): StructureFlowMode  {
+    protected getFlowMode(): StructureFlowMode {
         if (this.prms.h1.v > this.prms.W.v) {
             this.debug("Structure.getFlowMode(h1=" + this.prms.h1.v + ",W=" + this.prms.W.v + ")=ORIFICE");
             return StructureFlowMode.ORIFICE;
@@ -160,7 +168,7 @@ export abstract class Structure extends Nub {
     /**
      * Give the flow regime for a rectangular section : free, partially submerged or submerged flow
      */
-    protected getFlowRegime(): StructureFlowRegime  {
+    protected getFlowRegime(): StructureFlowRegime {
         // Weir have only two flow regimes: free and submerged flow
         // Orifice have three flow regimes: free, partially submerged and (totally) submerged
         if (this.prms.h2.v <= 2 / 3 * this.prms.h1.v) {
-- 
GitLab