Added ability to order the execution of dictionary adapter behaviors.
[castle.git] / Tools / NVelocity / src / NVelocity.Tests / DuckTestCase.cs
blob0bdb4269f88c43f3b92e2d10832ddb9107fc7831
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
15 namespace NVelocity
17 using System;
18 using System.IO;
19 using System.Text;
20 using NUnit.Framework;
21 using NVelocity.App;
23 #region Ducks
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);
62 return this;
65 public object Invoke(string method, params object[] args)
67 Log("invoked " + method + "_" + args.Length);
68 return this;
71 private void Log(string call)
73 sb.Append(call + Environment.NewLine);
76 public override string ToString()
78 return sb.ToString();
82 // I'm not a duck
83 public class Test1
85 private string name;
86 private decimal amount = (decimal) 0.2200;
88 public decimal Amount
90 get { return amount; }
93 public string Name
95 get { return name; }
96 set { name = value; }
100 #endregion
102 [TestFixture]
103 public class DuckTestCase
105 private VelocityEngine ve;
106 private Duck1 duck1;
107 private Duck2 duck2;
109 [SetUp]
110 public void Init()
112 ve = new VelocityEngine();
113 ve.Init();
115 duck1 = new Duck1();
116 duck2 = new Duck2();
119 [Test]
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());
134 [Test]
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());
145 [Test]
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());
159 [Test]
160 public void ArgumentsAreEvaluated()
162 Assert.AreEqual("invoked some message 1 message 2 ", Evaluate("$duck1.some($msg1, $msg2)"));
165 [Test]
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();