public class CounterTest { private Counter counter = new Counter("255"); private Element document = counter.toHtmlDocument(); @Test public void containsLinks() throws Exception { Element inc = document.findLinkByLabel("inc"); assertEquals("Inc has bad url", "?value=256", inc.getAttribute("href")); Element dec = document.findLinkByLabel("dec"); assertEquals("Dec has bad url", "?value=254", dec.getAttribute("href")); } @Test public void containsDisplay() throws Exception { Element display = document.findElementById("display"); assertEquals("Display shows wrong value", "0xff", display.contentsAsText()); } @Test public void defaultValueIsZero() throws Exception { document = new Counter(null).toHtmlDocument(); assertEquals("0x0", document.findElementById("display").contentsAsText()); } }
@Test public void countDownTest() { EventSource<Void> src1 = new EventSource<Void>(); EventSource<Void> src2 = new EventSource<Void>(); EventSource<Void> reset = new EventSource<Void>(); BiFunction<Integer, Void, Tuple2<Integer, Optional<String>>> countdown = (s, i) -> s == 1 ? t(3, Optional.of("COUNTDOWN REACHED")) : t(s - 1, Optional.empty()); EventStream<String> countdowns = StateMachine.init(3) .on(src1) .transmit(countdown) .on(src2) .transmit(countdown) .on(reset) .transition((s, i) -> 3) .toEventStream(); Counter counter = new Counter(); Subscription sub = countdowns.hook(x -> counter.inc()).pin(); src1.push(null); src2.push(null); assertEquals(0, counter.get()); src1.push(null); assertEquals(1, counter.getAndReset()); src2.push(null); src2.push(null); reset.push(null); assertEquals(0, counter.get()); src2.push(null); assertEquals(0, counter.get()); src1.push(null); assertEquals(0, counter.get()); src2.push(null); assertEquals(1, counter.getAndReset()); sub.unsubscribe(); src1.push(null); src1.push(null); src1.push(null); src1.push(null); src1.push(null); src1.push(null); assertEquals(0, counter.get()); }
public String doStuff() { counter.count().set(counter.count().get() + 1); return null; }