Skip to content

Commit 4f70fa3

Browse files
Luke Owlclaw2Luke Owlclaw2
authored andcommitted
Added test CheckGenerateTypeScriptOutput
Included new test files
1 parent db74b07 commit 4f70fa3

4 files changed

Lines changed: 190 additions & 6 deletions

File tree

tests/ClassLibrary1/SecondClass.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace ClassLibrary1
6+
{
7+
class SecondClass
8+
{
9+
public int MyProperty { get; set; }
10+
public SomeClass Complex1 { get; set; }
11+
public SomeSomeClass Complex2 { get; set; }
12+
}
13+
}
14+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace ClassLibrary1
6+
{
7+
public class ThirdClass
8+
{
9+
public int MyProperty { get; set; }
10+
public SomeClass Complex1 { get; set; }
11+
public SomeSomeClass Complex2 { get; set; }
12+
}
13+
}
14+

tests/TypeScriptDefinitionGenerator.Tests/IntellisenseParserTest.cs

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,5 +221,128 @@ public void _ShouldWorkProperly2()
221221

222222
}
223223

224+
[Test]
225+
[Explicit("Can't be run on build server, some problems on build server")]
226+
public void CheckGenerateTypeScriptOutput()
227+
{
228+
// get the DTE reference...
229+
DTE2 dte2 = (DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.15.0");
230+
231+
var worker = new SolutionWorker();
232+
worker.ExamineSolution(dte2.Solution);
233+
234+
var testFiles = new System.Collections.Generic.Dictionary<string, string>
235+
{
236+
{
237+
"SomeEnum.cs",
238+
"// ------------------------------------------------------------------------------\n" +
239+
"// <auto-generated>\n" +
240+
"// This file was generated by TypeScript Definition Generator v2.2.0.0\n" +
241+
"// </auto-generated>\n" +
242+
"// ------------------------------------------------------------------------------\n" +
243+
"export enum SomeEnum {\n" +
244+
" This,\n" +
245+
" That,\n" +
246+
"}\n"
247+
},
248+
{
249+
"SecondClass.cs",
250+
"// ------------------------------------------------------------------------------\n" +
251+
"// <auto-generated>\n" +
252+
"// This file was generated by TypeScript Definition Generator v2.2.0.0\n" +
253+
"// </auto-generated>\n" +
254+
"// ------------------------------------------------------------------------------\n" +
255+
"import { SomeClass } from \"./SomeClass.generated\";\n" +
256+
"import { SomeSomeClass } from \"./SomeSomeClass.generated\";\n" +
257+
"export interface SecondClass {\n" +
258+
" myProperty: number;\n" +
259+
" complex1: SomeClass;\n" +
260+
" complex2: SomeSomeClass;\n" +
261+
"}\n"
262+
},
263+
{
264+
"SomeClass.cs",
265+
"// ------------------------------------------------------------------------------\n" +
266+
"// <auto-generated>\n" +
267+
"// This file was generated by TypeScript Definition Generator v2.2.0.0\n" +
268+
"// </auto-generated>\n" +
269+
"// ------------------------------------------------------------------------------\n" +
270+
"import { SomeEnum } from \"./SomeEnum.generated\";\n" +
271+
"export interface SomeClass {\n" +
272+
" inc1: number;\n" +
273+
" some: SomeEnum;\n" +
274+
"}\n"
275+
},
276+
{
277+
"SomeSomeClass.cs",
278+
"// ------------------------------------------------------------------------------\n" +
279+
"// <auto-generated>\n" +
280+
"// This file was generated by TypeScript Definition Generator v2.2.0.0\n" +
281+
"// </auto-generated>\n" +
282+
"// ------------------------------------------------------------------------------\n" +
283+
"import { SomeClass } from \"./SomeClass.generated\";\n" +
284+
"export interface SomeSomeClass extends SomeClass {\n" +
285+
" inc2: number;\n" +
286+
" inc3: number;\n" +
287+
" inc4: number;\n" +
288+
" inc5: number;\n" +
289+
" inc6: string;\n" +
290+
" inc7: any;\n" +
291+
" inc8: number[];\n" +
292+
" inc9: number[];\n" +
293+
" inc10: number[];\n" +
294+
" inc11: number[];\n" +
295+
" inc12: boolean;\n" +
296+
" inc13: { [index: string]: number };\n" +
297+
" inc14: { [index: string]: number };\n" +
298+
"}\n"
299+
},
300+
{
301+
"ThirdClass.cs",
302+
"// ------------------------------------------------------------------------------\n" +
303+
"// <auto-generated>\n" +
304+
"// This file was generated by TypeScript Definition Generator v2.2.0.0\n" +
305+
"// </auto-generated>\n" +
306+
"// ------------------------------------------------------------------------------\n" +
307+
"import { SomeClass } from \"../SomeClass.generated\";\n" +
308+
"import { SomeSomeClass } from \"../SomeSomeClass.generated\";\n" +
309+
"export interface ThirdClass {\n" +
310+
" myProperty: number;\n" +
311+
" complex1: SomeClass;\n" +
312+
" complex2: SomeSomeClass;\n" +
313+
"}\n"
314+
}
315+
};
316+
317+
foreach (var testFile in testFiles)
318+
{
319+
var testFileName = testFile.Key;
320+
var expectedFile = testFile.Value;
321+
ProjectItem item = worker.GetProjectItem(dte2.Solution, testFileName);
322+
323+
Assert.NotNull(item, $"Could not find {testFileName}");
324+
325+
Options.SetOptionsOverrides(new OptionsOverride()
326+
{
327+
CamelCaseEnumerationValues = false,
328+
CamelCasePropertyNames = true,
329+
CamelCaseTypeNames = false,
330+
WebEssentials2015 = false,
331+
ClassInsteadOfInterface = false,
332+
DeclareModule = false,
333+
DefaultModuleName = "Server.Dtos",
334+
EOLType = EOLType.LF,
335+
IgnoreIntellisense = true,
336+
IndentTab = false,
337+
IndentTabSize = 2,
338+
UseNamespace = true,
339+
});
340+
var list = IntellisenseParser.ProcessFile(item).ToList();
341+
var sourceItemPath = item.Properties.Item("FullPath").Value as string;
342+
var tsFile = IntellisenseWriter.WriteTypeScript(list, sourceItemPath);
343+
344+
Assert.AreEqual(expectedFile, tsFile);
345+
}
346+
}
224347
}
225348
}

tests/TypeScriptDefinitionGenerator.Tests/SolutionWorker.cs

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,57 @@ namespace TypeScriptDefinitionGenerator.Tests
1010
{
1111
public class SolutionWorker
1212
{
13+
private const string PhysicalFolder_guid = "{6BB5F8EF-4483-11D3-8BCF-00C04F8EC28C}";
14+
15+
/// <summary>
16+
/// Recursively finds first item in solution matching <paramref name="filename"/>.
17+
/// </summary>
18+
/// <param name="solution">solution</param>
19+
/// <param name="filename">wanted filename</param>
20+
/// <returns>ProjectItem or null</returns>
1321
public ProjectItem GetProjectItem(Solution solution, string filename)
1422
{
23+
var sb = new StringBuilder();
1524
ProjectItem ret = null;
1625
// get all the projects
1726
foreach (Project project in solution.Projects)
1827
{
19-
// get all the items in each project
20-
foreach (ProjectItem item in project.ProjectItems)
28+
var foundFile = FindRecursive(filename, project.ProjectItems);
29+
if (foundFile != null)
2130
{
22-
// find this file and examine it
23-
if (item.Name == filename)
31+
return foundFile;
32+
}
33+
}
34+
35+
return ret;
36+
}
37+
38+
private ProjectItem FindRecursive(string filename, ProjectItems projectItems)
39+
{
40+
// get all the items in each project
41+
foreach (ProjectItem item in projectItems)
42+
{
43+
if (item.Kind == PhysicalFolder_guid)
44+
{
45+
var subProjectItems = item.ProjectItems as ProjectItems;
46+
if (subProjectItems != null)
2447
{
25-
ret = item;
48+
var foundFile = FindRecursive(filename, subProjectItems);
49+
if (foundFile != null)
50+
{
51+
return foundFile;
52+
}
2653
}
2754
}
55+
56+
// find this file and examine it
57+
if (item.Name == filename)
58+
{
59+
return item;
60+
}
2861
}
2962

30-
return ret;
63+
return null;
3164
}
3265

3366
public void ExamineSolution(Solution solution)

0 commit comments

Comments
 (0)