Skip to content

Commit 426be51

Browse files
committed
Add java8 unit test examples.
1 parent 342e553 commit 426be51

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright 2015 Valentyn Kolesnikov
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package com.github.underscore;
25+
26+
import java.lang.reflect.Method;
27+
import java.util.*;
28+
import org.junit.Test;
29+
import static java.util.Arrays.asList;
30+
import static org.junit.Assert.assertEquals;
31+
32+
/**
33+
* Underscore library unit test.
34+
*
35+
* @author Valentyn Kolesnikov
36+
*/
37+
public class ChainingTest {
38+
39+
/*
40+
var stooges = [{name: 'curly', age: 25}, {name: 'moe', age: 21}, {name: 'larry', age: 23}];
41+
var youngest = _.chain(stooges)
42+
.sortBy(function(stooge){ return stooge.age; })
43+
.map(function(stooge){ return stooge.name + ' is ' + stooge.age; })
44+
.first()
45+
.value();
46+
=> "moe is 21"
47+
*/
48+
@Test
49+
@SuppressWarnings("unchecked")
50+
public void chain() {
51+
final List<Map<String, Object>> stooges = new ArrayList<Map<String, Object>>() { {
52+
add(new LinkedHashMap<String, Object>() { { put("name", "curly"); put("age", 25); } });
53+
add(new LinkedHashMap<String, Object>() { { put("name", "moe"); put("age", 21); } });
54+
add(new LinkedHashMap<String, Object>() { { put("name", "larry"); put("age", 23); } });
55+
} };
56+
final String youngest = $.chain(stooges)
57+
.sortBy((item) -> {
58+
return (Integer) ((Map<String, Object>) item).get("age");
59+
}
60+
)
61+
.map((item) -> {
62+
return item.get("name") + " is " + item.get("age");
63+
}
64+
)
65+
.first().item();
66+
assertEquals("moe is 21", youngest);
67+
}
68+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright 2015 Valentyn Kolesnikov
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
package com.github.underscore;
25+
26+
import java.util.*;
27+
import org.junit.Test;
28+
import static java.util.Arrays.asList;
29+
import static org.junit.Assert.assertArrayEquals;
30+
import static org.junit.Assert.assertEquals;
31+
import static org.junit.Assert.assertFalse;
32+
import static org.junit.Assert.assertTrue;
33+
import static org.junit.Assert.fail;
34+
35+
/**
36+
* Underscore library unit test.
37+
*
38+
* @author Valentyn Kolesnikov
39+
*/
40+
public class UnderscoreTest {
41+
42+
@Test
43+
@SuppressWarnings("unchecked")
44+
public void stackoverflow() {
45+
// http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java?rq=1
46+
assertEquals("{D=67.3, B=67.4, C=67.4, A=99.5}", $.chain((new LinkedHashMap<String, Double>() { {
47+
put("A", 99.5);
48+
put("B", 67.4);
49+
put("C", 67.4);
50+
put("D", 67.3);
51+
} }).entrySet()).sortBy((item) -> item.getValue()).toMap().item().toString());
52+
}
53+
}

0 commit comments

Comments
 (0)