diff options
author | Xavier Noria <fxn@hashref.com> | 2012-08-07 16:55:28 +0200 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2012-08-07 16:57:28 +0200 |
commit | 2f58795e783150f2e1b1f6c64e305703f0061129 (patch) | |
tree | caa757a14597b9d6a65104bc5d85c009f9160820 /activesupport/test/core_ext/string_ext_test.rb | |
parent | 9cd1f697f1b4dded0f18e13c7ae12c7bd2dab1c0 (diff) | |
download | rails-2f58795e783150f2e1b1f6c64e305703f0061129.tar.gz rails-2f58795e783150f2e1b1f6c64e305703f0061129.tar.bz2 rails-2f58795e783150f2e1b1f6c64e305703f0061129.zip |
defines String#indent [closes #7263] [Xavier Noria & Ace Suares]
Diffstat (limited to 'activesupport/test/core_ext/string_ext_test.rb')
-rw-r--r-- | activesupport/test/core_ext/string_ext_test.rb | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index 3b08ebe35f..dc5ae0eafc 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -9,6 +9,7 @@ require 'active_support/core_ext/string' require 'active_support/time' require 'active_support/core_ext/string/strip' require 'active_support/core_ext/string/output_safety' +require 'active_support/core_ext/string/indent' module Ace module Base @@ -521,3 +522,58 @@ class StringExcludeTest < ActiveSupport::TestCase assert_equal true, 'foo'.exclude?('p') end end + +class StringIndentTest < ActiveSupport::TestCase + test 'does not indent strings that only contain newlines (edge cases)' do + ['', "\n", "\n" * 7].each do |str| + assert_nil str.indent!(8) + assert_equal str, str.indent(8) + assert_equal str, str.indent(1, "\t") + end + end + + test "by default, indents with spaces if the existing indentation uses them" do + assert_equal " foo\n bar", "foo\n bar".indent(4) + end + + test "by default, indents with tabs if the existing indentation uses them" do + assert_equal "\tfoo\n\t\t\bar", "foo\n\t\bar".indent(1) + end + + test "by default, indents with spaces as a fallback if there is no indentation" do + assert_equal " foo\n bar\n baz", "foo\nbar\nbaz".indent(3) + end + + # Nothing is said about existing indentation that mixes spaces and tabs, so + # there is nothing to test. + + test 'uses the indent char if passed' do + assert_equal <<EXPECTED, <<ACTUAL.indent(4, '.') +.... def some_method(x, y) +.... some_code +.... end +EXPECTED + def some_method(x, y) + some_code + end +ACTUAL + + assert_equal <<EXPECTED, <<ACTUAL.indent(2, ' ') + def some_method(x, y) + some_code + end +EXPECTED + def some_method(x, y) + some_code + end +ACTUAL + end + + test "does not indent blank lines by default" do + assert_equal " foo\n\n bar", "foo\n\nbar".indent(1) + end + + test 'indents blank lines if told so' do + assert_equal " foo\n \n bar", "foo\n\nbar".indent(1, nil, true) + end +end |