KarmaMessageProcessor implements MessageProcessor { @Override public void process(IncomeMessage message) { Matcher matcher = pattern.matcher(message.content()); StringBuilder replyMessage = new StringBuilder(); Set<String> evaluated = new HashSet<>(); while (matcher.find()) { String term = matcher.group("term"); String operation = matcher.group("operation"); if (!evaluated.contains(term.toLowerCase())) { int value = update(term, operation); replyMessage.append(buildReplyMessage(term, value)).append("\n"); evaluated.add(term.toLowerCase()); } } if (replyMessage.length() > 0) { replyMessage.deleteCharAt(replyMessage.length() - 1); message.sendBack(replyMessage.toString()); } } KarmaMessageProcessor(KarmaRepository repository); @Override void process(IncomeMessage message);  }
@Test public void testRaise() { Karma testKarma = new Karma("test"); Karma otherKarma = new Karma("other"); when(repository.find("test")).thenReturn(testKarma); when(repository.find("other")).thenReturn(otherKarma); Spec.given(processor) .expect(the(testKarma.value()), to().be(0)) .expect(the(otherKarma.value()), to().be(0)) .when(process("test++")) .expect(the(testKarma.value()), to().be(1)) .expect(the(otherKarma.value()), to().be(0)) .when(process("lorem ipsum test++, dolor")) .expect(the(testKarma.value()), to().be(2)) .expect(the(otherKarma.value()), to().be(0)) .when(process("lorem ipsum test++. dolor")) .expect(the(testKarma.value()), to().be(3)) .expect(the(otherKarma.value()), to().be(0)) .when(process("lorem ipsum test++! dolor")) .expect(the(testKarma.value()), to().be(4)) .expect(the(otherKarma.value()), to().be(0)) .when(process("lorem ipsum test++ test++ dolor")) .expect(the(testKarma.value()), to().be(5)) .expect(the(otherKarma.value()), to().be(0)); }
@Test public void testLower() { Karma testKarma = new Karma("test"); Karma otherKarma = new Karma("other"); when(repository.find("test")).thenReturn(testKarma); when(repository.find("other")).thenReturn(otherKarma); Spec.given(processor) .expect(the(testKarma.value()), to().be(0)) .expect(the(otherKarma.value()), to().be(0)) .when(process("test--")) .expect(the(testKarma.value()), to().be(-1)) .expect(the(otherKarma.value()), to().be(0)) .when(process("lorem ipsum test--, dolor")) .expect(the(testKarma.value()), to().be(-2)) .expect(the(otherKarma.value()), to().be(0)) .when(process("lorem ipsum test--. dolor")) .expect(the(testKarma.value()), to().be(-3)) .expect(the(otherKarma.value()), to().be(0)) .when(process("lorem ipsum test--! dolor")) .expect(the(testKarma.value()), to().be(-4)) .expect(the(otherKarma.value()), to().be(0)) .when(process("lorem ipsum test-- test-- dolor")) .expect(the(testKarma.value()), to().be(-5)) .expect(the(otherKarma.value()), to().be(0)); }
@Test public void testMultiple() { Karma testKarma = new Karma("test"); Karma otherKarma = new Karma("other"); when(repository.find("test")).thenReturn(testKarma); when(repository.find("other")).thenReturn(otherKarma); Spec.given(new KarmaMessageProcessor(repository)) .expect(the(testKarma.value()), to().be(0)) .expect(the(otherKarma.value()), to().be(0)) .when(process("test-- other++")) .expect(the(testKarma.value()), to().be(-1)) .expect(the(otherKarma.value()), to().be(1)) .when(process("test-- other++")) .expect(the(testKarma.value()), to().be(-2)) .expect(the(otherKarma.value()), to().be(2)) .when(process("test-- other--")) .expect(the(testKarma.value()), to().be(-3)) .expect(the(otherKarma.value()), to().be(1)) .when(process("test++ other++")) .expect(the(testKarma.value()), to().be(-2)) .expect(the(otherKarma.value()), to().be(2)) .when(process("test++ other--")) .expect(the(testKarma.value()), to().be(-1)) .expect(the(otherKarma.value()), to().be(1)) .when(process("test++ other--")) .expect(the(testKarma.value()), to().be(0)) .expect(the(otherKarma.value()), to().be(0)); }