Module: ShellTest::StringMethods
- Included in:
- FileMethods, ShellMethods
- Defined in:
- lib/shell_test/string_methods.rb
Instance Method Summary collapse
-
#_assert_str_equal(a, b, msg = nil) ⇒ Object
Same as assert_str_equal but does not outdent.
-
#_assert_str_match(a, b, msg = nil) ⇒ Object
Same as assert_str_match but does not outdent.
-
#assert_str_equal(a, b, msg = nil, &block) ⇒ Object
Asserts whether or not the a and b strings are equal, with a more readable output than assert_equal for large strings (especially large strings with significant whitespace).
-
#assert_str_match(a, b, msg = nil, &block) ⇒ Object
Asserts whether or not b is like a (which should be a Regexp), and provides a more readable output in the case of a failure as compared with assert_match.
-
#expand_ctrl_chars(str) ⇒ Object
Expands non-printable characters (ie control characters) in str to their common print equivalents.
-
#indent(str, indent = ' ') ⇒ Object
Indents each line of str as specified.
-
#outdent(str) ⇒ Object
Strips indentation off of the input string.
-
#whitespace_escape(str) ⇒ Object
Escapes non-printable characters into readable text.
Instance Method Details
#_assert_str_equal(a, b, msg = nil) ⇒ Object
Same as assert_str_equal but does not outdent.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/shell_test/string_methods.rb', line 11 def _assert_str_equal(a, b, msg=nil) if a == b assert true else flunk block_given? ? yield(a, b) : %Q{ ========================================================= #{msg} -------------------- expected output -------------------- #{whitespace_escape(a)} ------------------------ but was ------------------------ #{whitespace_escape(b)} ========================================================= } end end |
#_assert_str_match(a, b, msg = nil) ⇒ Object
Same as assert_str_match but does not outdent.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/shell_test/string_methods.rb', line 38 def _assert_str_match(a, b, msg=nil) if a.kind_of?(String) a = RegexpEscape.new(a) end if b =~ a assert true else flunk block_given? ? yield(a,b) : %Q{ ========================================================= #{msg} ----------------- expected output like ------------------ #{whitespace_escape(a)} ------------------------ but was ------------------------ #{whitespace_escape(b)} ========================================================= } end end |
#assert_str_equal(a, b, msg = nil, &block) ⇒ Object
Asserts whether or not the a and b strings are equal, with a more readable output than assert_equal for large strings (especially large strings with significant whitespace).
6 7 8 |
# File 'lib/shell_test/string_methods.rb', line 6 def assert_str_equal(a, b, msg=nil, &block) _assert_str_equal outdent(a), b, msg, &block end |
#assert_str_match(a, b, msg = nil, &block) ⇒ Object
Asserts whether or not b is like a (which should be a Regexp), and provides a more readable output in the case of a failure as compared with assert_match.
If a is a string then it is turned into a RegexpEscape.
32 33 34 35 |
# File 'lib/shell_test/string_methods.rb', line 32 def assert_str_match(a, b, msg=nil, &block) a = outdent(a) if a.kind_of?(String) _assert_str_match a, b, msg, &block end |
#expand_ctrl_chars(str) ⇒ Object
Expands non-printable characters (ie control characters) in str to their common print equivalents. Specifically:
ctrl char before after
null "ab\0c" "abc"
bell "ab\ac" "abc"
backspace "ab\bc" "ac"
horizonal tab "ab\tc" "ab\tc"
line feed "ab\nc" "ab\nc"
form feed "ab\fc" "ab\n c"
carraige return "ab\rc" "c"
98 99 100 101 102 103 |
# File 'lib/shell_test/string_methods.rb', line 98 def (str) str = str.gsub(/^.*?\r/, '') str.gsub!(/(\A#{"\b"}|.#{"\b"}|#{"\a"}|#{"\0"})/m, '') str.gsub!(/(^.*?)\f/) { "#{$1}\n#{' ' * $1.length}" } str end |
#indent(str, indent = ' ') ⇒ Object
Indents each line of str as specified.
59 60 61 62 63 |
# File 'lib/shell_test/string_methods.rb', line 59 def indent(str, indent=' ') str.split("\n").collect do |frag| "#{indent}#{frag}" end.join("\n") end |
#outdent(str) ⇒ Object
Strips indentation off of the input string.
66 67 68 |
# File 'lib/shell_test/string_methods.rb', line 66 def outdent(str) str =~ /\A(?:\s*?\n)( *)(.*)\z/m ? $2.gsub!(/^ {0,#{$1.length}}/, '') : str end |
#whitespace_escape(str) ⇒ Object
Escapes non-printable characters into readable text.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/shell_test/string_methods.rb', line 71 def whitespace_escape(str) str = str.to_s.gsub(/\s/) do |match| case match when "\n" then "\\n\n" when "\t" then "\\t" when "\r" then "\\r" when "\f" then "\\f" else match end end str.gsub!("\b", "\\b") str.gsub!("\a", "\\a") str.split("\0").join('\\0') end |