-
Notifications
You must be signed in to change notification settings - Fork 523
Add tests for WritableBuffer extensions and rename WriteAscii => WriteAsciiNoValidation #1553
Conversation
TFFT 👍 |
@@ -37,6 +37,12 @@ | |||
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" /> | |||
</ItemGroup> | |||
|
|||
<ItemGroup> | |||
<None Update="xunit.runner.json"> | |||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Attribute.
@@ -0,0 +1,5 @@ | |||
{ | |||
"$schema": "http://json.schemastore.org/xunit.runner.schema", | |||
"methodDisplay": "method", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a way to display as Class.Method
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wish. There are only two options. "classAndMethod"
and "method"
. The former is the default and is what results in the massively long test names in Test Explorer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😞
{ | ||
public class PipelineExtensionTests : IDisposable | ||
{ | ||
// ulong.MaxValue.ToString().Length |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make it readonly
and call that here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because it is used in an attribute it has to be a const.
// ulong.MaxValue.ToString().Length | ||
private const int _ulongMaxValueLength = 20; | ||
|
||
private readonly IPipe _pipe; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why shared state in tests? 😢
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
xUnit.net creates a new instance of the test class for every test that is run
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. I'm still sad but I can accept this. I think.
I like tests to be self-contained. They can call helper methods and such, but generally I avoid having class state like the plague. Ideally every test should be a self-contained mini spec of an expected behavior of the system.
[Theory] | ||
[InlineData(ulong.MinValue)] | ||
[InlineData(ulong.MaxValue)] | ||
[InlineData(4_8_15_16_23_42)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to test a random number somewhere between 0 and ulong.MaxValue. http://lost.wikia.com/wiki/The_Numbers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I'm not familiar with Lost references.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LOST
Assert.Equal(input.Length, reader.Buffer.Length); | ||
} | ||
|
||
private async Task<ReadResult> WriteAscii(string input) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Call it WriteAsciiNoValidation
too, otherwise it's a little confusing to read.
[InlineData("𤭢𐐝")] | ||
// non-ascii characters stored in 16 bits | ||
[InlineData("ñ٢⛄⛵")] | ||
public async Task WriteAsciiWritesOnlyOneBytePerChar(string input) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WriteAsciiNoValidationWritesOnlyOneBytePerChar
} | ||
|
||
[Theory] | ||
[InlineData(ulong.MinValue)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add case for single digit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like 0
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's ulong
not long
like my brain parsed so ignore this.
|
||
namespace Microsoft.AspNetCore.Server.KestrelTests | ||
{ | ||
public class PipelineExtensionTests : IDisposable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a WriteAsciiNoValidation
test encoding and verifying a single char, with data from 0-255. This guarantees no character value is special-cased in that method.
Edit: wording was super broken.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a test for everything in the ASCII range, 0x00 - 0x7F. As mentioned here #1508 (comment), characters outside the ASCII range should never reach this point...in theory.
[InlineData("!", new byte[] { 33 })] | ||
// null or empty | ||
[InlineData("", new byte[0])] | ||
[InlineData(default(string), new byte[0])] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Explicit null
.
{ | ||
const byte maxAscii = 0x7f; | ||
var writer = _pipe.Writer.Alloc(); | ||
for (var i = 0; i < maxAscii; i++) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use MemberData
instead of looping. Easier to figure out what's wrong if this ever breaks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it kill vstest performance even more?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems unnecessary to make it a theory. The test only has one assert. So if it goes wrong, the assert error would be
Assert.Equal() Failure
Expected: (x)
Actual: (y)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good.
AssertExtensions.Equal(Encoding.ASCII.GetBytes(testString), written.ToArray()); | ||
} | ||
|
||
public void Dispose() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: move this to the top
{ | ||
if (expected[i] != actual[i]) | ||
{ | ||
throw new XunitException($@"Expected byte at index {i} to be '\x{expected[i]}' but was '\x{actual[i]}'"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bytes will be printed in decimal. Seems like you wanted hex though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when green.
…eAsciiNoValidation
a8d67d1
to
686829d
Compare
Rebased. ⌚ CI |
Add tests for WritableBuffer.{WriteNumeric, WriteAscii}
Rename WriteAsciiNoValidation to make it clearer that it does not perform ascii validation.
Also, this changes the xunit display settings to make it easier to view tests in Test Explorer. (cref microsoft/vstest#623 )

Resolves #1508