jeudi 25 mai 2017

Jasmine Cannot read property 'length' of undefined

Hi am a newbie to Unit Testing I have written a program which takes 3 arguments two romanletters and one operator(+,-,/,*) it should perform operations and it should return the value in romanLetters am trying to write a unit testing for that problem it says cannot read property length of undefined i dont know why is happening my code runs fine though. Am attaching my code here

Mainjs File

var main=function(operand1,operand2,operator) {
    var concat,number;
    var operand1=romantonumerals(operand1);
    var operand2=romantonumerals(operand2);
    //var operator=operatorconversion(operator);
    console.log("operand1 "+operand1);
    console.log("operand2 "+operand2);
    if(operand1>operand2) {
      concat=operand1+operator+operand2;
    }
    else {
      concat=operand2+operator+operand1;
    }
    number=Math.round(eval(concat));
    console.log(number);
    if(number<500) {
      result=numeralstoroman(number)
    }
    else if(number===0) {
      result="Make sure its greater than 0";
    }
    else {
      result="Make sure your number gets less than 500 try with some other romans";
    }
    console.log(result);
    return result;
  };
  var numeralstoroman=function(number) {
    var roman="",temp;
    var ones=function(number) {
      switch (number) {
        case 1:
          roman+="I";
          break;
        case 2:
          roman+="II";
          break;
        case 3:
          roman+="III";
          break;
        case 4:
          roman+="IV";
          break;
        case 5:
          roman+="V";
          break;
        case 6:
          roman+="VI";
          break;
        case 7:
          roman+="VII";
          break;
        case 8:
          roman+="VIII";
          break;
        case 9:
          roman+="IX";
          break;
      }
    };
    var twos=function(number) {
      if(number>=10 && number<20) {
        roman+="X";
        ones(number%10);
      }
      else if(number>=20 && number30) {
        roman+="XX";
        ones(number%10);
      }
      else if(number>=30 && number<40) {
        roman+="XXX";
        ones(number%10);
      }
      else if(number>=40 && number<50) {
        roman+="XL";
        ones(number%10);
      }
      else if(number>=50 && number<60) {
        roman+="L";
        ones(number%10);
      }
      else if(number>=60 && number<70) {
        roman+="LX";
        ones(number%10);
      }
      else if(number>=70 && number<80) {
        roman+="LXX";
        ones(number%10);
      }
      else if(number>=80 && number<90) {
        roman+="LXXX";
        ones(number%10);
      }
      else if(number>=90 && number<100) {
        roman+="XC";
        ones(number%10);
      }
    };
    var threes=function(number) {
      if(number>=100 && number<200) {
          roman+="C";
          if(number%100<10) {
            ones(number%10);
          }
          else {
            twos(number%100);
          }
      }
      else if(number>=200 && number<300) {
          roman+="CC";
          if(number%100<10) {
            ones(number%10);
          }
          else {
            twos(number%100);
          }
      }
      else if(number>=300 && number<400) {
          roman+="CCC";
          if(number%100<10) {
            ones(number%10);
          }
          else {
            twos(number%100);
          }
      }
      else if(number>=400 && number<500) {
          roman+="CD";
          if(number%100<10) {
            ones(number%10);
          }
          else {
            twos(number%100);
          }
      }
    }
    if(number<10) {
      ones(number);
    }
    else if(number>=10 && number<100) {
      twos(number);
    }
    else if(number>=100 && number<500) {
      threes(number);
    }
    console.log(roman);
    return roman;
  };
  var character=function(str) {
    switch(str) {
      case "I":
        return 1
      case "V":
        return 5
      case "X":
        return 10
      case "L":
        return 50
      case "C":
        return 100
      case "D":
        return 500
      default:
        return -1;
    }
  }
  var romantonumerals=function(operand) {
    var no=0,i=0,char1="",char2="";
    while(i<operand.length) {
      char1=character(operand[i]);
      if(i+1<operand.length) {
        char2=character(operand[i+1]);
        if(char1>=char2) {
          no+=char1;
          i++;
        }
        else {
          no=no+(char2-char1);
          i+=2;
        }
      }
      else {
        no+=char1;
        i++;
      }
    }
    return no;
  };
  var ans=main("V","I","+");
  console.log(ans);

JasmineHTML FIle

<!DOCTYPE html>
  <html>
  <head>
    <meta charset="utf-8">
    <title>Jasmine Spec Runner v2.2.0</title>

    <link rel="shortcut icon" type="image/png" href="lib/jasmine-2.2.0/jasmine_favicon.png">
    <link rel="stylesheet" href="lib/jasmine-2.2.0/jasmine.css">

    <script src="lib/jasmine-2.2.0/jasmine.js"></script>
    <script src="lib/jasmine-2.2.0/jasmine-html.js"></script>
    <script src="lib/jasmine-2.2.0/boot.js"></script>

    <!-- include source files here... -->
    <script src="romtantonumeralstoroman.js"></script>
    <!-- include spec files here... -->
    <script src="spec.js"></script>


  </head>

  <body>
  </body>
  </html>

Jasmine Testing File

describe("A suite", function() {
it("contains spec with an expectation", function() {
    main();
    expect(main("V","I","+").toBe("VI"));
});
});

JSFIDDLE Link

http://ift.tt/2rCf4tA

Aucun commentaire:

Enregistrer un commentaire