Browse Source

Refactored TextTagParser to use ITextTagParser interface

master
Christopher 9 years ago
parent
commit
fe105e387e
  1. 11
      Assets/Fungus/UI/Scripts/ITextTagParser.cs
  2. 12
      Assets/Fungus/UI/Scripts/ITextTagParser.cs.meta
  3. 238
      Assets/Fungus/UI/Scripts/TextTagParser.cs
  4. 42
      Assets/Fungus/UI/Scripts/TextTagToken.cs
  5. 12
      Assets/Fungus/UI/Scripts/TextTagToken.cs.meta
  6. 70
      Assets/Fungus/UI/Scripts/Writer.cs
  7. 90
      Assets/Tests/UI/Editor/TextTagParserTests.cs

11
Assets/Fungus/UI/Scripts/ITextTagParser.cs

@ -0,0 +1,11 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace Fungus
{
public interface ITextTagParser
{
List<TextTagToken> Tokenize(string storyText);
}
}

12
Assets/Fungus/UI/Scripts/ITextTagParser.cs.meta

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 1e3f4aab68276483e9d40b120cc1cafc
timeCreated: 1473756939
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

238
Assets/Fungus/UI/Scripts/TextTagParser.cs

@ -10,46 +10,8 @@ namespace Fungus
/// <summary> /// <summary>
/// Parses a string for special Fungus text tags. /// Parses a string for special Fungus text tags.
/// </summary> /// </summary>
public class TextTagParser public class TextTagParser : ITextTagParser
{ {
public enum TokenType
{
Invalid,
Words, // A string of words
BoldStart, // b
BoldEnd, // /b
ItalicStart, // i
ItalicEnd, // /i
ColorStart, // color=red
ColorEnd, // /color
SizeStart, // size=20
SizeEnd, // /size
Wait, // w, w=0.5
WaitForInputNoClear, // wi
WaitForInputAndClear, // wc
WaitOnPunctuationStart, // wp, wp=0.5
WaitOnPunctuationEnd, // /wp
Clear, // c
SpeedStart, // s, s=60
SpeedEnd, // /s
Exit, // x
Message, // m=MessageName
VerticalPunch, // {vpunch=0.5}
HorizontalPunch, // {hpunch=0.5}
Punch, // {punch=0.5}
Flash, // {flash=0.5}
Audio, // {audio=Sound}
AudioLoop, // {audioloop=Sound}
AudioPause, // {audiopause=Sound}
AudioStop // {audiostop=Sound}
}
public class Token
{
public TokenType type = TokenType.Invalid;
public List<string> paramList;
}
public static string GetTagHelp() public static string GetTagHelp()
{ {
return "" + return "" +
@ -80,77 +42,16 @@ namespace Fungus
"\t{$VarName} Substitute variable"; "\t{$VarName} Substitute variable";
} }
public virtual List<Token> Tokenize(string storyText) protected virtual void AddWordsToken(List<TextTagToken> tokenList, string words)
{
List<Token> tokens = new List<Token>();
string pattern = @"\{.*?\}";
Regex myRegex = new Regex(pattern);
Match m = myRegex.Match(storyText); // m is the first match
int position = 0;
while (m.Success)
{
// Get bit leading up to tag
string preText = storyText.Substring(position, m.Index - position);
string tagText = m.Value;
if (preText != "")
{
AddWordsToken(tokens, preText);
}
AddTagToken(tokens, tagText);
position = m.Index + tagText.Length;
m = m.NextMatch();
}
if (position < storyText.Length)
{
string postText = storyText.Substring(position, storyText.Length - position);
if (postText.Length > 0)
{
AddWordsToken(tokens, postText);
}
}
// Remove all leading whitespace & newlines after a {c} or {wc} tag
// These characters are usually added for legibility when editing, but are not
// desireable when viewing the text in game.
bool trimLeading = false;
foreach (Token token in tokens)
{
if (trimLeading &&
token.type == TokenType.Words)
{
token.paramList[0] = token.paramList[0].TrimStart(' ', '\t', '\r', '\n');
}
if (token.type == TokenType.Clear ||
token.type == TokenType.WaitForInputAndClear)
{
trimLeading = true;
}
else
{
trimLeading = false;
}
}
return tokens;
}
protected virtual void AddWordsToken(List<Token> tokenList, string words)
{ {
Token token = new Token(); TextTagToken token = new TextTagToken();
token.type = TokenType.Words; token.type = TextTagToken.TokenType.Words;
token.paramList = new List<string>(); token.paramList = new List<string>();
token.paramList.Add(words); token.paramList.Add(words);
tokenList.Add(token); tokenList.Add(token);
} }
protected virtual void AddTagToken(List<Token> tokenList, string tagText) protected virtual void AddTagToken(List<TextTagToken> tokenList, string tagText)
{ {
if (tagText.Length < 3 || if (tagText.Length < 3 ||
tagText.Substring(0,1) != "{" || tagText.Substring(0,1) != "{" ||
@ -161,133 +62,133 @@ namespace Fungus
string tag = tagText.Substring(1, tagText.Length - 2); string tag = tagText.Substring(1, tagText.Length - 2);
TokenType type = TokenType.Invalid; var type = TextTagToken.TokenType.Invalid;
List<string> parameters = ExtractParameters(tag); List<string> parameters = ExtractParameters(tag);
if (tag == "b") if (tag == "b")
{ {
type = TokenType.BoldStart; type = TextTagToken.TokenType.BoldStart;
} }
else if (tag == "/b") else if (tag == "/b")
{ {
type = TokenType.BoldEnd; type = TextTagToken.TokenType.BoldEnd;
} }
else if (tag == "i") else if (tag == "i")
{ {
type = TokenType.ItalicStart; type = TextTagToken.TokenType.ItalicStart;
} }
else if (tag == "/i") else if (tag == "/i")
{ {
type = TokenType.ItalicEnd; type = TextTagToken.TokenType.ItalicEnd;
} }
else if (tag.StartsWith("color=")) else if (tag.StartsWith("color="))
{ {
type = TokenType.ColorStart; type = TextTagToken.TokenType.ColorStart;
} }
else if (tag == "/color") else if (tag == "/color")
{ {
type = TokenType.ColorEnd; type = TextTagToken.TokenType.ColorEnd;
} }
else if (tag.StartsWith("size=")) else if (tag.StartsWith("size="))
{ {
type = TokenType.SizeStart; type = TextTagToken.TokenType.SizeStart;
} }
else if (tag == "/size") else if (tag == "/size")
{ {
type = TokenType.SizeEnd; type = TextTagToken.TokenType.SizeEnd;
} }
else if (tag == "wi") else if (tag == "wi")
{ {
type = TokenType.WaitForInputNoClear; type = TextTagToken.TokenType.WaitForInputNoClear;
} }
if (tag == "wc") if (tag == "wc")
{ {
type = TokenType.WaitForInputAndClear; type = TextTagToken.TokenType.WaitForInputAndClear;
} }
else if (tag.StartsWith("wp=")) else if (tag.StartsWith("wp="))
{ {
type = TokenType.WaitOnPunctuationStart; type = TextTagToken.TokenType.WaitOnPunctuationStart;
} }
else if (tag == "wp") else if (tag == "wp")
{ {
type = TokenType.WaitOnPunctuationStart; type = TextTagToken.TokenType.WaitOnPunctuationStart;
} }
else if (tag == "/wp") else if (tag == "/wp")
{ {
type = TokenType.WaitOnPunctuationEnd; type = TextTagToken.TokenType.WaitOnPunctuationEnd;
} }
else if (tag.StartsWith("w=")) else if (tag.StartsWith("w="))
{ {
type = TokenType.Wait; type = TextTagToken.TokenType.Wait;
} }
else if (tag == "w") else if (tag == "w")
{ {
type = TokenType.Wait; type = TextTagToken.TokenType.Wait;
} }
else if (tag == "c") else if (tag == "c")
{ {
type = TokenType.Clear; type = TextTagToken.TokenType.Clear;
} }
else if (tag.StartsWith("s=")) else if (tag.StartsWith("s="))
{ {
type = TokenType.SpeedStart; type = TextTagToken.TokenType.SpeedStart;
} }
else if (tag == "s") else if (tag == "s")
{ {
type = TokenType.SpeedStart; type = TextTagToken.TokenType.SpeedStart;
} }
else if (tag == "/s") else if (tag == "/s")
{ {
type = TokenType.SpeedEnd; type = TextTagToken.TokenType.SpeedEnd;
} }
else if (tag == "x") else if (tag == "x")
{ {
type = TokenType.Exit; type = TextTagToken.TokenType.Exit;
} }
else if (tag.StartsWith("m=")) else if (tag.StartsWith("m="))
{ {
type = TokenType.Message; type = TextTagToken.TokenType.Message;
} }
else if (tag.StartsWith("vpunch") || else if (tag.StartsWith("vpunch") ||
tag.StartsWith("vpunch=")) tag.StartsWith("vpunch="))
{ {
type = TokenType.VerticalPunch; type = TextTagToken.TokenType.VerticalPunch;
} }
else if (tag.StartsWith("hpunch") || else if (tag.StartsWith("hpunch") ||
tag.StartsWith("hpunch=")) tag.StartsWith("hpunch="))
{ {
type = TokenType.HorizontalPunch; type = TextTagToken.TokenType.HorizontalPunch;
} }
else if (tag.StartsWith("punch") || else if (tag.StartsWith("punch") ||
tag.StartsWith("punch=")) tag.StartsWith("punch="))
{ {
type = TokenType.Punch; type = TextTagToken.TokenType.Punch;
} }
else if (tag.StartsWith("flash") || else if (tag.StartsWith("flash") ||
tag.StartsWith("flash=")) tag.StartsWith("flash="))
{ {
type = TokenType.Flash; type = TextTagToken.TokenType.Flash;
} }
else if (tag.StartsWith("audio=")) else if (tag.StartsWith("audio="))
{ {
type = TokenType.Audio; type = TextTagToken.TokenType.Audio;
} }
else if (tag.StartsWith("audioloop=")) else if (tag.StartsWith("audioloop="))
{ {
type = TokenType.AudioLoop; type = TextTagToken.TokenType.AudioLoop;
} }
else if (tag.StartsWith("audiopause=")) else if (tag.StartsWith("audiopause="))
{ {
type = TokenType.AudioPause; type = TextTagToken.TokenType.AudioPause;
} }
else if (tag.StartsWith("audiostop=")) else if (tag.StartsWith("audiostop="))
{ {
type = TokenType.AudioStop; type = TextTagToken.TokenType.AudioStop;
} }
if (type != TokenType.Invalid) if (type != TextTagToken.TokenType.Invalid)
{ {
Token token = new Token(); TextTagToken token = new TextTagToken();
token.type = type; token.type = type;
token.paramList = parameters; token.paramList = parameters;
tokenList.Add(token); tokenList.Add(token);
@ -315,5 +216,70 @@ namespace Fungus
} }
return paramsList; return paramsList;
} }
#region ITextTagParser implementation
public virtual List<TextTagToken> Tokenize(string storyText)
{
List<TextTagToken> tokens = new List<TextTagToken>();
string pattern = @"\{.*?\}";
Regex myRegex = new Regex(pattern);
Match m = myRegex.Match(storyText); // m is the first match
int position = 0;
while (m.Success)
{
// Get bit leading up to tag
string preText = storyText.Substring(position, m.Index - position);
string tagText = m.Value;
if (preText != "")
{
AddWordsToken(tokens, preText);
}
AddTagToken(tokens, tagText);
position = m.Index + tagText.Length;
m = m.NextMatch();
}
if (position < storyText.Length)
{
string postText = storyText.Substring(position, storyText.Length - position);
if (postText.Length > 0)
{
AddWordsToken(tokens, postText);
}
}
// Remove all leading whitespace & newlines after a {c} or {wc} tag
// These characters are usually added for legibility when editing, but are not
// desireable when viewing the text in game.
bool trimLeading = false;
foreach (TextTagToken token in tokens)
{
if (trimLeading &&
token.type == TextTagToken.TokenType.Words)
{
token.paramList[0] = token.paramList[0].TrimStart(' ', '\t', '\r', '\n');
}
if (token.type == TextTagToken.TokenType.Clear ||
token.type == TextTagToken.TokenType.WaitForInputAndClear)
{
trimLeading = true;
}
else
{
trimLeading = false;
}
}
return tokens;
}
#endregion
} }
} }

42
Assets/Fungus/UI/Scripts/TextTagToken.cs

@ -0,0 +1,42 @@
using System.Collections.Generic;
namespace Fungus
{
public class TextTagToken
{
public enum TokenType
{
Invalid,
Words, // A string of words
BoldStart, // b
BoldEnd, // /b
ItalicStart, // i
ItalicEnd, // /i
ColorStart, // color=red
ColorEnd, // /color
SizeStart, // size=20
SizeEnd, // /size
Wait, // w, w=0.5
WaitForInputNoClear, // wi
WaitForInputAndClear, // wc
WaitOnPunctuationStart, // wp, wp=0.5
WaitOnPunctuationEnd, // /wp
Clear, // c
SpeedStart, // s, s=60
SpeedEnd, // /s
Exit, // x
Message, // m=MessageName
VerticalPunch, // {vpunch=0.5}
HorizontalPunch, // {hpunch=0.5}
Punch, // {punch=0.5}
Flash, // {flash=0.5}
Audio, // {audio=Sound}
AudioLoop, // {audioloop=Sound}
AudioPause, // {audiopause=Sound}
AudioStop // {audiostop=Sound}
}
public TokenType type = TokenType.Invalid;
public List<string> paramList;
}
}

12
Assets/Fungus/UI/Scripts/TextTagToken.cs.meta

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: a1d7b8611768549fca12101bac0fe545
timeCreated: 1473757164
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

70
Assets/Fungus/UI/Scripts/Writer.cs

@ -281,7 +281,7 @@ namespace Fungus
return false; return false;
} }
protected virtual IEnumerator ProcessTokens(List<TextTagParser.Token> tokens, bool stopAudio, Action onComplete) protected virtual IEnumerator ProcessTokens(List<TextTagToken> tokens, bool stopAudio, Action onComplete)
{ {
// Reset control members // Reset control members
boldActive = false; boldActive = false;
@ -296,34 +296,34 @@ namespace Fungus
exitFlag = false; exitFlag = false;
isWriting = true; isWriting = true;
TextTagParser.TokenType previousTokenType = TextTagParser.TokenType.Invalid; TextTagToken.TokenType previousTokenType = TextTagToken.TokenType.Invalid;
foreach (TextTagParser.Token token in tokens) foreach (TextTagToken token in tokens)
{ {
switch (token.type) switch (token.type)
{ {
case TextTagParser.TokenType.Words: case TextTagToken.TokenType.Words:
yield return StartCoroutine(DoWords(token.paramList, previousTokenType)); yield return StartCoroutine(DoWords(token.paramList, previousTokenType));
break; break;
case TextTagParser.TokenType.BoldStart: case TextTagToken.TokenType.BoldStart:
boldActive = true; boldActive = true;
break; break;
case TextTagParser.TokenType.BoldEnd: case TextTagToken.TokenType.BoldEnd:
boldActive = false; boldActive = false;
break; break;
case TextTagParser.TokenType.ItalicStart: case TextTagToken.TokenType.ItalicStart:
italicActive = true; italicActive = true;
break; break;
case TextTagParser.TokenType.ItalicEnd: case TextTagToken.TokenType.ItalicEnd:
italicActive = false; italicActive = false;
break; break;
case TextTagParser.TokenType.ColorStart: case TextTagToken.TokenType.ColorStart:
if (CheckParamCount(token.paramList, 1)) if (CheckParamCount(token.paramList, 1))
{ {
colorActive = true; colorActive = true;
@ -331,66 +331,66 @@ namespace Fungus
} }
break; break;
case TextTagParser.TokenType.ColorEnd: case TextTagToken.TokenType.ColorEnd:
colorActive = false; colorActive = false;
break; break;
case TextTagParser.TokenType.SizeStart: case TextTagToken.TokenType.SizeStart:
if (TryGetSingleParam(token.paramList, 0, 16f, out sizeValue)) if (TryGetSingleParam(token.paramList, 0, 16f, out sizeValue))
{ {
sizeActive = true; sizeActive = true;
} }
break; break;
case TextTagParser.TokenType.SizeEnd: case TextTagToken.TokenType.SizeEnd:
sizeActive = false; sizeActive = false;
break; break;
case TextTagParser.TokenType.Wait: case TextTagToken.TokenType.Wait:
yield return StartCoroutine(DoWait(token.paramList)); yield return StartCoroutine(DoWait(token.paramList));
break; break;
case TextTagParser.TokenType.WaitForInputNoClear: case TextTagToken.TokenType.WaitForInputNoClear:
yield return StartCoroutine(DoWaitForInput(false)); yield return StartCoroutine(DoWaitForInput(false));
break; break;
case TextTagParser.TokenType.WaitForInputAndClear: case TextTagToken.TokenType.WaitForInputAndClear:
yield return StartCoroutine(DoWaitForInput(true)); yield return StartCoroutine(DoWaitForInput(true));
break; break;
case TextTagParser.TokenType.WaitOnPunctuationStart: case TextTagToken.TokenType.WaitOnPunctuationStart:
TryGetSingleParam(token.paramList, 0, punctuationPause, out currentPunctuationPause); TryGetSingleParam(token.paramList, 0, punctuationPause, out currentPunctuationPause);
break; break;
case TextTagParser.TokenType.WaitOnPunctuationEnd: case TextTagToken.TokenType.WaitOnPunctuationEnd:
currentPunctuationPause = punctuationPause; currentPunctuationPause = punctuationPause;
break; break;
case TextTagParser.TokenType.Clear: case TextTagToken.TokenType.Clear:
text = ""; text = "";
break; break;
case TextTagParser.TokenType.SpeedStart: case TextTagToken.TokenType.SpeedStart:
TryGetSingleParam(token.paramList, 0, writingSpeed, out currentWritingSpeed); TryGetSingleParam(token.paramList, 0, writingSpeed, out currentWritingSpeed);
break; break;
case TextTagParser.TokenType.SpeedEnd: case TextTagToken.TokenType.SpeedEnd:
currentWritingSpeed = writingSpeed; currentWritingSpeed = writingSpeed;
break; break;
case TextTagParser.TokenType.Exit: case TextTagToken.TokenType.Exit:
exitFlag = true; exitFlag = true;
break; break;
case TextTagParser.TokenType.Message: case TextTagToken.TokenType.Message:
if (CheckParamCount(token.paramList, 1)) if (CheckParamCount(token.paramList, 1))
{ {
Flowchart.BroadcastFungusMessage(token.paramList[0]); Flowchart.BroadcastFungusMessage(token.paramList[0]);
} }
break; break;
case TextTagParser.TokenType.VerticalPunch: case TextTagToken.TokenType.VerticalPunch:
{ {
float vintensity; float vintensity;
float time; float time;
@ -400,7 +400,7 @@ namespace Fungus
} }
break; break;
case TextTagParser.TokenType.HorizontalPunch: case TextTagToken.TokenType.HorizontalPunch:
{ {
float hintensity; float hintensity;
float time; float time;
@ -410,7 +410,7 @@ namespace Fungus
} }
break; break;
case TextTagParser.TokenType.Punch: case TextTagToken.TokenType.Punch:
{ {
float intensity; float intensity;
float time; float time;
@ -420,13 +420,13 @@ namespace Fungus
} }
break; break;
case TextTagParser.TokenType.Flash: case TextTagToken.TokenType.Flash:
float flashDuration; float flashDuration;
TryGetSingleParam(token.paramList, 0, 0.2f, out flashDuration); TryGetSingleParam(token.paramList, 0, 0.2f, out flashDuration);
Flash(flashDuration); Flash(flashDuration);
break; break;
case TextTagParser.TokenType.Audio: case TextTagToken.TokenType.Audio:
{ {
AudioSource audioSource = null; AudioSource audioSource = null;
if (CheckParamCount(token.paramList, 1)) if (CheckParamCount(token.paramList, 1))
@ -440,7 +440,7 @@ namespace Fungus
} }
break; break;
case TextTagParser.TokenType.AudioLoop: case TextTagToken.TokenType.AudioLoop:
{ {
AudioSource audioSource = null; AudioSource audioSource = null;
if (CheckParamCount(token.paramList, 1)) if (CheckParamCount(token.paramList, 1))
@ -455,7 +455,7 @@ namespace Fungus
} }
break; break;
case TextTagParser.TokenType.AudioPause: case TextTagToken.TokenType.AudioPause:
{ {
AudioSource audioSource = null; AudioSource audioSource = null;
if (CheckParamCount(token.paramList, 1)) if (CheckParamCount(token.paramList, 1))
@ -469,7 +469,7 @@ namespace Fungus
} }
break; break;
case TextTagParser.TokenType.AudioStop: case TextTagToken.TokenType.AudioStop:
{ {
AudioSource audioSource = null; AudioSource audioSource = null;
if (CheckParamCount(token.paramList, 1)) if (CheckParamCount(token.paramList, 1))
@ -505,7 +505,7 @@ namespace Fungus
} }
} }
protected virtual IEnumerator DoWords(List<string> paramList, TextTagParser.TokenType previousTokenType) protected virtual IEnumerator DoWords(List<string> paramList, TextTagToken.TokenType previousTokenType)
{ {
if (!CheckParamCount(paramList, 1)) if (!CheckParamCount(paramList, 1))
{ {
@ -515,8 +515,8 @@ namespace Fungus
string param = paramList[0]; string param = paramList[0];
// Trim whitespace after a {wc} or {c} tag // Trim whitespace after a {wc} or {c} tag
if (previousTokenType == TextTagParser.TokenType.WaitForInputAndClear || if (previousTokenType == TextTagToken.TokenType.WaitForInputAndClear ||
previousTokenType == TextTagParser.TokenType.Clear) previousTokenType == TextTagToken.TokenType.Clear)
{ {
param = param.TrimStart(' ', '\t', '\r', '\n'); param = param.TrimStart(' ', '\t', '\r', '\n');
} }
@ -832,8 +832,8 @@ namespace Fungus
tokenText += "{wi}"; tokenText += "{wi}";
} }
TextTagParser tagParser = new TextTagParser(); ITextTagParser tagParser = new TextTagParser();
List<TextTagParser.Token> tokens = tagParser.Tokenize(tokenText); List<TextTagToken> tokens = tagParser.Tokenize(tokenText);
gameObject.SetActive(true); gameObject.SetActive(true);

90
Assets/Tests/UI/Editor/TextTagParserTests.cs

@ -1,9 +1,7 @@
// This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus). // This code is part of the Fungus library (http://fungusgames.com) maintained by Chris Gregan (http://twitter.com/gofungus).
// It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE) // It is released for free under the MIT open source license (https://github.com/snozbot/fungus/blob/master/LICENSE)
using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine;
using Fungus; using Fungus;
#if UNITY_5_3_OR_NEWER #if UNITY_5_3_OR_NEWER
@ -20,7 +18,7 @@ public class TextTagParserTests
var textTagParser = new TextTagParser(); var textTagParser = new TextTagParser();
// Parse an example string, generate correct sequence of tags // Parse an example string, generate correct sequence of tags
List<TextTagParser.Token> tokens = textTagParser.Tokenize("Words " + List<TextTagToken> tokens = textTagParser.Tokenize("Words " +
"{b}bold test{/b}" + "{b}bold test{/b}" +
"{i}italic test{/i}" + "{i}italic test{/i}" +
"{color=red}color test{/color}" + "{color=red}color test{/color}" +
@ -41,139 +39,139 @@ public class TextTagParserTests
"{audiostop=Sound}"); "{audiostop=Sound}");
int i = 0; int i = 0;
Assert.That(tokens[i].type == TextTagParser.TokenType.Words); Assert.That(tokens[i].type == TextTagToken.TokenType.Words);
Assert.That(tokens[i].paramList[0] == "Words "); Assert.That(tokens[i].paramList[0] == "Words ");
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.BoldStart); Assert.That(tokens[i].type == TextTagToken.TokenType.BoldStart);
Assert.That(tokens[i].paramList.Count == 0); Assert.That(tokens[i].paramList.Count == 0);
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.Words); Assert.That(tokens[i].type == TextTagToken.TokenType.Words);
Assert.That(tokens[i].paramList[0] == "bold test"); Assert.That(tokens[i].paramList[0] == "bold test");
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.BoldEnd); Assert.That(tokens[i].type == TextTagToken.TokenType.BoldEnd);
Assert.That(tokens[i].paramList.Count == 0); Assert.That(tokens[i].paramList.Count == 0);
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.ItalicStart); Assert.That(tokens[i].type == TextTagToken.TokenType.ItalicStart);
Assert.That(tokens[i].paramList.Count == 0); Assert.That(tokens[i].paramList.Count == 0);
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.Words); Assert.That(tokens[i].type == TextTagToken.TokenType.Words);
Assert.That(tokens[i].paramList[0] == "italic test"); Assert.That(tokens[i].paramList[0] == "italic test");
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.ItalicEnd); Assert.That(tokens[i].type == TextTagToken.TokenType.ItalicEnd);
Assert.That(tokens[i].paramList.Count == 0); Assert.That(tokens[i].paramList.Count == 0);
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.ColorStart); Assert.That(tokens[i].type == TextTagToken.TokenType.ColorStart);
Assert.That(tokens[i].paramList[0] == "red"); Assert.That(tokens[i].paramList[0] == "red");
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.Words); Assert.That(tokens[i].type == TextTagToken.TokenType.Words);
Assert.That(tokens[i].paramList[0] == "color test"); Assert.That(tokens[i].paramList[0] == "color test");
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.ColorEnd); Assert.That(tokens[i].type == TextTagToken.TokenType.ColorEnd);
Assert.That(tokens[i].paramList.Count == 0); Assert.That(tokens[i].paramList.Count == 0);
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.SizeStart); Assert.That(tokens[i].type == TextTagToken.TokenType.SizeStart);
Assert.That(tokens[i].paramList[0] == "30"); Assert.That(tokens[i].paramList[0] == "30");
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.Words); Assert.That(tokens[i].type == TextTagToken.TokenType.Words);
Assert.That(tokens[i].paramList[0] == "size test"); Assert.That(tokens[i].paramList[0] == "size test");
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.SizeEnd); Assert.That(tokens[i].type == TextTagToken.TokenType.SizeEnd);
Assert.That(tokens[i].paramList.Count == 0); Assert.That(tokens[i].paramList.Count == 0);
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.Wait); Assert.That(tokens[i].type == TextTagToken.TokenType.Wait);
Assert.That(tokens[i].paramList.Count == 0); Assert.That(tokens[i].paramList.Count == 0);
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.Wait); Assert.That(tokens[i].type == TextTagToken.TokenType.Wait);
Assert.That(tokens[i].paramList[0] == "0.5"); Assert.That(tokens[i].paramList[0] == "0.5");
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.WaitForInputNoClear); Assert.That(tokens[i].type == TextTagToken.TokenType.WaitForInputNoClear);
Assert.That(tokens[i].paramList.Count == 0); Assert.That(tokens[i].paramList.Count == 0);
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.WaitForInputAndClear); Assert.That(tokens[i].type == TextTagToken.TokenType.WaitForInputAndClear);
Assert.That(tokens[i].paramList.Count == 0); Assert.That(tokens[i].paramList.Count == 0);
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.WaitOnPunctuationStart); Assert.That(tokens[i].type == TextTagToken.TokenType.WaitOnPunctuationStart);
Assert.That(tokens[i].paramList.Count == 0); Assert.That(tokens[i].paramList.Count == 0);
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.WaitOnPunctuationStart); Assert.That(tokens[i].type == TextTagToken.TokenType.WaitOnPunctuationStart);
Assert.That(tokens[i].paramList[0] == "0.5"); Assert.That(tokens[i].paramList[0] == "0.5");
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.WaitOnPunctuationEnd); Assert.That(tokens[i].type == TextTagToken.TokenType.WaitOnPunctuationEnd);
Assert.That(tokens[i].paramList.Count == 0); Assert.That(tokens[i].paramList.Count == 0);
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.Clear); Assert.That(tokens[i].type == TextTagToken.TokenType.Clear);
Assert.That(tokens[i].paramList.Count == 0); Assert.That(tokens[i].paramList.Count == 0);
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.SpeedStart); Assert.That(tokens[i].type == TextTagToken.TokenType.SpeedStart);
Assert.That(tokens[i].paramList.Count == 0); Assert.That(tokens[i].paramList.Count == 0);
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.SpeedStart); Assert.That(tokens[i].type == TextTagToken.TokenType.SpeedStart);
Assert.That(tokens[i].paramList[0] == "60"); Assert.That(tokens[i].paramList[0] == "60");
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.SpeedEnd); Assert.That(tokens[i].type == TextTagToken.TokenType.SpeedEnd);
Assert.That(tokens[i].paramList.Count == 0); Assert.That(tokens[i].paramList.Count == 0);
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.Exit); Assert.That(tokens[i].type == TextTagToken.TokenType.Exit);
Assert.That(tokens[i].paramList.Count == 0); Assert.That(tokens[i].paramList.Count == 0);
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.Message); Assert.That(tokens[i].type == TextTagToken.TokenType.Message);
Assert.That(tokens[i].paramList[0] == "Message"); Assert.That(tokens[i].paramList[0] == "Message");
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.VerticalPunch); Assert.That(tokens[i].type == TextTagToken.TokenType.VerticalPunch);
Assert.That(tokens[i].paramList[0] == "0.5"); Assert.That(tokens[i].paramList[0] == "0.5");
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.HorizontalPunch); Assert.That(tokens[i].type == TextTagToken.TokenType.HorizontalPunch);
Assert.That(tokens[i].paramList[0] == "0.5"); Assert.That(tokens[i].paramList[0] == "0.5");
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.Punch); Assert.That(tokens[i].type == TextTagToken.TokenType.Punch);
Assert.That(tokens[i].paramList[0] == "0.5"); Assert.That(tokens[i].paramList[0] == "0.5");
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.Flash); Assert.That(tokens[i].type == TextTagToken.TokenType.Flash);
Assert.That(tokens[i].paramList[0] == "0.5"); Assert.That(tokens[i].paramList[0] == "0.5");
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.Audio); Assert.That(tokens[i].type == TextTagToken.TokenType.Audio);
Assert.That(tokens[i].paramList[0] == "Sound"); Assert.That(tokens[i].paramList[0] == "Sound");
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.AudioLoop); Assert.That(tokens[i].type == TextTagToken.TokenType.AudioLoop);
Assert.That(tokens[i].paramList[0] == "Sound"); Assert.That(tokens[i].paramList[0] == "Sound");
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.AudioPause); Assert.That(tokens[i].type == TextTagToken.TokenType.AudioPause);
Assert.That(tokens[i].paramList[0] == "Sound"); Assert.That(tokens[i].paramList[0] == "Sound");
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.AudioStop); Assert.That(tokens[i].type == TextTagToken.TokenType.AudioStop);
Assert.That(tokens[i].paramList[0] == "Sound"); Assert.That(tokens[i].paramList[0] == "Sound");
Assert.That(tokens.Count == 34); Assert.That(tokens.Count == 34);
@ -185,38 +183,38 @@ public class TextTagParserTests
var textTagParser = new TextTagParser(); var textTagParser = new TextTagParser();
// Parse an example string, generate correct sequence of tags // Parse an example string, generate correct sequence of tags
List<TextTagParser.Token> tokens = textTagParser.Tokenize("Play sound{audio=BeepSound}{w=1} Play loop{audioloop=BeepSound}{w=3} Stop{audiostop=BeepSound}"); List<TextTagToken> tokens = textTagParser.Tokenize("Play sound{audio=BeepSound}{w=1} Play loop{audioloop=BeepSound}{w=3} Stop{audiostop=BeepSound}");
int i = 0; int i = 0;
Assert.That(tokens[i].type == TextTagParser.TokenType.Words); Assert.That(tokens[i].type == TextTagToken.TokenType.Words);
Assert.That(tokens[i].paramList[0] == "Play sound"); Assert.That(tokens[i].paramList[0] == "Play sound");
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.Audio); Assert.That(tokens[i].type == TextTagToken.TokenType.Audio);
Assert.That(tokens[i].paramList[0] == "BeepSound"); Assert.That(tokens[i].paramList[0] == "BeepSound");
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.Wait); Assert.That(tokens[i].type == TextTagToken.TokenType.Wait);
Assert.That(tokens[i].paramList[0] == "1"); Assert.That(tokens[i].paramList[0] == "1");
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.Words); Assert.That(tokens[i].type == TextTagToken.TokenType.Words);
Assert.That(tokens[i].paramList[0] == " Play loop"); Assert.That(tokens[i].paramList[0] == " Play loop");
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.AudioLoop); Assert.That(tokens[i].type == TextTagToken.TokenType.AudioLoop);
Assert.That(tokens[i].paramList[0] == "BeepSound"); Assert.That(tokens[i].paramList[0] == "BeepSound");
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.Wait); Assert.That(tokens[i].type == TextTagToken.TokenType.Wait);
Assert.That(tokens[i].paramList[0] == "3"); Assert.That(tokens[i].paramList[0] == "3");
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.Words); Assert.That(tokens[i].type == TextTagToken.TokenType.Words);
Assert.That(tokens[i].paramList[0] == " Stop"); Assert.That(tokens[i].paramList[0] == " Stop");
i++; i++;
Assert.That(tokens[i].type == TextTagParser.TokenType.AudioStop); Assert.That(tokens[i].type == TextTagToken.TokenType.AudioStop);
Assert.That(tokens[i].paramList[0] == "BeepSound"); Assert.That(tokens[i].paramList[0] == "BeepSound");
Assert.That(tokens.Count == 8); Assert.That(tokens.Count == 8);

Loading…
Cancel
Save