public void TextWriterStyles_CanEvaluateStyleWithRequiredArgs() { MockTextWriterStyleFactory mockFactory = new MockTextWriterStyleFactory(); TextWriterStyleBase command = mockFactory.Create("helloWorldArgs:firstName=Jordan,lastName=Bleu"); string result = command.Evaluate(null, null, null, -1, null); Assert.AreEqual(result, "Hello Jordan Bleu!"); }
public void TextWriterStyles_CanEvaluateStyleWithWackyExtraArgs() { MockTextWriterStyleFactory mockFactory = new MockTextWriterStyleFactory(); TextWriterStyleBase command = mockFactory.Create("helloworld:asdf=123"); string result = command.Evaluate(null, null, null, -1, null); Assert.AreEqual(result, "HELLO WORLD!"); }
public void ColorStyle_FailsSuccessfully() { MockTextWriterStyleFactory mockFactory = new MockTextWriterStyleFactory(); string hex = "#poop"; Assert.Throws <StyleValidationException>(() => { TextWriterStyleBase command = mockFactory.Create($"color:hex={hex}"); }); }
public void EndColorStyle_Works() { MockTextWriterStyleFactory mockFactory = new MockTextWriterStyleFactory(); TextWriterStyleBase command = mockFactory.Create("endcolor"); StringBuilder sb = new StringBuilder(); string result = command.Evaluate(null, null, sb, -1, null); Assert.AreEqual(sb.ToString(), "</color>"); }
public void ColorStyle_Works() { MockTextWriterStyleFactory mockFactory = new MockTextWriterStyleFactory(); string hex = "#AABBCC"; TextWriterStyleBase command = mockFactory.Create($"color:hex={hex}"); // This style should append to the builder not the text StringBuilder builder = new StringBuilder(); string result = command.Evaluate(null, null, builder, -1, null); Assert.AreEqual(builder.ToString(), $"<{hex}>"); }
public void TextWriterStyles_CanValidateRequiredArgs() { MockTextWriterStyleFactory mockFactory = new MockTextWriterStyleFactory(); try { TextWriterStyleBase command = mockFactory.Create("helloWorldArgs:firstName=Jordan"); } catch (InvalidOperationException) { Assert.Pass(); } // We shouldn't get this far because we're totes missing args Assert.Fail("Didn't catch any exceptions but we should have."); }
/// <summary> /// Creates an instance of the style command based on the name. Then parses out the passed /// in command arguments and prepares the instance for execution /// </summary> /// <returns>Fully Hydrated Style object</returns> public TextWriterStyleBase Create(string commandString) { if (string.IsNullOrWhiteSpace(commandString)) { throw new StyleParseException("Unable to parse empty command string. Please ensure there are no instnances of {} in the file."); } string[] commandStringParts = commandString.Split(':'); if (commandStringParts.Count() > 2) { throw new StyleParseException($"Too many instances of ':' found in command string: '{commandString}'"); } string command = commandStringParts[0].ToLower(); string args = string.Empty; if (commandStringParts.Count() > 1) { args = commandStringParts[1]; } if (!CommandRepository.ContainsKey(command)) { throw new StyleParseException($"Unrecognized command, '{command}'"); } Type commandType = CommandRepository[command]; if (!commandType.IsSubclassOf(typeof(TextWriterStyleBase))) { throw new StyleParseException($"Command type for '{command}' is typeof '{commandType.GetType()}', " + $"which does not inherit from {nameof(TextWriterStyleBase)}"); } TextWriterStyleBase style = (TextWriterStyleBase)Activator.CreateInstance(commandType); style.Initialize(args); return(style); }
public override void DelayedUpdate() { if (isAnimationReady) { if (!string.IsNullOrEmpty(text) && !IsDoneTyping) { IsDoneTyping = (charIndex >= text.Length); if (!IsDoneTyping) { char nextChar = text[charIndex]; if (nextChar.Equals('{')) { // Begin reading the remaining text as a command until we reach the } StringBuilder command = new StringBuilder(); bool foundEndTag = false; while (charIndex < text.Length) { charIndex++; char commandChar = text[charIndex]; if (commandChar.Equals('}')) { foundEndTag = true; break; } else if (commandChar.Equals('{')) { throw new UnityException("Unable to parse command string. Expected ending } but found another opening {"); } else { command.Append(text[charIndex]); } } if (!foundEndTag) { throw new UnityException("Unable to parse command string. Expected ending }"); } else { // Execute the command TextWriterStyleBase commandObject = styleFactory.Create(command.ToString()); string result = commandObject.Evaluate(this, textMeshComponent, chars, charIndex + 1, text); if (!string.IsNullOrEmpty(result)) { SpliceResult(result, charIndex); } } } else // Just a normal character { chars.Append(nextChar); } } if (beep != null) { audioSource.PlayOneShot(beep); } charIndex++; } } }