Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some tests of Swagger.ObjectModel #128

Merged
merged 5 commits into from
Oct 12, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Swagger.ObjectModel/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ namespace Swagger.ObjectModel
{
public static class Utilities
{
/// <summary>
/// convert a string to camelcase with special rules.
/// </summary>
Copy link
Owner

Choose a reason for hiding this comment

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

Not a fan of redundant comments usually

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All right , this comment needs to be remove !

/// <param name="val">the string need to be convertd</param>
/// <returns></returns>
public static string ToCamelCase(string val)
{
if (String.IsNullOrEmpty(val))
if (String.IsNullOrWhiteSpace(val))
{
return val;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Swagger.ObjectModel.Builders;
using Xunit;

namespace Swagger.ObjectModel.Tests.Builders
{
public class BodyParameterBuilderTest
{
private readonly BodyParameterBuilder builder;
private readonly string name;
private readonly Schema schema;
public BodyParameterBuilderTest()
Copy link
Owner

Choose a reason for hiding this comment

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

Missing a line break here

{
this.builder = new BodyParameterBuilder();

this.name = "name";
this.schema = new Schema
{
Description = "some description"
};
}

[Fact]
public void Should_ThrowRequiredFieldException_WhenNameOrSchemaIsNull()
{
// Assert
Assert.Throws(typeof(RequiredFieldException), () => builder.Build());
Assert.Throws(typeof(RequiredFieldException), () => builder.Name(name).Build());
Assert.Throws(typeof(RequiredFieldException), () => builder.Name(string.Empty).Schema(schema).Build());
}

[Fact]
public void Should_AbleToSetNameAndSchema()
{
// Arrange
Copy link
Owner

@yahehe yahehe Oct 10, 2017

Choose a reason for hiding this comment

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

Unnecessary comments
EDIT: I see now that you probably added these comments to be more in line with the tests that already exist. Don't follow that pattern, I'll clean those up

// Act
var bodyParameter = builder.Name(name)
.Schema(schema)
.Build();

// Assert
Assert.NotNull(bodyParameter);
Assert.Equal(name, bodyParameter.Name);
Assert.Same(schema, bodyParameter.Schema);
}

[Fact]
public void Should_AbleToSetDescription()
{
// Arrange
string description = "description";

// Act
var bodyParameter = builder.Name(name)
.Schema(schema)
.Description(description)
.Build();

// Assert
Assert.NotNull(bodyParameter);
Assert.Equal(description, bodyParameter.Description);
}
}
}
78 changes: 78 additions & 0 deletions test/Swagger.ObjectModel.Tests/Builders/ContactBuilderTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using Swagger.ObjectModel.Builders;
using Xunit;

namespace Swagger.ObjectModel.Tests.Builders
{
public class ContactBuilderTest
{
private readonly ContactBuilder builder;
public ContactBuilderTest()
{
this.builder = new ContactBuilder();
}

[Fact]
public void Should_AbleToSetName()
{
// Arrange
string name = "name";

// Act
var contact = builder.Name(name).Build();

// Assert
Assert.NotNull(contact);
Assert.Equal(name, contact.Name);
}

[Fact]
public void Should_AbleToSetEmailAddress()
{
// Arrange
string email = "[email protected]";

// Act
var contact = builder.EmailAddress(email).Build();

// Assert
Assert.NotNull(contact);
Assert.Equal(email, contact.EmailAddress);
}

[Fact]
public void Should_AbleToSetUrl()
{
// Arrange
string url = "https://github.com/yahehe/Nancy.Swagger";

// Act
var contact = builder.Url(url).Build();

// Assert
Assert.NotNull(contact);
Assert.Equal(url, contact.Url);
}

[Fact]
public void Should_AbleToSetAllProperties()
{
// Arrange
string name = "name";
string email = "[email protected]";
string url = "https://github.com/yahehe/Nancy.Swagger";

// Act
var contact = builder.Name(name)
.EmailAddress(email)
.Url(url)
.Build();

// Assert
Assert.NotNull(contact);
Assert.Equal(name, contact.Name);
Assert.Equal(email, contact.EmailAddress);
Assert.Equal(url, contact.Url);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Swagger.ObjectModel.Builders;
using Xunit;

namespace Swagger.ObjectModel.Tests.Builders
{
public class ExternalDocumentationBuilderTest
{
private readonly ExternalDocumentationBuilder builder;
private readonly string url = string.Empty;
public ExternalDocumentationBuilderTest()
{
this.builder = new ExternalDocumentationBuilder();
this.url = "https://github.com/yahehe/Nancy.Swagger";
}

[Fact]
public void Should_ThrowRequiredFieldException_WhenUrlIsNullOrEmpty()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, the tests are not all following the same format. But could you follow one of the current ones?
https://github.com/catcherwong/Nancy.Swagger/blob/ae38a293c225ff29d3bf6ada755847316c705e69/test/Nancy.Swagger.Tests/SwaggerModelDataBuilderTests.cs
The naming bothers me with the underscores.

Copy link
Owner

Choose a reason for hiding this comment

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

Oh really? I'm actually usually a fan of using _ to separate sentences like that:
[Should|ShouldNot][DoThisThing][When|If|ThisThing]

Copy link
Collaborator

Choose a reason for hiding this comment

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

I just feel like the keywords are helpful enough.

I'm fine either way though - I mostly brought it up because there was the precedent not to use them. Maybe rename the existing functions then?

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 find some Open Source projectss use this style for their tests , such as NancyFx . I use _ to separate for both of my own projects as well , so what I did is just follow this style .

{
// Assert
Assert.Throws(typeof(RequiredFieldException), () => builder.Url("").Build());
Assert.Throws(typeof(RequiredFieldException), () => builder.Url(null).Build());
}

[Fact]
public void Should_ThrowRequiredFieldException_WhenUrlIsNotSet()
{
// Assert
Assert.Throws(typeof(RequiredFieldException), () => builder.Build());
Assert.Throws(typeof(RequiredFieldException), () => builder.Description("desc").Build());
}

[Fact]
public void Should_AbleToSetUrl()
{
// Arrange
// Act
var externalDocumentation = builder.Url(url).Build();

// Assert
Assert.NotNull(externalDocumentation);
Assert.Equal(url, externalDocumentation.Url);
}

[Fact]
public void Should_AbleToSetDescription()
{
// Arrange
string description = "description";

// Act
var externalDocumentation = builder.Url(url)
.Description(description)
.Build();

// Assert
Assert.NotNull(externalDocumentation);
Assert.Equal(url, externalDocumentation.Url);
Assert.Equal(description, externalDocumentation.Description);
}
}
}
55 changes: 55 additions & 0 deletions test/Swagger.ObjectModel.Tests/Builders/HeaderBuilderTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Swagger.ObjectModel.Builders;
using Xunit;

namespace Swagger.ObjectModel.Tests.Builders
{
public class HeaderBuilderTest
{
private readonly HeaderBuilder builder;
public HeaderBuilderTest()
{
this.builder = new HeaderBuilder();
}

[Fact]
public void Should_ReturnEmptyHeader_WhenSetNothing()
{
// Arrange
// Act
var header = builder.Build();

// Assert
Assert.NotNull(header);
Assert.Null(header.Description);
Assert.Null(header.Default);
}

[Fact]
public void Should_AbleToSetDescription()
{
// Arrange
string description = "description";

// Act
var header = builder.Description(description).Build();

// Assert
Assert.NotNull(header);
Assert.Equal(description,header.Description);
}

[Fact]
public void Should_AbleToSetDefaultOfDataType()
{
// Arrange
object defaultValue = new object();

// Act
var header = builder.Default(defaultValue).Build();

// Assert
Assert.NotNull(header);
Assert.Equal(defaultValue, header.Default);
}
}
}
80 changes: 80 additions & 0 deletions test/Swagger.ObjectModel.Tests/UtilitiesTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using Should;
using Xunit;

namespace Swagger.ObjectModel.Tests
{
public class UtilitiesTest
{
[Fact]
public void ToCamelCase_EmptyOrNull_ShouldReturnItself()
{
// Arrange
string emptyString = string.Empty;
string nullString = null;
string whiteSpaceString = "";

// Act
var actual_empty = Utilities.ToCamelCase(emptyString);
var actual_null = Utilities.ToCamelCase(nullString);
var actual_whitespace = Utilities.ToCamelCase(whiteSpaceString);

// Assert
actual_empty.ShouldEqual(emptyString);
actual_null.ShouldEqual(actual_null);
actual_whitespace.ShouldEqual(whiteSpaceString);
}

[Fact]
public void ToCamelCase_StartWithDigit_ShouldReturnStartWith_()
{
// Arrange
string val = "123";

// Act
var actual = Utilities.ToCamelCase(val);

// Assert
actual.ShouldEqual("_123");
}

[Fact]
public void ToCamelCase_StartWithUpperLetter_ShouldReturnStartWithLowerLetter()
{
// Arrange
string val = "Abc";

// Act
var actual = Utilities.ToCamelCase(val);

// Assert
actual.ShouldEqual("abc");
}

[Fact]
public void ToCamelCase__ShouldReturnCamelString()
{
// Arrange
string val = "AbC123dEf";

// Act
var actual = Utilities.ToCamelCase(val);

// Assert
actual.ShouldEqual("abC123DEf");
}


[Fact]
public void ToCamelCase_WithSpecialSymbol__ShouldReturnCamelString()
{
// Arrange
string val = string.Format("{0}/{1}","get", "values");

// Act
var actual = Utilities.ToCamelCase(val);

// Assert
actual.ShouldEqual("getValues");
}
}
}