public void LinkingHelperTests_Nulls_ReturnsNull()
        {
            var answerDto = new AnswerDto()
            {
                LeftWord  = "a",
                RightWord = "b",
                Phrase    = "c"
            };

            var result = LinkingHelper.ParseUrlToAnswer(null);

            Assert.Null(result);

            result = LinkingHelper.ParseUrlToAnswer("    ");
            Assert.Null(result);

            result = LinkingHelper.ParseUrlToAnswer("random");
            Assert.Null(result);

            result = LinkingHelper.ParseUrlToAnswer(null, null);
            Assert.Null(result);

            result = LinkingHelper.ParseUrlToAnswer(null, "    ");
            Assert.Null(result);

            var commonStrings = new CommonStringsDto()
            {
                Best = "x", For = "y", Is = "z"
            };

            result = LinkingHelper.ParseUrlToAnswer(commonStrings, "random");
            Assert.Null(result);
        }
        public void LinkingHelperTests_ParseUrlToAnswer_Parses()
        {
            var answerDto = new AnswerDto()
            {
                LeftWord  = "a",
                RightWord = "b",
                Phrase    = "c"
            };

            var url = "/best-a-for-b-is-c";

            var result = LinkingHelper.ParseUrlToAnswer(url);

            Assert.Equal(result.LeftWord, answerDto.LeftWord);
            Assert.Equal(result.RightWord, answerDto.RightWord);
            Assert.Equal(result.Phrase, answerDto.Phrase);

            var commonStrings = new CommonStringsDto()
            {
                Best = "x", For = "y", Is = "z"
            };

            url = "/x-a-y-b-z-c";

            result = result = LinkingHelper.ParseUrlToAnswer(commonStrings, url);
            Assert.Equal(result.LeftWord, answerDto.LeftWord);
            Assert.Equal(result.RightWord, answerDto.RightWord);
            Assert.Equal(result.Phrase, answerDto.Phrase);
        }
        public void LinkingHelperTests_ConvertAnswerToUrl_Converts()
        {
            var answerDto = new AnswerDto()
            {
                LeftWord  = "a",
                RightWord = "b",
                Phrase    = "c"
            };

            var result = LinkingHelper.ConvertAnswerToUrl(answerDto);

            Assert.Equal(result, "/best-a-for-b-is-c");

            result = LinkingHelper.ConvertAnswerToText(answerDto);

            Assert.Equal(result, "Best a for b is c");

            var commonStrings = new CommonStringsDto()
            {
                Best = "x", For = "y", Is = "z"
            };

            result = LinkingHelper.ConvertAnswerToText(commonStrings, answerDto);
            Assert.Equal(result, "x a y b z c");

            result = LinkingHelper.ConvertAnswerToUrl(commonStrings, answerDto);
            Assert.Equal(result, "/x-a-y-b-z-c");

            result = LinkingHelper.ConvertAnswerToUrlWithCulture("f", commonStrings, answerDto);
            Assert.Equal(result, "/f/x-a-y-b-z-c");
        }
Пример #4
0
        public static AnswerDto ParseUrlToAnswer(CommonStringsDto commonStrings, string url)
        {
            const int GROUP_LEFT_WORD   = 1;
            const int GROUP_RIGHT_WORD  = 2;
            const int GROUP_PHRASE_WORD = 3;

            //const int NUMBER_GROUPS = 4;
            if (string.IsNullOrEmpty(url))
            {
                return(null);
            }
            if (string.IsNullOrWhiteSpace(url))
            {
                return(null);
            }

            //^/best\\s+(.*)\\s+for\\s+(.*)\\s+is\\s+(.*)
            var expression = "^/" + commonStrings.Best.ToLower() + "\\s+(.*)\\s+" + commonStrings.For.ToLower() + "\\s+(.*)\\s+" + commonStrings.Is.ToLower() + "\\s+(.*)";
            var matches    = Regex.Matches(url.Replace("-", " ").ToLower(), expression);

            // Good check but we will never run into this
            //if (matches == null) return null; // Should not be null. Could be count zero but not null.
            if (matches.Count == 0)
            {
                return(null);
            }
            // Good check but we will never run into this
            //if (matches[0].Groups.Count != NUMBER_GROUPS) return null;
            var answer = new AnswerDto()
            {
                LeftWord  = matches[0].Groups[GROUP_LEFT_WORD].Value,
                RightWord = matches[0].Groups[GROUP_RIGHT_WORD].Value,
                Phrase    = matches[0].Groups[GROUP_PHRASE_WORD].Value
            };

            return(answer);
        }
Пример #5
0
 public static string ConvertAnswerToUrlWithCulture(string culture, CommonStringsDto commonStrings, AnswerDto answer)
 {
     //return string.Format("/best {0} for {1} is {2}", answer.LeftWord, answer.RightWord, answer.Phrase).Replace(" ", "-");
     return(string.Format("/{0}/{1} {2} {3} {4} {5} {6}", culture, commonStrings.Best, answer.LeftWord, commonStrings.For,
                          answer.RightWord, commonStrings.Is, answer.Phrase).Replace(" ", "-"));
 }
Пример #6
0
 public static string ConvertAnswerToText(CommonStringsDto commonStrings, AnswerDto answer)
 {
     // best <left word> for <right word> is <phrase>
     return(string.Format("{0} {1} {2} {3} {4} {5}", commonStrings.Best, answer.LeftWord, commonStrings.For,
                          answer.RightWord, commonStrings.Is, answer.Phrase));
 }