Permalink
Browse files

UnitTest: Added Tests for IntellisenseType, added tests for Nullable,…

… Collections, etc.
  • Loading branch information...
1 parent 7ff1de9 commit dced0855f11e75c51d84914f051a3aceaabb0f6b @DerAlbertCom DerAlbertCom committed Jan 2, 2014
@@ -20,8 +20,8 @@ namespace MadsKristensen.EditorExtensions
[TextViewRole(PredefinedTextViewRoles.Document)]
public class IntellisenseParser : IWpfTextViewCreationListener
{
- public const string DefaultModuleName = "server";
- public const string ModuleNameAttributeName = "TypeScriptModule";
+ private const string DefaultModuleName = "server";
+ private const string ModuleNameAttributeName = "TypeScriptModule";
internal static class Ext
{
@@ -52,7 +52,7 @@ private void document_FileActionOccurred(object sender, TextDocumentFileActionEv
Process(e.FilePath);
}
- public static Task<bool> Process(string filePath)
+ private static Task<bool> Process(string filePath)
{
if (!File.Exists(filePath + Ext.JavaScript) && !File.Exists(filePath + Ext.TypeScript))
return Task.FromResult(false);
@@ -86,7 +86,7 @@ public static Task<bool> Process(string filePath)
}), DispatcherPriority.ApplicationIdle).Task;
}
- private static void AddScript(string filePath, string extension, List<IntellisenseObject> list)
+ private static void AddScript(string filePath, string extension, IEnumerable<IntellisenseObject> list)
{
string resultPath = filePath + extension;
@@ -166,7 +166,7 @@ private static void ProcessEnum(CodeEnum element, List<IntellisenseObject> list)
foreach (var codeEnum in element.Members.OfType<CodeElement>())
{
- var prop = new IntellisenseProperty()
+ var prop = new IntellisenseProperty
{
Name = codeEnum.Name,
};
@@ -201,7 +201,7 @@ private static IEnumerable<IntellisenseProperty> GetProperties(CodeElements prop
return from p in props.OfType<CodeProperty>()
where !p.Attributes.Cast<CodeAttribute>().Any(a => a.Name == "IgnoreDataMember")
where p.Getter != null && !p.Getter.IsShared && p.Getter.Access == vsCMAccess.vsCMAccessPublic
- select new IntellisenseProperty()
+ select new IntellisenseProperty
{
Name = GetName(p),
Type = GetType(p.Parent, p.Type, traversedTypes),
@@ -2,37 +2,42 @@
namespace MadsKristensen.EditorExtensions
{
-
public class IntellisenseType
{
/// <summary>
/// This is the name of this type as it appears in the source code
/// </summary>
public string CodeName { get; set; }
+
/// <summary>
/// Indicates whether this type is array. Is this property is true, then all other properties
/// describe not the type itself, but rather the type of the array's elements.
/// </summary>
public bool IsArray { get; set; }
+
/// <summary>
/// If this type is itself part of a source code file that has a .d.ts definitions file attached,
/// this property will contain the full (namespace-qualified) client-side name of that type.
/// Otherwise, this property is null.
/// </summary>
public string ClientSideReferenceName { get; set; }
+
/// <summary>
/// This is TypeScript-formed shape of the type (i.e. inline type definition). It is used for the case where
/// the type is not primitive, but does not have its own named client-side definition.
/// </summary>
public IEnumerable<IntellisenseProperty> Shape { get; set; }
- public bool IsPrimitive
+
+ public bool IsKnownType
{
get { return TypeScriptName != "any"; }
}
+
public string JavaScriptName
{
get { return GetTargetName(true); }
}
+
public string TypeScriptName
{
get { return GetTargetName(false); }
@@ -63,7 +68,7 @@ public string JavaScripLiteral
}
- string GetTargetName(bool js)
+ private string GetTargetName(bool js)
{
var t = CodeName.ToLowerInvariant().TrimEnd('?');
switch (t)
@@ -94,7 +99,12 @@ string GetTargetName(bool js)
return js ? "Boolean" : "boolean";
}
- return js ? "Object" : "any";
+ return js ? "Object" : GetComplexTypeScriptName();
+ }
+
+ private string GetComplexTypeScriptName()
+ {
+ return ClientSideReferenceName ?? "any";
}
}
}
@@ -50,7 +50,7 @@ private static string CamelCase(string name)
return name[0].ToString(CultureInfo.CurrentCulture).ToLower(CultureInfo.CurrentCulture) + name.Substring(1);
}
- static readonly Regex whitespaceTrimmer = new Regex(@"^\s+|\s+$|\s*[\r\n]+\s*", RegexOptions.Compiled);
+ private static readonly Regex whitespaceTrimmer = new Regex(@"^\s+|\s+$|\s*[\r\n]+\s*", RegexOptions.Compiled);
private static void WriteJavaScript(IEnumerable<IntellisenseObject> objects, StringBuilder sb)
{
@@ -71,7 +71,8 @@ private static void WriteJavaScript(IEnumerable<IntellisenseObject> objects, Str
var propertyName = CamelCasePropertyName(p.Name);
comment = p.Summary ?? "The " + propertyName + " property as defined in " + io.FullName;
comment = whitespaceTrimmer.Replace(comment, " ");
- sb.AppendLine("\t/// <field name=\"" + propertyName + "\" type=\"" + type + "\">" + SecurityElement.Escape(comment) + "</field>");
+ sb.AppendLine("\t/// <field name=\"" + propertyName + "\" type=\"" + type + "\">" +
+ SecurityElement.Escape(comment) + "</field>");
sb.AppendLine("\tthis." + propertyName + " = " + p.Type.JavaScripLiteral + ";");
}
@@ -88,7 +89,8 @@ private static void WriteTypeScript(IEnumerable<IntellisenseObject> objects, Str
foreach (IntellisenseObject io in ns)
{
- if (!string.IsNullOrEmpty(io.Summary)) sb.AppendLine("\t/** " + whitespaceTrimmer.Replace(io.Summary, "") + " */");
+ if (!string.IsNullOrEmpty(io.Summary))
+ sb.AppendLine("\t/** " + whitespaceTrimmer.Replace(io.Summary, "") + " */");
if (io.IsEnum)
{
sb.AppendLine("\tenum " + CamelCaseClassName(io.Name) + " {");
@@ -117,7 +119,8 @@ private static void WriteTypeScriptComment(IntellisenseProperty p, StringBuilder
sb.AppendLine("\t\t/** " + whitespaceTrimmer.Replace(p.Summary, "") + " */");
}
- private static void WriteTSInterfaceDefinition(StringBuilder sb, string prefix, IEnumerable<IntellisenseProperty> props)
+ private static void WriteTSInterfaceDefinition(StringBuilder sb, string prefix,
+ IEnumerable<IntellisenseProperty> props)
{
sb.AppendLine("{");
@@ -126,8 +129,7 @@ private static void WriteTSInterfaceDefinition(StringBuilder sb, string prefix,
WriteTypeScriptComment(p, sb);
sb.AppendFormat("{0}\t{1}: ", prefix, CamelCasePropertyName(p.Name));
- if (p.Type.IsPrimitive) sb.Append(p.Type.TypeScriptName);
- else if (!string.IsNullOrEmpty(p.Type.ClientSideReferenceName)) sb.Append(p.Type.ClientSideReferenceName);
+ if (p.Type.IsKnownType) sb.Append(p.Type.TypeScriptName);
else
{
if (p.Type.Shape == null) sb.Append("any");
@@ -155,4 +157,4 @@ private static void WriteFileToDisk(string fileName, StringBuilder sb)
//}
}
}
-}
+}
View
@@ -9,7 +9,12 @@
<ToolsOptions>
<ToolsOptionsCategory name="Environment">
<ToolsOptionsSubCategory name="TaskList">
- <PropertyValue name="CommentTokens" ArrayType="VT_VARIANT" ArrayElementCount="4"><PropertyValue name="0">TODO:2</PropertyValue><PropertyValue name="1">HACK:2</PropertyValue><PropertyValue name="2">UNDONE:2</PropertyValue><PropertyValue name="3">UnresolvedMergeConflict:3</PropertyValue></PropertyValue>
+ <PropertyValue name="CommentTokens" ArrayType="VT_VARIANT" ArrayElementCount="4">
+ <PropertyValue name="0">TODO:2</PropertyValue>
+ <PropertyValue name="1">HACK:2</PropertyValue>
+ <PropertyValue name="2">UNDONE:2</PropertyValue>
+ <PropertyValue name="3">UnresolvedMergeConflict:3</PropertyValue>
+ </PropertyValue>
<PropertyValue name="ConfirmTaskDeletion">true</PropertyValue>
<PropertyValue name="DontShowFilePaths">true</PropertyValue>
<PropertyValue name="WarnOnAddingHiddenItem">false</PropertyValue>
@@ -149,14 +154,14 @@
<PropertyValue name="InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis">false</PropertyValue>
<PropertyValue name="InsertSpaceAfterSemicolonInForStatements">true</PropertyValue>
<PropertyValue name="InsertSpaceBeforeAndAfterBinaryOperators">true</PropertyValue>
- <PropertyValue name="OnlyUseTaborEnterToCommit">false</PropertyValue>
+ <PropertyValue name="OnlyUseTaborEnterToCommit">true</PropertyValue>
<PropertyValue name="PlaceOpenBraceOnNewLineForControlBlocks">false</PropertyValue>
<PropertyValue name="PlaceOpenBraceOnNewLineForFunctions">false</PropertyValue>
<PropertyValue name="ShowErrorsAsWarnings">true</PropertyValue>
<PropertyValue name="ShowSyntaxErrors">true</PropertyValue>
</ToolsOptionsSubCategory>
<ToolsOptionsSubCategory name="C/C++ Specific">
- <PropertyValue name="AddSemicolonForClassTypes">false</PropertyValue>
+ <PropertyValue name="AddSemicolonForClassTypes">true</PropertyValue>
<PropertyValue name="AlignParameters">false</PropertyValue>
<PropertyValue name="AlwaysUseFallbackLocation">false</PropertyValue>
<PropertyValue name="AutoFormatOnBraceCompletion">true</PropertyValue>
@@ -167,8 +172,8 @@
<PropertyValue name="AutomaticOutliningOfPragmaRegions">true</PropertyValue>
<PropertyValue name="AutomaticOutliningOfStatementBlocks">false</PropertyValue>
<PropertyValue name="ColorizeInactiveBlocksDifferently">true</PropertyValue>
- <PropertyValue name="CompleteParensInRawString">false</PropertyValue>
- <PropertyValue name="CompleteSlashStar">false</PropertyValue>
+ <PropertyValue name="CompleteParensInRawString">true</PropertyValue>
+ <PropertyValue name="CompleteSlashStar">true</PropertyValue>
<PropertyValue name="DisableAggressiveMemberList">false</PropertyValue>
<PropertyValue name="DisableDatabase">false</PropertyValue>
<PropertyValue name="DisableDatabaseAutoUpdates">false</PropertyValue>
@@ -185,19 +190,19 @@
<PropertyValue name="DisableMemberListKeywords">false</PropertyValue>
<PropertyValue name="DisablePreLoadNavigateToCache">false</PropertyValue>
<PropertyValue name="DisableReferenceHighlighting">false</PropertyValue>
- <PropertyValue name="DisableReferencesResolving">true</PropertyValue>
+ <PropertyValue name="DisableReferencesResolving">false</PropertyValue>
<PropertyValue name="DisableSemanticColoring">false</PropertyValue>
<PropertyValue name="DisableSquiggles">false</PropertyValue>
<PropertyValue name="DoNotWarnIfFallbackLocationUsed">false</PropertyValue>
<PropertyValue name="EnableLogging">false</PropertyValue>
<PropertyValue name="EnableQuickInfoToolTips">true</PropertyValue>
<PropertyValue name="EnterOutliningModeWhenFilesOpen">true</PropertyValue>
- <PropertyValue name="EnumerateCommentTasks">false</PropertyValue>
+ <PropertyValue name="EnumerateCommentTasks">true</PropertyValue>
<PropertyValue name="FallbackLocation"></PropertyValue>
<PropertyValue name="GroupBrackets">true</PropertyValue>
<PropertyValue name="HideUnconfirmedReferencesResults">false</PropertyValue>
<PropertyValue name="HighlightMatchingTokens">true</PropertyValue>
- <PropertyValue name="InactiveCodeOpacityPercent">65</PropertyValue>
+ <PropertyValue name="InactiveCodeOpacityPercent">55</PropertyValue>
<PropertyValue name="IndentAccessSpecifiers">false</PropertyValue>
<PropertyValue name="IndentBlockContents">true</PropertyValue>
<PropertyValue name="IndentBraces">false</PropertyValue>
@@ -0,0 +1,12 @@
+declare module server {
+ interface CollectionModel {
+ AStringArray: string[];
+ AStringIEnumerable: string[];
+ AStringICollection: string[];
+ AStringIList: string[];
+ AStringList: string[];
+ AStringCollection: string[];
+ ASimpleList: server.Simple[];
+ ALongList: number[];
+ }
+}
@@ -0,0 +1,21 @@
+var server = server || {};
+/// <summary>The CollectionModel class as defined in Lib.Generation.CollectionModel</summary>
+server.CollectionModel = function() {
+ /// <field name="AStringArray" type="String[]">The AStringArray property as defined in Lib.Generation.CollectionModel</field>
+ this.AStringArray = [];
+ /// <field name="AStringIEnumerable" type="String[]">The AStringIEnumerable property as defined in Lib.Generation.CollectionModel</field>
+ this.AStringIEnumerable = [];
+ /// <field name="AStringICollection" type="String[]">The AStringICollection property as defined in Lib.Generation.CollectionModel</field>
+ this.AStringICollection = [];
+ /// <field name="AStringIList" type="String[]">The AStringIList property as defined in Lib.Generation.CollectionModel</field>
+ this.AStringIList = [];
+ /// <field name="AStringList" type="String[]">The AStringList property as defined in Lib.Generation.CollectionModel</field>
+ this.AStringList = [];
+ /// <field name="AStringCollection" type="String[]">The AStringCollection property as defined in Lib.Generation.CollectionModel</field>
+ this.AStringCollection = [];
+ /// <field name="ASimpleList" type="Object[]">The ASimpleList property as defined in Lib.Generation.CollectionModel</field>
+ this.ASimpleList = [];
+ /// <field name="ALongList" type="Number[]">The ALongList property as defined in Lib.Generation.CollectionModel</field>
+ this.ALongList = [];
+};
+
@@ -1,9 +1,14 @@
-namespace Lib.Generation
+using System;
+
+namespace Lib.Generation
{
public class Simple
{
+ public Simple ASimple { get; set; }
public string AString { get; set; }
public bool ABool { get; set; }
public int AnInt { get; set; }
+ public DateTime ADateTime { get; set; }
+
}
}
@@ -0,0 +1,9 @@
+declare module server {
+ interface Simple {
+ ASimple: server.Simple;
+ AString: string;
+ ABool: boolean;
+ AnInt: number;
+ ADateTime: Date;
+ }
+}
@@ -0,0 +1,15 @@
+var server = server || {};
+/// <summary>The Simple class as defined in Lib.Generation.Simple</summary>
+server.Simple = function() {
+ /// <field name="ASimple" type="Object">The ASimple property as defined in Lib.Generation.Simple</field>
+ this.ASimple = { };
+ /// <field name="AString" type="String">The AString property as defined in Lib.Generation.Simple</field>
+ this.AString = '';
+ /// <field name="ABool" type="Boolean">The ABool property as defined in Lib.Generation.Simple</field>
+ this.ABool = false;
+ /// <field name="AnInt" type="Number">The AnInt property as defined in Lib.Generation.Simple</field>
+ this.AnInt = 0;
+ /// <field name="ADateTime" type="Date">The ADateTime property as defined in Lib.Generation.Simple</field>
+ this.ADateTime = new Date();
+};
+
@@ -0,0 +1,14 @@
+using System;
+using System.Security.Permissions;
+
+namespace Lib.Generation
+{
+ public class SimpleNullable
+ {
+ public bool? ABool { get; set; }
+ public int? AnInt { get; set; }
+
+ public DateTime? ADateTime { get; set; }
+
+ }
+}
@@ -0,0 +1,7 @@
+declare module server {
+ interface SimpleNullable {
+ ABool: boolean;
+ AnInt: number;
+ ADateTime: Date;
+ }
+}
@@ -0,0 +1,11 @@
+var server = server || {};
+/// <summary>The SimpleNullable class as defined in Lib.Generation.SimpleNullable</summary>
+server.SimpleNullable = function() {
+ /// <field name="ABool" type="Boolean">The ABool property as defined in Lib.Generation.SimpleNullable</field>
+ this.ABool = false;
+ /// <field name="AnInt" type="Number">The AnInt property as defined in Lib.Generation.SimpleNullable</field>
+ this.AnInt = 0;
+ /// <field name="ADateTime" type="Date">The ADateTime property as defined in Lib.Generation.SimpleNullable</field>
+ this.ADateTime = new Date();
+};
+
@@ -40,9 +40,36 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Generation\CollectionModel.cs" />
+ <Compile Include="Generation\SimpleNullable.cs" />
<Compile Include="Generation\Simple.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
+ <ItemGroup>
+ <None Include="Generation\Simple.cs.js">
+ <DependentUpon>Simple.cs</DependentUpon>
+ </None>
+ </ItemGroup>
+ <ItemGroup>
+ <TypeScriptCompile Include="Generation\CollectionModel.cs.d.ts">
+ <DependentUpon>CollectionModel.cs</DependentUpon>
+ </TypeScriptCompile>
+ <TypeScriptCompile Include="Generation\Simple.cs.d.ts">
+ <DependentUpon>Simple.cs</DependentUpon>
+ </TypeScriptCompile>
+ <TypeScriptCompile Include="Generation\SimpleNullable.cs.d.ts">
+ <DependentUpon>SimpleNullable.cs</DependentUpon>
+ </TypeScriptCompile>
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="Generation\SimpleNullable.cs.js">
+ <DependentUpon>SimpleNullable.cs</DependentUpon>
+ </None>
+ </ItemGroup>
+ <ItemGroup>
+ <None Include="Generation\CollectionModel.cs.js">
+ <DependentUpon>CollectionModel.cs</DependentUpon>
+ </None>
+ </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Oops, something went wrong.

0 comments on commit dced085

Please sign in to comment.