Skip to content

Commit 9d92cc0

Browse files
CopilotOsirisTerje
andauthored
Document Using(StringComparison) and Using(CultureInfo) modifiers for string constraints (#1103)
* Initial plan * Document Using(StringComparison) and Using(CultureInfo) modifiers for string constraints Co-authored-by: OsirisTerje <[email protected]> * Fix CultureInfo examples to use more appropriate Turkish culture case comparison Co-authored-by: OsirisTerje <[email protected]> * Improve CultureInfo examples to show both case-sensitive and case-insensitive usage Co-authored-by: OsirisTerje <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: OsirisTerje <[email protected]>
1 parent ff48ec1 commit 9d92cc0

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

docs/articles/nunit/writing-tests/constraints/EndsWithConstraint.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ EndsWith(string expected)
1919

2020
```csharp
2121
...IgnoreCase
22+
...Using(StringComparison comparisonType)
23+
...Using(CultureInfo culture)
2224
```
2325

2426
## Examples of Use
@@ -30,6 +32,28 @@ Assert.That(phrase, Does.EndWith("!"));
3032
Assert.That(phrase, Does.EndWith("PASSING!").IgnoreCase);
3133
```
3234

35+
### Specifying a StringComparison
36+
37+
```csharp
38+
Assert.That("Hello World!", Does.EndWith("WORLD!").Using(StringComparison.OrdinalIgnoreCase));
39+
Assert.That("Hello World!", Does.EndWith("World!").Using(StringComparison.Ordinal));
40+
```
41+
42+
### Specifying a CultureInfo
43+
44+
The `Using(CultureInfo)` modifier allows for culture-specific string comparisons.
45+
It can be combined with `.IgnoreCase` for case-insensitive culture-aware comparisons:
46+
47+
```csharp
48+
// Using Turkish culture where 'i' and 'I' have special casing rules
49+
Assert.That("text TITLE", Does.EndWith("title").IgnoreCase.Using(new CultureInfo("tr-TR")));
50+
51+
// Culture-specific comparison without case-insensitivity
52+
Assert.That("Main Straße", Does.EndWith("Straße").Using(new CultureInfo("de-DE")));
53+
```
54+
3355
## Notes
3456

3557
1. **EndsWith** may appear only in the body of a constraint expression or when the inherited syntax is used.
58+
2. Only one `Using` modifier may be specified. Attempting to use multiple `Using` modifiers
59+
will throw an `InvalidOperationException`.

docs/articles/nunit/writing-tests/constraints/StartsWithConstraint.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ StartsWith(string expected)
1919

2020
```csharp
2121
...IgnoreCase
22+
...Using(StringComparison comparisonType)
23+
...Using(CultureInfo culture)
2224
```
2325

2426
## Examples of Use
@@ -30,7 +32,29 @@ Assert.That(phrase, Does.StartWith("Make"));
3032
Assert.That(phrase, Does.Not.StartWith("Break"));
3133
```
3234

35+
### Specifying a StringComparison
36+
37+
```csharp
38+
Assert.That("Hello World!", Does.StartWith("HELLO").Using(StringComparison.OrdinalIgnoreCase));
39+
Assert.That("Hello World!", Does.StartWith("Hello").Using(StringComparison.Ordinal));
40+
```
41+
42+
### Specifying a CultureInfo
43+
44+
The `Using(CultureInfo)` modifier allows for culture-specific string comparisons.
45+
It can be combined with `.IgnoreCase` for case-insensitive culture-aware comparisons:
46+
47+
```csharp
48+
// Using Turkish culture where 'i' and 'I' have special casing rules
49+
Assert.That("TITLE text", Does.StartWith("title").IgnoreCase.Using(new CultureInfo("tr-TR")));
50+
51+
// Culture-specific comparison without case-insensitivity
52+
Assert.That("Straße Street", Does.StartWith("Straße").Using(new CultureInfo("de-DE")));
53+
```
54+
3355
## Notes
3456

3557
1. **StartsWith** may appear only in the body of a constraint
3658
expression or when the inherited syntax is used.
59+
2. Only one `Using` modifier may be specified. Attempting to use multiple `Using` modifiers
60+
will throw an `InvalidOperationException`.

docs/articles/nunit/writing-tests/constraints/SubstringConstraint.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,35 @@ Does.Contain(string expected)
1818

1919
```csharp
2020
...IgnoreCase
21+
...Using(StringComparison comparisonType)
22+
...Using(CultureInfo culture)
2123
```
2224

2325
## Examples of Use
2426

2527
[!code-csharp[StringConstraintExamples](~/snippets/Snippets.NUnit/ConstraintExamples.cs#StringConstraintExamples)]
28+
29+
### Specifying a StringComparison
30+
31+
```csharp
32+
Assert.That("Hello World!", Does.Contain("WORLD").Using(StringComparison.OrdinalIgnoreCase));
33+
Assert.That("Hello World!", Does.Contain("World").Using(StringComparison.Ordinal));
34+
```
35+
36+
### Specifying a CultureInfo
37+
38+
The `Using(CultureInfo)` modifier allows for culture-specific string comparisons.
39+
It can be combined with `.IgnoreCase` for case-insensitive culture-aware comparisons:
40+
41+
```csharp
42+
// Using Turkish culture where 'i' and 'I' have special casing rules
43+
Assert.That("Hello TITLE World", Does.Contain("title").IgnoreCase.Using(new CultureInfo("tr-TR")));
44+
45+
// Culture-specific comparison without case-insensitivity
46+
Assert.That("Straße Street", Does.Contain("Straße").Using(new CultureInfo("de-DE")));
47+
```
48+
49+
## Notes
50+
51+
1. Only one `Using` modifier may be specified. Attempting to use multiple `Using` modifiers
52+
will throw an `InvalidOperationException`.

0 commit comments

Comments
 (0)