Skip to content

Commit 83a8e3b

Browse files
committed
C#: Add some more testcases.
1 parent 43d6bf0 commit 83a8e3b

File tree

5 files changed

+45
-8
lines changed

5 files changed

+45
-8
lines changed

csharp/ql/test/library-tests/parameters/Parameters.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@ public class Parameters
2525
public void M17([Optional, DefaultParameterValue(null)] object arg7) => throw null;
2626
public void M18([Optional, DefaultParameterValue(3)] int? arg8) => throw null;
2727
public void M19([Optional, DecimalConstant(1, 0, 0, 0, 103)] decimal arg9) => throw null;
28+
public void M20([Optional, DefaultParameterValue(7)] MyStruct arg10) => throw null;
29+
public void M21([Optional, DefaultParameterValue("mystring")] MyStruct arg10) => throw null;
2830

29-
public struct MyStruct { }
31+
public struct MyStruct
32+
{
33+
public static implicit operator MyStruct(int i) => new MyStruct();
34+
public static implicit operator MyStruct(string s) => new MyStruct();
35+
36+
}
3037
public enum MyEnum { A = 1, B = 2 }
3138
}

csharp/ql/test/library-tests/parameters/Parameters.cs_

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@ public class ParametersDll
2525
public void M17([Optional, DefaultParameterValue(null)] object arg7) => throw null;
2626
public void M18([Optional, DefaultParameterValue(3)] int? arg8) => throw null;
2727
public void M19([Optional, DecimalConstant(1, 0, 0, 0, 103)] decimal arg9) => throw null;
28+
public void M20([Optional, DefaultParameterValue(7)] MyStruct arg10) => throw null;
29+
public void M21([Optional, DefaultParameterValue("mystring")] MyStruct arg10) => throw null;
2830

29-
public struct MyStruct { }
31+
public struct MyStruct
32+
{
33+
public static implicit operator MyStruct(int i) => new MyStruct();
34+
public static implicit operator MyStruct(string s) => new MyStruct();
35+
36+
}
3037
public enum MyEnum { A = 1, B = 2 }
3138
}
Binary file not shown.

csharp/ql/test/library-tests/parameters/Parameters.expected

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ noDefaultValue
55
| Parameters.cs:8:17:8:18 | M2 | Parameters.cs:8:24:8:24 | a | 0 |
66
| Parameters.cs:12:17:12:18 | M6 | Parameters.cs:12:29:12:30 | s1 | 0 |
77
| Parameters.cs:13:17:13:18 | M7 | Parameters.cs:13:27:13:28 | e1 | 0 |
8+
| Parameters.cs:33:32:33:39 | implicit conversion | Parameters.cs:33:54:33:54 | i | 0 |
9+
| Parameters.cs:34:32:34:39 | implicit conversion | Parameters.cs:34:57:34:57 | s | 0 |
810
| Parameters.dll:0:0:0:0 | M1 | Parameters.dll:0:0:0:0 | a | 0 |
911
| Parameters.dll:0:0:0:0 | M1 | Parameters.dll:0:0:0:0 | b | 1 |
1012
| Parameters.dll:0:0:0:0 | M1 | Parameters.dll:0:0:0:0 | c | 2 |
1113
| Parameters.dll:0:0:0:0 | M2 | Parameters.dll:0:0:0:0 | a | 0 |
1214
| Parameters.dll:0:0:0:0 | M6 | Parameters.dll:0:0:0:0 | s1 | 0 |
1315
| Parameters.dll:0:0:0:0 | M7 | Parameters.dll:0:0:0:0 | e1 | 0 |
16+
| Parameters.dll:0:0:0:0 | implicit conversion | Parameters.dll:0:0:0:0 | i | 0 |
17+
| Parameters.dll:0:0:0:0 | implicit conversion | Parameters.dll:0:0:0:0 | s | 0 |
1418
withDefaultValue
1519
| Parameters.cs:8:17:8:18 | M2 | Parameters.cs:8:34:8:34 | b | 1 | Parameters.cs:8:38:8:41 | null | null |
1620
| Parameters.cs:8:17:8:18 | M2 | Parameters.cs:8:51:8:51 | c | 2 | Parameters.cs:8:55:8:70 | "default string" | default string |
@@ -39,6 +43,8 @@ withDefaultValue
3943
| Parameters.cs:25:17:25:19 | M17 | Parameters.cs:25:68:25:71 | arg7 | 0 | Parameters.cs:25:21:25:71 | default | null |
4044
| Parameters.cs:26:17:26:19 | M18 | Parameters.cs:26:63:26:66 | arg8 | 0 | Parameters.cs:26:21:26:66 | 3 | 3 |
4145
| Parameters.cs:27:17:27:19 | M19 | Parameters.cs:27:74:27:77 | arg9 | 0 | Parameters.cs:27:21:27:77 | 10.3 | 10.3 |
46+
| Parameters.cs:28:17:28:19 | M20 | Parameters.cs:28:67:28:71 | arg10 | 0 | Parameters.cs:28:21:28:71 | call to operator implicit conversion | - |
47+
| Parameters.cs:29:17:29:19 | M21 | Parameters.cs:29:76:29:80 | arg10 | 0 | Parameters.cs:29:21:29:80 | call to operator implicit conversion | - |
4248
| Parameters.dll:0:0:0:0 | M2 | Parameters.dll:0:0:0:0 | b | 1 | Parameters.dll:0:0:0:0 | default | null |
4349
| Parameters.dll:0:0:0:0 | M2 | Parameters.dll:0:0:0:0 | c | 2 | Parameters.dll:0:0:0:0 | "default string" | default string |
4450
| Parameters.dll:0:0:0:0 | M3 | Parameters.dll:0:0:0:0 | a | 0 | Parameters.dll:0:0:0:0 | 1 | 1 |
@@ -66,8 +72,15 @@ withDefaultValue
6672
| Parameters.dll:0:0:0:0 | M17 | Parameters.dll:0:0:0:0 | arg7 | 0 | Parameters.dll:0:0:0:0 | default | null |
6773
| Parameters.dll:0:0:0:0 | M18 | Parameters.dll:0:0:0:0 | arg8 | 0 | Parameters.dll:0:0:0:0 | 3 | 3 |
6874
| Parameters.dll:0:0:0:0 | M19 | Parameters.dll:0:0:0:0 | arg9 | 0 | Parameters.dll:0:0:0:0 | 10.3 | 10.3 |
75+
| Parameters.dll:0:0:0:0 | M20 | Parameters.dll:0:0:0:0 | arg10 | 0 | Parameters.dll:0:0:0:0 | call to operator implicit conversion | - |
76+
| Parameters.dll:0:0:0:0 | M21 | Parameters.dll:0:0:0:0 | arg10 | 0 | Parameters.dll:0:0:0:0 | call to operator implicit conversion | - |
6977
dateTimeDefaults
7078
| Parameters.cs:22:17:22:19 | M14 | Parameters.cs:22:64:22:67 | arg4 | Parameters.cs:22:21:22:67 | object creation of type DateTime | DateTime(long) | 14 |
7179
| Parameters.cs:23:17:23:19 | M15 | Parameters.cs:23:68:23:71 | arg5 | Parameters.cs:23:21:23:71 | object creation of type DateTime | DateTime(long) | 10001 |
7280
| Parameters.dll:0:0:0:0 | M14 | Parameters.dll:0:0:0:0 | arg4 | Parameters.dll:0:0:0:0 | object creation of type DateTime | DateTime(long) | 14 |
7381
| Parameters.dll:0:0:0:0 | M15 | Parameters.dll:0:0:0:0 | arg5 | Parameters.dll:0:0:0:0 | object creation of type DateTime | DateTime(long) | 10001 |
82+
implicitConversionDefaults
83+
| Parameters.cs:28:17:28:19 | M20 | Parameters.cs:28:67:28:71 | arg10 | Parameters.cs:28:21:28:71 | call to operator implicit conversion | Parameters.cs:28:21:28:71 | 7 | Int32 | 7 |
84+
| Parameters.cs:29:17:29:19 | M21 | Parameters.cs:29:76:29:80 | arg10 | Parameters.cs:29:21:29:80 | call to operator implicit conversion | Parameters.cs:29:21:29:80 | "mystring" | String | mystring |
85+
| Parameters.dll:0:0:0:0 | M20 | Parameters.dll:0:0:0:0 | arg10 | Parameters.dll:0:0:0:0 | call to operator implicit conversion | Parameters.dll:0:0:0:0 | 7 | Int32 | 7 |
86+
| Parameters.dll:0:0:0:0 | M21 | Parameters.dll:0:0:0:0 | arg10 | Parameters.dll:0:0:0:0 | call to operator implicit conversion | Parameters.dll:0:0:0:0 | "mystring" | String | mystring |

csharp/ql/test/library-tests/parameters/Parameters.ql

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,33 @@ query predicate noDefaultValue(Parameterizable container, Parameter p, int i) {
1616
not compilerGeneratedAttribute(container)
1717
}
1818

19-
query predicate withDefaultValue(Parameterizable container, Parameter p, int i, Expr e, string value) {
19+
private predicate defaultValue(Parameterizable container, Parameter p, int i, Expr e) {
2020
fromTestLocation(container) and
2121
p.hasDefaultValue() and
2222
container.getParameter(i) = p and
23-
p.getDefaultValue() = e and
23+
p.getDefaultValue() = e
24+
}
25+
26+
query predicate withDefaultValue(Parameterizable container, Parameter p, int i, Expr e, string value) {
27+
defaultValue(container, p, i, e) and
2428
(if exists(e.getValue()) then value = e.getValue() else value = "-") and
2529
not compilerGeneratedAttribute(container)
2630
}
2731

2832
query predicate dateTimeDefaults(
2933
Parameterizable container, Parameter p, ObjectCreation o, string constructor, string value
3034
) {
31-
fromTestLocation(container) and
32-
p.hasDefaultValue() and
33-
container.getAParameter() = p and
34-
p.getDefaultValue() = o and
35+
defaultValue(container, p, _, o) and
3536
o.getTarget().toStringWithTypes() = constructor and
3637
o.getAnArgument().getValue() = value and
3738
not compilerGeneratedAttribute(container)
3839
}
40+
41+
query predicate implicitConversionDefaults(
42+
Parameterizable container, Parameter p, OperatorCall o, Expr e, string type, string value
43+
) {
44+
defaultValue(container, p, _, o) and
45+
o.getAnArgument() = e and
46+
type = e.getType().toString() and
47+
value = e.getValue()
48+
}

0 commit comments

Comments
 (0)