public void TokenStringBuilder_WithTokens() { // arrange const string configKey = "configKey"; const string configValue = "A"; const string raw = " assets/{{configKey}}_{{season}}_{{invalid}}.png "; // act TokenStringBuilder builder = new TokenStringBuilder(raw, new InvariantDictionary <ConfigField> { [configKey] = new ConfigField(allowValues: new InvariantHashSet(configValue), allowBlank: false, allowMultiple: false, defaultValues: new InvariantHashSet { configValue }) { Value = new InvariantHashSet(configValue) } }); TokenString tokenStr = builder.Build(); // assert builder builder.RawValue.Should().Be(raw); builder.ConditionTokens.Should().HaveCount(1).And.BeEquivalentTo(ConditionKey.Season); builder.ConfigTokens.Should().HaveCount(1).And.BeEquivalentTo(configKey); builder.InvalidTokens.Should().HaveCount(1).And.BeEquivalentTo("invalid"); builder.HasAnyTokens.Should().BeTrue(); // assert token string tokenStr.Raw.Should().Be(raw.Replace("{{" + configKey + "}}", configValue), $"the config token should be substituted when the {nameof(TokenString)} is built"); tokenStr.ConditionTokens.Should().HaveCount(1).And.BeEquivalentTo(ConditionKey.Season); }
/// <summary>Parse a string which can contain tokens, and validate that it's valid.</summary> /// <param name="rawValue">The raw string which may contain tokens.</param> /// <param name="config">The player configuration.</param> /// <param name="error">An error phrase indicating why parsing failed (if applicable).</param> /// <param name="parsed">The parsed value.</param> private bool TryParseTokenString(string rawValue, InvariantDictionary <ConfigField> config, out string error, out TokenStringBuilder parsed) { // parse TokenStringBuilder builder = new TokenStringBuilder(rawValue, config); // validate unknown tokens if (builder.InvalidTokens.Any()) { parsed = null; error = $"found unknown tokens: {string.Join(", ", builder.InvalidTokens.OrderBy(p => p))}"; return(false); } // validate config tokens foreach (string key in builder.ConfigTokens) { ConfigField field = config[key]; if (field.AllowMultiple) { parsed = null; error = $"token {{{{{key}}}}} can't be used because that config field allows multiple values."; return(false); } } // looks OK parsed = builder; error = null; return(true); }
public void TokenStringBuilder_PlainString(string raw) { // act TokenStringBuilder builder = new TokenStringBuilder(raw, new InvariantDictionary <ConfigField>()); TokenString tokenStr = builder.Build(); // assert builder builder.RawValue.Should().Be(raw); builder.ConditionTokens.Should().HaveCount(0); builder.ConfigTokens.Should().HaveCount(0); builder.InvalidTokens.Should().HaveCount(0); builder.HasAnyTokens.Should().BeFalse(); // assert token string tokenStr.Raw.Should().Be(raw); tokenStr.ConditionTokens.Should().HaveCount(0); }