Skip to content

Commit e15b20f

Browse files
committed
C#: Add extensions test.
1 parent 5c5046a commit e15b20f

File tree

6 files changed

+311
-0
lines changed

6 files changed

+311
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
extensionTypeReceiverParameter
2+
| extensions.cs:6:5:17:5 | extension(String) | extensions.cs:6:22:6:22 | s |
3+
| extensions.cs:26:5:35:5 | extension(Object)<Object> | extensions.cs:26:20:26:20 | t |
4+
| extensions.cs:26:5:35:5 | extension(String)<String> | extensions.cs:26:20:26:20 | t |
5+
| extensions.cs:26:5:35:5 | extension(T)`1 | extensions.cs:26:20:26:20 | t |
6+
extensionTypeExtendedType
7+
| extensions.cs:6:5:17:5 | extension(String) | string |
8+
| extensions.cs:19:5:24:5 | extension(Object) | object |
9+
| extensions.cs:26:5:35:5 | extension(Object)<Object> | object |
10+
| extensions.cs:26:5:35:5 | extension(String)<String> | string |
11+
| extensions.cs:26:5:35:5 | extension(T)`1 | T |
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import csharp
2+
3+
query predicate extensionTypeReceiverParameter(ExtensionType et, Parameter p) {
4+
et.getFile().getBaseName() = "extensions.cs" and
5+
p = et.getReceiverParameter()
6+
}
7+
8+
query predicate extensionTypeExtendedType(ExtensionType et, string t) {
9+
et.getFile().getBaseName() = "extensions.cs" and
10+
t = et.getExtendedType().toStringWithTypes()
11+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
public static class MyExtensions
5+
{
6+
extension(string s)
7+
{
8+
public bool Prop1 => s.Length > 0;
9+
public bool Prop2 { get { return true; } set { } }
10+
public static bool StaticProp1 { get { return false; } }
11+
public bool M1() => s is not null;
12+
public string M2(string other) { return s + other; }
13+
public static int StaticM1() { return 0; }
14+
public static int StaticM2(string x) { return x.Length; }
15+
public static string operator *(int a, string b) { return ""; }
16+
public T StringGenericM1<T>(T t, object o) { return t; }
17+
}
18+
19+
extension(object)
20+
{
21+
public static int StaticObjectM1() { return 0; }
22+
public static int StaticObjectM2(string s) { return s.Length; }
23+
public static bool StaticProp => true;
24+
}
25+
26+
extension<T>(T t) where T : class
27+
{
28+
public bool GenericProp1 => t is not null;
29+
public bool GenericProp2 { get { return true; } set { } }
30+
public bool GenericM1() => t is not null;
31+
public void GenericM2<S>(S other) { }
32+
public void GenericStaticM1() { }
33+
public static void GenericStaticM2<S>(S other) { }
34+
public static T operator +(T a, T b) { return null; }
35+
}
36+
}
37+
38+
public static class ClassicExtensions
39+
{
40+
public static bool M3(this string s) => s is not null;
41+
}
42+
43+
public class C
44+
{
45+
public static void CallingExtensions()
46+
{
47+
var s = "Hello World.";
48+
49+
// Calling the extensions properties
50+
var x11 = s.Prop1;
51+
var x12 = s.Prop2;
52+
s.Prop2 = true;
53+
var x13 = string.StaticProp1;
54+
var x14 = object.StaticProp;
55+
56+
// Calling the extensions methods.
57+
var x21 = s.M1();
58+
var x22 = s.M2("!!!");
59+
var x23 = string.StaticM1();
60+
var x24 = string.StaticM2(s);
61+
var x25 = object.StaticObjectM1();
62+
var x26 = object.StaticObjectM2(s);
63+
64+
// Calling the extension operator.
65+
var x30 = 3 * s;
66+
67+
// Calling the classic extension method.
68+
var y = s.M3();
69+
70+
// Calling the compiler generated static extension methods.
71+
MyExtensions.M1(s);
72+
MyExtensions.M2(s, "!!!");
73+
MyExtensions.StaticM1();
74+
MyExtensions.StaticM2(s);
75+
MyExtensions.StaticObjectM1();
76+
MyExtensions.StaticObjectM2(s);
77+
78+
// Calling the compiler generated operator method.
79+
MyExtensions.op_Multiply(3, s);
80+
81+
// Calling the compiler generated methods used by the extension property accessors.
82+
MyExtensions.get_Prop1(s);
83+
MyExtensions.get_Prop2(s);
84+
MyExtensions.set_Prop2(s, false);
85+
MyExtensions.get_StaticProp();
86+
}
87+
88+
public static void CallingGenericExtensions()
89+
{
90+
var s = "Hello Generic World.";
91+
var o = new object();
92+
93+
// Calling generic extension method
94+
o.GenericM1();
95+
s.GenericM1();
96+
97+
// Calling the compiler generated static extension methods.
98+
MyExtensions.GenericM1(o);
99+
MyExtensions.GenericM1(s);
100+
101+
o.GenericM2(42);
102+
MyExtensions.GenericM2(o, 42);
103+
104+
s.StringGenericM1<int>(7, new object());
105+
MyExtensions.StringGenericM1<string>(s, "test", new object());
106+
}
107+
}

0 commit comments

Comments
 (0)