Debian Codegraph @

closure-library/closure/goog/string/stringbuffer_test.js

raw
1 // Copyright 2006 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.string.StringBufferTest');
16 goog.setTestOnly('goog.string.StringBufferTest');
17
18 goog.require('goog.string.StringBuffer');
19 goog.require('goog.testing.jsunit');
20
21 function testStringBuffer() {
22 var sb = new goog.string.StringBuffer();
23 sb.append('Four score');
24 sb.append(' ');
25 sb.append('and seven years ago.');
26 assertEquals('Test 1', 'Four score and seven years ago.', sb.toString());
27
28 sb.clear();
29 assertEquals('Test 2', '', sb.toString());
30
31 sb = new goog.string.StringBuffer('Four score ');
32 sb.append('and seven years ago.');
33 assertEquals('Test 3', 'Four score and seven years ago.', sb.toString());
34
35 // can pass in non-Strings?
36 sb = new goog.string.StringBuffer(1);
37 sb.append(2);
38 assertEquals('Test 4', '12', sb.toString());
39 }
40
41
42 function testStringBufferSet() {
43 var sb = new goog.string.StringBuffer('foo');
44 sb.set('bar');
45 assertEquals('Test 1', 'bar', sb.toString());
46 }
47
48
49 function testStringBufferMultiAppend() {
50 var sb = new goog.string.StringBuffer('hey', 'foo');
51 sb.append('bar', 'baz');
52 assertEquals('Test 1', 'heyfoobarbaz', sb.toString());
53
54 sb = new goog.string.StringBuffer();
55 sb.append(1, 2);
56 // should not add up to '3'
57 assertEquals('Test 2', '12', sb.toString());
58 }
59
60
61 function testStringBufferToString() {
62 var sb = new goog.string.StringBuffer('foo', 'bar');
63 assertEquals('Test 1', 'foobar', sb.toString());
64 }
65
66
67 function testStringBufferWithFalseFirstArgument() {
68 var sb = new goog.string.StringBuffer(0, 'more');
69 assertEquals('Test 1', '0more', sb.toString());
70
71 sb = new goog.string.StringBuffer(false, 'more');
72 assertEquals('Test 2', 'falsemore', sb.toString());
73
74 sb = new goog.string.StringBuffer('', 'more');
75 assertEquals('Test 3', 'more', sb.toString());
76
77 sb = new goog.string.StringBuffer(null, 'more');
78 assertEquals('Test 4', '', sb.toString());
79
80 sb = new goog.string.StringBuffer(undefined, 'more');
81 assertEquals('Test 5', '', sb.toString());
82 }
83
84
85 function testStringBufferGetLength() {
86 var sb = new goog.string.StringBuffer();
87 assertEquals(0, sb.getLength());
88
89 sb.append('foo');
90 assertEquals(3, sb.getLength());
91
92 sb.append('baroo');
93 assertEquals(8, sb.getLength());
94
95 sb.clear();
96 assertEquals(0, sb.getLength());
97 }