Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added tests for encode and small "fixes" for empty array and empty objec... #3

Open
wants to merge 1 commit into from

1 participant

@airportyh

...t output. Using node-tap and testem for testing. Tests ported from t/yamlish-writer.t in http://search.cpan.org/~ovid/Test-Harness-3.25/.

@airportyh airportyh Added tests for encode and small "fixes" for empty array and empty ob…
…ject output. Using node-tap and testem for testing. Tests ported from t/yamlish-writer.t in http://search.cpan.org/~ovid/Test-Harness-3.25/.
de6b90b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 2, 2012
  1. @airportyh

    Added tests for encode and small "fixes" for empty array and empty ob…

    airportyh authored
    …ject output. Using node-tap and testem for testing. Tests ported from t/yamlish-writer.t in http://search.cpan.org/~ovid/Test-Harness-3.25/.
This page is out of date. Refresh to see the latest.
Showing with 230 additions and 12 deletions.
  1. +183 −0 encode_tests.js
  2. +26 −9 package.json
  3. +9 −0 testem.json
  4. +12 −3 yamlish.js
View
183 encode_tests.js
@@ -0,0 +1,183 @@
+var test = require('tap').test
+var yamlish = require('./yamlish')
+var encode = yamlish.encode
+var decode = yamlish.decode
+var assert = require('assert')
+
+function repr(obj){
+ return JSON.stringify(obj)
+}
+
+test('Simple scalar', function(t){
+ t.equal(encode(1), '1')
+ t.end()
+})
+
+test('undefined and null', function(t){
+ t.equal(encode(undefined), '~')
+ t.equal(encode(null), '~')
+ t.end()
+})
+
+test('Unprintable', function(t){
+ t.equal(encode("\x01\n\t"), '\u0001')
+ t.end()
+})
+
+test('Simple array', function(t){
+ t.equal(encode([1, 2, 3]), "\n - 1\n - 2\n - 3")
+ t.end()
+})
+
+test('Empty array', function(t){
+ t.equal(encode([]), '[]')
+ t.end()
+})
+
+test('Empty object', function(t){
+ t.equal(encode({}), '{}')
+ t.end()
+})
+
+test('Array, two elements, undefined', function(t){
+ t.equal(encode([ undefined, undefined ]), '\n - ~\n - ~')
+ t.end()
+})
+
+test('Nested array', function(t){
+ t.equal(encode([ 1, 2, [3, 4], 5 ]), [
+ '\n - 1'
+ , '\n - 2'
+ , '\n - '
+ , '\n - 3'
+ , '\n - 4'
+ , '\n - 5'
+ ].join(''))
+ t.end()
+})
+
+test('Nested Empty', function(t){
+ t.equal(encode([ 1, 2, [], 5 ]), [
+ '\n - 1'
+ , '\n - 2'
+ , '\n - []'
+ , '\n - 5'
+ ].join(''))
+ t.end()
+})
+
+test('Simple object', function(t){
+ t.equal(encode({ one: '1', two: '2', three: '3' }), [
+ '\n one: 1'
+ , '\n two: 2'
+ , '\n three: 3'
+ ].join(''))
+ t.end()
+})
+
+test('Nested object', function(t){
+ t.equal(encode({
+ one: '1'
+ , two: '2'
+ , more: {
+ three: '3'
+ , four: '4'
+ }
+ }), [
+ '\n one: 1'
+ , '\n two: 2'
+ , '\n more: '
+ , '\n three: 3'
+ , '\n four: 4'].join(''))
+ t.end()
+})
+
+test('Nested empty', function(t){
+ t.equal(encode({ one: '1', two: '2', more: {} }), [
+ '\n one: 1'
+ , '\n two: 2'
+ , '\n more: {}'
+ ].join(''))
+ t.end()
+})
+
+test('Unprintable key', function(t){
+ t.equal(encode({ one: '1', "\x02": '2', three: '3' }), [
+ '\n one: 1'
+ , '\n \"\\u0002\": 2'
+ , '\n three: 3'
+ ].join(''))
+ t.end()
+})
+
+test('Empty key', function(t){
+ t.equal(encode({ '' : 'empty' }), '\n "": empty')
+ t.end()
+})
+
+test('Funky hash key', function(t){
+ t.equal(encode({ './frob' : 'is_frob' }), '\n "./frob": is_frob')
+ t.end()
+})
+
+test('Complex', function(t){
+ var complexObject = {
+ 'bill-to' : {
+ 'given' : 'Chris',
+ 'address' : {
+ 'city' : 'Royal Oak',
+ 'postal' : '48046',
+ 'lines' : "458 Walkman Dr.\nSuite #292\n",
+ 'state' : 'MI'
+ },
+ 'family' : 'Dumars'
+ },
+ 'invoice' : '34843',
+ 'date' : '2001-01-23',
+ 'tax' : '251.42',
+ 'product' : [
+ { 'sku' : 'BL394D',
+ 'quantity' : '4',
+ 'price' : '450.00',
+ 'description' : 'Basketball'
+ },
+ { 'sku' : 'BL4438H',
+ 'quantity' : '1',
+ 'price' : '2392.00',
+ 'description' : 'Super Hoop'
+ }
+ ],
+ 'comments' :
+ "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\n",
+ 'total' : '4443.52'
+ }
+ var expectedOutput = '\n\
+ \"bill-to\": \n\
+ given: Chris\n\
+ address: \n\
+ city: Royal Oak\n\
+ postal: 48046\n\
+ lines: |\n\
+ 458 Walkman Dr.\n\
+ Suite #292\n\
+ state: MI\n\
+ family: Dumars\n\
+ invoice: 34843\n\
+ date: 2001-01-23\n\
+ tax: 251.42\n\
+ product: \n\
+ - \n\
+ sku: BL394D\n\
+ quantity: 4\n\
+ price: 450.00\n\
+ description: Basketball\n\
+ - \n\
+ sku: BL4438H\n\
+ quantity: 1\n\
+ price: 2392.00\n\
+ description: Super Hoop\n\
+ comments: Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\n\
+ total: 4443.52'
+ t.equal(encode(complexObject), expectedOutput)
+ t.end()
+})
View
35 package.json
@@ -1,9 +1,26 @@
-{ "name" : "yamlish"
-, "description" : "Parser/encoder for the yamlish format"
-, "repository":"https://github.com/isaacs/yamlish"
-, "version" : "0.0.5"
-, "main" : "yamlish.js"
-, "keywords" : [ "yaml", "yamlish", "test", "anything", "protocol", "tap"]
-, "license" : { "type" : "MIT"
- , "url" : "http://github.com/isaacs/yamlish/raw/master/LICENSE" }
-, "author" : "Isaac Z. Schlueter <i@izs.me> (http://blog.izs.me/)" }
+{
+ "name": "yamlish",
+ "description": "Parser/encoder for the yamlish format",
+ "repository": "https://github.com/isaacs/yamlish",
+ "version": "0.0.5",
+ "main": "yamlish.js",
+ "keywords": [
+ "yaml",
+ "yamlish",
+ "test",
+ "anything",
+ "protocol",
+ "tap"
+ ],
+ "license": {
+ "type": "MIT",
+ "url": "http://github.com/isaacs/yamlish/raw/master/LICENSE"
+ },
+ "author": "Isaac Z. Schlueter <i@izs.me> (http://blog.izs.me/)",
+ "devDependencies": {
+ "tap": "~0.3.3"
+ },
+ "scripts": {
+ "test": "node *_tests.js"
+ }
+}
View
9 testem.json
@@ -0,0 +1,9 @@
+{
+ "launchers": {
+ "Test": {
+ "command": "node *_tests.js",
+ "protocol": "tap"
+ }
+ },
+ "launch_in_dev": ["Test"]
+}
View
15 yamlish.js
@@ -56,6 +56,10 @@ function encode (obj, indent) {
return obj.toString()
}
+ if (Array.isArray(obj) && obj.length === 0){
+ return '[]'
+ }
+
if (seen.indexOf(obj) !== -1) {
return "[Circular]"
}
@@ -79,15 +83,20 @@ function encode (obj, indent) {
var out = ""
if (Array.isArray(obj)) {
- var out = "\n" + indent + "- " +obj.map(function (item) {
+ out = "\n" + indent + "- " +obj.map(function (item) {
return encode(item, indent + " ", true)
- }).join("\n"+indent + "- ")
+ }).join("\n"+indent + "- ")
break
}
// an actual object
var keys = Object.keys(obj)
- , niceKeys = keys.map(function (k) {
+
+ if (keys.length === 0){ // Empty object
+ return '{}'
+ }
+
+ var niceKeys = keys.map(function (k) {
return (k.match(/^[a-zA-Z0-9_]+$/) ? k : JSON.stringify(k)) + ": "
})
//console.error(keys, niceKeys, obj)
Something went wrong with that request. Please try again.