Skip to content

Commit b8ee401

Browse files
author
tghamm
committed
Merge pull request #10 from castle-it/release-1.2
Release 1.2
2 parents f42b3d5 + 78713dd commit b8ee401

11 files changed

Lines changed: 353 additions & 38 deletions

File tree

Castle.Sharp2Js.Nuget/Package.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<package >
33
<metadata>
44
<id>sharp2Js</id>
5-
<version>1.1.1</version>
5+
<version>1.2.0</version>
66
<title>sharp2Js</title>
77
<authors>Grant Hamm, Jared Kremer</authors>
88
<owners>Castle Worldwide, Inc</owners>

Castle.Sharp2Js.Nuget/lib/Castle.Sharp2Js.xml

Lines changed: 28 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,54 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Diagnostics.CodeAnalysis;
35
using System.Linq;
46
using System.Runtime.Serialization;
57
using System.Text;
68
using System.Threading.Tasks;
79

810
namespace Castle.Sharp2Js.Tests.DTOs
911
{
12+
[ExcludeFromCodeCoverage]
1013
public class RecursiveTest
1114
{
1215
public string Name { get; set; }
1316
public List<RecursiveTest> RecursiveTests { get; set; }
1417
}
15-
18+
[ExcludeFromCodeCoverage]
1619
public class ArrayTypeTest
1720
{
1821
public string[] Strings { get; set; }
1922
}
20-
23+
[ExcludeFromCodeCoverage]
2124
public class AttributeInformationTest
2225
{
2326
[DataMember(Name = "TestName")]
2427
public string TypeName { get; set; }
2528
[IgnoreDataMember]
2629
public bool IgnoreMe { get; set; }
30+
[DefaultValue(23.19)]
31+
public double NumberValue1 { get; set; }
32+
33+
[DefaultValue("HelloWorld")]
34+
public string StringValue1 { get; set; }
35+
36+
[DataMember(Name = " ")]
37+
public string InvalidName1 { get; set; }
38+
39+
public List<StructTest> StructTests { get; set; }
40+
}
41+
[ExcludeFromCodeCoverage]
42+
public struct StructTest
43+
{
44+
public int SructValue { get; set; }
45+
}
46+
[ExcludeFromCodeCoverage]
47+
public class CamelCaseTest
48+
{
49+
public string WKTPolygon { get; set; }
50+
public string alreadyUnderscored { get; set; }
51+
public string RegularCased { get; set; }
2752
}
2853

2954
}

Castle.Sharp2Js.Tests/JsGeneratorTests.cs

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics.CodeAnalysis;
4+
using System.Linq;
35
using Castle.Sharp2Js.SampleData;
46
using Castle.Sharp2Js.Tests.DTOs;
7+
using Jint.Parser.Ast;
58
using NUnit.Framework;
69

710
namespace Castle.Sharp2Js.Tests
811
{
12+
[ExcludeFromCodeCoverage]
913
[TestFixture]
1014
public class JsGeneratorTests
1115
{
@@ -182,6 +186,168 @@ public void DataMemberAttributeHandling()
182186

183187
}
184188

189+
[Test]
190+
public void DefaultValueHandling()
191+
{
192+
//Generate a basic javascript model from a C# class
193+
194+
var modelType = typeof(AttributeInformationTest);
195+
196+
var outputJs = JsGenerator.Generate(new[] { modelType }, new JsGeneratorOptions()
197+
{
198+
ClassNameConstantsToRemove = new List<string>() { "Dto" },
199+
CamelCase = true,
200+
IncludeMergeFunction = false,
201+
OutputNamespace = "models",
202+
RespectDataMemberAttribute = true,
203+
RespectDefaultValueAttribute = true
204+
});
205+
206+
Assert.IsTrue(!string.IsNullOrEmpty(outputJs));
207+
208+
var js = new Jint.Parser.JavaScriptParser();
209+
210+
try
211+
{
212+
js.Parse(outputJs);
213+
}
214+
catch (Exception ex)
215+
{
216+
Assert.Fail("Expected no exception parsing javascript, but got: " + ex.Message);
217+
}
218+
219+
220+
}
221+
222+
[Test]
223+
public void UnexpectedStateHandling()
224+
{
225+
//Generate a basic javascript model from a C# class
226+
227+
var modelType = typeof(AttributeInformationTest);
228+
229+
var outputJs = JsGenerator.Generate(new[] { modelType }, new JsGeneratorOptions()
230+
{
231+
ClassNameConstantsToRemove = null,
232+
CamelCase = true,
233+
IncludeMergeFunction = false,
234+
OutputNamespace = "models",
235+
RespectDataMemberAttribute = false,
236+
RespectDefaultValueAttribute = false
237+
});
238+
239+
Assert.IsTrue(!string.IsNullOrEmpty(outputJs));
240+
241+
var js = new Jint.Parser.JavaScriptParser();
242+
243+
try
244+
{
245+
js.Parse(outputJs);
246+
}
247+
catch (Exception ex)
248+
{
249+
Assert.Fail("Expected no exception parsing javascript, but got: " + ex.Message);
250+
}
251+
252+
253+
}
254+
255+
[Test]
256+
public void FatalStateHandling()
257+
{
258+
//Generate a basic javascript model from a C# class
259+
260+
var modelType = typeof(AttributeInformationTest);
261+
var tempOptions = JsGenerator.Options;
262+
JsGenerator.Options = null;
263+
264+
Assert.Catch<ArgumentNullException>((() =>
265+
{
266+
var outputJs = JsGenerator.Generate(new[] { modelType });
267+
}), "Expected engine to throw ArguementNullException when Options are null");
268+
269+
JsGenerator.Options = tempOptions;
270+
271+
}
272+
273+
274+
[Test]
275+
public void CamelCaseHandling()
276+
{
277+
//Generate a basic javascript model from a C# class
278+
279+
var modelType = typeof(CamelCaseTest);
280+
281+
var outputJs = JsGenerator.Generate(new[] { modelType }, new JsGeneratorOptions()
282+
{
283+
ClassNameConstantsToRemove = new List<string>() { "Dto" },
284+
CamelCase = true,
285+
IncludeMergeFunction = false,
286+
OutputNamespace = "models",
287+
RespectDataMemberAttribute = true,
288+
RespectDefaultValueAttribute = true
289+
});
290+
291+
Assert.IsTrue(!string.IsNullOrEmpty(outputJs));
292+
293+
var js = new Jint.Parser.JavaScriptParser();
294+
295+
Program res = null;
296+
297+
try
298+
{
299+
res = js.Parse(outputJs);
300+
301+
}
302+
catch (Exception ex)
303+
{
304+
Assert.Fail("Expected no exception parsing javascript, but got: " + ex.Message);
305+
}
306+
307+
308+
var classExpression = res.Body.First().As<ExpressionStatement>();
309+
310+
var functionDefinition =
311+
classExpression.Expression.As<AssignmentExpression>().Right.As<FunctionExpression>()
312+
.Body.As<BlockStatement>()
313+
.Body;
314+
315+
var firstMemberDefinition =
316+
functionDefinition.Skip(1)
317+
.Take(1)
318+
.First()
319+
.As<ExpressionStatement>()
320+
.Expression.As<AssignmentExpression>();
321+
322+
var memberName = firstMemberDefinition.Left.As<MemberExpression>().Property.As<Identifier>().Name;
323+
324+
Assert.IsTrue(memberName == "wktPolygon");
325+
326+
var secondMemberDefinition =
327+
functionDefinition.Skip(2)
328+
.Take(1)
329+
.First()
330+
.As<ExpressionStatement>()
331+
.Expression.As<AssignmentExpression>();
332+
333+
var secondMemberName = secondMemberDefinition.Left.As<MemberExpression>().Property.As<Identifier>().Name;
334+
335+
Assert.IsTrue(secondMemberName == "alreadyUnderscored");
336+
337+
var thirdMemberDefinition =
338+
functionDefinition.Skip(3)
339+
.Take(1)
340+
.First()
341+
.As<ExpressionStatement>()
342+
.Expression.As<AssignmentExpression>();
343+
344+
var thirdMemberName = thirdMemberDefinition.Left.As<MemberExpression>().Property.As<Identifier>().Name;
345+
346+
Assert.IsTrue(thirdMemberName == "regularCased");
347+
348+
349+
}
350+
185351
}
186352

187353

Castle.Sharp2Js.Tests/SampleGeneration.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ models.AddressInformation = function (cons, overrideObj) {
88
this.Name = cons.Name;
99
this.Address = cons.Address;
1010
this.ZipCode = cons.ZipCode;
11-
if (!overrideObj.OwnerInformation) {
12-
this.Owner = new models.OwnerInformation(cons.Owner);
13-
} else {
14-
this.Owner = new overrideObj.OwnerInformation(cons.Owner, overrideObj);
11+
this.Owner = null;
12+
if (cons.Owner) {
13+
if (!overrideObj.OwnerInformation) {
14+
this.Owner = new models.OwnerInformation(cons.Owner);
15+
} else {
16+
this.Owner = new overrideObj.OwnerInformation(cons.Owner, overrideObj);
17+
}
1518
}
1619
this.Features = new Array(cons.Features == null ? 0 : cons.Features.length );
1720
if(cons.Features != null) {

0 commit comments

Comments
 (0)