diff options
author | Jonathan Coe <jbcoe@google.com> | 2020-04-28 14:11:09 +0100 |
---|---|---|
committer | Jonathan Coe <jbcoe@google.com> | 2020-04-28 14:11:09 +0100 |
commit | 44ad58b9915d29eb48be4f89368be6d30d174825 (patch) | |
tree | 8bd4fc3522c878a068c9d280c315d7c7582355ff /clang/unittests/Format/FormatTestCSharp.cpp | |
parent | 476ba8127bfa4553bf5ce1654cd844803e8d6dea (diff) | |
download | llvm-44ad58b9915d29eb48be4f89368be6d30d174825.zip llvm-44ad58b9915d29eb48be4f89368be6d30d174825.tar.gz llvm-44ad58b9915d29eb48be4f89368be6d30d174825.tar.bz2 |
[clang-format] Improved parser for C# properties
Summary:
Added some examples of properties from Microsoft documentation as test cases.
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties
Configuration support will be added in a follow up patch to address whether automatic properties are formatted as
```
Type MyType { get; set }
```
or
```
Type MyType
{ get; set }
```
Reviewers: krasimir, MyDeveloperDay
Reviewed By: krasimir
Subscribers: cfe-commits
Tags: #clang-format, #clang
Differential Revision: https://reviews.llvm.org/D78915
Diffstat (limited to 'clang/unittests/Format/FormatTestCSharp.cpp')
-rw-r--r-- | clang/unittests/Format/FormatTestCSharp.cpp | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/clang/unittests/Format/FormatTestCSharp.cpp b/clang/unittests/Format/FormatTestCSharp.cpp index d37a533..75112a7 100644 --- a/clang/unittests/Format/FormatTestCSharp.cpp +++ b/clang/unittests/Format/FormatTestCSharp.cpp @@ -613,6 +613,64 @@ public string Name { set => _name = value; })", Style); + + // Examples taken from + // https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties + verifyFormat(R"( +// Expression body definitions +public class SaleItem { + public decimal Price { + get => _cost; + set => _cost = value; + } +})", + Style); + + verifyFormat(R"( +// Properties with backing fields +class TimePeriod { + public double Hours { + get { return _seconds / 3600; } + set { + if (value < 0 || value > 24) + throw new ArgumentOutOfRangeException( + $"{nameof(value)} must be between 0 and 24."); + _seconds = value * 3600; + } + } +})", + Style); + + verifyFormat(R"( +// Auto-implemented properties +public class SaleItem { + public decimal Price { get; set; } +})", + Style); + + // Add column limit to wrap long lines. + Style.ColumnLimit = 100; + + // Examples with assignment to default value. + verifyFormat(R"( +// Long assignment to default value +class MyClass { + public override VeryLongNamedTypeIndeed VeryLongNamedValue { get; set } = + VeryLongNamedTypeIndeed.Create(DefaultFirstArgument, DefaultSecondArgument, + DefaultThirdArgument); +})", + Style); + + verifyFormat(R"( +// Long assignment to default value with expression body +class MyClass { + public override VeryLongNamedTypeIndeed VeryLongNamedValue { + get => veryLongNamedField; + set => veryLongNamedField = value; + } = VeryLongNamedTypeIndeed.Create(DefaultFirstArgument, DefaultSecondArgument, + DefaultThirdArgument); +})", + Style); } TEST_F(FormatTestCSharp, CSharpSpaces) { |