vendredi 24 février 2017

How to write a test case for my code

Is there a way for me to write a test case for my code? Do i need to remove anything before i test it using a test suite?

I am new to testing and would like to know i can test the code below.

var hello = "Hello, ";

function greet(name){

//Requirement UpperCase
function upperCase(){
if (name.toUpperCase() === name) { //uppercase string
  console.log(hello.toUpperCase() + name + '!');
} 
else {
  console.log(hello + name + ".");
}

}
//Requirement last element
function namesArray(){
if (name.length > 1){
var lastElement = name.pop();
console.log(hello + name + " and " + lastElement + ".");
}

else{
  console.log(hello + name + ".");
}

}

//Comparing name//

if (name == null) {
console.log(hello + "my friend.")
}
else if(typeof name === 'string'){//will accept strings and return them.
upperCase();
}
else if (Array.isArray(name)){//will accept arrays and return them.
namesArray();
}

}

greet()
greet("James")
greet("Ruth");
greet(["Barry", "Kate"])
greet(["Kerry", "Mike", "Snake", "FOX"])

Aucun commentaire:

Enregistrer un commentaire