Skip to content

Commit 375b3d0

Browse files
committed
C#: Convert testing of cs/invalid-string-formatting to inline expectations and adjust some of the testcases.
1 parent e7f10dc commit 375b3d0

8 files changed

+144
-147
lines changed

csharp/ql/test/query-tests/API Abuse/FormatInvalid/FormatInvalid.cs

+51-51
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,38 @@ void FormatStringTests()
2424
String.Format("{0, -10 :0.000}", 1);
2525

2626
// BAD: Invalid format string
27-
String.Format("{ 0}", 1);
27+
String.Format("{ 0}", 1); // $ Alert
2828

2929
// BAD: Invalid format string
30-
String.Format("{0,--1}", 1);
30+
String.Format("{0,--1}", 1); // $ Alert
3131

3232
// BAD: Invalid format string
33-
String.Format("{0:{}}", 1);
33+
String.Format("{0:{}}", 1); // $ Alert
3434

3535
// BAD: Invalid format string
36-
String.Format("%d", 1);
36+
String.Format("%d", 1); // $ Alert Sink
3737

3838
// BAD: } { in the middle.
39-
String.Format("{{0}-{1}}", 0, 1);
39+
String.Format("{{0}-{1}}", 0, 1); // $ Alert
4040

4141
// BAD: This is invalid
42-
String.Format("{0}}", 0, 1);
42+
String.Format("{0}}", 0, 1); // $ Alert
4343

4444
// BAD: Invalid
45-
string.Format("{foo{0}}", 0);
45+
string.Format("{foo{0}}", 0); // $ Alert
4646

4747
// GOOD: {{ is output as {
48-
String.Format("{{sdc}}", 0);
48+
String.Format("{{sdc}}{0}", 0);
4949

5050
// BAD: Invalid: Stray }
51-
String.Format("}", 0);
51+
String.Format("}{0}", 0); // $ Alert
5252

5353
// GOOD: {{ output as {
54-
String.Format("new {0} ({1} => {{", 0);
54+
String.Format("new {0} ({1} => {{", 0, 1);
5555

5656
// GOOD: Literal {{ and }}
57-
String.Format("{{", "");
58-
String.Format("{{{{}}", "");
57+
String.Format("{0}{{", 0);
58+
String.Format("{0}{{{{}}", 0);
5959
}
6060

6161
IFormatProvider fp;
@@ -72,49 +72,49 @@ void FormatMethodTests()
7272
Format("}", 0);
7373

7474
// BAD: All of these are format methods with an invalid string.
75-
String.Format("}", 0);
76-
String.Format("}", ps);
77-
String.Format(fp, "}", ps);
78-
String.Format("}", 0, 1);
79-
String.Format("}", 0, 1, 2);
80-
String.Format("}", 0, 1, 2, 3);
81-
82-
sb.AppendFormat("}", 0);
83-
sb.AppendFormat("}", ps);
84-
sb.AppendFormat(fp, "}", ps);
85-
sb.AppendFormat("}", 0, 1);
86-
sb.AppendFormat("}", 0, 1, 2);
87-
sb.AppendFormat("}", 0, 1, 2, 3);
88-
89-
Console.WriteLine("}", 0);
90-
Console.WriteLine("}", ps);
91-
Console.WriteLine("}", 0, 1);
92-
Console.WriteLine("}", 0, 1, 2);
93-
Console.WriteLine("}", 0, 1, 2, 3);
94-
95-
tw.WriteLine("}", 0);
96-
tw.WriteLine("}", ps);
97-
tw.WriteLine("}", 0, 1);
98-
tw.WriteLine("}", 0, 1, 2);
99-
tw.WriteLine("}", 0, 1, 2, 3);
100-
101-
System.Diagnostics.Debug.WriteLine("}", ps);
102-
System.Diagnostics.Trace.TraceError("}", 0);
103-
System.Diagnostics.Trace.TraceInformation("}", 0);
104-
System.Diagnostics.Trace.TraceWarning("}", 0);
105-
ts.TraceInformation("}", 0);
106-
107-
Console.Write("}", 0);
108-
Console.Write("}", 0, 1);
109-
Console.Write("}", 0, 1, 2);
110-
Console.Write("}", 0, 1, 2, 3);
75+
String.Format("}", 0); // $ Alert
76+
String.Format("}", ps); // $ Alert
77+
String.Format(fp, "}", ps); // $ Alert
78+
String.Format("}", 0, 1); // $ Alert
79+
String.Format("}", 0, 1, 2); // $ Alert
80+
String.Format("}", 0, 1, 2, 3); // $ Alert
81+
82+
sb.AppendFormat("}", 0); // $ Alert
83+
sb.AppendFormat("}", ps); // $ Alert
84+
sb.AppendFormat(fp, "}", ps); // $ Alert
85+
sb.AppendFormat("}", 0, 1); // $ Alert
86+
sb.AppendFormat("}", 0, 1, 2); // $ Alert
87+
sb.AppendFormat("}", 0, 1, 2, 3); // $ Alert
88+
89+
Console.WriteLine("}", 0); // $ Alert
90+
Console.WriteLine("}", ps); // $ Alert
91+
Console.WriteLine("}", 0, 1); // $ Alert
92+
Console.WriteLine("}", 0, 1, 2); // $ Alert
93+
Console.WriteLine("}", 0, 1, 2, 3); // $ Alert
94+
95+
tw.WriteLine("}", 0); // $ Alert
96+
tw.WriteLine("}", ps); // $ Alert
97+
tw.WriteLine("}", 0, 1); // $ Alert
98+
tw.WriteLine("}", 0, 1, 2); // $ Alert
99+
tw.WriteLine("}", 0, 1, 2, 3); // $ Alert
100+
101+
System.Diagnostics.Debug.WriteLine("}", ps); // $ Alert
102+
System.Diagnostics.Trace.TraceError("}", 0); // $ Alert
103+
System.Diagnostics.Trace.TraceInformation("}", 0); // $ Alert
104+
System.Diagnostics.Trace.TraceWarning("}", 0); // $ Alert
105+
ts.TraceInformation("}", 0); // $ Alert
106+
107+
Console.Write("}", 0); // $ Alert
108+
Console.Write("}", 0, 1); // $ Alert
109+
Console.Write("}", 0, 1, 2); // $ Alert
110+
Console.Write("}", 0, 1, 2, 3); // $ Alert
111111

112112
System.Diagnostics.Debug.WriteLine("}", ""); // GOOD
113113
System.Diagnostics.Debug.Write("}", ""); // GOOD
114114

115-
System.Diagnostics.Debug.Assert(true, "Error", "}", ps);
116-
sw.Write("}", 0);
117-
System.Diagnostics.Debug.Print("}", ps);
115+
System.Diagnostics.Debug.Assert(true, "Error", "}", ps); // $ Alert
116+
sw.Write("}", 0); // $ Alert
117+
System.Diagnostics.Debug.Print("}", ps); // $ Alert
118118

119119
Console.WriteLine("}"); // GOOD
120120
}

0 commit comments

Comments
 (0)