Browse Source

Added Scalatest specs for ExtensionFunctions and ImplicitMethods

Abhinav Sarkar 10 years ago
parent
commit
5b4e39a2fe

+ 2
- 0
SpelHelper.iml View File

@@ -17,10 +17,12 @@
17 17
     <output-test url="file://$MODULE_DIR$/target/test-classes" />
18 18
     <content url="file://$MODULE_DIR$">
19 19
       <sourceFolder url="file://$MODULE_DIR$/target/generated-sources/annotations" isTestSource="false" />
20
+      <sourceFolder url="file://$MODULE_DIR$/target/generated-sources/test-annotations" isTestSource="false" />
20 21
       <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
21 22
       <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
22 23
       <sourceFolder url="file://$MODULE_DIR$/src/test/scala" isTestSource="true" />
23 24
       <excludeFolder url="file://$MODULE_DIR$/target/classes" />
25
+      <excludeFolder url="file://$MODULE_DIR$/target/site" />
24 26
       <excludeFolder url="file://$MODULE_DIR$/target/surefire-reports" />
25 27
       <excludeFolder url="file://$MODULE_DIR$/target/test-classes" />
26 28
     </content>

+ 1
- 1
src/main/java/net/abhinavsarkar/spelhelper/ImplicitMethodResolver.java View File

@@ -87,7 +87,7 @@ final class ImplicitMethodResolver implements MethodResolver {
87 87
                                     newArgumentTypes);
88 88
                     MethodExecutor wrappedExecutor = executor == null ? null
89 89
                             : new ImplicitMethodExecutor(executor);
90
-                    if(wrappedExecutor == null) {
90
+                    if (wrappedExecutor == null) {
91 91
                         CACHE.putIfAbsent(cacheKey, NULL_ME);
92 92
                     }
93 93
                     return wrappedExecutor;

+ 35
- 0
src/test/scala/net/abhinavsarkar/spelhelper/ExtensionFunctionsSpec.scala View File

@@ -0,0 +1,35 @@
1
+package net.abhinavsarkar.spelhelper
2
+
3
+import org.scalatest.junit.JUnitRunner
4
+import org.junit.runner.RunWith
5
+import org.scalatest.FlatSpec
6
+import org.scalatest.junit.ShouldMatchersForJUnit
7
+import java.util.{Set => JSet, HashSet,
8
+  List => JList, ArrayList,
9
+  Map => JMap, HashMap}
10
+
11
+@RunWith(classOf[JUnitRunner])
12
+class ExtensionFunctionsSpec extends FlatSpec with ShouldMatchersForJUnit {
13
+
14
+  "Extension Function 'list'" should "return a java.util.List " in {
15
+    val list: JList[String] = new ArrayList
16
+    List("a", "b", "c") foreach { list add _ }
17
+    new SpelHelper().evalExpression("#list('a','b','c')",
18
+      new {}, classOf[JList[String]]) should equal(list)
19
+  }
20
+
21
+  "Extension Function 'set'" should "return a java.util.Set " in {
22
+    val set: JSet[String] = new HashSet
23
+    List("a", "b", "c") foreach { set add _ }
24
+    new SpelHelper().evalExpression("#set('a','b','c')",
25
+      new {}, classOf[JSet[String]]) should equal(set)
26
+  }
27
+
28
+  "Extension Function 'map'" should "return a java.util.Map " in {
29
+    val map: JMap[String,Int] = new HashMap
30
+    List("a", "b", "c").zipWithIndex.foreach { x => map.put(x._1, x._2) }
31
+    new SpelHelper().evalExpression("#map(#list('a','b','c'),#list(0,1,2))",
32
+      new {}, classOf[JMap[String,Int]]) should equal(map)
33
+  }
34
+
35
+}

+ 52
- 0
src/test/scala/net/abhinavsarkar/spelhelper/ImplicitMethodsSpec.scala View File

@@ -0,0 +1,52 @@
1
+package net.abhinavsarkar.spelhelper
2
+
3
+import org.scalatest.junit.JUnitRunner
4
+import org.junit.runner.RunWith
5
+import org.scalatest.FlatSpec
6
+import org.scalatest.junit.ShouldMatchersForJUnit
7
+import java.util.{HashSet, Set => JSet, List => JList, ArrayList}
8
+
9
+@RunWith(classOf[JUnitRunner])
10
+class ImplicitMethodsSpec extends FlatSpec with ShouldMatchersForJUnit {
11
+
12
+  "Implicit Function 'distinct' on List" should
13
+      "return distinct items in a list " in {
14
+    val set: JSet[String] = new HashSet
15
+    set add "a"; set add "b"
16
+    new SpelHelper().evalExpression("#list('a','b','a').distinct()",
17
+      new {}, classOf[JSet[String]]) should equal(set)
18
+  }
19
+
20
+  "Implicit Function 'sorted' on List" should
21
+      "return a sorted list " in {
22
+    val list: JList[String] = new ArrayList
23
+    List("a", "b", "c") foreach { list add _ }
24
+    new SpelHelper().evalExpression("#list('c','b','a').sorted()",
25
+      new {}, classOf[JList[String]]) should equal(list)
26
+  }
27
+
28
+  "Implicit Function 'reversed' on List" should
29
+      "return a reversed list " in {
30
+    val list: JList[String] = new ArrayList
31
+    List("a", "b", "c") foreach { list add _ }
32
+    new SpelHelper().evalExpression("#list('c','b','a').reversed()",
33
+      new {}, classOf[JList[String]]) should equal(list)
34
+  }
35
+
36
+  "Implicit Function 'take' on List" should
37
+      "return a list containing first n items of a list " in {
38
+    val list: JList[String] = new ArrayList
39
+    List("a", "b", "c") foreach { list add _ }
40
+    new SpelHelper().evalExpression("#list('a','b','c','d').take(3)",
41
+      new {}, classOf[JList[String]]) should equal(list)
42
+  }
43
+
44
+  "Implicit Function 'drop' on List" should
45
+      "return a list containing items after the first n items of a list " in {
46
+    val list: JList[String] = new ArrayList
47
+    List("c", "d") foreach { list add _ }
48
+    new SpelHelper().evalExpression("#list('a','b','c','d').drop(2)",
49
+      new {}, classOf[JList[String]]) should equal(list)
50
+  }
51
+  
52
+}

Loading…
Cancel
Save