Saturday, 6 July 2013

Coding the Simplest Testing Framework (Java)

There are loads of testing frameworks out there. Some very complex, powerful and excellent. I wouldn't be without them, but they're potentially quite difficult for those unused to the concept. While working on an embedded project, with very few tools at my disposal, I quickly whacked together this simple testing framework and decided to blog it.

Essentially you would put this import at the top of your code:

import static net.intrepidis.library.SimplestTest.test;

Then you can use it like this:
        test(7+8).is(15);

What will happen is that if the test fails then an exception is thrown. Essentially that is all there is to it. Proper testing frameworks catch this and display very helpful messages at the end of all the tests. Please visit this other blog for some real-world examples:
Using TotallyLazy the functional library for Java

For a "proper" testing framework I recommend Hamcrest. Meanwhile, below is the actual SimplestTest code. As you can see, it's very simple... The bottom half is specifically for accepting TotallyLazy sequences.

package net.intrepidis.library;

import com.googlecode.totallylazy.Arrays;
import com.googlecode.totallylazy.Sequence;
import com.googlecode.totallylazy.Sequences;
import java.util.List;
import static com.googlecode.totallylazy.Sequences.sequence;

public class SimplestTest {
    public static <T> TestObject<T> test(T o) {
        return new TestObject<T>(o);
    }
    public static class TestObject<T> {
        private final T o;
        public TestObject(T o) {
            this.o = o;
        }
        public void is(T r) throws Exception {
            if (!toCheck(o).equals(r)) {
                throw new Exception("is but \"" + o + "\" != \"" + r + "\"");
            }
        }
        public void isnt(T r) throws Exception {
            if (toCheck(o).equals(r)) {
                throw new Exception("isnt but \"" + o + "\" == \"" + r + "\"");
            }
        }
        private static Object toCheck(Object o) {
            if (o instanceof CharSequence) {
                return o.toString();
            }
            return o;
        }
    }
    
    public static <T> TestSequence<T> test(Sequence<T> s) {
        return new TestSequence<T>(s);
    }
    public static class TestSequence<T> {
        private final Sequence<T> s;
        public TestSequence(Sequence<T> s) {
            this.s = s;
        }
        public void is(T...a) throws Exception {
            List<T> list = Arrays.list(a);
            if (!Sequences.equalTo(s, list)) {
                throw new Exception("is but " + s.toString() + " != " + sequence(a).toString());
            }
        }
        public void isnt(T...a) throws Exception {
            List<T> list = Arrays.list(a);
            if (Sequences.equalTo(s, list)) {
                throw new Exception("isnt but " + s.toString() + " == " + sequence(a).toString());
            }
        }
    }
}

This code is completely free to use by anybody. It is held under the Do What You Want To Public License: http://tinyurl.com/DWYWTPL

No comments:

Post a Comment