1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
20 using NUnit
.Framework
;
25 public class Duck1
: IDuck
27 public void SetInvoke(string propName
, object value)
29 throw new NotImplementedException();
32 public object GetInvoke(string propName
)
34 return "get invoked " + propName
;
37 public object Invoke(string method
, params object[] args
)
39 String arguments
= string.Empty
;
41 foreach(object arg
in args
)
43 arguments
+= arg
+ " ";
46 return "invoked " + method
+ " " + arguments
;
50 public class Duck2
: IDuck
52 private StringBuilder sb
= new StringBuilder();
54 public void SetInvoke(string propName
, object value)
56 Log("set invoked " + propName
+ " " + value);
59 public object GetInvoke(string propName
)
61 Log("get invoked " + propName
);
65 public object Invoke(string method
, params object[] args
)
67 Log("invoked " + method
+ "_" + args
.Length
);
71 private void Log(string call
)
73 sb
.Append(call
+ Environment
.NewLine
);
76 public override string ToString()
86 private decimal amount
= (decimal) 0.2200;
90 get { return amount; }
103 public class DuckTestCase
105 private VelocityEngine ve
;
112 ve
= new VelocityEngine();
120 public void PropertyRead()
122 Assert
.AreEqual("get invoked name", Evaluate("$duck1.name"));
123 Assert
.AreEqual("get invoked some", Evaluate("$duck1.some"));
125 Evaluate("$duck2.style.border");
127 StringBuilder expected
= new StringBuilder();
128 expected
.Append("get invoked style" + Environment
.NewLine
);
129 expected
.Append("get invoked border" + Environment
.NewLine
);
131 Assert
.AreEqual(expected
.ToString(), duck2
.ToString());
135 public void PropertyWrite()
137 Evaluate("#set($duck2.Name = 'aaa')");
139 StringBuilder expected
= new StringBuilder();
140 expected
.Append("set invoked Name aaa" + Environment
.NewLine
);
142 Assert
.AreEqual(expected
.ToString(), duck2
.ToString());
146 public void MethodInvocations()
148 Assert
.AreEqual("invoked some arg1 arg2 ", Evaluate("$duck1.some('arg1', 'arg2')"));
149 Assert
.AreEqual("invoked some arg1 ", Evaluate("$duck1.some('arg1')"));
150 Assert
.AreEqual("invoked some ", Evaluate("$duck1.some()"));
151 Evaluate("$duck2.set()");
153 StringBuilder expected
= new StringBuilder();
154 expected
.Append("invoked set_0" + Environment
.NewLine
);
156 Assert
.AreEqual(expected
.ToString(), duck2
.ToString());
160 public void ArgumentsAreEvaluated()
162 Assert
.AreEqual("invoked some message 1 message 2 ", Evaluate("$duck1.some($msg1, $msg2)"));
166 public void Quoting()
168 decimal amount
= (decimal) 0.2200;
169 Assert
.AreEqual("invoked some '" + amount
+ "' ", Evaluate("$duck1.some($test.Amount.to_squote)"));
170 Assert
.AreEqual("invoked some \"" + amount
+ "\" ", Evaluate("$duck1.some($test.Amount.to_quote)"));
171 Assert
.AreEqual("invoked some \"" + amount
+ "\" ", Evaluate("$duck1.some($test.Amount.to_quote)"));
172 Assert
.AreEqual("invoked some \"message 1\" \"message 2\" ", Evaluate("$duck1.some($msg1.to_quote, $msg2.to_quote)"));
173 Assert
.AreEqual("invoked some 'message 1' 'message 2' ", Evaluate("$duck1.some($msg1.to_squote, $msg2.to_squote)"));
176 private string Evaluate(string toEvaluate
)
178 StringWriter sw
= new StringWriter();
180 VelocityContext c
= new VelocityContext();
182 c
.Put("duck1", duck1
);
183 c
.Put("duck2", duck2
);
184 c
.Put("test", new Test1());
185 c
.Put("msg1", "message 1");
186 c
.Put("msg2", "message 2");
188 Assert
.IsTrue(ve
.Evaluate(c
, sw
, "eval1", toEvaluate
));
190 return sw
.GetStringBuilder().ToString();