1
// Copyright 2004-2008 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.
15 namespace Castle
.MonoRail
.Framework
.Tests
.JSGeneration
17 using Castle
.MonoRail
.Framework
.JSGeneration
;
18 using NUnit
.Framework
;
21 public class JSCodeGeneratorTestCase
23 private JSCodeGenerator codeGen
;
28 codeGen
= new JSCodeGenerator();
32 public void Call_CorrectlyRecordsFunctionCall()
34 codeGen
.Call("alert", "'hey'");
36 Assert
.AreEqual("alert('hey');\r\n", codeGen
.Lines
.ToString());
40 public void Call_CorrectlyRecordsFunctionCallsInARow()
42 codeGen
.Call("alert", "'hey'");
43 codeGen
.Call("window.href.change");
44 codeGen
.Call("math.sum", 1, 2);
46 Assert
.AreEqual("alert('hey');\r\nwindow.href.change();\r\nmath.sum(1,2);\r\n", codeGen
.Lines
.ToString());
50 public void Write_JustTransferContentToBuffer()
52 codeGen
.Write("something");
54 Assert
.AreEqual("something", codeGen
.Lines
.ToString());
58 public void Record_JustTransferContentToBufferWithNewLine()
60 codeGen
.AppendLine("something");
62 Assert
.AreEqual("something;\r\n", codeGen
.Lines
.ToString());
66 public void AppendLine_JustTransferContentToBufferWithNewLine()
68 codeGen
.AppendLine("something");
70 Assert
.AreEqual("something;\r\n", codeGen
.Lines
.ToString());
74 public void ReplaceTailByPeriod_FixesTheContentCorrectly()
76 codeGen
.AppendLine("something");
77 codeGen
.ReplaceTailByPeriod();
79 Assert
.AreEqual("something.", codeGen
.Lines
.ToString());
83 public void ReplaceTailByPeriod_IgnoresEmptyBuffer()
85 codeGen
.ReplaceTailByPeriod();
87 Assert
.AreEqual("", codeGen
.Lines
.ToString());
91 public void RemoveTail_FixesTheContentCorrectly()
93 codeGen
.AppendLine("something");
96 Assert
.AreEqual("something", codeGen
.Lines
.ToString());
100 public void GenerateFinalJsCode_CanGenerateEmptyCall()
102 string expected
= "try \n{\n}\ncatch(e)\n{\n" +
103 "alert('JS error ' + e.toString());\n" +
106 Assert
.AreEqual(expected
, codeGen
.GenerateFinalJsCode());