Skip to content

Commit

Permalink
Add some tests and fix some stuff (w.i.p.)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kami committed May 16, 2011
1 parent d7c5d0a commit f28339e
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/terminal.js
Expand Up @@ -61,7 +61,7 @@ function getStylesLength(string) {
style = styleNames[i];
if (string.indexOf(sprintf('[%s]', style)) !== -1 &&
string.indexOf(sprintf('[/%s]', style)) !== -1) {
STYLESLength += (style.length + 5);
STYLESLength += ((style.length * 2) + 5);
}
}

Expand Down Expand Up @@ -387,6 +387,7 @@ function puts() {
exports.USE_ANSI_CODES = USE_ANSI_CODES;
exports.PRINT_FUNC = PRINT_FUNC;

exports.getStylesLength = getStylesLength;
exports.lpad = lpad;
exports.rpad = rpad;
exports.printTable = printTable;
Expand Down
108 changes: 108 additions & 0 deletions tests/test-terminal.js
@@ -0,0 +1,108 @@
/*
* Licensed to Cloudkick, Inc ('Cloudkick') under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* Cloudkick licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

var terminal = require('../index');

exports['test_getStylesLength'] = function(test, assert) {
var i, string, styleLength;
var strings = [
'foobar',
'[blue]foo[/blue]',
'[bold][blue]foobar[/blue] moo[/bold]'
];
var styleLengths = [
0,
13,
26
];

for (i = 0; i < strings.length; i++) {
string = strings[i];
styleLength = styleLengths[i];

assert.equal(terminal.getStylesLength(string), styleLength);
}

test.finish();
};

exports['test_lpad_and_rpad'] = function(test, assert) {
var i, string, padWidth;
var strings = [
'foo',
'foobar',
'bar x',
];
var padWidths = [
20,
10,
20,
30
];

for (i = 0; i < strings.length; i++) {
string = strings[i];
padWidth = padWidths[i];

assert.equal(terminal.lpad(string, padWidth).length, padWidth);
assert.ok(terminal.lpad(string, padWidth).indexOf(string) === padWidth - string.length);
assert.equal(terminal.rpad(string, padWidth).length, padWidth);
assert.ok(terminal.rpad(string, padWidth).indexOf(string) === 0);
}

test.finish();
};

exports['test_printWrapped'] = function(test, assert) {
test.finish();
};

exports['test_prompt'] = function(test, assert) {
test.finish();
};

exports['test_formatTags'] = function(test, assert) {
test.finish();
};

exports['test_stylize'] = function(test, assert) {
test.finish();
};


exports['test_stripStyles'] = function(test, assert) {
var i, string, expectedString;
var strings = [
'foobar',
'[blue]foo[/blue]',
'[bold][blue]foobar[/blue] moo[/bold]'
];
var expectedStrings = [
'foobar',
'foo',
'foobar moo'
];

for (i = 0; i < strings.length; i++) {
string = strings[i];
expectedString = expectedStrings[i];

assert.equal(terminal.stripStyles(string), expectedString);
}

test.finish();
};

0 comments on commit f28339e

Please sign in to comment.