public void TokenStringBuilder_WithTokens() { // arrange const string configKey = "configKey"; const string configValue = "A"; const string tokenKey = "season"; const string raw = " assets/{{configKey}}_{{season}}_{{ invalid }}.png "; var context = new GenericTokenContext(modId => false); context.Save(new ImmutableToken(configKey, new InvariantHashSet { configValue })); context.Save(new ImmutableToken(tokenKey, new InvariantHashSet { "A" })); // act TokenString tokenStr = new TokenString(raw, context); IContextualState diagnosticState = tokenStr.GetDiagnosticState(); // assert tokenStr.Raw.Should().Be("assets/{{configKey}}_{{season}}_{{invalid}}.png"); tokenStr.GetTokenPlaceholders(recursive: false).Should().HaveCount(3); tokenStr.GetTokenPlaceholders(recursive: false).Select(name => name.Text).Should().BeEquivalentTo(new[] { "{{configKey}}", "{{season}}", "{{invalid}}" }); tokenStr.IsReady.Should().BeFalse(); tokenStr.HasAnyTokens.Should().BeTrue(); tokenStr.IsSingleTokenOnly.Should().BeFalse(); diagnosticState.IsReady.Should().BeFalse(); diagnosticState.UnreadyTokens.Should().BeEmpty(); diagnosticState.InvalidTokens.Should().HaveCount(1).And.BeEquivalentTo("invalid"); diagnosticState.Errors.Should().BeEmpty(); }
public void TokenStringBuilder_WithSingleToken(string raw, string parsed) { // arrange const string configKey = "tokenKey"; var context = new GenericTokenContext(modId => false); context.Save(new ImmutableToken(configKey, new InvariantHashSet { "value" })); // act TokenString tokenStr = new TokenString(raw, context); IContextualState diagnosticState = tokenStr.GetDiagnosticState(); // assert tokenStr.Raw.Should().Be(parsed); tokenStr.GetTokenPlaceholders(recursive: false).Should().HaveCount(1); tokenStr.GetTokenPlaceholders(recursive: false).Select(p => p.Text).Should().BeEquivalentTo("{{" + configKey + "}}"); tokenStr.HasAnyTokens.Should().BeTrue(); tokenStr.IsSingleTokenOnly.Should().BeTrue(); diagnosticState.IsReady.Should().BeTrue(); diagnosticState.UnreadyTokens.Should().BeEmpty(); diagnosticState.InvalidTokens.Should().BeEmpty(); diagnosticState.Errors.Should().BeEmpty(); }
/// <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="tokenContext">The tokens available for this content pack.</param> /// <param name="migrator">The migrator which validates and migrates content pack data.</param> /// <param name="error">An error phrase indicating why parsing failed (if applicable).</param> /// <param name="parsed">The parsed value.</param> private bool TryParseStringTokens(string rawValue, IContext tokenContext, IMigration migrator, out string error, out ITokenString parsed) { // parse parsed = new TokenString(rawValue, tokenContext); if (!migrator.TryMigrate(parsed, out error)) { return(false); } // validate unknown tokens IContextualState state = parsed.GetDiagnosticState(); if (state.InvalidTokens.Any()) { error = $"found unknown tokens ({string.Join(", ", state.InvalidTokens.OrderBy(p => p))})"; parsed = null; return(false); } // validate tokens foreach (LexTokenToken lexToken in parsed.GetTokenPlaceholders(recursive: false)) { IToken token = tokenContext.GetToken(lexToken.Name, enforceContext: false); if (token == null) { error = $"'{lexToken}' can't be used as a token because that token could not be found."; // should never happen parsed = null; return(false); } } // looks OK error = null; return(true); }
/// <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="assumeModIds">Mod IDs to assume are installed for purposes of token validation.</param> /// <param name="path">The path to the value from the root content file.</param> /// <param name="error">An error phrase indicating why parsing failed (if applicable).</param> /// <param name="parsed">The parsed value.</param> public bool TryParseString(string rawValue, InvariantHashSet assumeModIds, LogPathBuilder path, out string error, out IManagedTokenString parsed) { // parse lexical bits var bits = new Lexer().ParseBits(rawValue, impliedBraces: false).ToArray(); foreach (ILexToken bit in bits) { if (!this.Migrator.TryMigrate(bit, out error)) { parsed = null; return(false); } } // get token string parsed = new TokenString(bits, this.Context, path); if (!this.Migrator.TryMigrate(parsed, out error)) { return(false); } // validate tokens foreach (LexTokenToken lexToken in parsed.GetTokenPlaceholders(recursive: false)) { if (!this.TryValidateToken(lexToken, assumeModIds, out error)) { return(false); } } // looks OK error = null; return(true); }
public void TokenStringBuilder_PlainString(string raw) { // act TokenString tokenStr = new TokenString(raw, new GenericTokenContext(modId => false)); IContextualState diagnosticState = tokenStr.GetDiagnosticState(); // assert tokenStr.Raw.Should().Be(raw.Trim()); tokenStr.GetTokenPlaceholders(recursive: false).Should().HaveCount(0); tokenStr.HasAnyTokens.Should().BeFalse(); diagnosticState.IsReady.Should().BeTrue(); diagnosticState.UnreadyTokens.Should().BeEmpty(); diagnosticState.InvalidTokens.Should().BeEmpty(); diagnosticState.Errors.Should().BeEmpty(); }
/// <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="assumeModIds">Mod IDs to assume are installed for purposes of token validation.</param> /// <param name="error">An error phrase indicating why parsing failed (if applicable).</param> /// <param name="parsed">The parsed value.</param> public bool TryParseStringTokens(string rawValue, InvariantHashSet assumeModIds, out string error, out IParsedTokenString parsed) { // parse parsed = new TokenString(rawValue, this.Context); if (!this.Migrator.TryMigrate(parsed, out error)) { return(false); } // validate tokens foreach (LexTokenToken lexToken in parsed.GetTokenPlaceholders(recursive: false)) { if (!this.TryValidateToken(lexToken, assumeModIds, out error)) { return(false); } } // looks OK error = null; return(true); }