Skip to content

Commit

Permalink
* CustomUriFunction - Added test using ODataUriParser.
Browse files Browse the repository at this point in the history
* Changed name of some methods :
'MatchSignatureToBuiltInFunction' to 'MatchSignatureToUriFunction'.
'BindAsBuiltInFunction' to 'BindAsUriFunction'.
  • Loading branch information
YogiBear52 authored and LaylaLiu committed Jan 13, 2016
1 parent 174da80 commit afd7a99
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ internal static SingleValueNode[] ValidateArgumentsAreSingleValue(string functio
/// <param name="argumentNodes">The nodes of the arguments, can be new {null,null}.</param>
/// <param name="signatures">The signatures to match against</param>
/// <returns>Returns the matching signature or throws</returns>
internal static FunctionSignatureWithReturnType MatchSignatureToBuiltInFunction(string functionName, SingleValueNode[] argumentNodes, FunctionSignatureWithReturnType[] signatures)
internal static FunctionSignatureWithReturnType MatchSignatureToUriFunction(string functionName, SingleValueNode[] argumentNodes, FunctionSignatureWithReturnType[] signatures)
{
FunctionSignatureWithReturnType signature;
IEdmTypeReference[] argumentTypes = argumentNodes.Select(s => s.TypeReference).ToArray();
Expand Down Expand Up @@ -202,7 +202,7 @@ internal QueryNode BindFunctionCall(FunctionCallToken functionCallToken)
// If there isn't, bind as built-in function
// Bind all arguments
List<QueryNode> argumentNodes = new List<QueryNode>(functionCallToken.Arguments.Select(ar => this.bindMethod(ar)));
return BindAsBuiltInFunction(functionCallToken, argumentNodes);
return BindAsUriFunction(functionCallToken, argumentNodes);
}

/// <summary>
Expand Down Expand Up @@ -250,7 +250,7 @@ internal bool TryBindDottedIdentifierAsFunctionCall(DottedIdentifierToken dotted
/// <param name="functionCallToken">the function call token to bind</param>
/// <param name="argumentNodes">list of semantically bound arguments</param>
/// <returns>A function call node bound to this function.</returns>
private QueryNode BindAsBuiltInFunction(FunctionCallToken functionCallToken, List<QueryNode> argumentNodes)
private QueryNode BindAsUriFunction(FunctionCallToken functionCallToken, List<QueryNode> argumentNodes)
{
if (functionCallToken.Source != null)
{
Expand All @@ -270,7 +270,7 @@ private QueryNode BindAsBuiltInFunction(FunctionCallToken functionCallToken, Lis
// Do some validation and get potential built-in functions that could match what we saw
FunctionSignatureWithReturnType[] signatures = GetUriFunctionSignatures(functionCallTokenName);
SingleValueNode[] argumentNodeArray = ValidateArgumentsAreSingleValue(functionCallTokenName, argumentNodes);
FunctionSignatureWithReturnType signature = MatchSignatureToBuiltInFunction(functionCallTokenName, argumentNodeArray, signatures);
FunctionSignatureWithReturnType signature = MatchSignatureToUriFunction(functionCallTokenName, argumentNodeArray, signatures);
if (signature.ReturnType != null)
{
TypePromoteArguments(signature, argumentNodes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.OData.Core.Tests.UriParser;

namespace Microsoft.OData.Core.Tests.ScenarioTests.UriParser
{
Expand Down Expand Up @@ -513,6 +514,34 @@ public void RemoveCustomFunction_RemoveFunctionWithSameNameAndSignature_OtherOve

#endregion

#region ODataUriParser

[Fact]
public void ParseWithCustomUriFunction()
{
try
{
FunctionSignatureWithReturnType myStringFunction
= new FunctionSignatureWithReturnType(EdmCoreModel.Instance.GetBoolean(true), EdmCoreModel.Instance.GetString(true), EdmCoreModel.Instance.GetString(true));

// Add a custom uri function
CustomUriFunctions.AddCustomUriFunction("mystringfunction", myStringFunction);

var fullUri = new Uri("http://www.odata.com/OData/People" + "?$filter=mystringfunction(Name, 'BlaBla')");
ODataUriParser parser = new ODataUriParser(HardCodedTestModel.TestModel, new Uri("http://www.odata.com/OData/"), fullUri);

var startsWithArgs = parser.ParseFilter().Expression.ShouldBeSingleValueFunctionCallQueryNode("mystringfunction").And.Parameters.ToList();
startsWithArgs[0].ShouldBeSingleValuePropertyAccessQueryNode(HardCodedTestModel.GetPersonNameProp());
startsWithArgs[1].ShouldBeConstantQueryNode("BlaBla");
}
finally
{
CustomUriFunctions.RemoveCustomUriFunction("mystringfunction");
}
}

#endregion

#region Private Methods

private FunctionSignatureWithReturnType[] GetCustomFunctionSignaturesOrNull(string customFunctionName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public void MatchArgumentsToSignatureDuplicateSignature()
};
var signatures = this.GetDuplicateIndexOfFunctionSignatureForTest();

Action bind = () => FunctionCallBinder.MatchSignatureToBuiltInFunction(
Action bind = () => FunctionCallBinder.MatchSignatureToUriFunction(
"IndexOf",
new SingleValueNode[] {
new SingleValuePropertyAccessNode(new ConstantNode(null)/*parent*/, new EdmStructuralProperty(new EdmEntityType("MyNamespace", "MyEntityType"), "myPropertyName", argumentNodes[0].GetEdmTypeReference())),
Expand All @@ -318,7 +318,7 @@ public void MatchArgumentsToSignature()
};
var signatures = this.GetHardCodedYearFunctionSignatureForTest();

var result = FunctionCallBinder.MatchSignatureToBuiltInFunction(
var result = FunctionCallBinder.MatchSignatureToUriFunction(
"year",
argumentNodes.Select(s => (SingleValueNode)s).ToArray(),
signatures);
Expand All @@ -336,7 +336,7 @@ public void MatchArgumentsToSignatureNoMatchEmpty()
new ConstantNode(4)
};

Action bind = () => FunctionCallBinder.MatchSignatureToBuiltInFunction(
Action bind = () => FunctionCallBinder.MatchSignatureToUriFunction(
"year",
new SingleValueNode[] {
new SingleValuePropertyAccessNode(new ConstantNode(null)/*parent*/, new EdmStructuralProperty(new EdmEntityType("MyNamespace", "MyEntityType"), "myPropertyName", argumentNodes[0].GetEdmTypeReference()))},
Expand All @@ -356,7 +356,7 @@ public void MatchArgumentsToSignatureNoMatchContainsSignatures()
new ConstantNode(4)
};

Action bind = () => FunctionCallBinder.MatchSignatureToBuiltInFunction(
Action bind = () => FunctionCallBinder.MatchSignatureToUriFunction(
"year",
new SingleValueNode[] {
new SingleValuePropertyAccessNode(new ConstantNode(null)/*parent*/, new EdmStructuralProperty(new EdmEntityType("MyNamespace", "MyEntityType"), "myPropertyName", argumentNodes[0].GetEdmTypeReference()))},
Expand All @@ -370,7 +370,7 @@ public void MatchArgumentsToSignatureNoMatchContainsSignatures()
[Fact]
public void MatchArgumentsToSignatureWillPickRightSignatureForSomeNullArgumentTypes()
{
var result = FunctionCallBinder.MatchSignatureToBuiltInFunction(
var result = FunctionCallBinder.MatchSignatureToUriFunction(
"substring",
new SingleValueNode[] {
new SingleValuePropertyAccessNode(new ConstantNode(null)/*parent*/, new EdmStructuralProperty(new EdmEntityType("MyNamespace", "MyEntityType"), "myPropertyName", EdmCoreModel.Instance.GetString(true))),
Expand Down

0 comments on commit afd7a99

Please sign in to comment.