forked from jetheredge/SquishIt
/
CoffeeScriptTests.cs
115 lines (93 loc) · 3.35 KB
/
CoffeeScriptTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using NUnit.Framework;
using SquishIt.CoffeeScript;
using SquishIt.CoffeeScript.Coffee;
using SquishIt.Framework.Files;
using SquishIt.Framework.Utilities;
using SquishIt.Tests.Helpers;
namespace SquishIt.Tests
{
[TestFixture, Platform(Exclude = "Unix, Linux, Mono")]
public class CoffeeScriptTests
{
//TODO: should probably have more tests here
JavaScriptBundleFactory javaScriptBundleFactory;
IHasher hasher;
[SetUp]
public void Setup()
{
javaScriptBundleFactory = new JavaScriptBundleFactory();
var retryableFileOpener = new RetryableFileOpener();
hasher = new Hasher(retryableFileOpener);
}
[Test]
public void CanBundleJavascriptWithArbitraryCoffeeScript()
{
var coffee = "alert 'test' ";
var tag = javaScriptBundleFactory
.WithDebuggingEnabled(false)
.Create()
.WithPreprocessor(new CoffeeScriptPreprocessor())
.AddString(coffee, ".coffee")
.Render("~/brewed.js");
var compiled = javaScriptBundleFactory.FileWriterFactory.Files[TestUtilities.PrepareRelativePath(@"brewed.js")];
Assert.AreEqual(@"(function(){alert(""test"")}).call(this)", compiled);
Assert.AreEqual(@"<script type=""text/javascript"" src=""brewed.js?r=hash""></script>", tag);
}
[Test]
public void CanBundleJavascriptInDebugWithArbitraryCoffeeScript()
{
var coffee = "alert 'test' ";
var tag = javaScriptBundleFactory
.WithDebuggingEnabled(true)
.Create()
.WithPreprocessor(new CoffeeScriptPreprocessor())
.AddString(coffee, ".coffee")
.Render("~/brewed.js");
Assert.AreEqual("<script type=\"text/javascript\">(function() {\n alert('test');\n}).call(this);\n</script>\r\n", tag);
}
}
[TestFixture]
public class CoffeescriptCompilerTests
{
[Test, Platform(Exclude = "Unix, Linux, Mono")]
public void CompileWithSimpleAlertSucceeds()
{
var compiler = new CoffeeScriptCompiler();
string result = compiler.Compile("alert 'test' ");
Assert.AreEqual("(function() {\n alert('test');\n}).call(this);\n", result);
}
[Test, Platform(Exclude = "Unix, Linux, Mono")]
public void CompileWithComplexScriptSucceeds()
{
string source = @"# Assignment:
number = 42
opposite = true
# Conditions:
number = -42 if opposite
# Functions:
square = (x) -> x * x
# Arrays:
list = [1, 2, 3, 4, 5]
# Objects:
math =
root: Math.sqrt
square: square
cube: (x) -> x * square x
# Splats:
race = (winner, runners...) ->
print winner, runners
# Existence:
alert 'I knew it!' if elvis?";
var compiler = new CoffeeScriptCompiler();
string result = compiler.Compile(source);
}
[Test, Platform(Include = "Unix, Linux, Mono")]
public void CompileFailsGracefullyOnMono()
{
var compiler = new CoffeeScriptCompiler();
var exception = Assert.Throws(typeof(NotSupportedException), () => compiler.Compile(""));
Assert.AreEqual("Coffeescript not yet supported for mono.", exception.Message);
}
}
}