stringbuffer_test.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. goog.provide('goog.string.StringBufferTest');
  15. goog.setTestOnly('goog.string.StringBufferTest');
  16. goog.require('goog.string.StringBuffer');
  17. goog.require('goog.testing.jsunit');
  18. function testStringBuffer() {
  19. var sb = new goog.string.StringBuffer();
  20. sb.append('Four score');
  21. sb.append(' ');
  22. sb.append('and seven years ago.');
  23. assertEquals('Test 1', 'Four score and seven years ago.', sb.toString());
  24. sb.clear();
  25. assertEquals('Test 2', '', sb.toString());
  26. sb = new goog.string.StringBuffer('Four score ');
  27. sb.append('and seven years ago.');
  28. assertEquals('Test 3', 'Four score and seven years ago.', sb.toString());
  29. // can pass in non-Strings?
  30. sb = new goog.string.StringBuffer(1);
  31. sb.append(2);
  32. assertEquals('Test 4', '12', sb.toString());
  33. }
  34. function testStringBufferSet() {
  35. var sb = new goog.string.StringBuffer('foo');
  36. sb.set('bar');
  37. assertEquals('Test 1', 'bar', sb.toString());
  38. }
  39. function testStringBufferMultiAppend() {
  40. var sb = new goog.string.StringBuffer('hey', 'foo');
  41. sb.append('bar', 'baz');
  42. assertEquals('Test 1', 'heyfoobarbaz', sb.toString());
  43. sb = new goog.string.StringBuffer();
  44. sb.append(1, 2);
  45. // should not add up to '3'
  46. assertEquals('Test 2', '12', sb.toString());
  47. }
  48. function testStringBufferToString() {
  49. var sb = new goog.string.StringBuffer('foo', 'bar');
  50. assertEquals('Test 1', 'foobar', sb.toString());
  51. }
  52. function testStringBufferWithFalseFirstArgument() {
  53. var sb = new goog.string.StringBuffer(0, 'more');
  54. assertEquals('Test 1', '0more', sb.toString());
  55. sb = new goog.string.StringBuffer(false, 'more');
  56. assertEquals('Test 2', 'falsemore', sb.toString());
  57. sb = new goog.string.StringBuffer('', 'more');
  58. assertEquals('Test 3', 'more', sb.toString());
  59. sb = new goog.string.StringBuffer(null, 'more');
  60. assertEquals('Test 4', '', sb.toString());
  61. sb = new goog.string.StringBuffer(undefined, 'more');
  62. assertEquals('Test 5', '', sb.toString());
  63. }
  64. function testStringBufferGetLength() {
  65. var sb = new goog.string.StringBuffer();
  66. assertEquals(0, sb.getLength());
  67. sb.append('foo');
  68. assertEquals(3, sb.getLength());
  69. sb.append('baroo');
  70. assertEquals(8, sb.getLength());
  71. sb.clear();
  72. assertEquals(0, sb.getLength());
  73. }