|
| 1 | +// Copyright (c) 2018 Daniel Grunwald |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of this |
| 4 | +// software and associated documentation files (the "Software"), to deal in the Software |
| 5 | +// without restriction, including without limitation the rights to use, copy, modify, merge, |
| 6 | +// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
| 7 | +// to whom the Software is furnished to do so, subject to the following conditions: |
| 8 | +// |
| 9 | +// The above copyright notice and this permission notice shall be included in all copies or |
| 10 | +// substantial portions of the Software. |
| 11 | +// |
| 12 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 13 | +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
| 14 | +// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
| 15 | +// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 16 | +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 17 | +// DEALINGS IN THE SOFTWARE. |
| 18 | + |
| 19 | +using System; |
| 20 | + |
| 21 | +namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty |
| 22 | +{ |
| 23 | + // Note: for null coalescing without assignment (??), see |
| 24 | + // LiftedOperators.cs, NullPropagation.cs and ThrowExpressions.cs. |
| 25 | + internal class NullCoalescingAssign |
| 26 | + { |
| 27 | + private class MyClass |
| 28 | + { |
| 29 | + public static string? StaticField1; |
| 30 | + public static int? StaticField2; |
| 31 | + public string? InstanceField1; |
| 32 | + public int? InstanceField2; |
| 33 | + |
| 34 | + public static string? StaticProperty1 { get; set; } |
| 35 | + public static int? StaticProperty2 { get; set; } |
| 36 | + |
| 37 | + public string? InstanceProperty1 { get; set; } |
| 38 | + public int? InstanceProperty2 { get; set; } |
| 39 | + |
| 40 | + public string? this[string name] { |
| 41 | + get { |
| 42 | + return null; |
| 43 | + } |
| 44 | + set { |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public int? this[int index] { |
| 49 | + get { |
| 50 | + return null; |
| 51 | + } |
| 52 | + set { |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private struct MyStruct |
| 58 | + { |
| 59 | + public string? InstanceField1; |
| 60 | + public int? InstanceField2; |
| 61 | + public string? InstanceProperty1 { get; set; } |
| 62 | + public int? InstanceProperty2 { get; set; } |
| 63 | + |
| 64 | + public string? this[string name] { |
| 65 | + get { |
| 66 | + return null; |
| 67 | + } |
| 68 | + set { |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public int? this[int index] { |
| 73 | + get { |
| 74 | + return null; |
| 75 | + } |
| 76 | + set { |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private static T Get<T>() |
| 82 | + { |
| 83 | + return default(T); |
| 84 | + } |
| 85 | + |
| 86 | + private static ref T GetRef<T>() |
| 87 | + { |
| 88 | + throw null; |
| 89 | + } |
| 90 | + |
| 91 | + private static void Use<T>(T t) |
| 92 | + { |
| 93 | + } |
| 94 | + |
| 95 | + public static void Locals() |
| 96 | + { |
| 97 | + string? local1 = null; |
| 98 | + int? local2 = null; |
| 99 | + local1 ??= "Hello"; |
| 100 | + local2 ??= 42; |
| 101 | + Use(local1 ??= "World"); |
| 102 | + Use(local2 ??= 42); |
| 103 | + } |
| 104 | + |
| 105 | + public static void StaticFields() |
| 106 | + { |
| 107 | + MyClass.StaticField1 ??= "Hello"; |
| 108 | + MyClass.StaticField2 ??= 42; |
| 109 | + Use(MyClass.StaticField1 ??= "World"); |
| 110 | + Use(MyClass.StaticField2 ??= 42); |
| 111 | + } |
| 112 | + |
| 113 | + public static void InstanceFields() |
| 114 | + { |
| 115 | + Get<MyClass>().InstanceField1 ??= "Hello"; |
| 116 | + Get<MyClass>().InstanceField2 ??= 42; |
| 117 | + Use(Get<MyClass>().InstanceField1 ??= "World"); |
| 118 | + Use(Get<MyClass>().InstanceField2 ??= 42); |
| 119 | + } |
| 120 | + |
| 121 | + public static void ArrayElements() |
| 122 | + { |
| 123 | + Get<string[]>()[Get<int>()] ??= "Hello"; |
| 124 | + Get<int?[]>()[Get<int>()] ??= 42; |
| 125 | + Use(Get<string[]>()[Get<int>()] ??= "World"); |
| 126 | + Use(Get<int?[]>()[Get<int>()] ??= 42); |
| 127 | + } |
| 128 | + |
| 129 | + public static void InstanceProperties() |
| 130 | + { |
| 131 | + Get<MyClass>().InstanceProperty1 ??= "Hello"; |
| 132 | + Get<MyClass>().InstanceProperty2 ??= 42; |
| 133 | + Use(Get<MyClass>().InstanceProperty1 ??= "World"); |
| 134 | + Use(Get<MyClass>().InstanceProperty2 ??= 42); |
| 135 | + } |
| 136 | + |
| 137 | + public static void StaticProperties() |
| 138 | + { |
| 139 | + MyClass.StaticProperty1 ??= "Hello"; |
| 140 | + MyClass.StaticProperty2 ??= 42; |
| 141 | + Use(MyClass.StaticProperty1 ??= "World"); |
| 142 | + Use(MyClass.StaticProperty2 ??= 42); |
| 143 | + } |
| 144 | + |
| 145 | + public static void RefReturn() |
| 146 | + { |
| 147 | + GetRef<string>() ??= "Hello"; |
| 148 | + GetRef<int?>() ??= 42; |
| 149 | + Use(GetRef<string>() ??= "World"); |
| 150 | + Use(GetRef<int?>() ??= 42); |
| 151 | + } |
| 152 | + |
| 153 | + public static void Dynamic() |
| 154 | + { |
| 155 | + Get<dynamic>().X ??= "Hello"; |
| 156 | + Get<dynamic>().Y ??= 42; |
| 157 | + Use(Get<dynamic>().X ??= "Hello"); |
| 158 | + Use(Get<dynamic>().Y ??= 42); |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments