var tap = require('tap'),
    plan = tap.plan,
    test = tap.test,
    parser = require('../index');
var console = require('console'), util = require('util');

var conf47 = new parser.parser( {
				 ConfigFile: "complex.cfg",
				 InterPolateVars: true,
				 DefaultConfig: { this: "that", default: "imported" },
				 MergeDuplicateBlocks: true,
				 MergeDuplicateOptions: true,
				 StrictVars: true,
				 SplitPolicy: 'custom',
				 SplitDelimiter: '\s*=\s*',
				 IncludeGlob: true,
				 IncludeAgain: true,
				 IncludeRelative: true,
				 AutoTrue: true,
				 FlagBits: { someflags: { LOCK: 1, RW: 2, TAINT: 3 } },
				 StoreDelimiter: ' = ',
				 SlashIsDirectory: true,
				 SaveSorted: true
				} );

var h47 = conf47.getall();
var expect47 = {
          'var3': 'blah',
          'z1': {
                    'blak': '11111',
                    'nando': '9999'
                  },
          'a': {
                   'b': {
                            'm': {
                                     '9323': {
                                                 'g': '000',
                                                 'long': 'another long line'
                                               }
                                   },
                            'x': '9323',
                            'z': 'rewe'
                          }
                 },
          'onflag': true,
          'var2': 'zeppelin',
          'ignore': '$set',
          'quote': 'this should be \'kept: $set\' and not be \'$set!\'',
          'x5': {
                    'klack': '11111'
                  },
          'set': 'blah',
          'line': 'along line',
          'this': 'that',
          'imported': 'got that from imported config',
		'someflags': {
                           'RW': 2,
                           'LOCK': 1,
                           'TAINT': 3
                         },
          'var1': 'zero',
          'offflag': false,
          'cmd': 'mart@gw.intx.foo:22',
          'default': 'imported',
          'host': 'gw.intx.foo',
          'nando': '11111',
          'auch ätzendes': 'muss gehen',
          'Directory': {
                           '/': {
                                    'mode': '755'
                                  }
                         },
          'hansa': {
                       'z1': {
                                 'blak': '11111',
                                 'nando': '9999'
                               },
                       'Directory': {
                                        '/': {
                                                 'mode': '755'
                                               }
                                      },
                       'block': {
                                    '0': {
                                             'value': false
                                           }
                                  },
                       'x5': {
                                 'klack': '11111'
                               },
                       'Files': {
                                    '~/*.pl': {
                                                  'Options': '+Indexes'
                                                }
                                  },
                       'nando': '11111'
                     },
          'block': {
                       '0': {
                                'value': false
                              }
                     },
          'Files': {
                       '~/*.pl': {
                                     'Options': '+Indexes'
                                   }
                     },
          'a [[weird]] heredoc': 'has to\nwork\ntoo!'
};
test("complexity test", function(t) {
  t.plan(1);
  t.isDeeply(h47,expect47, "complexity test");
  t.end();
});

/*
# check if sorted save works
$conf47->save_file("t/complex.out", \%conf47);
open T, "<t/complex.out";
my $got47 = join '', <T>;
close T;
my $sorted = qq(
imported = got that from imported config
line = along line
nando = 11111
offflag = 0
onflag = 1);
if ($got47 =~ /\Q$sorted\E/) {
  pass("Testing sorted save");
}
else {
  fail("Testing sorted save");
}
*/

@@ -93,16 +93,58 @@
 /index')
+,%0A    fu = require('../lib/file-utils.js')
 ;%0Avar co
@@ -3786,16 +3786,255 @@
 oo!'%0A%7D;%0A
+conf47.save_file('complex.out');%0Avar d = fu.getFile('complex.out');%0A%0Avar tr = new RegExp(%22imported = got that from imported config%5C%5Cnline = along line%5C%5Cnnando = 11111%5C%5Cnoffflag = false%5C%5Cnonflag = true%22, 'mg');%0A%0Avar passfail = tr.test(d);%0A%0A
 test(%22co
@@ -4072,17 +4072,17 @@
  t.plan(
-1
+2
 );%0A  t.i
@@ -4131,378 +4131,60 @@
   t.
-end();%0A%7D);%0A%0A/*%0A# check if sorted save works%0A$conf47-%3Esave_file(%22t/complex.out%22, %5C%25conf47);%0Aopen T, %22%3Ct/complex.out%22;%0Amy $got47 = join '', %3CT%3E;%0Aclose T;%0Amy $sorted = qq(%0Aimported = got that from imported config%0Aline = along line%0Anando = 11111%0Aoffflag = 0%0Aonflag = 1);%0Aif ($got47 =~ /%5CQ$sorted%5CE/) %7B%0A  pass(%22Testing sorted save%22);%0A%7D%0Aelse %7B%0A  fail(%22Testing sorted save%22
+is(passfail,true,%22Testing sorted save%22);%0A  t.end(
 );%0A%7D
-%0A*/
+);
 %0A

add in the second part of the testing from the original