Skip to content

Commit

Permalink
Added unit tests. Modified replaceNull() method in base class.
Browse files Browse the repository at this point in the history
  • Loading branch information
mheadd committed Sep 28, 2010
1 parent 057b058 commit e0674b6
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/base.js
Expand Up @@ -80,7 +80,7 @@ Record = function(attempts, bargein, beep, choices, format, maxSilence, maxTime,
this.required = required;
this.say = serializeProperty(say);
this.timeout = timeout;
this.transcription = (typeof(transcription) == 'Object') ? serializeProperty(transcription) : recording;
this.transcription = (typeof(transcription) == 'Object') ? serializeProperty(transcription) : transcription;
this.url = url;
this.password = password;
this.username = username;
Expand Down Expand Up @@ -167,9 +167,9 @@ function serializeProperty(object) {

// Helper method to remove null values from rendered JSON.
function replaceNull (key, value) {
if (value === null || typeof(value) == 'undefined' || value == '') {
if (value === null) {
return undefined;
}
}
return value;
};

Expand Down
115 changes: 115 additions & 0 deletions tests/tests.js
@@ -0,0 +1,115 @@
/**
* A set of unit tests for the Tropo WebAPI classes.
* run by executing:
* ~$ node tropo-webapi-node/tests/tests.js
*/

require('../lib/tropo-webapi');
var assert = require('assert');
var sys = require('sys');

var sayExpected = '{"tropo":[{ "say":{"value":"Hello, World.","required":true,"voice":"carmen"}}]}';
var askExpected = '{"tropo":[{ "ask":{"choices":{"value":"[5 DIGITS]"},"bargein":true,"name":"foo","required":true,"say":{"value":"Please say your account number."},"timeout":30}}]}';
var callExpected = '{"tropo":[{ "call":{"to":"3055195825"}}]}';
var conferenceExpected = '{"tropo":[{ "conference":{"id":1234,"mute":false,"name":"foo","playTones":false,"terminator":"#"}}]}'
var hangupExpected = '{"tropo":[{ "hangup":{}}]}';
var messageExpected = '{"tropo":[{ "message":{"say":{"value":"This is an announcement"},"to":"3055195825","answerOnMedia":false,"channel":"TEXT","from":"3055551212","network":"SMS","timeout":10,"voice":"kate"}}]}'
var recordExpected = '{"tropo":[{ "record":{"beep":true,"choices":{"value":"[5 DIGITS]"},"maxSilence":5,"method":"POST","name":"foo","required":true,"say":{"value":"Please say your account number"}}}]}';

var numTests = 0;
var numFailed = 0;

sys.puts('***** Starting Test Run ************');
sys.puts(' ');
sys.puts('Say Test: ' + sayTest(sayExpected));
sys.puts('Ask Test: ' + askTest(askExpected));
sys.puts('Call Test: ' + callTest(callExpected));
sys.puts('Conference Test: ' + conferenceTest(conferenceExpected));
sys.puts('Hangup Test: ' + hangupTest(hangupExpected));
sys.puts('Message Test: ' + messageTest(messageExpected));
sys.puts('Record Test: ' + recordTest(recordExpected));
sys.puts('');
sys.puts('Total Tests Run: ' + numTests);
sys.puts('Total Failed: ' + numFailed);
sys.puts(' ');
sys.puts('***** Test Run Complete ************');

// A test for the Tropo Say object.
function sayTest(expected) {

var tropo = new TropoWebAPI();
tropo.say("Hello, World.", null, null, true, "carmen");
return runTest(TropoJSON(tropo), expected);

}

// A test for the Tropo Ask object.
function askTest(expected) {

var tropo = new TropoWebAPI();
var say = new Say("Please say your account number.");
var choices = new Choices("[5 DIGITS]");
tropo.ask(choices, null, true, null, "foo", null, true, say, 30, null);
return runTest(TropoJSON(tropo), expected);
}

// A test for the Tropo Call object.
function callTest(expected) {

var tropo = new TropoWebAPI();
tropo.call("3055195825");
return runTest(TropoJSON(tropo), expected);
}

// A test for the Tropo Conference object.
function conferenceTest(expected) {

var tropo = new TropoWebAPI();
tropo.conference(1234, false, "foo", false, null, "#");
return runTest(TropoJSON(tropo), expected);
}

// A test for the Tropo Hangup object.
function hangupTest(expected) {

var tropo = new TropoWebAPI();
tropo.hangup();
return runTest(TropoJSON(tropo), expected)
}

// A test for the Tropo Message object.
function messageTest(expected) {

var tropo = new TropoWebAPI();
var say = new Say("This is an announcement");
tropo.message(say, "3055195825", false, "TEXT", "3055551212", null, "SMS", null, 10, "kate");
return runTest(TropoJSON(tropo), expected)

}

// A test for the Tropo Record object.
function recordTest(expected) {

var tropo = new TropoWebAPI();
var say = new Say("Please say your account number");
var choices = new Choices("[5 DIGITS]");
tropo.record(null, null, true, choices, null, 5, null, "POST", null, "foo", true, say);
return runTest(TropoJSON(tropo), expected)

}

// A helper method to run the assertion test.
function runTest(actual, expected) {

numTests++;
try {
assert.equal(actual, expected)
return 'Passed.';
}

catch (err) {
numFailed++;
return 'Failed.\n' + err;
}

}

0 comments on commit e0674b6

Please sign in to comment.