コード例 #1
0
        public void Contains_LabelAsProvided_ResultAsExpected(String actual, Boolean expected, params String[] labels)
        {
            ArgumentRelations instance = new ArgumentRelations();

            foreach (String label in labels)
            {
                instance.Append(label, null);
            }

            Assert.That(instance.Contains(actual), Is.EqualTo(expected));
        }
コード例 #2
0
        /// <summary>
        /// Assigns the token labels to their corresponding arguments.
        /// </summary>
        /// <remarks>
        /// This method assigns the token labels to their corresponding arguments.
        /// </remarks>
        /// <param name="tokens">
        /// The list of tokens to be processed.
        /// </param>
        /// <param name="arguments">
        /// The list of arguments to be assigned.
        /// </param>
        /// <returns>
        /// An instance of an <see cref="IArgumentRelations"/> derived class to
        /// obtains all assignments from.
        /// </returns>
        public static IArgumentRelations Assign(IEnumerable <BaseToken> tokens, Object[] arguments)
        {
            if (tokens == null)
            {
                return(RelationsAssigner.Default);
            }

            if (arguments == null)
            {
                arguments = new Object[0];
            }

            const String format = "Value{0}";

            ArgumentRelations result = new ArgumentRelations();

            Boolean indexed = tokens.IsAssignAllByIndex();

            foreach (BaseToken token in tokens)
            {
                if (token is TextToken)
                {
                    continue;
                }

                Int32  index = -1;
                String label = null;
                Object value = null;

                if (indexed)
                {
                    // Use index as label if all items are processed "by index".
                    index = token.ToIndex();
                    label = String.Format(format, index);

                    // Skip already assigned relations, if necessary. This may occur
                    // for example in cases like "text {0} is {1}, but same as {0}".
                    // In such a case the value at index {0} has been handled already.
                    if (result.Contains(label))
                    {
                        continue;
                    }
                }
                else
                {
                    index = token.Rating;

                    if (token.IsNumbering)
                    {
                        // This is some kind of fallback that can occur together with mixed
                        // formattings, for example something like "text {0} is {lbl1}, but
                        // same as {1}". In such a case the "rating" is used as index, but
                        // current token does not have a valid label. Therefore, take "rating"
                        // as index and create a label for affected value. Uniqueness does
                        // not need to be checked because of rating is already unique.
                        label = String.Format(format, index);
                    }
                    else
                    {
                        // Uniqueness in never guaranteed in this case, because of the Message
                        // Template Rules allow multiple labels of same content. On the other
                        // hand, all arguments have to be resolved by their order anyway.
                        label = token.ToLabel();
                    }
                }

                if (index >= 0 && index < arguments.Length)
                {
                    value = arguments[index];
                }

                result.Append(label, value);
            }

            return(result);
        }