### Question:
Template { public String resolve(String s) { final StringBuilder sb = new StringBuilder(s); int position = 0; int start; while ((start = sb.indexOf("{{", position)) >= 0) { int end = sb.indexOf("}}",start); if( end > 0 ) { final var key = sb.substring(start + 2, end); if( params.containsKey(key) && params.get(key) != null ){ final var value = params.get(key); sb.replace(start,end+2,value); position = start + value.length(); }else{ position = end + 2; } }else{ break; } } return sb.toString(); } Template(Map<String, String> params); String resolve(String s);  }### Answer:
@Test @DisplayName("When there are no params every string just returns as is") void emptyTest(){ final var sut = new Template(JVM8Tools.asMap()); final var samples = Arrays.asList("alma", "{{kirte}}", "just {a{{nithing"); for( final var sample : samples) { Assertions.assertEquals(sample, sut.resolve(sample)); } }
@Test @DisplayName("When there are params they are replaced") void goodTest(){ final var sut = new Template(JVM8Tools.asMap("a","b", "huhh","spooky")); Assertions.assertEquals("this is a spooky baboon", sut.resolve("this is a {{huhh}} {{a}}a{{a}}oon")); }
@Test @DisplayName("Parameters replaces also at the start") void startTest(){ final var sut = new Template(JVM8Tools.asMap("a", "b", "huhh", "spooky")); Assertions.assertEquals("bthis is {{a...}} spooky bab{{oon", sut.resolve("{{a}}this is {{a...}} {{huhh}} {{a}}a{{a}}{{oon")); }
@Test @DisplayName("When there are params they are replaced but not the undefined") void goodTestStill(){ final var sut = new Template(JVM8Tools.asMap("a","b", "huhh","spooky")); Assertions.assertEquals("this is {{a...}} spooky baboon", sut.resolve("this is {{a...}} {{huhh}} {{a}}a{{a}}oon")); }
@Test @DisplayName("Unterminated placeholders are handled gracefully") void unterminatedTest(){ final var sut = new Template(JVM8Tools.asMap("a","b", "huhh","spooky")); Assertions.assertEquals("this is {{a...}} spooky bab{{oon", sut.resolve("this is {{a...}} {{huhh}} {{a}}a{{a}}{{oon")); }