/// <summary> /// Constructs a new Configuration Provider /// </summary> public ValueCollectionProvider() { Sources = new Dictionary<string, IValueProvider>(StringComparer.OrdinalIgnoreCase); _reusableTokenReplace = new TokenReplace(Sources); }
public void TokenReplace_ComplexReplace() { var original = @"Select * From Users Where UserId = [QueryString:UserName||[AppSettings:DefaultUserName||0]] or UserId = [AppSettings:RootUserId] or UserId = [Parameters:TestKey:Subkey||27] and a token with sub-token for fallback [QueryString:Id||[Module:SubId]] some tests [] shouldn't capture and [ ] shouldn't capture either, + [MyName] shouldn't either nor should [ something and a [bad token without property] and a [Source::SubkeyWithoutKey] but this should [token:key] again Now try a token which returns a token: [AppSettings:UserNameMaybeFromUrl||Johny]"; // Even without recurrence it should process the fallback-token at least once var expectedNoRecurrance = @"Select * From Users Where UserId = Daniel or UserId = -1 or UserId = 27 and a token with sub-token for fallback 4567 some tests [] shouldn't capture and [ ] shouldn't capture either, + [MyName] shouldn't either nor should [ something and a [bad token without property] and a [Source::SubkeyWithoutKey] but this should What a Token! again Now try a token which returns a token: [QueryString:UserName||Samantha]"; var expectedRecurrance = @"Select * From Users Where UserId = Daniel or UserId = -1 or UserId = 27 and a token with sub-token for fallback 4567 some tests [] shouldn't capture and [ ] shouldn't capture either, + [MyName] shouldn't either nor should [ something and a [bad token without property] and a [Source::SubkeyWithoutKey] but this should What a Token! again Now try a token which returns a token: Daniel"; var qs = new StaticValueProvider("QueryString"); qs.Properties.Add("UserName", "Daniel"); //qs.Properties.Add("Id", "7"); var mod = new StaticValueProvider("Module"); mod.Properties.Add("SubId", "4567"); var appS = new StaticValueProvider("AppSettings"); appS.Properties.Add("DefaultUserName", "Name Unknown"); appS.Properties.Add("RootUserId", "-1"); appS.Properties.Add("UserNameMaybeFromUrl", "[QueryString:UserName||Samantha]"); var tok = new StaticValueProvider("token"); tok.Properties.Add("key", "What a Token!"); var sources = new Dictionary<string, IValueProvider>(); sources.Add(qs.Name.ToLower(),qs); sources.Add(mod.Name.ToLower(), mod); sources.Add(appS.Name.ToLower(), appS); sources.Add(tok.Name.ToLower(), tok); TokenReplace tr = new TokenReplace(sources); var resultNoRecurrance = tr.ReplaceTokens(original); Assert.AreEqual(expectedNoRecurrance, resultNoRecurrance, "No Recurrance"); var resultRecurrance = tr.ReplaceTokens(original, 2); Assert.AreEqual(expectedRecurrance, resultRecurrance, "With 2x looping"); }