Search in sources :

Example 1 with CtBiScannerDefault

use of spoon.reflect.visitor.CtBiScannerDefault in project spoon by INRIA.

the class MainTest method checkContractCtScanner.

private void checkContractCtScanner(CtPackage pack) {
    class Counter {

        int scan, enter, exit = 0;
    }
    final Counter counter = new Counter();
    final Counter counterInclNull = new Counter();
    new CtScanner() {

        @Override
        public void scan(CtElement element) {
            counterInclNull.scan++;
            if (element != null) {
                counter.scan++;
            }
            super.scan(element);
        }

        @Override
        public void enter(CtElement element) {
            counter.enter++;
            super.enter(element);
        }

        @Override
        public void exit(CtElement element) {
            counter.exit++;
            super.exit(element);
        }
    }.scan(pack);
    // contract: when enter is called, exit is also called
    assertTrue(counter.enter == counter.exit);
    // contract: all scanned elements call enter
    assertTrue(counter.enter == counter.scan);
    Counter counterBiScan = new Counter();
    class ActualCounterScanner extends CtBiScannerDefault {

        @Override
        public void biScan(CtElement element, CtElement other) {
            counterBiScan.scan++;
            if (element == null) {
                if (other != null) {
                    Assert.fail("element can't be null if other isn't null.");
                }
            } else if (other == null) {
                Assert.fail("other can't be null if element isn't null.");
            } else {
                // contract: all elements have been cloned and are still equal
                assertEquals(element, other);
                assertFalse(element == other);
            }
            super.biScan(element, other);
        }
    }
    final ActualCounterScanner actual = new ActualCounterScanner();
    actual.biScan(pack, pack.clone());
    // contract: scan and biscan are executed the same number of times
    assertEquals(counterInclNull.scan, counterBiScan.scan);
    // for pure beauty: parallel visit of the same tree!
    Counter counterBiScan2 = new Counter();
    new CtBiScannerDefault() {

        @Override
        public void biScan(CtElement element, CtElement other) {
            counterBiScan2.scan++;
            // we have the exact same element
            assertSame(element, other);
            super.biScan(element, other);
        }
    }.biScan(pack, pack);
    // contract: scan and biscan are executed the same number of times
    assertEquals(counterInclNull.scan, counterBiScan2.scan);
}
Also used : CtBiScannerDefault(spoon.reflect.visitor.CtBiScannerDefault) CtElement(spoon.reflect.declaration.CtElement) CtScanner(spoon.reflect.visitor.CtScanner)

Aggregations

CtElement (spoon.reflect.declaration.CtElement)1 CtBiScannerDefault (spoon.reflect.visitor.CtBiScannerDefault)1 CtScanner (spoon.reflect.visitor.CtScanner)1