@@ -11,12 +11,8 @@ object DoSomethingMacro {
11
11
12
12
val r = c.Expr [S ] {
13
13
c.macroApplication match {
14
- case q " $_.DoSomethingOps[ $_]( $v).willBe( $_.returned).by[ $_]( $obj. $method[.. $targs](... $args))( $_) " =>
15
- val newArgs = args.map(a => transformArgs(c)(a))
16
- q " _root_.org.mockito.MockitoSugar.doReturn( $v).when( $obj). $method[.. $targs](... $newArgs) "
17
-
18
- case q " $_.DoSomethingOps[ $_]( $v).willBe( $_.returned).by[ $_]( $obj. $method[.. $targs])( $_) " =>
19
- q " _root_.org.mockito.MockitoSugar.doReturn( $v).when( $obj). $method[.. $targs] "
14
+ case q " $_.DoSomethingOps[ $_]( $v).willBe( $_.returned).by[ $_]( $invocation)( $_) " =>
15
+ transformInvocation(c)(invocation, q " _root_.org.mockito.MockitoSugar.doReturn( $v) " )
20
16
21
17
case o => throw new Exception (s " Couldn't recognize ${show(o)}" )
22
18
}
@@ -30,12 +26,8 @@ object DoSomethingMacro {
30
26
31
27
val r = c.Expr [S ] {
32
28
c.macroApplication match {
33
- case q " $_. $cls[ $_]( $v).willBe( $_.returnedF).by[ $f, $s]( $obj. $method[.. $targs](... $args))(.. $_) " if cls.toString.startsWith(" DoSomethingOps" ) =>
34
- val newArgs = args.map(a => transformArgs(c)(a))
35
- q " _root_.org.mockito. ${packageName(c)(cls)}. ${className(c)(cls, " Mockito" )}.doReturnF[ $f, $s]( $v)( $F).when( $obj). $method[.. $targs](... $newArgs) "
36
-
37
- case q " $_. $cls[ $_]( $v).willBe( $_.returnedF).by[ $f, $s]( $obj. $method[.. $targs])(.. $_) " if cls.toString.startsWith(" DoSomethingOps" ) =>
38
- q " _root_.org.mockito. ${packageName(c)(cls)}. ${className(c)(cls, " Mockito" )}.doReturnF[ $f, $s]( $v)( $F).when( $obj). $method[.. $targs] "
29
+ case q " $_. $cls[ $_]( $v).willBe( $_.returnedF).by[ $f, $s]( $invocation)(.. $_) " if cls.toString.startsWith(" DoSomethingOps" ) =>
30
+ transformInvocation(c)(invocation, q " _root_.org.mockito. ${packageName(c)(cls)}. ${className(c)(cls, " Mockito" )}.doReturnF[ $f, $s]( $v) " )
39
31
40
32
case o => throw new Exception (s " Couldn't recognize ${show(o)}" )
41
33
}
@@ -49,12 +41,8 @@ object DoSomethingMacro {
49
41
50
42
val r = c.Expr [S ] {
51
43
c.macroApplication match {
52
- case q " $_. $cls[ $_]( $v).willBe( $_.returnedFG).by[ $f, $g, $s]( $obj. $method[.. $targs](... $args))(.. $_) " if cls.toString.startsWith(" DoSomethingOps" ) =>
53
- val newArgs = args.map(a => transformArgs(c)(a))
54
- q " _root_.org.mockito. ${packageName(c)(cls)}. ${className(c)(cls, " Mockito" )}.doReturnFG[ $f, $g, $s]( $v)( $F, $G).when( $obj). $method[.. $targs](... $newArgs) "
55
-
56
- case q " $_. $cls[ $_]( $v).willBe( $_.returnedFG).by[ $f, $g, $s]( $obj. $method[.. $targs])(.. $_) " if cls.toString.startsWith(" DoSomethingOps" ) =>
57
- q " _root_.org.mockito. ${packageName(c)(cls)}. ${className(c)(cls, " Mockito" )}.doReturnFG[ $f, $g, $s]( $v)( $F, $G).when( $obj). $method[.. $targs] "
44
+ case q " $_. $cls[ $_]( $v).willBe( $_.returnedFG).by[ $f, $g, $s]( $invocation)(.. $_) " if cls.toString.startsWith(" DoSomethingOps" ) =>
45
+ transformInvocation(c)(invocation, q " _root_.org.mockito. ${packageName(c)(cls)}. ${className(c)(cls, " Mockito" )}.doReturnFG[ $f, $g, $s]( $v) " )
58
46
59
47
case o => throw new Exception (s " Couldn't recognize ${show(o)}" )
60
48
}
@@ -68,12 +56,8 @@ object DoSomethingMacro {
68
56
69
57
val r = c.Expr [S ] {
70
58
c.macroApplication match {
71
- case q " $_. $cls[.. $_]( $v).willBe( $_.answered).by[ $_]( $obj. $method[.. $targs](... $args))( $_) " if cls.toString.startsWith(" DoSomethingOps" ) =>
72
- val newArgs = args.map(a => transformArgs(c)(a))
73
- q " _root_.org.mockito.MockitoSugar.doAnswer( $v).when( $obj). $method[.. $targs](... $newArgs) "
74
-
75
- case q " $_. $cls[.. $_]( $v).willBe( $_.answered).by[ $_]( $obj. $method[.. $targs])( $_) " if cls.toString.startsWith(" DoSomethingOps" ) =>
76
- q " _root_.org.mockito.MockitoSugar.doAnswer( $v).when( $obj). $method[.. $targs] "
59
+ case q " $_. $cls[.. $_]( $v).willBe( $_.answered).by[ $_]( $invocation)( $_) " if cls.toString.startsWith(" DoSomethingOps" ) =>
60
+ transformInvocation(c)(invocation, q " _root_.org.mockito.MockitoSugar.doAnswer( $v) " )
77
61
78
62
case o => throw new Exception (s " Couldn't recognize ${show(o)}" )
79
63
}
@@ -87,12 +71,8 @@ object DoSomethingMacro {
87
71
88
72
val r = c.Expr [S ] {
89
73
c.macroApplication match {
90
- case q " $_. $cls[.. $ftargs]( $v).willBe( $_.answeredF).by[ $f, $_]( $obj. $method[.. $targs](... $args))(.. $_) " if cls.toString.startsWith(" DoSomethingOps" ) =>
91
- val newArgs = args.map(a => transformArgs(c)(a))
92
- q " _root_.org.mockito. ${packageName(c)(cls)}. ${className(c)(cls, " Mockito" )}.doAnswerF[ $f, .. $ftargs]( $v).when( $obj). $method[.. $targs](... $newArgs) "
93
-
94
- case q " $_. $cls[.. $ftargs]( $v).willBe( $_.answeredF).by[ $f, $_]( $obj. $method[.. $targs])(.. $_) " if cls.toString.startsWith(" DoSomethingOps" ) =>
95
- q " _root_.org.mockito. ${packageName(c)(cls)}. ${className(c)(cls, " Mockito" )}.doAnswerF[ $f, .. $ftargs]( $v).when( $obj). $method[.. $targs] "
74
+ case q " $_. $cls[.. $ftargs]( $v).willBe( $_.answeredF).by[ $f, $_]( $invocation)(.. $_) " if cls.toString.startsWith(" DoSomethingOps" ) =>
75
+ transformInvocation(c)(invocation, q " _root_.org.mockito. ${packageName(c)(cls)}. ${className(c)(cls, " Mockito" )}.doAnswerF[ $f, .. $ftargs]( $v) " )
96
76
97
77
case o => throw new Exception (s " Couldn't recognize ${show(o)}" )
98
78
}
@@ -106,12 +86,8 @@ object DoSomethingMacro {
106
86
107
87
val r = c.Expr [S ] {
108
88
c.macroApplication match {
109
- case q " $_. $cls[.. $ftargs]( $v).willBe( $_.answeredFG).by[ $f, $g, $_]( $obj. $method[.. $targs](... $args))(.. $_) " if cls.toString.startsWith(" DoSomethingOps" ) =>
110
- val newArgs = args.map(a => transformArgs(c)(a))
111
- q " _root_.org.mockito. ${packageName(c)(cls)}. ${className(c)(cls, " Mockito" )}.doAnswerFG[ $f, $g, .. $ftargs]( $v).when( $obj). $method[.. $targs](... $newArgs) "
112
-
113
- case q " $_. $cls[.. $ftargs]( $v).willBe( $_.answeredFG).by[ $f, $g, $_]( $obj. $method[.. $targs])(.. $_) " if cls.toString.startsWith(" DoSomethingOps" ) =>
114
- q " _root_.org.mockito. ${packageName(c)(cls)}. ${className(c)(cls, " Mockito" )}.doAnswerFG[ $f, $g, .. $ftargs]( $v).when( $obj). $method[.. $targs] "
89
+ case q " $_. $cls[.. $ftargs]( $v).willBe( $_.answeredFG).by[ $f, $g, $_]( $invocation)(.. $_) " if cls.toString.startsWith(" DoSomethingOps" ) =>
90
+ transformInvocation(c)(invocation, q " _root_.org.mockito. ${packageName(c)(cls)}. ${className(c)(cls, " Mockito" )}.doAnswerFG[ $f, $g, .. $ftargs]( $v) " )
115
91
116
92
case o => throw new Exception (s " Couldn't recognize answeredFG ${show(o)}" )
117
93
}
@@ -125,12 +101,8 @@ object DoSomethingMacro {
125
101
126
102
val r = c.Expr [T ] {
127
103
c.macroApplication match {
128
- case q " $_.ThrowSomethingOps[ $_]( $v).willBe( $_.thrown).by[ $_]( $obj. $method[.. $targs](... $args))( $_) " =>
129
- val newArgs = args.map(a => transformArgs(c)(a))
130
- q " _root_.org.mockito.MockitoSugar.doThrow( $v).when( $obj). $method[.. $targs](... $newArgs) "
131
-
132
- case q " $_.ThrowSomethingOps[ $_]( $v).willBe( $_.thrown).by[ $_]( $obj. $method[.. $targs])( $_) " =>
133
- q " _root_.org.mockito.MockitoSugar.doThrow( $v).when( $obj). $method[.. $targs] "
104
+ case q " $_.ThrowSomethingOps[ $_]( $v).willBe( $_.thrown).by[ $_]( $invocation)( $_) " =>
105
+ transformInvocation(c)(invocation, q " _root_.org.mockito.MockitoSugar.doThrow( $v) " )
134
106
135
107
case o => throw new Exception (s " Couldn't recognize ${show(o)}" )
136
108
}
@@ -144,12 +116,8 @@ object DoSomethingMacro {
144
116
145
117
val r = c.Expr [T ] {
146
118
c.macroApplication match {
147
- case q " $_. $cls[ $e]( $v).willBe( $_.raised).by[ $f, $t]( $obj. $method[.. $targs](... $args))(.. $_) " if cls.toString.startsWith(" DoSomethingOps" ) =>
148
- val newArgs = args.map(a => transformArgs(c)(a))
149
- q " _root_.org.mockito. ${packageName(c)(cls)}. ${className(c)(cls, " Mockito" )}.doFailWith[ $f, $e, $t]( $v)( $F).when( $obj). $method[.. $targs](... $newArgs) "
150
-
151
- case q " $_. $cls[ $e]( $v).willBe( $_.raised).by[ $f, $t]( $obj. $method[.. $targs])(.. $_) " if cls.toString.startsWith(" DoSomethingOps" ) =>
152
- q " _root_.org.mockito. ${packageName(c)(cls)}. ${className(c)(cls, " Mockito" )}.doFailWith[ $f, $e, $t]( $v)( $F).when( $obj). $method[.. $targs] "
119
+ case q " $_. $cls[ $e]( $v).willBe( $_.raised).by[ $f, $t]( $invocation)(.. $_) " if cls.toString.startsWith(" DoSomethingOps" ) =>
120
+ transformInvocation(c)(invocation, q " _root_.org.mockito. ${packageName(c)(cls)}. ${className(c)(cls, " Mockito" )}.doFailWith[ $f, $e, $t]( $v) " )
153
121
154
122
case o => throw new Exception (s " Couldn't recognize ${show(o)}" )
155
123
}
@@ -163,12 +131,8 @@ object DoSomethingMacro {
163
131
164
132
val r = c.Expr [T ] {
165
133
c.macroApplication match {
166
- case q " $_. $cls[ $e]( $v).willBe( $_.raisedG).by[ $f, $g, $t]( $obj. $method[.. $targs](... $args))(.. $_) " if cls.toString.startsWith(" DoSomethingOps" ) =>
167
- val newArgs = args.map(a => transformArgs(c)(a))
168
- q " _root_.org.mockito. ${packageName(c)(cls)}. ${className(c)(cls, " Mockito" )}.doFailWithG[ $f, $g, $e, $t]( $v)( $F, $G).when( $obj). $method[.. $targs](... $newArgs) "
169
-
170
- case q " $_. $cls[ $e]( $v).willBe( $_.raisedG).by[ $f, $g, $t]( $obj. $method[.. $targs])(.. $_) " if cls.toString.startsWith(" DoSomethingOps" ) =>
171
- q " _root_.org.mockito. ${packageName(c)(cls)}. ${className(c)(cls, " Mockito" )}.doFailWithG[ $f, $g, $e, $t]( $v)( $F, $G).when( $obj). $method[.. $targs] "
134
+ case q " $_. $cls[ $e]( $v).willBe( $_.raisedG).by[ $f, $g, $t]( $invocation)(.. $_) " if cls.toString.startsWith(" DoSomethingOps" ) =>
135
+ transformInvocation(c)(invocation, q " _root_.org.mockito. ${packageName(c)(cls)}. ${className(c)(cls, " Mockito" )}.doFailWithG[ $f, $g, $e, $t]( $v) " )
172
136
173
137
case o => throw new Exception (s " Couldn't recognize ${show(o)}" )
174
138
}
@@ -182,17 +146,51 @@ object DoSomethingMacro {
182
146
183
147
val r = c.Expr [T ] {
184
148
c.macroApplication match {
185
- case q " $_.theRealMethod.willBe( $_.called).by[ $_]( $obj. $method[.. $targs](... $args)) " =>
186
- val newArgs = args.map(a => transformArgs(c)(a))
187
- q " _root_.org.mockito.MockitoSugar.doCallRealMethod.when( $obj). $method[.. $targs](... $newArgs) "
188
-
189
- case q " $_.theRealMethod.willBe( $_.called).by[ $_]( $obj. $method[.. $targs]) " =>
190
- q " _root_.org.mockito.MockitoSugar.doCallRealMethod.when( $obj). $method[.. $targs] "
149
+ case q " $_.theRealMethod.willBe( $_.called).by[ $_]( $invocation) " =>
150
+ transformInvocation(c)(invocation, q " _root_.org.mockito.MockitoSugar.doCallRealMethod " )
191
151
192
152
case o => throw new Exception (s " Couldn't recognize ${show(o)}" )
193
153
}
194
154
}
195
155
if (c.settings.contains(" mockito-print-do-something" )) println(show(r.tree))
196
156
r
197
157
}
158
+
159
+ private def transformInvocation (c : blackbox.Context )(invocation : c.Tree , action : c.Tree ): c.Tree = {
160
+ import c .universe ._
161
+
162
+ val pf : PartialFunction [c.Tree , c.Tree ] = {
163
+ case q " $obj. $method[.. $targs](... $args) " =>
164
+ val newArgs = args.map(a => transformArgs(c)(a))
165
+ q " $action.when( $obj). $method[.. $targs](... $newArgs) "
166
+ case q " $obj. $method[.. $targs] " =>
167
+ q " $action.when( $obj). $method[.. $targs] "
168
+ }
169
+
170
+ if (pf.isDefinedAt(invocation))
171
+ pf(invocation)
172
+ else if (pf.isDefinedAt(invocation.children.last)) {
173
+ val values = invocation.children
174
+ .dropRight(1 )
175
+ .collect {
176
+ case q " $_ val $name: $_ = $value" => name.toString -> value.asInstanceOf [c.Tree ]
177
+ }
178
+ .toMap
179
+
180
+ val nonMatchers = invocation.children.dropRight(1 ).collect {
181
+ case t @ q " $_ val $_: $_ = $value" if ! isMatcher(c)(value) => t
182
+ }
183
+
184
+ invocation.children.last match {
185
+ case q " $obj. $method[.. $targs](... $args) " =>
186
+ val newArgs = args.map { a =>
187
+ transformArgs(c)(a).map {
188
+ case p if show(p).startsWith(" x$" ) => transformArg(c)(values(p.toString))
189
+ case other => other
190
+ }
191
+ }
192
+ q " .. $nonMatchers; $action.when( $obj). $method[.. $targs](... $newArgs) "
193
+ }
194
+ } else throw new Exception (s " Couldn't recognize invocation ${show(invocation)}" )
195
+ }
198
196
}
0 commit comments