Created
September 9, 2015 21:18
-
-
Save automatonic/5fd72f9582d5edc172a6 to your computer and use it in GitHub Desktop.
Some tests to investigate the precedence/priority of UriTemplate wildcards in a UriTemplateTable for C#
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
var baseUri = new Uri("foo://testy"); | |
//Test1 - "*" | |
var templates1 = new Dictionary<UriTemplate, object>() | |
{ | |
{ new UriTemplate("*") , "[star]" }, | |
}; | |
var templateTable1 = new UriTemplateTable(baseUri, templates1); | |
templateTable1 | |
.Match(new Uri("foo://testy")) | |
.Select(match => match.Data) | |
.Dump("T1.1 - foo://testy"); //Result: [star] | |
templateTable1 | |
.Match(new Uri("foo://testy/")) | |
.Select(match => match.Data) | |
.Dump("T1.2 - foo://testy/"); //Result: [star] | |
templateTable1 | |
.Match(new Uri("foo://testy/a")) | |
.Select(match => match.Data) | |
.Dump("T1.3 - foo://testy/a"); //Result: [star] | |
//Test2 - Slash | |
var templates2 = new Dictionary<UriTemplate, object>() | |
{ | |
{ new UriTemplate("/") , "[slash]" }, | |
{ new UriTemplate("a") , "[literal]" }, | |
}; | |
var templateTable2 = new UriTemplateTable(baseUri, templates2); | |
templateTable2 | |
.Match(new Uri("foo://testy")) | |
.Select(match => match.Data) | |
.Dump("T2.1 - foo://testy"); //Result: [slash] | |
templateTable2 | |
.Match(new Uri("foo://testy/")) | |
.Select(match => match.Data) | |
.Dump("T2.2 - foo://testy/"); //Result: [slash] | |
templateTable2 | |
.Match(new Uri("foo://testy/a")) | |
.Select(match => match.Data) | |
.Dump("T2.3 - foo://testy/a"); //Result: [literal] | |
//Test3 - Precedence between star and slash | |
var templates3 = new Dictionary<UriTemplate, object>() | |
{ | |
{ new UriTemplate("*") , "[star]" }, | |
{ new UriTemplate("/") , "[slash]" }, | |
{ new UriTemplate("a") , "[literal]" }, | |
}; | |
var templateTable3 = new UriTemplateTable(baseUri, templates3); | |
templateTable3 | |
.Match(new Uri("foo://testy/")) | |
.Select(match => match.Data) | |
.Dump("T3.1 - foo://testy/"); //Result: [slash] | |
templateTable3 | |
.Match(new Uri("foo://testy")) | |
.Select(match => match.Data) | |
.Dump("T3.2 - foo://testy"); //Result: [slash] | |
templateTable3 | |
.Match(new Uri("foo://testy/a")) | |
.Select(match => match.Data) | |
.Dump("T3.3 - foo://testy/a"); //Result: [literal] | |
//Test 4 - Precedence between /* and literal | |
var templates4 = new Dictionary<UriTemplate, object>() | |
{ | |
{ new UriTemplate("/*") , "[slash-star]" }, | |
{ new UriTemplate("a") , "[literal]" }, | |
}; | |
var templateTable4 = new UriTemplateTable(baseUri, templates4); | |
templateTable4 | |
.Match(new Uri("foo://testy/")) | |
.Select(match => match.Data) | |
.Dump("T4.1 - foo://testy/"); //Result: [slash-star] | |
templateTable4 | |
.Match(new Uri("foo://testy")) | |
.Select(match => match.Data) | |
.Dump("T4.2 - foo://testy"); //Result: [slash-star] | |
templateTable4 | |
.Match(new Uri("foo://testy/a")) | |
.Select(match => match.Data) | |
.Dump("T4.3 - foo://testy/a"); //Result: [literal] | |
templateTable4 | |
.Match(new Uri("foo://testy/apple")) | |
.Select(match => match.Data) | |
.Dump("T4.4 - foo://testy/apple"); //Result: [literal] |
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
<Query Kind="Statements"> | |
<Reference><RuntimeDirectory>\System.ServiceModel.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Xaml.dll</Reference> | |
<Reference><RuntimeDirectory>\System.ServiceModel.Internals.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Runtime.Serialization.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Configuration.dll</Reference> | |
<Reference><RuntimeDirectory>\SMDiagnostics.dll</Reference> | |
<Reference><RuntimeDirectory>\System.IdentityModel.dll</Reference> | |
<Reference><RuntimeDirectory>\System.DirectoryServices.dll</Reference> | |
<Reference><RuntimeDirectory>\Microsoft.Transactions.Bridge.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Web.Services.dll</Reference> | |
<Reference><RuntimeDirectory>\System.EnterpriseServices.dll</Reference> | |
<Reference><RuntimeDirectory>\System.IdentityModel.Selectors.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Web.ApplicationServices.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Messaging.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Runtime.DurableInstancing.dll</Reference> | |
<Reference><RuntimeDirectory>\System.ServiceProcess.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Net.Http.dll</Reference> | |
<Reference><RuntimeDirectory>\System.ServiceModel.Activation.dll</Reference> | |
<Reference><RuntimeDirectory>\System.Security.dll</Reference> | |
<Namespace>System</Namespace> | |
</Query> | |
var baseUri = new Uri("foo://testy"); | |
//Test1 - "*" | |
var templates1 = new Dictionary<UriTemplate, object>() | |
{ | |
{ new UriTemplate("*") , "[star]" }, | |
}; | |
var templateTable1 = new UriTemplateTable(baseUri, templates1); | |
templateTable1 | |
.Match(new Uri("foo://testy")) | |
.Select(match => match.Data) | |
.Dump("T1.1 - foo://testy"); //Result: [star] | |
templateTable1 | |
.Match(new Uri("foo://testy/")) | |
.Select(match => match.Data) | |
.Dump("T1.2 - foo://testy/"); //Result: [star] | |
templateTable1 | |
.Match(new Uri("foo://testy/a")) | |
.Select(match => match.Data) | |
.Dump("T1.3 - foo://testy/a"); //Result: [star] | |
//Test2 - Slash | |
var templates2 = new Dictionary<UriTemplate, object>() | |
{ | |
{ new UriTemplate("/") , "[slash]" }, | |
{ new UriTemplate("a") , "[literal]" }, | |
}; | |
var templateTable2 = new UriTemplateTable(baseUri, templates2); | |
templateTable2 | |
.Match(new Uri("foo://testy")) | |
.Select(match => match.Data) | |
.Dump("T2.1 - foo://testy"); //Result: [slash] | |
templateTable2 | |
.Match(new Uri("foo://testy/")) | |
.Select(match => match.Data) | |
.Dump("T2.2 - foo://testy/"); //Result: [slash] | |
templateTable2 | |
.Match(new Uri("foo://testy/a")) | |
.Select(match => match.Data) | |
.Dump("T2.3 - foo://testy/a"); //Result: [literal] | |
//Test3 - Precedence between star and slash | |
var templates3 = new Dictionary<UriTemplate, object>() | |
{ | |
{ new UriTemplate("*") , "[star]" }, | |
{ new UriTemplate("/") , "[slash]" }, | |
{ new UriTemplate("a") , "[literal]" }, | |
}; | |
var templateTable3 = new UriTemplateTable(baseUri, templates3); | |
templateTable3 | |
.Match(new Uri("foo://testy/")) | |
.Select(match => match.Data) | |
.Dump("T3.1 - foo://testy/"); //Result: [slash] | |
templateTable3 | |
.Match(new Uri("foo://testy")) | |
.Select(match => match.Data) | |
.Dump("T3.2 - foo://testy"); //Result: [slash] | |
templateTable3 | |
.Match(new Uri("foo://testy/a")) | |
.Select(match => match.Data) | |
.Dump("T3.3 - foo://testy/a"); //Result: [literal] | |
//Test 4 - Precedence between /* and literal | |
var templates4 = new Dictionary<UriTemplate, object>() | |
{ | |
{ new UriTemplate("/*") , "[slash-star]" }, | |
{ new UriTemplate("a") , "[literal]" }, | |
}; | |
var templateTable4 = new UriTemplateTable(baseUri, templates4); | |
templateTable4 | |
.Match(new Uri("foo://testy/")) | |
.Select(match => match.Data) | |
.Dump("T4.1 - foo://testy/"); //Result: [slash-star] | |
templateTable4 | |
.Match(new Uri("foo://testy")) | |
.Select(match => match.Data) | |
.Dump("T4.2 - foo://testy"); //Result: [slash-star] | |
templateTable4 | |
.Match(new Uri("foo://testy/a")) | |
.Select(match => match.Data) | |
.Dump("T4.3 - foo://testy/a"); //Result: [literal] | |
templateTable4 | |
.Match(new Uri("foo://testy/apple")) | |
.Select(match => match.Data) | |
.Dump("T4.4 - foo://testy/apple"); //Result: [literal] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment