Merge branch 'master' of git@github.com:elliottcable/rat
[rat.git] / spec / rat / core_ext / string_spec.rb
blob270c5ef7c94e46148bb673b90b05ae74de0e5a4d
1 require File.dirname(__FILE__) + '/../../spec_helper'
2 require 'rat/core_ext/string'
4 describe String do
5   describe "#constantize" do
6     it "should constantize a simple word" do
7       "test".constantize.should == "Test"
8     end
9     
10     it "should constantize a snake_case word" do
11       "snake_case".constantize.should == "SnakeCase"
12     end
13     
14     it "should constantize a simple string" do
15       "a simple string".constantize.should == "ASimpleString"
16     end
17   end
18   
19   describe "#/" do
20     it "should concatenate another string onto this string with File::SEPARATOR" do
21       ("abc" / "def").should == "abc#{File::SEPARATOR}def"
22     end
23   end
24   
25   describe "#length" do
26     it "should be UTF-8 safe" do
27       "▲▼▶◀".length.should == 4
28       "▁▂▃▄▅▆▇█".length.should == 8
29     end
30     
31     it "should be UTF-16 safe" do
32       pending("unsure how to manage this - most editors won't even print these right")
33       "".length.should == 1
34     end
35   end
36   
37   describe "#fixed_split" do
38     it "should work correctly when the last character is the same as the seperator" do
39       '.a.b.c.'.fixed_split('.').should == ['','a','b','c','']
40     end
41   end
42   
43   describe "#split_at" do
44     it "should split a string into groups at a given length" do
45       'abcdefghijkl'.split_at(3).should == ['abc','def','ghi','jkl']
46     end
47   end
48   
49   describe '#indent' do
50     it "should indent a single-line string" do
51       string = 'abcdef'
52       indented = string.indent('  ')
53       indented.should == '  abcdef'
54     end
55   
56     it "should indent a multi-line string using another string" do
57       string = "abcdef\nghijkl"
58       indented = string.indent('- ')
59       indented.should == "- abcdef\n- ghijkl"
60     end
61     
62     it "should indent a multi-line string using a width" do
63       string = "abcdef\nghijkl"
64       indented = string.indent(2)
65       indented.should == "  abcdef\n  ghijkl"
66     end
67   
68     it "should preserve inline whitespace" do
69       string = "begin\n  puts 'whee!'\nend"
70       indented = string.indent('  ')
71       indented.should == "  begin\n    puts 'whee!'\n  end"
72     end
73     
74     it "should preserve prefixed whitespace" do
75       string = "\nI am low"
76       indented = string.indent('  ')
77       indented.should == "  \n  I am low"
78     end
79     
80     it "should preserve postfixed whitespace" do
81       string = "I am high\n"
82       indented = string.indent('  ')
83       indented.should == "  I am high\n  "
84     end
85     
86     it "should raise if incorrectly duck punched" do
87       lambda { "a string".indent(2..4) }.should raise_error ArgumentError,
88         "2..4 is neither string-ish nor numeric-ish"
89     end
90   end
92   describe '#wrap' do
93     it "should split a string as close to the boundary as possible" do
94       string = "This is a string"
95       wrapped = string.wrap(10)
96       wrapped.should == "This is a \nstring"
97     end
98     
99     it "should split a word if no natural split is available" do
100       string = "Pneumonoultramicroscopicsilicovolcanokoniosis"
101       wrapped = string.wrap(15)
102       wrapped.should == "Pneumonoultrami\ncroscopicsilico\nvolcanokoniosis"
103     end
104     
105     it "should prefer to split a word if the nearest natural split is too far from the boundary" do
106       string = "This was pneumonoultramicroscopicsilicovolcanokoniosis"
107       wrapped = string.wrap(15)
108       wrapped.should == "This was pneumo\nnoultramicrosco\npicsilicovolcan\nokoniosis"
109     end
110     
111     it "should correctly handle line returns" do
112       string = "\n  \n  This is a string\n  "
113       wrapped = string.wrap(10)
114       wrapped.should == "\n  \n  This is \na string\n  "
115     end
116   end