|
| 1 | +using System; |
| 2 | +using MonoDroid.Generation; |
| 3 | +using NUnit.Framework; |
| 4 | + |
| 5 | +namespace generatortests |
| 6 | +{ |
| 7 | + [TestFixture] |
| 8 | + public class MethodVisibilityFixupTests |
| 9 | + { |
| 10 | + CodeGenerationOptions opt; |
| 11 | + GenericParameterDefinitionList list; |
| 12 | + CodeGeneratorContext context; |
| 13 | + |
| 14 | + [SetUp] |
| 15 | + public void SetUp () |
| 16 | + { |
| 17 | + opt = new CodeGenerationOptions (); |
| 18 | + list = new GenericParameterDefinitionList (); |
| 19 | + context = new CodeGeneratorContext (); |
| 20 | + } |
| 21 | + |
| 22 | + [Test] |
| 23 | + public void FixupBaseOverride () |
| 24 | + { |
| 25 | + var base_class = new TestClass ("java.lang.Object", "com.mypackage.Foo"); |
| 26 | + var this_class = new TestClass ("com.mypackage.Foo", "com.mypackage.MyClass"); |
| 27 | + |
| 28 | + base_class.Methods.Add (SupportTypeBuilder.CreateMethod (base_class, "DoSomething", opt)); |
| 29 | + base_class.Methods [0].Visibility = "protected"; |
| 30 | + opt.SymbolTable.AddType (base_class); |
| 31 | + |
| 32 | + this_class.Methods.Add (SupportTypeBuilder.CreateMethod (this_class, "DoSomething", opt)); |
| 33 | + this_class.Methods [0].Visibility = "public"; |
| 34 | + this_class.Methods [0].IsOverride = true; |
| 35 | + opt.SymbolTable.AddType (this_class); |
| 36 | + |
| 37 | + base_class.Validate (opt, list, context); |
| 38 | + this_class.Validate (opt, list, context); |
| 39 | + |
| 40 | + MethodVisibilityFixup.Fixup (this_class); |
| 41 | + |
| 42 | + Assert.AreEqual ("protected", this_class.Methods [0].Visibility); |
| 43 | + } |
| 44 | + |
| 45 | + [Test] |
| 46 | + public void FixupRecursiveBaseOverride () |
| 47 | + { |
| 48 | + // This throws a middle class into the inheritance tree that does |
| 49 | + // not override the method to ensure we look recursively |
| 50 | + var base_class = new TestClass ("java.lang.Object", "com.mypackage.Foo"); |
| 51 | + var middle_class = new TestClass ("com.mypackage.Foo", "com.mypackage.Middle"); |
| 52 | + var this_class = new TestClass ("com.mypackage.Middle", "com.mypackage.MyClass"); |
| 53 | + |
| 54 | + base_class.Methods.Add (SupportTypeBuilder.CreateMethod (base_class, "DoSomething", opt)); |
| 55 | + base_class.Methods [0].Visibility = "protected"; |
| 56 | + opt.SymbolTable.AddType (base_class); |
| 57 | + |
| 58 | + opt.SymbolTable.AddType (middle_class); |
| 59 | + |
| 60 | + this_class.Methods.Add (SupportTypeBuilder.CreateMethod (this_class, "DoSomething", opt)); |
| 61 | + this_class.Methods [0].Visibility = "public"; |
| 62 | + this_class.Methods [0].IsOverride = true; |
| 63 | + opt.SymbolTable.AddType (this_class); |
| 64 | + |
| 65 | + base_class.Validate (opt, list, context); |
| 66 | + this_class.Validate (opt, list, context); |
| 67 | + |
| 68 | + MethodVisibilityFixup.Fixup (this_class); |
| 69 | + |
| 70 | + Assert.AreEqual ("protected", this_class.Methods [0].Visibility); |
| 71 | + } |
| 72 | + |
| 73 | + [Test] |
| 74 | + public void FixupNestedBaseOverride () |
| 75 | + { |
| 76 | + // This tests: |
| 77 | + // - MyClass: public override void DoSomething () |
| 78 | + // |- MiddleClass: public override void DoSomething () |
| 79 | + // |- BaseClass: protected virtual void DoSomething () |
| 80 | + var base_class = new TestClass ("java.lang.Object", "com.mypackage.Foo"); |
| 81 | + var middle_class = new TestClass ("com.mypackage.Foo", "com.mypackage.Middle"); |
| 82 | + var this_class = new TestClass ("com.mypackage.Middle", "com.mypackage.MyClass"); |
| 83 | + |
| 84 | + base_class.Methods.Add (SupportTypeBuilder.CreateMethod (base_class, "DoSomething", opt)); |
| 85 | + base_class.Methods [0].Visibility = "protected"; |
| 86 | + opt.SymbolTable.AddType (base_class); |
| 87 | + |
| 88 | + middle_class.Methods.Add (SupportTypeBuilder.CreateMethod (middle_class, "DoSomething", opt)); |
| 89 | + middle_class.Methods [0].Visibility = "public"; |
| 90 | + middle_class.Methods [0].IsOverride = true; |
| 91 | + opt.SymbolTable.AddType (middle_class); |
| 92 | + |
| 93 | + this_class.Methods.Add (SupportTypeBuilder.CreateMethod (this_class, "DoSomething", opt)); |
| 94 | + this_class.Methods [0].Visibility = "public"; |
| 95 | + this_class.Methods [0].IsOverride = true; |
| 96 | + opt.SymbolTable.AddType (this_class); |
| 97 | + |
| 98 | + base_class.Validate (opt, list, context); |
| 99 | + middle_class.Validate (opt, list, context); |
| 100 | + this_class.Validate (opt, list, context); |
| 101 | + |
| 102 | + MethodVisibilityFixup.Fixup (this_class); |
| 103 | + |
| 104 | + Assert.AreEqual ("protected", this_class.Methods [0].Visibility); |
| 105 | + } |
| 106 | + |
| 107 | + [Test] |
| 108 | + public void FixupBaseProtectedInternalOverride () |
| 109 | + { |
| 110 | + var base_class = new TestClass ("java.lang.Object", "com.mypackage.Foo"); |
| 111 | + var this_class = new TestClass ("com.mypackage.Foo", "com.mypackage.MyClass"); |
| 112 | + |
| 113 | + base_class.Methods.Add (SupportTypeBuilder.CreateMethod (base_class, "DoSomething", opt)); |
| 114 | + base_class.Methods [0].Visibility = "protected internal"; |
| 115 | + opt.SymbolTable.AddType (base_class); |
| 116 | + |
| 117 | + this_class.Methods.Add (SupportTypeBuilder.CreateMethod (this_class, "DoSomething", opt)); |
| 118 | + this_class.Methods [0].Visibility = "public"; |
| 119 | + this_class.Methods [0].IsOverride = true; |
| 120 | + opt.SymbolTable.AddType (this_class); |
| 121 | + |
| 122 | + base_class.Validate (opt, list, context); |
| 123 | + this_class.Validate (opt, list, context); |
| 124 | + |
| 125 | + MethodVisibilityFixup.Fixup (this_class); |
| 126 | + |
| 127 | + Assert.AreEqual ("protected internal", this_class.Methods [0].Visibility); |
| 128 | + } |
| 129 | + |
| 130 | + [Test] |
| 131 | + public void IgnoreValidPublic () |
| 132 | + { |
| 133 | + var base_class = new TestClass ("java.lang.Object", "com.mypackage.Foo"); |
| 134 | + var this_class = new TestClass ("com.mypackage.Foo", "com.mypackage.MyClass"); |
| 135 | + |
| 136 | + base_class.Methods.Add (SupportTypeBuilder.CreateMethod (base_class, "DoSomething", opt)); |
| 137 | + base_class.Methods [0].Visibility = "public"; |
| 138 | + opt.SymbolTable.AddType (base_class); |
| 139 | + |
| 140 | + this_class.Methods.Add (SupportTypeBuilder.CreateMethod (this_class, "DoSomething", opt)); |
| 141 | + this_class.Methods [0].Visibility = "public"; |
| 142 | + this_class.Methods [0].IsOverride = true; |
| 143 | + opt.SymbolTable.AddType (this_class); |
| 144 | + |
| 145 | + base_class.Validate (opt, list, context); |
| 146 | + this_class.Validate (opt, list, context); |
| 147 | + |
| 148 | + MethodVisibilityFixup.Fixup (this_class); |
| 149 | + |
| 150 | + Assert.AreEqual ("public", this_class.Methods [0].Visibility); |
| 151 | + } |
| 152 | + |
| 153 | + [Test] |
| 154 | + public void FixupPropertyBaseOverride () |
| 155 | + { |
| 156 | + var base_class = new TestClass ("java.lang.Object", "com.mypackage.Foo"); |
| 157 | + var this_class = new TestClass ("com.mypackage.Foo", "com.mypackage.MyClass"); |
| 158 | + |
| 159 | + base_class.Properties.Add (SupportTypeBuilder.CreateProperty (base_class, "Count", "int", opt)); |
| 160 | + base_class.Properties [0].Getter.Visibility = "protected"; |
| 161 | + opt.SymbolTable.AddType (base_class); |
| 162 | + |
| 163 | + this_class.Properties.Add (SupportTypeBuilder.CreateProperty (this_class, "Count", "int", opt)); |
| 164 | + this_class.Properties [0].Getter.Visibility = "public"; |
| 165 | + this_class.Properties [0].Getter.IsOverride = true; |
| 166 | + opt.SymbolTable.AddType (this_class); |
| 167 | + |
| 168 | + base_class.Validate (opt, list, context); |
| 169 | + this_class.Validate (opt, list, context); |
| 170 | + |
| 171 | + MethodVisibilityFixup.Fixup (this_class); |
| 172 | + |
| 173 | + Assert.AreEqual ("protected", this_class.Properties [0].Getter.Visibility); |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments