Skip to content

Commit

Permalink
Sonar Quality Gate (#577)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Feb 3, 2019
1 parent 3e179f2 commit de623fb
Show file tree
Hide file tree
Showing 26 changed files with 2,497 additions and 67 deletions.
268 changes: 268 additions & 0 deletions src/Core/Abstractions.Tests/ErrorBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Xunit;

namespace HotChocolate
{
public class ErrorBuilderTests
{
[Fact]
public void FromError()
{
// arrange
IError error = new Error { Message = "123" };

// act
ErrorBuilder builder = ErrorBuilder.FromError(error);
error = builder.Build();

// assert
Assert.Equal("123", error.Message);
}

[Fact]
public void FromError_WithExtensions()
{
// arrange
IError error = new Error
{
Message = "123",
Extensions = ImmutableDictionary<string, object>
.Empty
.Add("foo", "bar")
};

// act
ErrorBuilder builder = ErrorBuilder.FromError(error);
error = builder.Build();

// assert
Assert.Equal("123", error.Message);
Assert.Collection(error.Extensions,
t => Assert.Equal("bar", t.Value));
}

[Fact]
public void FromError_WithLocations()
{
// arrange
IError error = new Error
{
Message = "123",
Locations = ImmutableList<Location>
.Empty
.Add(new Location(1, 2))
};

// act
ErrorBuilder builder = ErrorBuilder.FromError(error);
error = builder.Build();

// assert
Assert.Equal("123", error.Message);
Assert.Collection(error.Locations,
t => Assert.Equal(1, t.Line));
}

[Fact]
public void FromError_ErrorNull_ArgumentNullException()
{
// arrange
// act
Action action = () => ErrorBuilder.FromError(null);

// assert
Assert.Throws<ArgumentNullException>(action);
}

[Fact]
public void SetMessage_MessageNull_ArgumentException()
{
// arrange
// act
Action action = () => ErrorBuilder.New().SetMessage(null);

// assert
Assert.Throws<ArgumentException>(action);
}

[Fact]
public void SetMessage_Bar_ErrorCodeIsFoo()
{
// arrange
// act
IError error = ErrorBuilder.New()
.SetMessage("bar")
.Build();

// assert
Assert.Equal("bar", error.Message);
}

[Fact]
public void SetCode_Foo_ErrorCodeIsFoo()
{
// arrange
// act
IError error = ErrorBuilder.New()
.SetMessage("bar")
.SetCode("foo")
.Build();

// assert
Assert.Equal("foo", error.Code);
Assert.Collection(error.Extensions,
t => Assert.Equal("foo", t.Value));
}

[Fact]
public void SetPath_Foo_PathIsFooWithCount1()
{
// arrange
// act
IError error = ErrorBuilder.New()
.SetMessage("bar")
.SetPath(new List<object> { "foo" })
.Build();

// assert
Assert.Collection(error.Path,
t => Assert.Equal("foo", t.ToString()));
}

[Fact]
public void SetPathObject_Foo_PathIsFooWithCount1()
{
// arrange
// act
IError error = ErrorBuilder.New()
.SetMessage("bar")
.SetPath(Path.New("foo"))
.Build();

// assert
Assert.Collection(error.Path,
t => Assert.Equal("foo", t.ToString()));
}

[Fact]
public void AddLocation()
{
// arrange
// act
IError error = ErrorBuilder.New()
.SetMessage("bar")
.AddLocation(new Location(2, 3))
.Build();

// assert
Assert.Collection(error.Locations,
t => Assert.Equal(2, t.Line));
}

[Fact]
public void AddLocation2()
{
// arrange
// act
IError error = ErrorBuilder.New()
.SetMessage("bar")
.AddLocation(new Location(2, 3))
.AddLocation(new Location(4, 5))
.Build();

// assert
Assert.Collection(error.Locations,
t => Assert.Equal(2, t.Line),
t => Assert.Equal(4, t.Line));
}

[Fact]
public void AddLocation3()
{
// arrange
// act
IError error = ErrorBuilder.New()
.SetMessage("bar")
.AddLocation(2, 3)
.AddLocation(new Location(4, 5))
.Build();

// assert
Assert.Collection(error.Locations,
t => Assert.Equal(2, t.Line),
t => Assert.Equal(4, t.Line));
}

[Fact]
public void AddLocation_LineSmallerThan1_ArgumentException()
{
// arrange
// act
Action action = () => ErrorBuilder.New()
.AddLocation(0, 3);

// assert
Assert.Throws<ArgumentOutOfRangeException>(action);
}

[Fact]
public void AddLocation_ColumnSmallerThan1_ArgumentException()
{
// arrange
// act
Action action = () => ErrorBuilder.New()
.AddLocation(2, 0);

// assert
Assert.Throws<ArgumentOutOfRangeException>(action);
}

[Fact]
public void SetException()
{
// arrange
var exception = new Exception();

// act
IError error = ErrorBuilder.New()
.SetMessage("bar")
.SetException(exception)
.Build();

// assert
Assert.Equal(exception, error.Exception);
}

[Fact]
public void SetExtension()
{
// arrange
// act
IError error = ErrorBuilder.New()
.SetMessage("bar")
.SetExtension("a", "b")
.SetExtension("a", "c")
.SetExtension("c", "d")
.Build();

// assert
Assert.Collection(error.Extensions.OrderBy(t => t.Key),
t => Assert.Equal("c", t.Value),
t => Assert.Equal("d", t.Value));
}

[Fact]
public void Build_NoMessage_InvalidOperationException()
{
// arrange
// act
Action action = () => ErrorBuilder.New().Build();

// assert
Assert.Throws<InvalidOperationException>(action);
}
}
}
Loading

0 comments on commit de623fb

Please sign in to comment.