Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2014 IBM Corp. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | **/ |
| 16 | var should = require("should"); |
| 17 | var util = require("../../red/util"); |
| 18 | |
| 19 | describe("red/util", function() { |
| 20 | describe('ensureString', function() { |
| 21 | it('strings are preserved', function() { |
| 22 | util.ensureString('string').should.equal('string'); |
| 23 | }); |
| 24 | it('Buffer is converted', function() { |
| 25 | var s = util.ensureString(new Buffer('foo')); |
| 26 | s.should.equal('foo'); |
| 27 | (typeof s).should.equal('string'); |
| 28 | }); |
| 29 | it('Object is converted to JSON', function() { |
| 30 | var s = util.ensureString({foo: "bar"}); |
| 31 | (typeof s).should.equal('string'); |
| 32 | should.deepEqual(JSON.parse(s), {foo:"bar"}); |
| 33 | }); |
| 34 | it('stringifies other things', function() { |
| 35 | var s = util.ensureString(123); |
| 36 | (typeof s).should.equal('string'); |
| 37 | s.should.equal('123'); |
| 38 | }); |
| 39 | }); |
| 40 | |
| 41 | describe('ensureBuffer', function() { |
| 42 | it('Buffers are preserved', function() { |
| 43 | var b = new Buffer(''); |
| 44 | util.ensureBuffer(b).should.equal(b); |
| 45 | }); |
| 46 | it('string is converted', function() { |
| 47 | var b = util.ensureBuffer('foo'); |
| 48 | var expected = new Buffer('foo'); |
| 49 | for (var i = 0; i < expected.length; i++) { |
| 50 | b[i].should.equal(expected[i]); |
| 51 | } |
| 52 | Buffer.isBuffer(b).should.equal(true); |
| 53 | }); |
| 54 | it('Object is converted to JSON', function() { |
| 55 | var obj = {foo: "bar"} |
| 56 | var b = util.ensureBuffer(obj); |
| 57 | Buffer.isBuffer(b).should.equal(true); |
| 58 | should.deepEqual(JSON.parse(b), obj); |
| 59 | }); |
| 60 | it('stringifies other things', function() { |
| 61 | var b = util.ensureBuffer(123); |
| 62 | Buffer.isBuffer(b).should.equal(true); |
| 63 | var expected = new Buffer('123'); |
| 64 | for (var i = 0; i < expected.length; i++) { |
| 65 | b[i].should.equal(expected[i]); |
| 66 | } |
| 67 | }); |
| 68 | }); |
| 69 | }); |
| 70 | |