Skip to content

Commit 2ca5ec0

Browse files
committed
C#: Add some string interpolation tests with alignment and formatting.
1 parent af6e1bd commit 2ca5ec0

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
public class MyStringInterpolationClass
4+
{
5+
6+
public void M()
7+
{
8+
float i = 3.14159f;
9+
const int align = 5;
10+
var x1 = $"Hello, Pi {i}";
11+
var x2 = $"Hello, Pi {i:F1}";
12+
var x3 = $"Hello, Pi {i,6}";
13+
var x4 = $"Hello, Pi {i,6:F3}";
14+
var x5 = $"Hello, Pi {i,align}";
15+
var x6 = $"Hello, Pi {i,align:F2}";
16+
}
17+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
semmle-extractor-options: /nostdlib /noconfig
2+
semmle-extractor-options: --load-sources-from-project:${testdir}/../../resources/stubs/_frameworks/Microsoft.NETCore.App/Microsoft.NETCore.App.csproj
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
inserts
2+
| StringInterpolation.cs:10:18:10:33 | $"..." | StringInterpolation.cs:10:31:10:31 | access to local variable i |
3+
| StringInterpolation.cs:11:18:11:36 | $"..." | StringInterpolation.cs:11:31:11:31 | access to local variable i |
4+
| StringInterpolation.cs:12:18:12:35 | $"..." | StringInterpolation.cs:12:31:12:31 | access to local variable i |
5+
| StringInterpolation.cs:13:18:13:38 | $"..." | StringInterpolation.cs:13:31:13:31 | access to local variable i |
6+
| StringInterpolation.cs:14:18:14:39 | $"..." | StringInterpolation.cs:14:31:14:31 | access to local variable i |
7+
| StringInterpolation.cs:15:18:15:42 | $"..." | StringInterpolation.cs:15:31:15:31 | access to local variable i |
8+
texts
9+
| StringInterpolation.cs:10:18:10:33 | $"..." | StringInterpolation.cs:10:20:10:29 | "Hello, Pi " |
10+
| StringInterpolation.cs:11:18:11:36 | $"..." | StringInterpolation.cs:11:20:11:29 | "Hello, Pi " |
11+
| StringInterpolation.cs:12:18:12:35 | $"..." | StringInterpolation.cs:12:20:12:29 | "Hello, Pi " |
12+
| StringInterpolation.cs:13:18:13:38 | $"..." | StringInterpolation.cs:13:20:13:29 | "Hello, Pi " |
13+
| StringInterpolation.cs:14:18:14:39 | $"..." | StringInterpolation.cs:14:20:14:29 | "Hello, Pi " |
14+
| StringInterpolation.cs:15:18:15:42 | $"..." | StringInterpolation.cs:15:20:15:29 | "Hello, Pi " |
15+
interpolationInsertsWithAlign
16+
| StringInterpolation.cs:12:18:12:35 | $"..." | StringInterpolation.cs:12:31:12:31 | access to local variable i | StringInterpolation.cs:12:33:12:33 | 6 |
17+
| StringInterpolation.cs:13:18:13:38 | $"..." | StringInterpolation.cs:13:31:13:31 | access to local variable i | StringInterpolation.cs:13:33:13:33 | 6 |
18+
| StringInterpolation.cs:14:18:14:39 | $"..." | StringInterpolation.cs:14:31:14:31 | access to local variable i | StringInterpolation.cs:14:33:14:37 | access to local variable align |
19+
| StringInterpolation.cs:15:18:15:42 | $"..." | StringInterpolation.cs:15:31:15:31 | access to local variable i | StringInterpolation.cs:15:33:15:37 | access to local variable align |
20+
interpolationInsertsWithFormat
21+
| StringInterpolation.cs:11:18:11:36 | $"..." | StringInterpolation.cs:11:31:11:31 | access to local variable i | StringInterpolation.cs:11:32:11:34 | "F1" |
22+
| StringInterpolation.cs:13:18:13:38 | $"..." | StringInterpolation.cs:13:31:13:31 | access to local variable i | StringInterpolation.cs:13:34:13:36 | "F3" |
23+
| StringInterpolation.cs:15:18:15:42 | $"..." | StringInterpolation.cs:15:31:15:31 | access to local variable i | StringInterpolation.cs:15:38:15:40 | "F2" |
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import csharp
2+
3+
query predicate inserts(InterpolatedStringExpr expr, Expr e) {
4+
expr.getAnInsert() = e // and inSpecificSource(expr)
5+
}
6+
7+
query predicate texts(InterpolatedStringExpr expr, StringLiteral literal) {
8+
expr.getAText() = literal // and inSpecificSource(expr)
9+
}
10+
11+
query predicate interpolationInsertsWithAlign(InterpolatedStringExpr expr, Expr insert, Expr align) {
12+
exists(InterpolatedStringInsertExpr e | expr.getInterpolatedInsert(_) = e |
13+
insert = e.getInsert() and
14+
align = e.getAlignment()
15+
)
16+
}
17+
18+
query predicate interpolationInsertsWithFormat(
19+
InterpolatedStringExpr expr, Expr insert, StringLiteral format
20+
) {
21+
exists(InterpolatedStringInsertExpr e | expr.getInterpolatedInsert(_) = e |
22+
insert = e.getInsert() and
23+
format = e.getFormat()
24+
)
25+
}

0 commit comments

Comments
 (0)