forked from Unity-Technologies/unityscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefresh-tests.boo
executable file
·184 lines (136 loc) · 5.12 KB
/
refresh-tests.boo
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env booi
"""
Generates test fixtures from files under tests/
First line of test case can specify a nunit attribute to go with it:
* "// ignore reason" for [Ignore("reason")]
* "// category name" for [Category("name")]
"""
import System
import System.IO
import Boo.Lang.PatternMatching
def Main():
GenerateProjectTestFixture("src/UnityScript.Tests/ProjectIntegrationTestFixture.Generated.boo", """
namespace UnityScript.Tests
import NUnit.Framework
partial class ProjectIntegrationTestFixture:
""", "tests/projects")
GenerateTestFixture("src/UnityScript.Tests/ParserTestFixture.Generated.boo", """
namespace UnityScript.Tests
import NUnit.Framework
partial class ParserTestFixture:
""", "tests/parser")
GenerateTestFixture("src/UnityScript.Tests/SemanticsTestFixture.boo", """
namespace UnityScript.Tests
import NUnit.Framework
[TestFixture]
class SemanticsTestFixture(AbstractSemanticsTestFixture):
""", "tests/semantics")
GenerateTestFixture("src/UnityScript.Tests/StrictIntegrationTestFixture.Generated.boo", """
namespace UnityScript.Tests
import NUnit.Framework
[TestFixture]
partial class StrictIntegrationTestFixture(AbstractIntegrationTestFixture):
""", "tests/integration")
GenerateTestFixture("src/UnityScript.Tests/DuckyIntegrationTestFixture.boo", """
namespace UnityScript.Tests
import NUnit.Framework
[TestFixture]
class DuckyIntegrationTestFixture(AbstractIntegrationTestFixture):
override def SetCompilationOptions():
super()
Parameters.Strict = false
""", "tests/integration", "tests/ducky")
GenerateTestFixture("src/UnityScript.Tests/PragmaTestFixture.boo", """
namespace UnityScript.Tests
import NUnit.Framework
[TestFixture]
class PragmaTestFixture(AbstractIntegrationTestFixture):
""", "tests/pragma")
GenerateTestFixture("src/UnityScript.Tests/GenericsTestFixture.Generated.boo", """
namespace UnityScript.Tests
import NUnit.Framework
[TestFixture]
class GenericsTestFixture(AbstractIntegrationTestFixture):
""", "tests/generics")
GenerateTestFixture("src/UnityScript.Tests/EvalTestFixture.boo", """
namespace UnityScript.Tests
import NUnit.Framework
[TestFixture]
class EvalTestFixture(AbstractIntegrationTestFixture):
""", "tests/eval")
GenerateTestFixture("src/UnityScript.Tests/ExpandoTestFixture.boo", """
namespace UnityScript.Tests
import NUnit.Framework
[TestFixture]
class ExpandoTestFixture(AbstractIntegrationTestFixture):
override def SetCompilationOptions():
super()
Parameters.Expando = true
""", "tests/expando")
GenerateTestFixture("src/UnityScript.Tests/ErrorMessagesTestFixture.Generated.boo", """
namespace UnityScript.Tests
import NUnit.Framework
partial class ErrorMessagesTestFixture:
""", "tests/error-messages")
GenerateTestFixture("src/UnityScript.Tests/StackTraceTestFixture.Generated.boo", """
namespace UnityScript.Tests
import NUnit.Framework
partial class StackTraceTestFixture:
""", "tests/stacktrace")
def GetTestCaseName(fname as string):
return Path.GetFileNameWithoutExtension(fname).Replace("-", "_").Replace(".", "_")
def CategoryAttributeFor(testFile as string):
"""
If the first line of the test case file starts with // category CategoryName
then return a suitable [CategoryName()] attribute.
"""
match FirstLineOf(testFile):
case /\/\/\s*ignore\s+(?<reason>.*)/:
return "[Ignore(\"${reason[0].Value.Trim()}\")]"
case /\/\/\s*category\s+(?<name>.*)/:
return "[Category(\"${name[0].Value.Trim()}\")]"
otherwise:
return ""
def FirstLineOf(fname as string):
using reader=File.OpenText(fname):
return reader.ReadLine()
def GenerateTestFixture(targetFile as string, header as string, *srcDirs as (string)):
contents = GenerateTestFixtureSource(header, srcDirs, JavascriptFilesIn, CategoryAttributeFor)
WriteFileIfChanged(targetFile, contents)
def GenerateProjectTestFixture(targetFile as string, header as string, *srcDirs as (string)):
contents = GenerateTestFixtureSource(header, srcDirs, { dir | Directory.GetDirectories(dir) }, { dir | "" })
WriteFileIfChanged(targetFile, contents)
def WriteFileIfChanged(targetFile as string, contents as string):
if ShouldReplaceContent(targetFile, contents):
File.WriteAllText(targetFile, contents)
def ShouldReplaceContent(fname as string, contents as string):
if not File.Exists(fname): return true
return ns(File.ReadAllText(fname)) != ns(contents)
def ns(s as string):
"""
Normalize string.
"""
return s.Trim().Replace("\r\n", Environment.NewLine)
def GenerateTestFixtureSource(
header as string,
srcDirs as string*,
testCaseProducer as callable(string) as string*,
categoryProducer as callable(string) as string):
writer=StringWriter()
writer.Write(header)
for srcDir in srcDirs:
count = 0
for testCase in testCaseProducer(srcDir):
++count
categoryAttribute = categoryProducer(testCase)
writer.Write("""
$categoryAttribute
[Test] def ${GetTestCaseName(testCase)}():
RunTestCase("${testCase.Replace('\\', '/')}")
""")
print "\t$count test cases found in $srcDir."
return writer.ToString()
def JavascriptFilesIn(dir as string):
jsFiles = array(fname for fname in Directory.GetFiles(dir) if fname.EndsWith(".js"))
Array.Sort(jsFiles, StringComparer.Ordinal)
return jsFiles