7879c6fafa9d2c84ea1e0e718b90b7295a86300b
[erd-marker.git] / spec / erd_handler / label_spec.rb
1 require 'spec_helper'
2
3 module ErdHandler
4 describe Label do
5 describe '#original' do
6 it "reports the string it was initialised with" do
7 test_label = "Test label"
8 l1 = Label.new test_label
9 l1.original.should == test_label
10 end
11 end # original
12
13 describe '#processed' do
14 it "reports the original if no processing has been done" do
15 test_label = "Test label"
16 l1 = Label.new test_label
17 l1.processed.should == [test_label]
18 l1.original.should == test_label
19 end
20 end # processed
21
22 describe '#split' do
23 it "splits the original on the specified regexp" do
24 l1 = Label.new "Test label"
25 l1.split(/ /)
26 l1.processed.should == ["Test", "label"]
27 l1.original.should == "Test label"
28
29 l1 = Label.new "Test_label"
30 l1.split(/_/)
31 l1.processed.should == ["Test", "label"]
32
33 l1 = Label.new "Test label_string"
34 l1.split(/[ _]/)
35 l1.processed.should == ["Test", "label", "string"]
36 end
37
38 it "splits the original on camel case" do
39 l1 = Label.new "TestLabel"
40 l1.split :camel_case => true
41 l1.processed.should == ["Test", "Label"]
42 l1.original.should == "TestLabel"
43
44 l2 = Label.new "testLabel"
45 l2.split :camel_case => true
46 l2.processed.should == ["test", "Label"]
47 l2.original.should == "testLabel"
48 end
49
50 it "doesn't split the original on camel case if asked not to" do
51 l1 = Label.new "TestLabel"
52 l1.split :camel_case => false
53 l1.processed.should == ["TestLabel"]
54 l1.original.should == "TestLabel"
55
56 l2 = Label.new "TestLabel"
57 l2.split :camel_case => nil
58 l2.processed.should == ["TestLabel"]
59 l2.original.should == "TestLabel"
60 end
61
62 it "splits the original on numbers" do
63 l1 = Label.new "Test123Label"
64 l1.split :numbers => true
65 l1.processed.should == ["Test", "123", "Label"]
66 l1.original.should == "Test123Label"
67
68 l2 = Label.new "test1label"
69 l2.split :numbers => true
70 l2.processed.should == ["test", "1", "label"]
71 l2.original.should == "test1label"
72 end
73
74 it "doesn't split the original on numbers if asked not to" do
75 l1 = Label.new "Test123Label"
76 l1.split :numbers => false
77 l1.processed.should == ["Test123Label"]
78 l1.original.should == "Test123Label"
79
80 l2 = Label.new "Test123Label"
81 l2.split :numbers => nil
82 l2.processed.should == ["Test123Label"]
83 l2.original.should == "Test123Label"
84 end
85
86 it "splits the original using a default regexp" do
87 l1 = Label.new "Test label_string"
88 l1.split
89 l1.processed.should == ["Test", "label", "string"]
90 end
91
92 it "splits the original on camel case by default" do
93 l1 = Label.new "TestLabel"
94 l1.split
95 l1.processed.should == ["Test", "Label"]
96 l1.original.should == "TestLabel"
97 end
98
99 it "splits the original on numbers by default" do
100 l1 = Label.new "Test123Label"
101 l1.split
102 l1.processed.should == ["Test", "123", "Label"]
103 l1.original.should == "Test123Label"
104 end
105
106 it "splits the original on punctuation, whitespace, camel case, and numbers by default" do
107 l1 = Label.new "TestLabel is_split, 123 he,said456Fred"
108 l1.split
109 l1.processed.should == ["Test", "Label", "is", "split","123", "he", "said", "456", "Fred"]
110 l1.original.should == "TestLabel is_split, 123 he,said456Fred"
111 end
112
113 it "is idempotent" do
114 l1 = Label.new "TestLabel is_split, 123 he,said456Fred"
115 res1 = l1.split.dup
116 res2 = l1.split
117 res1.processed.should == res2.processed
118 l1.original.should == "TestLabel is_split, 123 he,said456Fred"
119 end
120 end # split
121
122 describe "#downcase" do
123 it "downcases all parts of the processed label" do
124 l1 = Label.new "Test label_string"
125 l1.split.downcase
126 l1.processed.should == ["test", "label", "string"]
127 end
128 end # downcase
129
130 describe "#stem" do
131 it "stems all parts of the processed label" do
132 l1 = Label.new "testing labeller string pontificated"
133 l1.split.stem
134 l1.processed.should == ["test", "label", "string", "pontif"]
135 end
136 end # stem
137
138 describe "#tidy" do
139 it "tidies a label" do
140 l1 = Label.new "testingLabeller string, he_pontificated"
141 l2 = Label.new l1.original
142 l1.tidy
143 l2.split.downcase.stem
144 l1.processed.should == l2.processed
145
146 end
147 end # tidy
148
149 end
150 end