Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Add tests for WritableBuffer extensions and rename WriteAscii => WriteAsciiNoValidation #1553

Merged
merged 1 commit into from
Mar 29, 2017

Conversation

natemcmaster
Copy link
Contributor

@natemcmaster natemcmaster commented Mar 24, 2017

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 )
image

Resolves #1508

@benaadams
Copy link
Contributor

Also, this changes the xunit display settings to make it easier to view the write test in Test Explorer

TFFT 👍

@@ -37,6 +37,12 @@
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>

<ItemGroup>
<None Update="xunit.runner.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Copy link
Contributor

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",
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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
Copy link
Contributor

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.

Copy link
Contributor Author

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;
Copy link
Contributor

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? 😢

Copy link
Contributor Author

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

Copy link
Contributor

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)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Contributor Author

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

Copy link
Contributor

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.

Copy link
Member

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)
Copy link
Contributor

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)
Copy link
Contributor

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)]
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like 0? :trollface:

Copy link
Contributor

@cesarblum cesarblum Mar 24, 2017

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
Copy link
Contributor

@cesarblum cesarblum Mar 24, 2017

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.

Copy link
Contributor Author

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])]
Copy link
Contributor

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++)
Copy link
Contributor

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.

Copy link
Contributor

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?

Copy link
Contributor Author

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)

Copy link
Contributor

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()
Copy link
Contributor

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]}'");
Copy link
Contributor

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.

Copy link
Contributor

@cesarblum cesarblum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit: when green.

@natemcmaster
Copy link
Contributor Author

@halter73 ok if I merge, or should I wait for your transport refactor #1551?

@natemcmaster natemcmaster added blocked Blocked and removed blocked Blocked labels Mar 29, 2017
@natemcmaster natemcmaster force-pushed the namc/buffer-ext-test branch from a8d67d1 to 686829d Compare March 29, 2017 23:14
@natemcmaster
Copy link
Contributor Author

Rebased. ⌚ CI

@natemcmaster natemcmaster merged commit 686829d into dev Mar 29, 2017
@natemcmaster natemcmaster deleted the namc/buffer-ext-test branch March 29, 2017 23:40
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants