SpartanRefactoring / Main

Eclipse plugin that performs automatic refactoring of Java source code, making it shorter, more idiomatic and more readable
https://www.spartan.org.il
100 stars 56 forks source link

Testing Refactorings #1644

Open dormaayan opened 5 years ago

dormaayan commented 5 years ago

List of steps for test refactoring:

  1. Convert simple non fluent Junit to simple assertions (TODO: add list)
  2. Convert harmcrest to arithmetical expressions
  3. Consolidate consequtive assertions
  4. Consolidate methods with common setup and different tests
  5. Remove verification of non-exception
  6. Use expected exception instead of try \ catch
  7. Split independent tests
  8. Sane paramatrization of tests
yossigil commented 5 years ago


import java.math.BigInteger;
import java.util.*;

import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.IntFunction;
import java.util.function.BiConsumer;

interface Selfie<Self extends Selfie<Self>> {
}

class OO<_1, _2> {
    _1 _1;
    _2 _2;

    public OO(_1 _1, _2 _2) {
        this._1 = _1;
        this._2 = _2;
    }
}

class OI<_1> {
    _1 _1;
    int _2;

    public OI(_1 _1, int _2) {
        this._1 = _1;
        this._2 = _2;
    }
}

class OOs<_1, _2> extends ArrayList<OO<_1, _2>> implements Selfie<OOs<_1, _2>> {
    private static final long serialVersionUID = 1L;
    private List<BiConsumer<_1, _2>> tests = new ArrayList<>();

    public OOs(BiConsumer<_1, _2> f) {
        tests.add(f);

    }

    OOs<_1, _2> against(_1 _1, _2 _2) {
        add(new OO<>(_1, _2));
        return this;
    }

    OOs<_1, _2> and(_1 _1, _2 _2) {
        return against(_1, _2);
    }

    public OOs<_1, _2> go() {
        return this;
    }

    public OOs<_1, _2> to(BiConsumer<A, B> f) {
        return this;
    }
}

class OIs<_1, _2> extends ArrayList<OI<_1>> {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    OIs<_1, _2> $(_1 _1, int _2) {
        add(new OI<>(_1, _2));
        return this;
    }

    public OIs<_1, _2> to(BiConsumer<A, B> f, BiConsumer<A, B>... fs) {
        // TODO Auto-generated method stub
        return this;
    }
}

class A {
}

class B {
}

class Main {
    public static void testFib(Integer n, BigInteger b) {
        assert fib(n).equals(b);
    }

    public static BigInteger fib(Integer n) {
        if (n == null)
            return BigInteger.ZERO;
        return fib1(n.intValue());
    }

    public static BigInteger sqrt(Integer n) {
        if (n == null)
            return BigInteger.ZERO;
        return fib1(n.intValue());
    }

    public static BigInteger fib1(int n) {
        if (n <= 1)
            return BigInteger.ZERO;
        else
            return fib1(n - 1).add(fib1(n - 2));
    }

    static int inc(int a) {
        return a + 1;
    }

    public static void main(String[] args) {
        Tuples.to(Main::testFib).against(12, BigInteger.ZERO).and(3, BigInteger.valueOf(3))
                .and(4, BigInteger.valueOf(4)).go();
        Tuples.to(Main::inc).of(0, 1).of(1, 2).of(1, 2);
    }

    static void f(A a, B b) {
    }

    static void g(A a, B b) {
    };

    static B h(A a) {
        return null;
    };
}

class Tuples {
    static <A, B> OOs<A, B> to(BiConsumer<A, B> f) {
        return new OOs<A, B>(f);
    }

    static <A> Os<A> to2(Consumer<A> f) {
        return new Os<A>();
    }

    static IIs to(IntFunction f) {
        return new IIs(f);
    }

}

class IIs {

    private IntFunction f;

    public IIs(IntFunction f) {
        this.f = f;
    }

    public IIs of(int a, int b) {
        return this;
    }

}

class Os<O> {
}

class MapTest {
    static {
        Tuples.to2(MapTest::MapTest);
    }

    private Map<String, String> map;

    MapTest(Map<String, String> map) {
        this.map = map;
    }
}
`