Exemple #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ParameterizedString"/>
        /// class by using the provided list of
        /// <see cref="ParameterizedStringPart"/>
        /// </summary>
        /// <param name="parts">A list of <see cref="ParameterizedStringPart"/>
        /// objects that compose this instance.</param>
        /// <param name="delimiter">The delimiter used to delimits the parameters.
        /// within the string.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="parts"/> or
        /// <paramref name="delimiter"/> is <c>null</c></exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="parts"/> contains one or more <c>null</c> elements.
        /// </exception>
        public static ParameterizedString FromParameterizedStringPartCollection(
            IEnumerable <ParameterizedStringPart> parts, string delimiter)
        {
            if (parts == null || delimiter == null)
            {
                throw new ArgumentNullException((parts == null)
          ? "parts"
          : "delimiter");
            }

            ParameterizedString str = new ParameterizedString();

            StringBuilder builder = new StringBuilder();

            foreach (ParameterizedStringPart part in parts)
            {
                if (part == null)
                {
                    throw new ArgumentException("part");
                }

                str.parts_.Add(part);
                if (part.IsParameter)
                {
                    builder.Append(delimiter).Append(part.Value).Append(delimiter);
                    str.parameters_.AddIfAbsent((ParameterizedStringPartParameter)part);
                    continue;
                }
                builder.Append(part.Value);
            }
            str.flat_string = builder.ToString();

            return(str);
        }
        public void ShouldThrowArgumentExceptionWhenOneOrMoreElementsAreNull()
        {
            Assert.Throws <ArgumentException>(delegate()
            {
                ParameterizedStringPart[] parts = new ParameterizedStringPart[2];
                parts[0] = new ParameterizedStringPartParameter("name");
                parts[1] = null;

                ParameterizedString s =
                    ParameterizedString.FromParameterizedStringPartCollection(parts, "$");
            });
        }
    public void ShouldThrowArgumentNullExceptionWhenArgIsNull() {
      Assert.Throws<ArgumentNullException>(delegate() {
        ParameterizedString s = new ParameterizedString(null);
      });

      Assert.Throws<ArgumentNullException>(delegate() {
        ParameterizedString s = new ParameterizedString(null, "delimiter");
      });

      Assert.Throws<ArgumentNullException>(delegate() {
        ParameterizedString s = new ParameterizedString("string", null);
      });
    }
        public void ShouldThrowArgumentNullExceptionWhenArgIsNull()
        {
            Assert.Throws <ArgumentNullException>(delegate() {
                ParameterizedString s = new ParameterizedString(null);
            });

            Assert.Throws <ArgumentNullException>(delegate() {
                ParameterizedString s = new ParameterizedString(null, "delimiter");
            });

            Assert.Throws <ArgumentNullException>(delegate() {
                ParameterizedString s = new ParameterizedString("string", null);
            });
        }
    public void SholdReplaceTheParameterToItsValueOnFinalString() {
      ParameterizedString s =
        new ParameterizedString("The parameter value is $parameter$");
      Assert.AreEqual(string.Empty, s.ToString());

      s.Parse();
      s.Parameters[0].Value = "VALUE";
      Assert.AreEqual("The parameter value is VALUE", s.ToString());

      s = new ParameterizedString(
        "The parameter value is $parameter$ and more.");

      s.Parse();
      s.Parameters[0].Value = "VALUE";
      Assert.AreEqual("The parameter value is VALUE and more.", s.ToString());
    }
        public void SholdReplaceTheParameterToItsValueOnFinalString()
        {
            ParameterizedString s =
                new ParameterizedString("The parameter value is $parameter$");

            Assert.AreEqual(string.Empty, s.ToString());

            s.Parse();
            s.Parameters[0].Value = "VALUE";
            Assert.AreEqual("The parameter value is VALUE", s.ToString());

            s = new ParameterizedString(
                "The parameter value is $parameter$ and more.");

            s.Parse();
            s.Parameters[0].Value = "VALUE";
            Assert.AreEqual("The parameter value is VALUE and more.", s.ToString());
        }
    public void ShouldConsiderOnlyDelimiterEnclosedStringsAsParameters() {
      ParameterizedString s =
        new ParameterizedString(
          "This is a parameter $parameter$ and this is not $parameter");
      s.Parse();

      Assert.AreEqual(1, s.Parameters.Count);
      Assert.AreEqual("parameter", s.Parameters[0].Name);
      Assert.AreEqual("This is a parameter  and this is not $parameter",
        s.ToString());

      s = new ParameterizedString(
          "Spaces is part of the parameter: $parameter with spaces$");
      s.Parse();

      Assert.AreEqual(1, s.Parameters.Count);
      Assert.AreEqual("parameter with spaces", s.Parameters[0].Name);
    }
        public void ShouldConsiderOnlyDelimiterEnclosedStringsAsParameters()
        {
            ParameterizedString s =
                new ParameterizedString(
                    "This is a parameter $parameter$ and this is not $parameter");

            s.Parse();

            Assert.AreEqual(1, s.Parameters.Count);
            Assert.AreEqual("parameter", s.Parameters[0].Name);
            Assert.AreEqual("This is a parameter  and this is not $parameter",
                            s.ToString());

            s = new ParameterizedString(
                "Spaces is part of the parameter: $parameter with spaces$");
            s.Parse();

            Assert.AreEqual(1, s.Parameters.Count);
            Assert.AreEqual("parameter with spaces", s.Parameters[0].Name);
        }
    public void ShouldUseSpaceAsTerminatorWhenSpecified() {
      ParameterizedString s =
        new ParameterizedString(
          "This is a $parameter$ and this is the same $parameter again.");
      s.UseSpaceAsTerminator = true;
      s.Parse();

      Assert.AreEqual(1, s.Parameters.Count);
      Assert.AreEqual("parameter", s.Parameters[0].Name);
      Assert.AreEqual("This is a  and this is the same  again.", s.ToString());

      s =
        new ParameterizedString(
          "This is a $parameter$ and this is the another $parameter2");
      s.UseSpaceAsTerminator = true;
      s.Parse();

      Assert.AreEqual(2, s.Parameters.Count);
      Assert.AreNotEqual(-1, s.Parameters.IndexOf("parameter"));
      Assert.AreNotEqual(-1, s.Parameters.IndexOf("parameter2"));
      Assert.AreEqual("This is a  and this is the another ", s.ToString());
    }
        public void ShouldUseSpaceAsTerminatorWhenSpecified()
        {
            ParameterizedString s =
                new ParameterizedString(
                    "This is a $parameter$ and this is the same $parameter again.");

            s.UseSpaceAsTerminator = true;
            s.Parse();

            Assert.AreEqual(1, s.Parameters.Count);
            Assert.AreEqual("parameter", s.Parameters[0].Name);
            Assert.AreEqual("This is a  and this is the same  again.", s.ToString());

            s =
                new ParameterizedString(
                    "This is a $parameter$ and this is the another $parameter2");
            s.UseSpaceAsTerminator = true;
            s.Parse();

            Assert.AreEqual(2, s.Parameters.Count);
            Assert.AreNotEqual(-1, s.Parameters.IndexOf("parameter"));
            Assert.AreNotEqual(-1, s.Parameters.IndexOf("parameter2"));
            Assert.AreEqual("This is a  and this is the another ", s.ToString());
        }
    /// <summary>
    /// Initializes a new instance of the <see cref="ParameterizedString"/>
    /// class by using the provided list of
    /// <see cref="ParameterizedStringPart"/>
    /// </summary>
    /// <param name="parts">A list of <see cref="ParameterizedStringPart"/>
    /// objects that compose this instance.</param>
    /// <param name="delimiter">The delimiter used to delimits the parameters.
    /// within the string.</param>
    /// <exception cref="ArgumentNullException">
    /// <paramref name="parts"/> or
    /// <paramref name="delimiter"/> is <c>null</c></exception>
    /// <exception cref="ArgumentException">
    /// <paramref name="parts"/> contains one or more <c>null</c> elements.
    /// </exception>
    public static ParameterizedString FromParameterizedStringPartCollection(
      IEnumerable<ParameterizedStringPart> parts, string delimiter) {
      if (parts == null || delimiter == null) {
        throw new ArgumentNullException((parts == null)
          ? "parts"
          : "delimiter");
      }

      ParameterizedString str = new ParameterizedString();

      StringBuilder builder = new StringBuilder();
      foreach (ParameterizedStringPart part in parts) {
        if (part == null) {
          throw new ArgumentException("part");
        }

        str.parts_.Add(part);
        if (part.IsParameter) {
          builder.Append(delimiter).Append(part.Value).Append(delimiter);
          str.parameters_.AddIfAbsent((ParameterizedStringPartParameter) part);
          continue;
        }
        builder.Append(part.Value);
      }
      str.flat_string = builder.ToString();

      return str;
    }