Type Systems
Automated Testing
Static analysis
Human review
Human review
describe("email validator", function(){
it("should bail if the first argument is not a sting", function(){
expect(isValidEmail(3)).to.be.false;
});
it("should validate", function(){
expect(isValidEmail("foo@bar.com")).to.be.true;
});
it("should validate an array of strings if provided", function(){
expect(isValidEmail(["foo@bar.com", "baz@foo.net"])).to.be.true;
});
});expect(isValidEmail(3)).to.be.false;
expect(isValidEmail(3)).to.throw.an.error
("isValidEmail expected a string, got a Number");
expect(isValidEmail(["foo@bar.com", "baz@foo.net"]))
.to.be.true;
expect(isValidEmail(
["foo@bar.com", "baz@foo.net", "jim.jeff@jefferson.net", "cindy@cindyweb.it", ...]))
[ "foo@bar.com"
, "baz@foo.net"
, "jim.jeff@jefferson.net"
, "cindy@cindyweb.it"
, ...]))
//missing
"erica@统统.c"
it("should place the third argument as the age of the User object", function(){
var age = 5;
var user = new User("Jennifer", "female", age);
expect(user.age).to.equal(age);
});it("should place the third argument as the age of the User object", function(){
var age = _.random(0, 10000);
var user = new User("Jennifer", "female", age);
expect(user.age).to.equal(age);
});it("should place the third argument as the age of the User object", function(){
var age = null;
var user = null;
for(var i = 0; i < 100; i++){
age = _.random(0, 10000);
user = new User("Jennifer", "female", age);
expect(user.age).to.equal(age);
}
});JSC.test(
"Third argument is age",
function (verdict, age) {
return verdict(new User("jennifer", "female", age));
},
[ JSC.integer(10) ],
function (age, user) {
return user.age == age;
}
);testcheck.check(testcheck.property(
[gen.int],
function (age) {
var user = new User("jennifer", "female", age);
return user.age == age;
}
));expect(isValidEmail(3)).to.be.false;
expect(isValidEmail(3)).to.throw.an.error
("isValidEmail expected a string, got a Number");
(no I am not prepared to substantiate my opinion here)
And since PureScript is typed, the problem space is cleanly limited. Developers do not need to
handle mis-matched types.
it "the third argument should be age on the user object" $
let
checkAge :: Number -> Boolean
checkAge n = (newUser "jennifer" "female" n).age == n
in quickCheck checkAgeit "the third argument should be age on the user object" $
let
checkAge :: Number -> Result
checkAge n = let user = newUser "jennifer" "female" n
in user.age == n
<?> "Age test, it failed, FAILED!"
<> "\n when n = " <> show n
<> "\n newUser produced : "
<> "\n " <> show user
in quickCheck checkAge(<?>) :: Boolean -> String -> Resultinstance userArb :: Arbitrary User where
arbitrary = newUser <$> arbitrary
<*> arbitrary
<*> arbitrarycheckYounger :: User -> User -> Boolean
checkYounger u@(User {age = a}) u'@(User {age = a'}) =
let res = younger u u'
in if a < a' then res == true else res == false
quickCheck checkYounger instance userCoarb :: CoArbitrary User where
coarbitrary (User {age = a}) = coarbitrary a checkReadWriteFancyRef :: (User -> User) -> User -> Boolean
checkReadWriteFancyRef arrow user = extract $ do
userRef <- newFancyRef user
writeFancyRef userRef arrow
user' <- readFancyRef userRef
return $ user == user'
quickCheck checkReadWriteFancyRef