[rspec-devel] Stories in other languages

David Chelimsky dchelimsky at gmail.com
Fri Feb 1 16:40:05 EST 2008


Would you please submit this to our tracker at http://rspec.lighthouseapp.com?

Gracias,
David

On Feb 1, 2008 2:43 PM, Fernando García Samblas
<fernando.garcia at the-cocktail.com> wrote:
>
> Hello,
>
> I've just subscribed to the RSpec lists and first of all I'd like to
> thank you all for such a great work. I'm really having a great time
> developing with RSpec.
>
> I think it would be nice for developers with non-english clients to open
> the possibility for writing RSpec Stories in other languages.
>
> Currently the story template words ("Story", "Scenario", "Given", etc.)
> are hard coded, but it shouldn't be difficult to manage them as a
> configurable hash.
>
> I don't have a good knowledge of the code behind RSpec but I've managed
> to modify 1.1.3 (r3281) code to achieve that goal. Now I can run my
> brand-new spanish stories!
>
> I send the differences with the 3281 revision attached. Before I run the
> stories I set the value of the words:
>
>    Spec::Runner.configure do |config|
>      config.with_story_template_words({
>        :story          => "Relato",
>        :scenario       => 'Escenario',
>        :given          => 'Dado',
>        :given_scenario => 'DadoElEscenario',
>        :when           => 'Cuando',
>        :then           => 'Entonces',
>        :and            => 'Y'
>      })
>    end
>
> I'm sure it's not the best way to implement the functionality, but I
> just want to let you know the desire and a (very) simple way to get it
> (partially, I only use PlainText output) done.
>
> Thanks for listening ;)
>
> Cheers!
> Nando
>
> --
> Fernando García Samblas
> fernando.garcia at the-cocktail.com
>
> The Cocktail
> C/ Salamanca 17
> 28020 Madrid
> +34 91 567 06 05
>
>
> Index: lib/spec/story/runner/story_parser.rb
> ===================================================================
> --- lib/spec/story/runner/story_parser.rb       (revision 7)
> +++ lib/spec/story/runner/story_parser.rb       (working copy)
> @@ -26,13 +26,20 @@
>          def process_line(line)
>            line.strip!
>            case line
> -          when /^Story: /           then @state.story(line)
> -          when /^Scenario: /        then @state.scenario(line)
> -          when /^Given:? /          then @state.given(line)
> -          when /^GivenScenario:? /  then @state.given_scenario(line)
> -          when /^When:? /           then @state.event(line)
> -          when /^Then:? /           then @state.outcome(line)
> -          when /^And:? /            then @state.one_more_of_the_same(line)
> +          when /^#{Spec::Runner.configuration.story_template_words[:story]}: /:
> +            @state.story(line)
> +          when /^#{Spec::Runner.configuration.story_template_words[:scenario]}: /:
> +            @state.scenario(line)
> +          when /^#{Spec::Runner.configuration.story_template_words[:given]}:? /:
> +            @state.given(line)
> +          when /^#{Spec::Runner.configuration.story_template_words[:given_scenario]}:? /:
> +            @state.given_scenario(line)
> +          when /^#{Spec::Runner.configuration.story_template_words[:when]}:? /:
> +            @state.event(line)
> +          when /^#{Spec::Runner.configuration.story_template_words[:then]}:? /:
> +            @state.outcome(line)
> +          when /^#{Spec::Runner.configuration.story_template_words[:and]}:? /:
> +            @state.one_more_of_the_same(line)
>            else                           @state.other(line)
>            end
>          end
> @@ -48,13 +55,17 @@
>
>          def create_story()
>            unless @current_story_lines.empty?
> -            @story_mediator.create_story(@current_story_lines[0].gsub("Story: ",""), @current_story_lines[1..-1].join("\n"))
> +            @story_mediator.create_story(
> +              @current_story_lines[0].gsub(
> +                "#{Spec::Runner.configuration.story_template_words[:story]}: ",""),
> +              @current_story_lines[1..-1].join("\n"))
>              @current_story_lines.clear
>            end
>          end
>
>          def create_scenario(title)
> -          @story_mediator.create_scenario(title.gsub("Scenario: ",""))
> +          @story_mediator.create_scenario(title.gsub(
> +              "#{Spec::Runner.configuration.story_template_words[:scenario]}: ",""))
>          end
>
>          def create_given(name)
> @@ -126,8 +137,9 @@
>            def remove_tag_from(tag, line)
>              tokens = line.split
>              # validation of tag can go here
> -            tokens[0].downcase.match(/#{tag.to_s}:?/) ?
> -                                (tokens[1..-1].join(' ')) : line
> +            tokens[0].downcase.match(
> +              /#{Spec::Runner.configuration.story_template_words[tag].downcase}:?/) ?
> +              (tokens[1..-1].join(' ')) : line
>            end
>
>            def eof
> Index: lib/spec/example/configuration.rb
> ===================================================================
> --- lib/spec/example/configuration.rb   (revision 7)
> +++ lib/spec/example/configuration.rb   (working copy)
> @@ -124,7 +124,56 @@
>          )
>          example_group.append_after(scope, &proc)
>        end
> +
>
> +      # Let you write your stories in other languages. Example:
> +      #
> +      #   Spec::Runner.configure do |config|
> +      #     config.with_story_template_words({
> +      #       :story          => "Relato",
> +      #       :scenario       => 'Escenario',
> +      #       :given          => 'Dado',
> +      #       :given_scenario => 'DadoElEscenario',
> +      #       :when           => 'Cuando',
> +      #       :then           => 'Entonces',
> +      #       :and            => 'Y'
> +      #     })
> +      #   end
> +      #
> +      # And then write your story in spanish, for example:
> +      #
> +      #   Relato: La portada de Mi Sitio en La Red
> +      #
> +      #     Como un usuario de La Red
> +      #     Quiero ver la portada de Mi Sitio
> +      #     Para ver que contenidos y servicios me ofrece
> +      #
> +      #     Escenario: usuario sin sesión
> +      #       Dado un usuario sin sesión iniciada
> +      #
> +      #       Cuando visita http://www.example.com/
> +      #
> +      #       Entonces puede ver el formulario para iniciar sesión
> +      #       Y el enlace para crear su cuenta
> +      #
> +
> +      STORY_TEMPLATE_WORDS = {
> +        :story          => 'Story',
> +        :scenario       => 'Scenario',
> +        :given          => 'Given',
> +        :given_scenario => 'GivenScenario',
> +        :when           => 'When',
> +        :then           => 'Then',
> +        :and            => 'And'
> +      }
> +      def with_story_template_words(template_words)
> +        @template_words = STORY_TEMPLATE_WORDS.merge(template_words)
> +      end
> +
> +      def story_template_words
> +        @template_words ||= STORY_TEMPLATE_WORDS
> +      end
> +
>      private
>
>        def scope_and_options(*args)
> Index: lib/spec/runner/formatter/story/plain_text_formatter.rb
> ===================================================================
> --- lib/spec/runner/formatter/story/plain_text_formatter.rb     (revision 7)
> +++ lib/spec/runner/formatter/story/plain_text_formatter.rb     (working copy)
> @@ -21,7 +21,7 @@
>
>            def story_started(title, narrative)
>              @current_story_title = title
> -            @output.puts "Story: #{title}\n\n"
> +            @output.puts "#{Spec::Runner.configuration.story_template_words[:story]}: #{title}\n\n"
>              narrative.each_line do |line|
>                @output.print "  "
>                @output.print line
> @@ -36,7 +36,7 @@
>            def scenario_started(story_title, scenario_name)
>              @current_scenario_name = scenario_name
>              @scenario_already_failed = false
> -            @output.print "\n\n  Scenario: #{scenario_name}"
> +            @output.print "\n\n  #{Spec::Runner.configuration.story_template_words[:scenario]}: #{scenario_name}"
>              @scenario_ok = true
>            end
>
> @@ -110,9 +110,9 @@
>              desc_string = description.step_name
>              arg_regexp = description.arg_regexp
>              text = if(type == @previous_type)
> -              "\n    And "
> +              "\n    #{Spec::Runner.configuration.story_template_words[:and]} "
>              else
> -              "\n\n    #{type.to_s.capitalize} "
> +              "\n\n    #{Spec::Runner.configuration.story_template_words[type]} "
>              end
>              i = -1
>              text << desc_string.gsub(arg_regexp) { |param| args[i+=1] }
>
> _______________________________________________
> rspec-devel mailing list
> rspec-devel at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-devel
>


More information about the rspec-devel mailing list