From 2cabd589ac6a2fcd88b3c23aa50541536d2c8b71 Mon Sep 17 00:00:00 2001 From: Doug Bunting Date: Tue, 28 Mar 2017 23:02:39 -0700 Subject: [PATCH] Add more `ExpressionHelper` tests - #5792 --- .../Internal/ExpressionHelperTest.cs | 73 ++++++++++++++++--- .../Model/Constants.cs | 15 ++++ .../Model/Model.cs | 10 +++ 3 files changed, 88 insertions(+), 10 deletions(-) create mode 100644 test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Model/Constants.cs create mode 100644 test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Model/Model.cs diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/ExpressionHelperTest.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/ExpressionHelperTest.cs index 1401678738..b7137b1457 100644 --- a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/ExpressionHelperTest.cs +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Internal/ExpressionHelperTest.cs @@ -19,23 +19,18 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal { var i = 3; var value = "Test"; - var Model = new TestModel(); var key = "TestModel"; var myModels = new List(); var models = new List(); var modelTest = new TestModel(); var modelType = typeof(TestModel); - return new TheoryData + var data = new TheoryData { { (Expression>)(model => model.SelectedCategory), "SelectedCategory" }, - { - (Expression>)(m => Model.SelectedCategory), - "SelectedCategory" - }, { (Expression>)(model => model.SelectedCategory.CategoryName), "SelectedCategory.CategoryName" @@ -56,10 +51,6 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal (Expression>)(model => value), "value" }, - { - (Expression>)(m => Model), - string.Empty - }, { (Expression>)(model => models[0].SelectedCategory.CategoryId), "models[0].SelectedCategory.CategoryId" @@ -120,7 +111,57 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal (Expression, int>>)(model => model.FirstOrDefault().PreferredCategories.FirstOrDefault().CategoryId), "CategoryId" }, + // Constants are not supported. + { + // Namespace never appears in expresison name. "Model" there doesn't matter. + (Expression>)(m => Microsoft.AspNetCore.Mvc.ViewFeatures.Model.Constants.WoodstockYear), + string.Empty + }, + { + // Class name never appears in expresion name. "Model" there doesn't matter. + (Expression>)(m => Model.Constants.WoodstockYear), + string.Empty + }, + // ExpressionHelper treats static properties like other member accesses. Similarly to + // RazorPage.Model, name "Model" is ignored at LHS of these expressions. This is a rare case because + // static properties are the only leftmost member accesses that can reach beyond the current class. + { + (Expression>)(m => Model.Constants.Model.Name), + "Name" + }, + { + (Expression>)(m => AStaticClass.Model), + string.Empty + }, + { + (Expression>)(m => AStaticClass.Test), + "Test" + }, + { + (Expression>)(m => AnotherStaticClass.Model.Name), + "Name" + }, + { + (Expression>)(m => AnotherStaticClass.Test.Name), + "Test.Name" + }, }; + + { + // Nearly impossible in a .cshtml file because model is a keyword. + var model = "Some string"; + data.Add((Expression>)(m => model), string.Empty); + } + + { + // Model property in RazorPage is "special" (in a good way). + var Model = new TestModel(); + data.Add((Expression>)(m => Model), string.Empty); + data.Add((Expression>)(model => Model), string.Empty); + data.Add((Expression>)(m => Model.SelectedCategory), "SelectedCategory"); + } + + return data; } } @@ -378,5 +419,17 @@ namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal public string MainCategory { get; set; } public string SubCategory { get; set; } } + + private static class AStaticClass + { + public static string Model { get; set; } + public static string Test { get; set; } + } + + private static class AnotherStaticClass + { + public static Model.Model Model { get; set; } + public static Model.Model Test { get; set; } + } } } diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Model/Constants.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Model/Constants.cs new file mode 100644 index 0000000000..4a5fee5b43 --- /dev/null +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Model/Constants.cs @@ -0,0 +1,15 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Model +{ + public static class Constants + { + public const int WoodstockYear = 1969; + + public static Model Model { get; } = new Model + { + Name = "Woodstock", + }; + } +} diff --git a/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Model/Model.cs b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Model/Model.cs new file mode 100644 index 0000000000..d38f673122 --- /dev/null +++ b/test/Microsoft.AspNetCore.Mvc.ViewFeatures.Test/Model/Model.cs @@ -0,0 +1,10 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Model +{ + public class Model + { + public string Name { get; set; } + } +}