module ShellTest::StringMethods

Public Instance Methods

_assert_str_equal(a, b, msg=nil) { |a, b| ... } click to toggle source

Same as assert_str_equal but does not outdent.

   # File lib/shell_test/string_methods.rb
11     def _assert_str_equal(a, b, msg=nil)
12       if a == b
13         assert true
14       else
15         flunk block_given? ? yield(a, b) : %Q{
16 =========================================================
17 #{msg}
18 -------------------- expected output --------------------
19 #{whitespace_escape(a)}
20 ------------------------ but was ------------------------
21 #{whitespace_escape(b)}
22 =========================================================
23 }
24       end
25     end
_assert_str_match(a, b, msg=nil) { |a,b| ... } click to toggle source

Same as assert_str_match but does not outdent.

   # File lib/shell_test/string_methods.rb
38     def _assert_str_match(a, b, msg=nil)
39       if a.kind_of?(String)
40         a = RegexpEscape.new(a)
41       end
42 
43       if b =~ a
44         assert true
45       else
46         flunk block_given? ? yield(a,b) : %Q{
47 =========================================================
48 #{msg}
49 ----------------- expected output like ------------------
50 #{whitespace_escape(a)}
51 ------------------------ but was ------------------------
52 #{whitespace_escape(b)}
53 =========================================================
54 }
55       end
56     end
assert_str_equal(a, b, msg=nil, &block) click to toggle source

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).

  # File lib/shell_test/string_methods.rb
6 def assert_str_equal(a, b, msg=nil, &block)
7   _assert_str_equal outdent(a), b, msg, &block
8 end
assert_str_match(a, b, msg=nil, &block) click to toggle source

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.

   # File lib/shell_test/string_methods.rb
32 def assert_str_match(a, b, msg=nil, &block)
33   a = outdent(a) if a.kind_of?(String)
34   _assert_str_match a, b, msg, &block
35 end
expand_ctrl_chars(str) click to toggle source

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"
    # File lib/shell_test/string_methods.rb
 98 def expand_ctrl_chars(str)
 99   str = str.gsub(/^.*?\r/, '')
100   str.gsub!(/(\A#{"\b"}|.#{"\b"}|#{"\a"}|#{"\0"})/m, '')
101   str.gsub!(/(^.*?)\f/) { "#{$1}\n#{' ' * $1.length}" }
102   str
103 end
indent(str, indent=' ') click to toggle source

Indents each line of str as specified.

   # File lib/shell_test/string_methods.rb
59 def indent(str, indent='  ')
60   str.split("\n").collect do |frag|
61     "#{indent}#{frag}"
62   end.join("\n")
63 end
outdent(str) click to toggle source

Strips indentation off of the input string.

   # File lib/shell_test/string_methods.rb
66 def outdent(str)
67   str =~ /\A(?:\s*?\n)( *)(.*)\z/m ? $2.gsub!(/^ {0,#{$1.length}}/, '') : str
68 end
whitespace_escape(str) click to toggle source

Escapes non-printable characters into readable text.

   # File lib/shell_test/string_methods.rb
71 def whitespace_escape(str)
72   str = str.to_s.gsub(/\s/) do |match|
73     case match
74     when "\n" then "\\n\n"
75     when "\t" then "\\t"
76     when "\r" then "\\r"
77     when "\f" then "\\f"
78     else match
79     end
80   end
81   str.gsub!("\b", "\\b")
82   str.gsub!("\a", "\\a")
83   str.split("\0").join('\\0')
84 end