1 describe("Ext.String", function() {
3 describe("ellipsis", function() {
4 var ellipsis = Ext.String.ellipsis,
5 shortString = "A short string",
6 longString = "A somewhat longer string";
8 it("should keep short strings intact", function() {
9 expect(ellipsis(shortString, 100)).toEqual(shortString);
12 it("should truncate a longer string", function() {
13 expect(ellipsis(longString, 10)).toEqual("A somew...");
16 describe("word break", function() {
17 var longStringWithDot = "www.sencha.com",
18 longStringWithExclamationMark = "Yeah!Yeah!Yeah!",
19 longStringWithQuestionMark = "Who?When?What?";
21 it("should find a word break on ' '", function() {
22 expect(ellipsis(longString, 10, true)).toEqual("A...");
25 it("should be able to break on '.'", function() {
26 expect(ellipsis(longStringWithDot, 9, true)).toEqual("www...");
29 it("should be able to break on '!'", function() {
30 expect(ellipsis(longStringWithExclamationMark, 9, true)).toEqual("Yeah...");
33 it("should be able to break on '?'", function() {
34 expect(ellipsis(longStringWithQuestionMark, 8, true)).toEqual("Who...");
39 describe("escapeRegex", function() {
41 escapeRegex = Ext.String.escapeRegex;
43 it("should escape minus", function() {
46 expect(escapeRegex(str)).toEqual("12 \\- 175");
49 it("should escape dot", function() {
50 str = "Brian is in the kitchen.";
52 expect(escapeRegex(str)).toEqual("Brian is in the kitchen\\.");
55 it("should escape asterisk", function() {
58 expect(escapeRegex(str)).toEqual("12 \\* 175");
61 it("should escape plus", function() {
64 expect(escapeRegex(str)).toEqual("12 \\+ 175");
67 it("should escape question mark", function() {
70 expect(escapeRegex(str)).toEqual("What else \\?");
73 it("should escape caret", function() {
76 expect(escapeRegex(str)).toEqual("\\^\\^");
79 it("should escape dollar", function() {
82 expect(escapeRegex(str)).toEqual("500\\$");
85 it("should escape open brace", function() {
86 str = "something{stupid";
88 expect(escapeRegex(str)).toEqual("something\\{stupid");
91 it("should escape close brace", function() {
92 str = "something}stupid";
94 expect(escapeRegex(str)).toEqual("something\\}stupid");
97 it("should escape open bracket", function() {
98 str = "something[stupid";
100 expect(escapeRegex(str)).toEqual("something\\[stupid");
103 it("should escape close bracket", function() {
104 str = "something]stupid";
106 expect(escapeRegex(str)).toEqual("something\\]stupid");
109 it("should escape open parenthesis", function() {
110 str = "something(stupid";
112 expect(escapeRegex(str)).toEqual("something\\(stupid");
115 it("should escape close parenthesis", function() {
116 str = "something)stupid";
118 expect(escapeRegex(str)).toEqual("something\\)stupid");
121 it("should escape vertival bar", function() {
122 str = "something|stupid";
124 expect(escapeRegex(str)).toEqual("something\\|stupid");
127 it("should escape forward slash", function() {
128 str = "something/stupid";
130 expect(escapeRegex(str)).toEqual("something\\/stupid");
133 it("should escape backslash", function() {
134 str = "something\\stupid";
136 expect(escapeRegex(str)).toEqual("something\\\\stupid");
140 describe("htmlEncode", function() {
141 var htmlEncode = Ext.String.htmlEncode,
144 it("should replace ampersands", function() {
145 str = "Fish & Chips";
147 expect(htmlEncode(str)).toEqual("Fish & Chips");
150 it("should replace less than", function() {
151 str = "Fish > Chips";
153 expect(htmlEncode(str)).toEqual("Fish > Chips");
156 it("should replace greater than", function() {
157 str = "Fish < Chips";
159 expect(htmlEncode(str)).toEqual("Fish < Chips");
162 it("should replace double quote", function() {
163 str = 'Fish " Chips';
165 expect(htmlEncode(str)).toEqual("Fish " Chips");
169 describe("htmlDecode", function() {
170 var htmlDecode = Ext.String.htmlDecode,
173 it("should replace ampersands", function() {
174 str = "Fish & Chips";
176 expect(htmlDecode(str)).toEqual("Fish & Chips");
179 it("should replace less than", function() {
180 str = "Fish > Chips";
182 expect(htmlDecode(str)).toEqual("Fish > Chips");
185 it("should replace greater than", function() {
186 str = "Fish < Chips";
188 expect(htmlDecode(str)).toEqual("Fish < Chips");
191 it("should replace double quote", function() {
192 str = 'Fish " Chips';
194 expect(htmlDecode(str)).toEqual('Fish " Chips');
198 describe("escaping", function() {
199 var escape = Ext.String.escape;
201 it("should leave an empty string alone", function() {
202 expect(escape('')).toEqual('');
205 it("should leave a non-empty string without escapable characters alone", function() {
206 expect(escape('Ed')).toEqual('Ed');
209 it("should correctly escape a double backslash", function() {
210 expect(escape("\\")).toEqual("\\\\");
213 it("should correctly escape a single backslash", function() {
214 expect(escape('\'')).toEqual('\\\'');
217 it("should correctly escape a mixture of escape and non-escape characters", function() {
218 expect(escape('\'foo\\')).toEqual('\\\'foo\\\\');
222 describe("formatting", function() {
223 var format = Ext.String.format;
225 it("should leave a string without format parameters alone", function() {
226 expect(format('Ed')).toEqual('Ed');
229 it("should ignore arguments that don't map to format params", function() {
230 expect(format("{0} person", 1, 123)).toEqual("1 person");
233 it("should accept several format parameters", function() {
234 expect(format("{0} person {1}", 1, 'came')).toEqual('1 person came');
238 describe("leftPad", function() {
239 var leftPad = Ext.String.leftPad;
241 it("should pad the left side of an empty string", function() {
242 expect(leftPad("", 5)).toEqual(" ");
245 it("should pad the left side of a non-empty string", function() {
246 expect(leftPad("Ed", 5)).toEqual(" Ed");
249 it("should not pad a string where the character count already exceeds the pad count", function() {
250 expect(leftPad("Abraham", 5)).toEqual("Abraham");
253 it("should allow a custom padding character", function() {
254 expect(leftPad("Ed", 5, "0")).toEqual("000Ed");
258 describe("when toggling between two values", function() {
259 var toggle = Ext.String.toggle;
261 it("should use the first toggle value if the string is not already one of the toggle values", function() {
262 expect(toggle("Aaron", "Ed", "Abe")).toEqual("Ed");
265 it("should toggle to the second toggle value if the string is currently the first", function() {
266 expect(toggle("Ed", "Ed", "Abe")).toEqual("Abe");
269 it("should toggle to the first toggle value if the string is currently the second", function() {
270 expect(toggle("Abe", "Ed", "Abe")).toEqual("Ed");
274 describe("trimming", function() {
275 var trim = Ext.String.trim;
277 it("should not modify an empty string", function() {
278 expect(trim("")).toEqual("");
281 it("should not modify a string with no whitespace", function() {
282 expect(trim("Abe")).toEqual("Abe");
285 it("should trim a whitespace-only string", function() {
286 expect(trim(" ")).toEqual("");
289 it("should trim leading whitespace", function() {
290 expect(trim(" Ed")).toEqual("Ed");
293 it("should trim trailing whitespace", function() {
294 expect(trim("Ed ")).toEqual("Ed");
297 it("should trim leading and trailing whitespace", function() {
298 expect(trim(" Ed ")).toEqual("Ed");
301 it("should not trim whitespace between words", function() {
302 expect(trim("Fish and chips")).toEqual("Fish and chips");
303 expect(trim(" Fish and chips ")).toEqual("Fish and chips");
306 it("should trim tabs", function() {
307 expect(trim("\tEd")).toEqual("Ed");
310 it("should trim a mixture of tabs and whitespace", function() {
311 expect(trim("\tEd ")).toEqual("Ed");
315 describe("urlAppend", function(){
316 var urlAppend = Ext.String.urlAppend;
318 it("should leave the string untouched if the second argument is empty", function(){
319 expect(urlAppend('sencha.com')).toEqual('sencha.com');
322 it("should append a ? if one doesn't exist", function(){
323 expect(urlAppend('sencha.com', 'foo=bar')).toEqual('sencha.com?foo=bar');
326 it("should append any new values with & if a ? exists", function(){
327 expect(urlAppend('sencha.com?x=y', 'foo=bar')).toEqual('sencha.com?x=y&foo=bar');
331 describe("capitalize", function(){
332 var capitalize = Ext.String.capitalize;
334 it("should handle an empty string", function(){
335 expect(capitalize('')).toEqual('');
338 it("should capitalize the first letter of the string", function(){
339 expect(capitalize('open')).toEqual('Open');
342 it("should leave the first letter capitalized if it is already capitalized", function(){
343 expect(capitalize('Closed')).toEqual('Closed');
346 it("should capitalize a single letter", function(){
347 expect(capitalize('a')).toEqual('A');
350 it("should not capitalize even when spaces are included", function(){
351 expect(capitalize('this is a sentence')).toEqual('This is a sentence');