-
-
Notifications
You must be signed in to change notification settings - Fork 722
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a539208
commit 6a2b153
Showing
6 changed files
with
291 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
using System; | ||
using HotChocolate.Language; | ||
using Xunit; | ||
|
||
namespace HotChocolate.Types | ||
{ | ||
public class InputCoercionTests | ||
{ | ||
/// <summary> | ||
/// Converts according to input coercion rules. | ||
/// </summary> | ||
[Fact] | ||
public void ConvertAccordingToInputCoercionRules() | ||
{ | ||
InputIsCoercedCorrectly<BooleanType, BooleanValueNode, bool>( | ||
new BooleanValueNode(true), true); | ||
InputIsCoercedCorrectly<BooleanType, BooleanValueNode, bool>( | ||
new BooleanValueNode(false), false); | ||
InputIsCoercedCorrectly<IntType, IntValueNode, int>( | ||
new IntValueNode("123"), 123); | ||
InputIsCoercedCorrectly<FloatType, IntValueNode, double>( | ||
new IntValueNode("123"), 123d); | ||
InputIsCoercedCorrectly<FloatType, FloatValueNode, double>( | ||
new FloatValueNode("123.456"), 123.456d); | ||
InputIsCoercedCorrectly<StringType, StringValueNode, string>( | ||
new StringValueNode("abc123"), "abc123"); | ||
InputIsCoercedCorrectly<IdType, StringValueNode, string>( | ||
new StringValueNode("123456"), "123456"); | ||
} | ||
|
||
/// <summary> | ||
/// Does not convert when input coercion rules reject a value. | ||
/// </summary> | ||
[Fact] | ||
public void ConvertAccordingToInputCoercionRules2() | ||
{ | ||
InputCannotBeCoercedCorrectly<BooleanType, IntValueNode>( | ||
new IntValueNode("123")); | ||
InputCannotBeCoercedCorrectly<IntType, FloatValueNode>( | ||
new FloatValueNode("123.123")); | ||
InputCannotBeCoercedCorrectly<IntType, BooleanValueNode>( | ||
new BooleanValueNode(true)); | ||
InputCannotBeCoercedCorrectly<IntType, StringValueNode>( | ||
new StringValueNode("123.123")); | ||
InputCannotBeCoercedCorrectly<FloatType, StringValueNode>( | ||
new StringValueNode("123")); | ||
InputCannotBeCoercedCorrectly<StringType, FloatValueNode>( | ||
new FloatValueNode("123.456")); | ||
InputCannotBeCoercedCorrectly<StringType, BooleanValueNode>( | ||
new BooleanValueNode(false)); | ||
InputIsCoercedCorrectly<IdType, StringValueNode, string>( | ||
new StringValueNode("123456"), "123456"); | ||
} | ||
|
||
[Fact] | ||
public void InputListIsInstanceOf() | ||
{ | ||
InputListIsInstanceOfInternal<BooleanType>( | ||
new ListValueNode(new BooleanValueNode(true))); | ||
InputListIsInstanceOfInternal<BooleanType>( | ||
new BooleanValueNode(true)); | ||
|
||
InputListIsNotInstanceOfInternal<BooleanType>( | ||
new ListValueNode(new IValueNode[] { | ||
new BooleanValueNode(true), | ||
new StringValueNode("123") })); | ||
InputListIsNotInstanceOfInternal<BooleanType>( | ||
new StringValueNode("123")); | ||
} | ||
|
||
[Fact] | ||
public void ListCanBeCoercedFromListValue() | ||
{ | ||
// arrange | ||
var type = new ListType(new BooleanType()); | ||
var list = new ListValueNode( | ||
new[] { | ||
new BooleanValueNode(true), | ||
new BooleanValueNode(false)}); | ||
|
||
// act | ||
object coercedValue = type.ParseLiteral(list); | ||
|
||
// assert | ||
Assert.IsType<bool[]>(coercedValue); | ||
Assert.Collection((bool[])coercedValue, | ||
t => Assert.True(t), | ||
t => Assert.False(t)); | ||
} | ||
|
||
[Fact] | ||
public void ListCanBeCoercedFromListElementValue() | ||
{ | ||
// arrange | ||
var type = new ListType(new BooleanType()); | ||
var element = new BooleanValueNode(true); | ||
|
||
// act | ||
object coercedValue = type.ParseLiteral(element); | ||
|
||
// assert | ||
Assert.IsType<bool[]>(coercedValue); | ||
Assert.Collection((bool[])coercedValue, | ||
t => Assert.True(t)); | ||
} | ||
|
||
[Fact] | ||
public void ListCannotBeCoercedFromMixedList() | ||
{ | ||
// arrange | ||
var type = new ListType(new BooleanType()); | ||
var list = new ListValueNode( | ||
new IValueNode[] { | ||
new BooleanValueNode(true), | ||
new StringValueNode("foo")}); | ||
|
||
// act | ||
Action action = () => type.ParseLiteral(list); | ||
|
||
// assert | ||
Assert.Throws<ArgumentException>(action); | ||
} | ||
|
||
[Fact] | ||
public void ListCannotBeCoercedIfElementTypeDoesNotMatch() | ||
{ | ||
// arrange | ||
var type = new ListType(new BooleanType()); | ||
var element = new StringValueNode("foo"); | ||
|
||
// act | ||
Action action = () => type.ParseLiteral(element); | ||
|
||
// assert | ||
Assert.Throws<ArgumentException>(action); | ||
} | ||
|
||
private void InputIsCoercedCorrectly<TType, TLiteral, TExpected>( | ||
TLiteral literal, TExpected expectedValue) | ||
where TType : ScalarType, new() | ||
where TLiteral : IValueNode | ||
{ | ||
// arrange | ||
var type = new TType(); | ||
|
||
// act | ||
object coercedValue = type.ParseLiteral(literal); | ||
|
||
// assert | ||
Assert.IsType<TExpected>(coercedValue); | ||
Assert.Equal(expectedValue, coercedValue); | ||
} | ||
|
||
private void InputCannotBeCoercedCorrectly<TType, TLiteral>( | ||
TLiteral literal) | ||
where TType : ScalarType, new() | ||
where TLiteral : IValueNode | ||
{ | ||
// arrange | ||
var type = new TType(); | ||
|
||
// act | ||
Action action = () => type.ParseLiteral(literal); | ||
|
||
// assert | ||
Assert.Throws<ArgumentException>(action); | ||
} | ||
|
||
private void InputListIsInstanceOfInternal<TElement>( | ||
IValueNode literal) | ||
where TElement : ScalarType, new() | ||
{ | ||
// arrange | ||
var type = new ListType(new TElement()); | ||
|
||
// act | ||
bool isInstanceOfType = type.IsInstanceOfType(literal); | ||
|
||
// assert | ||
Assert.True(isInstanceOfType); | ||
} | ||
|
||
private void InputListIsNotInstanceOfInternal<TElement>( | ||
IValueNode literal) | ||
where TElement : ScalarType, new() | ||
{ | ||
// arrange | ||
var type = new ListType(new TElement()); | ||
|
||
// act | ||
bool isInstanceOfType = type.IsInstanceOfType(literal); | ||
|
||
// assert | ||
Assert.False(isInstanceOfType); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/Core.Tests/__snapshots__/EnumValueIsCoercedToListValue.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"Data": { | ||
"heroes": [ | ||
{ | ||
"name": "Luke Skywalker" | ||
} | ||
] | ||
}, | ||
"Errors": null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters