'use strict';

var test = require('tape');
var tryjson = require('./index');

test('parsing valid json', function (t) {
    var obj, objs = [
        null,
        '',
        'txt',
        6,
        -0.2,
        1e6,
        1/3,
        [],
        {},
        true,
        false,
        [[[[[1]]]]],
        {a: 1, b: 2},
        ["a", 2, 3, null],
        [{a:1},{b:2},{c:[[3]]}],
    ];
    t.plan(3 * objs.length);
    objs.forEach(function (o) {
        var json = JSON.stringify(o);
        obj = tryjson.parse(json);
        t.equal(typeof o, typeof obj, json + ' should have the same type');
        t.equal(Array.isArray(o), Array.isArray(obj), json + ' should have the same array status');
        t.deepEqual(o, obj, json + ' should be deeply equal');
    });
});

test('parsing invalid json', function (t) {
    var obj, json = [
        'nul',
        "{'a':1,'b':2}",
        '{"a":1,"b"}',
        '["a",2,3,nul]',
        '{a:1,b:2}',
        '[1,2,3',
        '',
        JSON,
        0/0
    ];
    t.plan(json.length);
    json.forEach(function (s) {
        obj = tryjson.parse(s);
        t.ok(obj === undefined, s + ' should be undefined');
    });
});

test('strigifying valid objects', function (t) {
    var obj, objs = [
        null,
        '',
        'txt',
        6,
        -0.2,
        1e6,
        1/3,
        [],
        {},
        true,
        false,
        [[[[[1]]]]],
        {a: 1, b: 2},
        ["a", 2, 3, null],
        [{a:1},{b:2},{c:[[3]]}],
    ];
    t.plan(3 * objs.length);
    objs.forEach(function (o) {
        var json = tryjson.stringify(o);
        obj = JSON.parse(json);
        t.equal(typeof o, typeof obj, json + ' should have the same type');
        t.equal(Array.isArray(o), Array.isArray(obj), json + ' should have the same array status');
        t.deepEqual(o, obj, json + ' should be deeply equal');
    });
});

test('strigifying invalid objects', function (t) {
    var obj, json, objs = [
        {n: 1},
        {n: 2},
        {n: 3},
        null,
        0/0,
        undefined
    ];
    t.plan(objs.length);
    objs[0].a = objs[0];
    objs[1].a = [[[[[[[[[[objs[2]]]]]]]]]]];
    objs[2].a = {a: {a: {a: {a: {a: objs[1]}}}}};
    objs.forEach(function (s, i) {
        json = tryjson.stringify(s);
        obj = JSON.parse(json);
        t.ok(obj === null, 'object ' + i + ' should be null');
    });
});

test('parsing invalid json with fallback', function (t) {
    var obj, json = [
        'nul',
        "{'a':1,'b':2}",
        '{"a":1,"b"}',
        '["a",2,3,nul]',
        '{a:1,b:2}',
        '[1,2,3',
        '',
        JSON,
        0/0
    ], fallback = [
        null,
        undefined,
        '',
        {a: 1},
        JSON
    ];
    t.plan(json.length * fallback.length);
    json.forEach(function (s) {
        fallback.forEach( function (f) {
            obj = tryjson.parse(s, f);
            t.ok(obj === f, s + ' should be parsed to fallback');
        });
    });
});

@@ -1003,24 +1003,167 @@
         0/0%0A
+    %5D, fallback = %5B%0A        'no fallback',%0A        0,%0A        null,%0A        false,%0A        undefined,%0A        '',%0A        %7Ba: 1%7D,%0A        JSON%0A
     %5D;%0A    t
@@ -1175,24 +1175,42 @@
 (json.length
+ * fallback.length
 );%0A    json.
@@ -1224,32 +1224,119 @@
 (function (s) %7B%0A
+        fallback.forEach(function (f) %7B%0A            if (f === 'no fallback') %7B%0A        
         obj = tr
@@ -1351,32 +1351,40 @@
 rse(s);%0A        
+      
+  
 t.ok(obj === und
@@ -1411,18 +1411,188 @@
  be 
-undefined'
+parsed to undefined');%0A            %7D else %7B%0A                obj = tryjson.parse(s, f);%0A                t.ok(obj === f, s + ' should be parsed to fallback');%0A            %7D%0A        %7D
 );%0A 
@@ -2822,596 +2822,4 @@
 %7D);%0A
-%0Atest('parsing invalid json with fallback', function (t) %7B%0A    var obj, json = %5B%0A        'nul',%0A        %22%7B'a':1,'b':2%7D%22,%0A        '%7B%22a%22:1,%22b%22%7D',%0A        '%5B%22a%22,2,3,nul%5D',%0A        '%7Ba:1,b:2%7D',%0A        '%5B1,2,3',%0A        '',%0A        JSON,%0A        0/0%0A    %5D, fallback = %5B%0A        null,%0A        undefined,%0A        '',%0A        %7Ba: 1%7D,%0A        JSON%0A    %5D;%0A    t.plan(json.length * fallback.length);%0A    json.forEach(function (s) %7B%0A        fallback.forEach( function (f) %7B%0A            obj = tryjson.parse(s, f);%0A            t.ok(obj === f, s + ' should be parsed to fallback');%0A        %7D);%0A    %7D);%0A%7D);%0A

refactor parsing tests