|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.Diagnostics.CodeAnalysis; |
| 4 | +using System.Linq; |
3 | 5 | using Castle.Sharp2Js.SampleData; |
4 | 6 | using Castle.Sharp2Js.Tests.DTOs; |
| 7 | +using Jint.Parser.Ast; |
5 | 8 | using NUnit.Framework; |
6 | 9 |
|
7 | 10 | namespace Castle.Sharp2Js.Tests |
8 | 11 | { |
| 12 | + [ExcludeFromCodeCoverage] |
9 | 13 | [TestFixture] |
10 | 14 | public class JsGeneratorTests |
11 | 15 | { |
@@ -182,6 +186,168 @@ public void DataMemberAttributeHandling() |
182 | 186 |
|
183 | 187 | } |
184 | 188 |
|
| 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 | + |
185 | 351 | } |
186 | 352 |
|
187 | 353 |
|
|
0 commit comments