As I've mentioned before, I quite like unit tests. At the hands of Adam I've learnt to love and respect them. So when I had to write a simple function to ensure a password:
- Is at least eight characters
- Contains an upper-case character
- Contains a lower-case character
- Contains a digit
- Contains a special character
I thought this would be a great reason to add some new unit tests in. Plus I'll be damned if I'm going to do all the typing on a phone to test and register all the various cases I can think of. As it happens this turned out to be a great idea because there was at least one case I'd missed.
Kids, unit tests work!
I thought I'd turn this into a challenge, how would you write the desired function? I'll provide the unit tests and you can impress me with your regex or your clever functions! Feel free to use any language you so desire. Winner gets the glory!
@SmallTest
public void testBlank(){
String password = "";
assertFalse(Utils.isPasswordValid(password));
}
@SmallTest
public void testLower(){
String password = "a";
assertFalse(Utils.isPasswordValid(password));
}
@SmallTest
public void testUpper(){
String password = "A";
assertFalse(Utils.isPasswordValid(password));
}
@SmallTest
public void testNumber(){
String password = "123";
assertFalse(Utils.isPasswordValid(password));
}
@SmallTest
public void testPadded(){
String password = " ";
assertFalse(Utils.isPasswordValid(password));
}
@SmallTest
public void testWhite(){
String password = "\t\t";
assertFalse(Utils.isPasswordValid(password));
}
@SmallTest
public void testGood(){
String password = "ab12CD*!";
assertTrue(Utils.isPasswordValid(password));
}
@SmallTest
public void testNoUpper(){
String password = "ab12cd*!";
assertFalse(Utils.isPasswordValid(password));
}
@SmallTest
public void testNoLower(){
String password = "AB12CD*!";
assertFalse(Utils.isPasswordValid(password));
}
@SmallTest
public void testNoNumber(){
String password = "abcdCD*!";
assertFalse(Utils.isPasswordValid(password));
}
@SmallTest
public void testNoSymbol(){
String password = "abcdCD12";
assertFalse(Utils.isPasswordValid(password));
}
@SmallTest
public void testGoodLong(){
String password = "abcdCD12*&asdbbs167672HGSAHGAS!&^";
assertTrue(Utils.isPasswordValid(password));
}
@SmallTest
public void testGoodOne(){
String password = "aaaaaA1\"";
assertTrue(Utils.isPasswordValid(password));
}