Skip to content

Commit 250693b

Browse files
committed
Add unit test to convert and filter list of strings.
1 parent 37435dc commit 250693b

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/test/java/com/github/underscore/UnderscoreTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,4 +352,46 @@ public void stackoverflow5() {
352352
assertEquals("[107, 108, 109]", $.difference(selected, $.intersection(original, selected)).toString());
353353
assertEquals("[12, 17, 101]", $.difference(original, selected).toString());
354354
}
355+
356+
@Test
357+
@SuppressWarnings("unchecked")
358+
public void jobtest() {
359+
String[] strings = {
360+
"Sound boy proceed to blast into the galaxy",
361+
"Go back rocket man into the sky you'll see",
362+
"Hear it all the time, come back rewind",
363+
"Aliens are watching up in the sky",
364+
"Sound boy process to blast into the galaxy",
365+
"No one gonna harm you",
366+
"They all want you to play I watch the birds of prey"
367+
};
368+
List<Map<String, Object>> result = (List<Map<String, Object>>) $.chain(asList(strings))
369+
.map(
370+
new Function1<String, Map<String, Object>>() {
371+
public Map<String, Object> apply(String item) {
372+
Map<String, Object> resultItem = new LinkedHashMap<String, Object>();
373+
resultItem.put("string", item);
374+
resultItem.put("longestWord", $.chain(asList(item.split("\\s+"))).map(
375+
new Function1<String, Integer>() {
376+
public Integer apply(String item) {
377+
return item.length();
378+
}
379+
})
380+
.max().item());
381+
return resultItem;
382+
}
383+
})
384+
.sortBy(new Function1<Map<String, Object>, Integer>() {
385+
public Integer apply(Map<String, Object> item) {
386+
return -((Integer) item.get("longestWord"));
387+
}
388+
})
389+
.limit(5)
390+
.value();
391+
assertEquals("[{string=Aliens are watching up in the sky, longestWord=8}, "
392+
+ "{string=Sound boy proceed to blast into the galaxy, longestWord=7}, "
393+
+ "{string=Sound boy process to blast into the galaxy, longestWord=7}, "
394+
+ "{string=Go back rocket man into the sky you'll see, longestWord=6}, "
395+
+ "{string=Hear it all the time, come back rewind, longestWord=6}]", result.toString());
396+
}
355397
}

0 commit comments

Comments
 (0)