Added simplecov
[erd-marker.git] / spec / erd_handler / label_spec.rb
1 require 'spec_helper'
2
3 module ErdHandler
4 describe Label do
5 describe '#initialize' do
6 it "should give an error if not given an original string" do
7 # Label.new.should raise_error(ArgumentError)
8 expect {Label.new}.to raise_error(ArgumentError)
9 end
10
11 it "should create a copy of the original as the processed string" do
12 test_label = "Test label"
13 l1 = Label.new test_label
14 l1.original.should == test_label
15 end
16
17 it "should tidy the processed string if asked" do
18 test_label = "testingLabeller string, he_pontificated"
19 l1 = Label.new test_label.dup, true
20 l2 = Label.new test_label.dup
21 l2.split.downcase.stem
22 l1.processed.should == l2.processed
23 end
24 end # initialze
25
26 describe '#original' do
27 it "reports the string it was initialised with" do
28 test_label = "Test label"
29 l1 = Label.new test_label
30 l1.original.should == test_label
31 #l1 = Label.new
32 #l1.original.should == ""
33 end
34 end # original
35
36 describe '#processed' do
37 it "reports the original if no processing has been done" do
38 test_label = "Test label"
39 l1 = Label.new test_label
40 l1.processed.should == [test_label]
41 l1.original.should == test_label
42 end
43 end # processed
44
45 describe '#split' do
46 it "splits the original on the specified regexp" do
47 l1 = Label.new "Test label"
48 l1.split(/ /)
49 l1.processed.should == ["Test", "label"]
50 l1.original.should == "Test label"
51
52 l1 = Label.new "Test_label"
53 l1.split(/_/)
54 l1.processed.should == ["Test", "label"]
55
56 l1 = Label.new "Test label_string"
57 l1.split(/[ _]/)
58 l1.processed.should == ["Test", "label", "string"]
59 end
60
61 it "splits the original on camel case" do
62 l1 = Label.new "TestLabel"
63 l1.split :camel_case => true
64 l1.processed.should == ["Test", "Label"]
65 l1.original.should == "TestLabel"
66
67 l2 = Label.new "testLabel"
68 l2.split :camel_case => true
69 l2.processed.should == ["test", "Label"]
70 l2.original.should == "testLabel"
71 end
72
73 it "doesn't split the original on camel case if asked not to" do
74 l1 = Label.new "TestLabel"
75 l1.split :camel_case => false
76 l1.processed.should == ["TestLabel"]
77 l1.original.should == "TestLabel"
78
79 l2 = Label.new "TestLabel"
80 l2.split :camel_case => nil
81 l2.processed.should == ["TestLabel"]
82 l2.original.should == "TestLabel"
83 end
84
85 it "splits the original on numbers" do
86 l1 = Label.new "Test123Label"
87 l1.split :numbers => true
88 l1.processed.should == ["Test", "123", "Label"]
89 l1.original.should == "Test123Label"
90
91 l2 = Label.new "test1label"
92 l2.split :numbers => true
93 l2.processed.should == ["test", "1", "label"]
94 l2.original.should == "test1label"
95 end
96
97 it "doesn't split the original on numbers if asked not to" do
98 l1 = Label.new "Test123Label"
99 l1.split :numbers => false
100 l1.processed.should == ["Test123Label"]
101 l1.original.should == "Test123Label"
102
103 l2 = Label.new "Test123Label"
104 l2.split :numbers => nil
105 l2.processed.should == ["Test123Label"]
106 l2.original.should == "Test123Label"
107 end
108
109 it "splits the original using a default regexp" do
110 l1 = Label.new "Test label_string\tfred"
111 l1.split
112 l1.processed.should == ["Test", "label", "string", "fred"]
113 end
114
115 it "splits the original on camel case by default" do
116 l1 = Label.new "TestLabel"
117 l1.split
118 l1.processed.should == ["Test", "Label"]
119 l1.original.should == "TestLabel"
120 end
121
122 it "splits the original on numbers by default" do
123 l1 = Label.new "Test123Label"
124 l1.split
125 l1.processed.should == ["Test", "123", "Label"]
126 l1.original.should == "Test123Label"
127 end
128
129 it "splits the original on punctuation, whitespace, camel case, and numbers by default" do
130 l1 = Label.new "TestLabel is_split, 123 he,said456Fred"
131 l1.split
132 l1.processed.should == ["Test", "Label", "is", "split","123", "he", "said", "456", "Fred"]
133 l1.original.should == "TestLabel is_split, 123 he,said456Fred"
134 end
135
136 it "is idempotent" do
137 l1 = Label.new "TestLabel is_split, 123 he,said456Fred"
138 res1 = l1.split.dup
139 res2 = l1.split
140 res1.processed.should == res2.processed
141 l1.original.should == "TestLabel is_split, 123 he,said456Fred"
142 end
143 end # split
144
145 describe "#downcase" do
146 it "downcases all parts of the processed label" do
147 l1 = Label.new "Test label_string"
148 l1.split.downcase
149 l1.processed.should == ["test", "label", "string"]
150 end
151 end # downcase
152
153 describe "#stem" do
154 it "stems all parts of the processed label" do
155 l1 = Label.new "testing labeller string pontificated"
156 l1.split.stem
157 l1.processed.should == ["test", "label", "string", "pontif"]
158 end
159 end # stem
160
161 describe "#tidy" do
162 it "tidies a label" do
163 l1 = Label.new "testingLabeller string, he_pontificated"
164 l2 = Label.new l1.original
165 l1.tidy
166 l2.split.downcase.stem
167 l1.processed.should == l2.processed
168 end
169 end # tidy
170
171 describe "#length" do
172 it "returns the length of the processed label" do
173 l1 = Label.new "testingLabeller string, he_pontificated"
174 l1.tidy
175 l1.length.should == l1.processed.join('').length
176 end
177 end # length
178
179 describe "#levenshtein" do
180 it "calculates the Levenshtein distance of the processed string" do
181 l1 = Label.new "Fred"
182 l1.levenshtein("Fred").should == 0
183 l1.levenshtein("Free").should == 1
184 l1.levenshtein("").should == 4
185 l2 = Label.new ""
186 l2.levenshtein("Free").should == 4
187 l2.levenshtein("").should == 0
188 l3 = Label.new "meilenstein"
189 l3.levenshtein("levenshtein").should == 4
190 l4 = Label.new "testingLabeller string, he_pontificated"
191 l4.tidy.levenshtein("testlabelstringhepontif").should == 0
192 l4.tidy.levenshtein("testlabelXstringhepontif").should == 1
193 end
194
195 it "calculates the Levenshtein distance between Labels" do
196 l1 = Label.new "meilenstein"
197 l2 = Label.new "levenshtein"
198 l1.levenshtein(l2).should == 4
199 end
200 end
201
202 end
203 end