From 75760c4aa2997f8deffafdfcbf97faf2b32f5a65 Mon Sep 17 00:00:00 2001 From: Gurjeet Singh Date: Sun, 15 Jun 2014 17:09:34 -0400 Subject: [PATCH] Improve unit tests of escape-literal/identifier, and remove them from integration tests. Improve the code and clarity of unit tests in escape-tests.js. And removed the related integration tests since it has been demonstrated in the unit tests that a connection is not needed for escaping the literals and identifiers. --- test/integration/client/escape-tests.js | 153 --------------------- test/unit/client/escape-tests.js | 169 ++++++------------------ 2 files changed, 44 insertions(+), 278 deletions(-) delete mode 100644 test/integration/client/escape-tests.js diff --git a/test/integration/client/escape-tests.js b/test/integration/client/escape-tests.js deleted file mode 100644 index 40214e0..0000000 --- a/test/integration/client/escape-tests.js +++ /dev/null @@ -1,153 +0,0 @@ -var helper = require(__dirname + '/test-helper'); - -function createClient(callback) { - var client = new Client(helper.config); - client.connect(function(err) { - return callback(client); - }); -} - -test('escapeLiteral: no special characters', function() { - createClient(function(client) { - var expected = "'hello world'"; - var actual = client.escapeLiteral('hello world'); - assert.equal(expected, actual); - client.end(); - }); -}); - -test('escapeLiteral: contains double quotes only', function() { - createClient(function(client) { - var expected = "'hello \" world'"; - var actual = client.escapeLiteral('hello " world'); - assert.equal(expected, actual); - client.end(); - }); -}); - -test('escapeLiteral: contains single quotes only', function() { - createClient(function(client) { - var expected = "'hello \'\' world'"; - var actual = client.escapeLiteral('hello \' world'); - assert.equal(expected, actual); - client.end(); - }); -}); - -test('escapeLiteral: contains backslashes only', function() { - createClient(function(client) { - var expected = " E'hello \\\\ world'"; - var actual = client.escapeLiteral('hello \\ world'); - assert.equal(expected, actual); - client.end(); - }); -}); - -test('escapeLiteral: contains single quotes and double quotes', function() { - createClient(function(client) { - var expected = "'hello '' \" world'"; - var actual = client.escapeLiteral('hello \' " world'); - assert.equal(expected, actual); - client.end(); - }); -}); - -test('escapeLiteral: contains double quotes and backslashes', function() { - createClient(function(client) { - var expected = " E'hello \\\\ \" world'"; - var actual = client.escapeLiteral('hello \\ " world'); - assert.equal(expected, actual); - client.end(); - }); -}); - -test('escapeLiteral: contains single quotes and backslashes', function() { - createClient(function(client) { - var expected = " E'hello \\\\ '' world'"; - var actual = client.escapeLiteral('hello \\ \' world'); - assert.equal(expected, actual); - client.end(); - }); -}); - -test('escapeLiteral: contains single quotes, double quotes, and backslashes', function() { - createClient(function(client) { - var expected = " E'hello \\\\ '' \" world'"; - var actual = client.escapeLiteral('hello \\ \' " world'); - assert.equal(expected, actual); - client.end(); - }); -}); - -test('escapeIdentifier: no special characters', function() { - createClient(function(client) { - var expected = '"hello world"'; - var actual = client.escapeIdentifier('hello world'); - assert.equal(expected, actual); - client.end(); - }); -}); - -test('escapeIdentifier: contains double quotes only', function() { - createClient(function(client) { - var expected = '"hello "" world"'; - var actual = client.escapeIdentifier('hello " world'); - assert.equal(expected, actual); - client.end(); - }); -}); - -test('escapeIdentifier: contains single quotes only', function() { - createClient(function(client) { - var expected = '"hello \' world"'; - var actual = client.escapeIdentifier('hello \' world'); - assert.equal(expected, actual); - client.end(); - }); -}); - -test('escapeIdentifier: contains backslashes only', function() { - createClient(function(client) { - var expected = '"hello \\ world"'; - var actual = client.escapeIdentifier('hello \\ world'); - assert.equal(expected, actual); - client.end(); - }); -}); - -test('escapeIdentifier: contains single quotes and double quotes', function() { - createClient(function(client) { - var expected = '"hello \' "" world"'; - var actual = client.escapeIdentifier('hello \' " world'); - assert.equal(expected, actual); - client.end(); - }); -}); - -test('escapeIdentifier: contains double quotes and backslashes', function() { - return createClient(function(client) { - var expected = '"hello \\ "" world"'; - var actual = client.escapeIdentifier('hello \\ " world'); - assert.equal(expected, actual); - client.end(); - return; - }); -}); - -test('escapeIdentifier: contains single quotes and backslashes', function() { - createClient(function(client) { - var expected = '"hello \\ \' world"'; - var actual = client.escapeIdentifier('hello \\ \' world'); - assert.equal(expected, actual); - client.end(); - }); -}); - -test('escapeIdentifier: contains single quotes, double quotes, and backslashes', function() { - createClient(function(client) { - var expected = '"hello \\ \' "" world"'; - var actual = client.escapeIdentifier('hello \\ \' " world'); - assert.equal(expected, actual); - client.end(); - }); -}); diff --git a/test/unit/client/escape-tests.js b/test/unit/client/escape-tests.js index 40214e0..e3f638a 100644 --- a/test/unit/client/escape-tests.js +++ b/test/unit/client/escape-tests.js @@ -7,147 +7,66 @@ function createClient(callback) { }); } -test('escapeLiteral: no special characters', function() { - createClient(function(client) { - var expected = "'hello world'"; - var actual = client.escapeLiteral('hello world'); +var testLit = function(testName, input, expected) { + test(testName, function(){ + var client = new Client(helper.config); + var actual = client.escapeLiteral(input); assert.equal(expected, actual); - client.end(); }); -}); +}; -test('escapeLiteral: contains double quotes only', function() { - createClient(function(client) { - var expected = "'hello \" world'"; - var actual = client.escapeLiteral('hello " world'); +var testIdent = function(testName, input, expected) { + test(testName, function(){ + var client = new Client(helper.config); + var actual = client.escapeIdentifier(input); assert.equal(expected, actual); - client.end(); }); -}); +}; -test('escapeLiteral: contains single quotes only', function() { - createClient(function(client) { - var expected = "'hello \'\' world'"; - var actual = client.escapeLiteral('hello \' world'); - assert.equal(expected, actual); - client.end(); - }); -}); +testLit('escapeLiteral: no special characters', + 'hello world', "'hello world'"); -test('escapeLiteral: contains backslashes only', function() { - createClient(function(client) { - var expected = " E'hello \\\\ world'"; - var actual = client.escapeLiteral('hello \\ world'); - assert.equal(expected, actual); - client.end(); - }); -}); +testLit('escapeLiteral: contains double quotes only', + 'hello " world', "'hello \" world'"); -test('escapeLiteral: contains single quotes and double quotes', function() { - createClient(function(client) { - var expected = "'hello '' \" world'"; - var actual = client.escapeLiteral('hello \' " world'); - assert.equal(expected, actual); - client.end(); - }); -}); +testLit('escapeLiteral: contains single quotes only', + 'hello \' world', "'hello \'\' world'"); -test('escapeLiteral: contains double quotes and backslashes', function() { - createClient(function(client) { - var expected = " E'hello \\\\ \" world'"; - var actual = client.escapeLiteral('hello \\ " world'); - assert.equal(expected, actual); - client.end(); - }); -}); +testLit('escapeLiteral: contains backslashes only', + 'hello \\ world', " E'hello \\\\ world'"); -test('escapeLiteral: contains single quotes and backslashes', function() { - createClient(function(client) { - var expected = " E'hello \\\\ '' world'"; - var actual = client.escapeLiteral('hello \\ \' world'); - assert.equal(expected, actual); - client.end(); - }); -}); +testLit('escapeLiteral: contains single quotes and double quotes', + 'hello \' " world', "'hello '' \" world'"); -test('escapeLiteral: contains single quotes, double quotes, and backslashes', function() { - createClient(function(client) { - var expected = " E'hello \\\\ '' \" world'"; - var actual = client.escapeLiteral('hello \\ \' " world'); - assert.equal(expected, actual); - client.end(); - }); -}); +testLit('escapeLiteral: contains double quotes and backslashes', + 'hello \\ " world', " E'hello \\\\ \" world'"); -test('escapeIdentifier: no special characters', function() { - createClient(function(client) { - var expected = '"hello world"'; - var actual = client.escapeIdentifier('hello world'); - assert.equal(expected, actual); - client.end(); - }); -}); +testLit('escapeLiteral: contains single quotes and backslashes', + 'hello \\ \' world', " E'hello \\\\ '' world'"); -test('escapeIdentifier: contains double quotes only', function() { - createClient(function(client) { - var expected = '"hello "" world"'; - var actual = client.escapeIdentifier('hello " world'); - assert.equal(expected, actual); - client.end(); - }); -}); +testLit('escapeLiteral: contains single quotes, double quotes, and backslashes', + 'hello \\ \' " world', " E'hello \\\\ '' \" world'"); -test('escapeIdentifier: contains single quotes only', function() { - createClient(function(client) { - var expected = '"hello \' world"'; - var actual = client.escapeIdentifier('hello \' world'); - assert.equal(expected, actual); - client.end(); - }); -}); +testIdent('escapeIdentifier: no special characters', + 'hello world', '"hello world"'); -test('escapeIdentifier: contains backslashes only', function() { - createClient(function(client) { - var expected = '"hello \\ world"'; - var actual = client.escapeIdentifier('hello \\ world'); - assert.equal(expected, actual); - client.end(); - }); -}); +testIdent('escapeIdentifier: contains double quotes only', + 'hello " world', '"hello "" world"'); -test('escapeIdentifier: contains single quotes and double quotes', function() { - createClient(function(client) { - var expected = '"hello \' "" world"'; - var actual = client.escapeIdentifier('hello \' " world'); - assert.equal(expected, actual); - client.end(); - }); -}); +testIdent('escapeIdentifier: contains single quotes only', + 'hello \' world', '"hello \' world"'); -test('escapeIdentifier: contains double quotes and backslashes', function() { - return createClient(function(client) { - var expected = '"hello \\ "" world"'; - var actual = client.escapeIdentifier('hello \\ " world'); - assert.equal(expected, actual); - client.end(); - return; - }); -}); +testIdent('escapeIdentifier: contains backslashes only', + 'hello \\ world', '"hello \\ world"'); -test('escapeIdentifier: contains single quotes and backslashes', function() { - createClient(function(client) { - var expected = '"hello \\ \' world"'; - var actual = client.escapeIdentifier('hello \\ \' world'); - assert.equal(expected, actual); - client.end(); - }); -}); +testIdent('escapeIdentifier: contains single quotes and double quotes', + 'hello \' " world', '"hello \' "" world"'); -test('escapeIdentifier: contains single quotes, double quotes, and backslashes', function() { - createClient(function(client) { - var expected = '"hello \\ \' "" world"'; - var actual = client.escapeIdentifier('hello \\ \' " world'); - assert.equal(expected, actual); - client.end(); - }); -}); +testIdent('escapeIdentifier: contains double quotes and backslashes', + 'hello \\ " world', '"hello \\ "" world"'); + +testIdent('escapeIdentifier: contains single quotes and backslashes', + 'hello \\ \' world', '"hello \\ \' world"'); + +testIdent('escapeIdentifier: contains single quotes, double quotes, and backslashes', + 'hello \\ \' " world', '"hello \\ \' "" world"');