public void Init() { var loggerStub = Substitute.For <ILog>(); var strategyFactoryStub = Substitute.For <ITranslationStrategyFactory>(); _strategyStub = Substitute.For <ITranslationStrategy>(); strategyFactoryStub.CreateForMethod(Arg.Is("leetspeak")).Returns(_strategyStub); strategyFactoryStub.CreateForMethod(Arg.Is("NotExistingMethod")).Returns((ITranslationStrategy)null); _service = new TranslationService(strategyFactoryStub, loggerStub); }
/// <summary> /// Transforms the passed text into a friendly url (slug) /// using the provided Separator and the provided Strategy or Strategies. /// </summary> /// <param name="text">The text to be translated.</param> /// <param name="separator">The separator to be used when encoutering whitespaces.</param> /// <param name="strategy">The strategy or strategies to provide extra transformations.</param> /// <returns>The text transformed into a friendly url (slug) using the provided separator and strategies.</returns> /// <exception cref="ArgumentNullException">Thrown when text is null.</exception> public static string ToSlug(this string text, string separator, ITranslationStrategy strategy) { if (text == null) { throw new ArgumentNullException(nameof(text)); } return(text .Split() .Where(t => t.Length != 0) .Select(t => strategy.Translate(t)) .Join(separator)); }
public GoogleTranslationService(ITranslationStrategy translationStrategy) { _translationStrategy = translationStrategy; }
public static string Translate(string phrase, ITranslationStrategy strategy) { return(strategy.Translate(phrase)); }
/// <summary> /// Transforms the passed text into a friendly url (slug) /// using the default Separator "-" and the provided Strategy or Strategies. /// </summary> /// <param name="text">The text to be translated.</param> /// <param name="strategy">The strategy or strategies to provide extra transformations.</param> /// <returns>The text transformed into a friendly url (slug) using the provided separator.</returns> /// <exception cref="ArgumentNullException">Thrown when text is null.</exception> public static string ToSlug(this string text, ITranslationStrategy strategy) => ToSlug(text, DefaultSeparator, strategy);