Ejemplo n.º 1
0
 public void ShouldReturnOriginalTextIfNoVariables()
 {
     var replacer = new TextReplacer();
     var variables = new Mock<ITextVariables>();
     variables.Setup(v => v["host"]).Returns("example.com");
     Assert.That(replacer.ReplaceVariables("http://example.com/blah/blah", variables.Object), Is.EqualTo("http://example.com/blah/blah"));
 }
Ejemplo n.º 2
0
 public void ShouldEscapeDoubleBraces()
 {
     var replacer = new TextReplacer();
     var variables = new Mock<ITextVariables>();
     variables.Setup(v => v["host"]).Returns("example.com");
     Assert.That(replacer.ReplaceVariables("http://{{host}}/blah/blah", variables.Object), Is.EqualTo("http://{host}/blah/blah"));
     Assert.That(replacer.ReplaceVariables("http://{{host}", variables.Object), Is.EqualTo("http://{host}"));
     Assert.That(replacer.ReplaceVariables("http://{", variables.Object), Is.EqualTo("http://{"));
     Assert.That(replacer.ReplaceVariables("http://}}", variables.Object), Is.EqualTo("http://}"));
     Assert.That(replacer.ReplaceVariables("http://{{", variables.Object), Is.EqualTo("http://{"));
     Assert.That(replacer.ReplaceVariables("http://{{{host}}}/blah/blah", variables.Object), Is.EqualTo("http://{example.com}/blah/blah"));
     Assert.That(replacer.ReplaceVariables("{{http://host/blah/blah", variables.Object), Is.EqualTo("{http://host/blah/blah"));
 }
Ejemplo n.º 3
0
        public void Run(CancellationToken cancellationToken)
        {
            lock (_busy)
            {
                if (Immutable) return;

                if (_state != MicroTaskStates.None) _log.Error("_state != MicroTaskStates.None");

                if (cancellationToken.IsCancellationRequested) return;
                State = MicroTaskStates.Reading;
                var text = _file.ReadText();
                var replacer = new TextReplacer(text);

                if (cancellationToken.IsCancellationRequested) return;
                State = MicroTaskStates.Searching;
                var matches = new List<RelatedMatch>();
                var m = _regex.RelatedMatch(text, 0, replacer);
                while (m.Success)
                {
                    CurrentStagePercentage = (double)(m.StartIndex + m.Length) / text.Length;
                    matches.Add(m);
                    if (cancellationToken.IsCancellationRequested) return;
                    m = _regex.RelatedMatch(text, m.StartIndex + m.Length, replacer);
                }
                if (matches.Count == 0) matches.Add(m);

                if (!matches[0].Success)
                {
                    Immutable = true;
                    State = MicroTaskStates.Complete;
                    return;
                }

                if (cancellationToken.IsCancellationRequested) return;
                State = MicroTaskStates.Replacing;
                foreach (var relatedMatch in matches)
                    replacer.Replace(relatedMatch, _replacement.CreateCopyWithGroups(relatedMatch));

                State = MicroTaskStates.BuildingResult;
                var result = replacer.BuildResult();
                ReplacesCount += matches.Count;

                if (cancellationToken.IsCancellationRequested) return;
                State = MicroTaskStates.SavingResult;
                _file.WriteText(result);

                State = MicroTaskStates.Complete;
            }
        }
Ejemplo n.º 4
0
 public void ShouldThrowExceptionIfVariableNotComplete()
 {
     var replacer = new TextReplacer();
     var variables = new Mock<ITextVariables>();
     Assert.That(() => replacer.ReplaceVariables("http://{host", variables.Object), Throws.InstanceOf(typeof (ParseException)));
 }