Skip to content

Instantly share code, notes, and snippets.

@automatonic
Created September 9, 2015 21:18
Show Gist options
  • Save automatonic/5fd72f9582d5edc172a6 to your computer and use it in GitHub Desktop.
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#
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]
<Query Kind="Statements">
<Reference>&lt;RuntimeDirectory&gt;\System.ServiceModel.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.Xaml.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.ServiceModel.Internals.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.Runtime.Serialization.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.Configuration.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\SMDiagnostics.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.IdentityModel.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.DirectoryServices.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\Microsoft.Transactions.Bridge.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.Web.Services.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.EnterpriseServices.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.IdentityModel.Selectors.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.Web.ApplicationServices.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.Messaging.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.Runtime.DurableInstancing.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.ServiceProcess.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.Net.Http.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.ServiceModel.Activation.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\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