<commit_before>const { assert } = require('chai');

const { get } = require('./index');
const { parse, Errors } = require('./parse');
const { parse_filter } = require('./parse_filter');


const test2 = (func, t)	=> {
	t.forEach(_itm => {
		let var0    = _itm[0];
		let expected = _itm[1];
		
		it('"' + var0 + '" -> '+ JSON.stringify(expected).replace(/\n/g, ' '),function(){
			assert.deepEqual(func(var0), expected);
		});
	})
}

const test_get = (t) => {
	t.forEach(_itm => {
		let path     = _itm[0];
		let o        = _itm[1];
		let expected = _itm[2];
		it('"' + path + '" of '+JSON.stringify(o).replace(/\n/g, ' ')+' -> ' + JSON.stringify(expected).replace(/\n/g, ' '),function(){
			let actual =get(o, path);
			assert.deepEqual(actual, expected);
		});
	});
}

describe('parse', function(){
	test2(parse, [
		["a",                [{any:"a"}]                                                              ], 
		[".a",               [{next:"a"}]                                                             ], 
		["a  b",             [{any:"a"}, {any:"b"}]                                                   ], 
		["a.x b",            [{any:"a"}, {next:"x"}, {any:"b"}]                                       ],   
		["a[ x = 1 ] b",     [{any:"a", transformation:[{filter:"x = 1"}]}, {any:"b"}]                ], 
		["a[x= 1].c b",      [{any:"a", transformation:[{filter:"x= 1"}]}, {next:"c"}, {any:"b"}]     ], 
		["a.c[x=1 ]  b",     [{any:"a"}, {next:"c", transformation:[{filter:"x=1"}]}, {any:"b"}]      ], 
		["a.c[ x.y = 1 ] b", [{any:"a"}, {next:"c", transformation:[{filter:"x.y = 1"}]}, {any:"b"}]  ],

		["[x=1\\=1]",        [{any:"*", transformation:[{filter:"x=1\\=1"}]}]                         ],
		["\\[x=1\\=1\\]",    [{any:"\\[x=1\\=1\\]"}]                                                  ],
		[" a\\ b  ",         [{any:"a\\ b"}]                                                          ], 		
		
		["[x=1][y=2]",       [{any:"*", transformation:[{filter:"x=1"},{filter:"y=2"}]}]              ],

		[":a",               [{any:"*", transformation:[{pseudo:"a"}]}]                               ],
		[":a:rfg ",          [{any:"*", transformation:[{pseudo:"a"}, {pseudo:"rfg"}]}]               ],
		["[x=1]:a",          [{any:"*", transformation:[{filter:"x=1"}, {pseudo:"a"}]}]               ]
	]);

	it('"[x=1]b:a" -> error',function(){
		assert.throws(()=>{parse("[x=1]b:a")}, Errors.CHARS_AFTER_TRANSFORMATION);
	});
	it('"..a" -> error',function(){
		assert.throws(()=>{parse("..a")}, Errors.DOT_WITHOUT_LEVEL_NAME);
	});
	it('"[[" -> error',function(){
		assert.throws(()=>{parse("[[")}, Errors.FILTER_OPEN_FAIL);
	});
	it('"]" -> error',function(){
		assert.throws(()=>{parse("]")}, Errors.FILTER_CLOSE_FAIL);
	});
});

describe('parse_filter', function(){
	test2(parse_filter, [
		[undefined,          {left:""}                                              ],
		['',                 {left:""}                                              ],
		["a",                {left:"a"}                                             ], 
		["=b",               {left:"",    right:'b',   delimiter:'='}               ], 
		["a=b",              {left:"a",   right:'b',   delimiter:'='}               ], 
		[" x y = b c ",      {left:"x y", right:'b c', delimiter:'='}               ], 
		["a=b=c",            {left:"a",   right:'b=c', delimiter:'='}               ], 
		["a\\=b=c",          {left:"a=b", right:'c',   delimiter:'='}               ], 
		["a=b\\=c",          {left:"a",   right:'b=c', delimiter:'='}               ],
		['a*=b',             {left:"a",   right:'b',   delimiter:'*='}              ]
	]);
});

describe('simple get 0 level', function(){
	test_get([
		["",  1,     [1]      ], 
		["",  {a:1}, [{a:1}]  ] 
	]);
});

describe('simple get 1 level', function(){
	test_get([
		["*",     1,                            [1]                                       ], 
		["*",     {a:1},                        [{a:1}, 1]                                ], 
		["*",     {a:{c:1}, b:2},               [{a:{c:1},b:2}, {c:1}, 1, 2]              ], 
		
		["a",     1,                            []                                        ], 
		["a",     {b:1},                        []                                        ], 
		["a",     {a:1},                        [1]                                       ], 
		["a",     {a:[1, 2]},                   [1, 2]                                    ], 
		["a",     {a:{b:"x"}},                  [{b:"x"}]                                 ], 
		["a",     {b:{bb:"xxx", a:1}},          [1]                                       ], 
		["a",     [{a:1}, {a:2}, {b:3}],        [1,2]                                     ], 
		["a",     [[{a:1}, {a:2}], [{a:3}]],    [1,2,3]                                   ], 
		["a",     [[[{a:1}, {a:2}], [{a:3}]],[[{a:4}, {a:5}],[{a:6}]]],     [1,2,3,4,5,6] ], 

		["a",     {a:{a:[1, 2]}},               [{a:[1, 2]}, 1, 2]                        ],
		[".a",    {a:{a:[1, 2]}},               [{a:[1, 2]}]                              ],
		["a",     [[[[[{a:1}]]]]],              [1]                                       ]
	]);
});

describe('simple get 2 level', function(){
	test_get([
		["a.b",   {a:{b:"x"}},                  ["x"]                                     ], 

		["a *",   {x:{a:1, z:{a:2}}},           [1, 2]                                    ], 
		["a b",   {a:{b:{c:1}}},                [{c:1}]                                   ], 
		["a b",   {a:{c:{b:1}}},                [1]                                       ],
		
		["* a",   {a:{a:1}},                    [{"a":1},1]                               ],
		["a *",   {a:{a:1}},                    [{"a":1},1]                               ]
	]);
});

describe('simple get 3 level', function(){
	let o = {
		a:{
			b:{
				b:{
					c:1, 
				}, 
				c:{
					d:2
				}
			}, 
			c:0,
			x:{
				b:{
					c:3 
				} 
			}
		}
	}
	test_get([
		["a.b.c", {a:{b:{c:[1,2]}}},            [1,2]          ], 
		["a.b.c", {a:{b:[ {c:1}, {c:2} ]}},     [1,2]          ], 
		["a.b.c", {a:[ {b:{c:1}}, {b:{c:2}} ]}, [1,2]          ], 
		["a.b.c", o,                            [{d:2}]        ], 
		["a.b c", o,                            [{d:2}, 1]     ],
		["a b c", o,                            [{d:2}, 1, 3]  ],
		["a b c", {a:{b:{b:{c:1}}}},            [1]            ],
		["a b c", {a:{b:{b:{c:{z:1}}}}},        [{z:1}]        ],

		[".*", {a:1, b:2},                      [{a:1,b:2},1,2]], //not so good as I expected - there is no way to get "next" level

		["a * c", {a:{b:{c:[1,2]},c:3}},        [3,1,2]        ], //not so good as I expected - there is no way to get "next" level
		[".a.*.c", {a:{b:{c:[1,2]},c:3}},       [3,1,2]        ]  //not so good as I expected - there is no way to get second level
	]);
});

describe('pseudo', function(){
	test_get([
		[':empty',  {a:{b:1}},  [1]   ]
	]);
});

describe('filter', function(){
	let o = {
		x:{
			a:[<commit_msg>fix .*<commit_after>const { assert } = require('chai');

const { get } = require('./index');
const { parse, Errors } = require('./parse');
const { parse_filter } = require('./parse_filter');


const test2 = (func, t)	=> {
	t.forEach(_itm => {
		let var0    = _itm[0];
		let expected = _itm[1];
		
		it('"' + var0 + '" -> '+ JSON.stringify(expected).replace(/\n/g, ' '),function(){
			assert.deepEqual(func(var0), expected);
		});
	})
}

const test_get = (t) => {
	t.forEach(_itm => {
		let path     = _itm[0];
		let o        = _itm[1];
		let expected = _itm[2];
		it('"' + path + '" of '+JSON.stringify(o).replace(/\n/g, ' ')+' -> ' + JSON.stringify(expected).replace(/\n/g, ' '),function(){
			let actual =get(o, path);
			assert.deepEqual(actual, expected);
		});
	});
}

describe('parse', function(){
	test2(parse, [
		["a",                [{any:"a"}]                                                              ], 
		[".a",               [{next:"a"}]                                                             ], 
		["a  b",             [{any:"a"}, {any:"b"}]                                                   ], 
		["a.x b",            [{any:"a"}, {next:"x"}, {any:"b"}]                                       ],   
		["a[ x = 1 ] b",     [{any:"a", transformation:[{filter:"x = 1"}]}, {any:"b"}]                ], 
		["a[x= 1].c b",      [{any:"a", transformation:[{filter:"x= 1"}]}, {next:"c"}, {any:"b"}]     ], 
		["a.c[x=1 ]  b",     [{any:"a"}, {next:"c", transformation:[{filter:"x=1"}]}, {any:"b"}]      ], 
		["a.c[ x.y = 1 ] b", [{any:"a"}, {next:"c", transformation:[{filter:"x.y = 1"}]}, {any:"b"}]  ],

		["[x=1\\=1]",        [{any:"*", transformation:[{filter:"x=1\\=1"}]}]                         ],
		["\\[x=1\\=1\\]",    [{any:"\\[x=1\\=1\\]"}]                                                  ],
		[" a\\ b  ",         [{any:"a\\ b"}]                                                          ], 		
		
		["[x=1][y=2]",       [{any:"*", transformation:[{filter:"x=1"},{filter:"y=2"}]}]              ],

		[":a",               [{any:"*", transformation:[{pseudo:"a"}]}]                               ],
		[":a:rfg ",          [{any:"*", transformation:[{pseudo:"a"}, {pseudo:"rfg"}]}]               ],
		["[x=1]:a",          [{any:"*", transformation:[{filter:"x=1"}, {pseudo:"a"}]}]               ]
	]);

	it('"[x=1]b:a" -> error',function(){
		assert.throws(()=>{parse("[x=1]b:a")}, Errors.CHARS_AFTER_TRANSFORMATION);
	});
	it('"..a" -> error',function(){
		assert.throws(()=>{parse("..a")}, Errors.DOT_WITHOUT_LEVEL_NAME);
	});
	it('"[[" -> error',function(){
		assert.throws(()=>{parse("[[")}, Errors.FILTER_OPEN_FAIL);
	});
	it('"]" -> error',function(){
		assert.throws(()=>{parse("]")}, Errors.FILTER_CLOSE_FAIL);
	});
});

describe('parse_filter', function(){
	test2(parse_filter, [
		[undefined,          {left:""}                                              ],
		['',                 {left:""}                                              ],
		["a",                {left:"a"}                                             ], 
		["=b",               {left:"",    right:'b',   delimiter:'='}               ], 
		["a=b",              {left:"a",   right:'b',   delimiter:'='}               ], 
		[" x y = b c ",      {left:"x y", right:'b c', delimiter:'='}               ], 
		["a=b=c",            {left:"a",   right:'b=c', delimiter:'='}               ], 
		["a\\=b=c",          {left:"a=b", right:'c',   delimiter:'='}               ], 
		["a=b\\=c",          {left:"a",   right:'b=c', delimiter:'='}               ],
		['a*=b',             {left:"a",   right:'b',   delimiter:'*='}              ]
	]);
});

describe('simple get 0 level', function(){
	test_get([
		["",  1,     [1]      ], 
		["",  {a:1}, [{a:1}]  ] 
	]);
});

describe('simple get 1 level', function(){
	test_get([
		["*",     1,                            [1]                                       ], 
		["*",     {a:1},                        [{a:1}, 1]                                ], 
		["*",     {a:{c:1}, b:2},               [{a:{c:1},b:2}, {c:1}, 1, 2]              ], 
		
		["a",     1,                            []                                        ], 
		["a",     {b:1},                        []                                        ], 
		["a",     {a:1},                        [1]                                       ], 
		["a",     {a:[1, 2]},                   [1, 2]                                    ], 
		["a",     {a:{b:"x"}},                  [{b:"x"}]                                 ], 
		["a",     {b:{bb:"xxx", a:1}},          [1]                                       ], 
		["a",     [{a:1}, {a:2}, {b:3}],        [1,2]                                     ], 
		["a",     [[{a:1}, {a:2}], [{a:3}]],    [1,2,3]                                   ], 
		["a",     [[[{a:1}, {a:2}], [{a:3}]],[[{a:4}, {a:5}],[{a:6}]]],     [1,2,3,4,5,6] ], 

		["a",     {a:{a:[1, 2]}},               [{a:[1, 2]}, 1, 2]                        ],
		[".a",    {a:{a:[1, 2]}},               [{a:[1, 2]}]                              ],
		["a",     [[[[[{a:1}]]]]],              [1]                                       ]
	]);
});

describe('simple get 2 level', function(){
	test_get([
		["a.b",   {a:{b:"x"}},                  ["x"]                                     ], 

		["a *",   {x:{a:1, z:{a:2}}},           [1, 2]                                    ], 
		["a b",   {a:{b:{c:1}}},                [{c:1}]                                   ], 
		["a b",   {a:{c:{b:1}}},                [1]                                       ],
		
		["* a",   {a:{a:1}},                    [{"a":1},1]                               ],
		["a *",   {a:{a:1}},                    [{"a":1},1]                               ]
	]);
});

describe('simple get 3 level', function(){
	let o = {
		a:{
			b:{
				b:{
					c:1, 
				}, 
				c:{
					d:2
				}
			}, 
			c:0,
			x:{
				b:{
					c:3 
				} 
			}
		}
	}
	test_get([
		["a.b.c", {a:{b:{c:[1,2]}}},            [1,2]          ], 
		["a.b.c", {a:{b:[ {c:1}, {c:2} ]}},     [1,2]          ], 
		["a.b.c", {a:[ {b:{c:1}}, {b:{c:2}} ]}, [1,2]          ], 
		["a.b.c", o,                            [{d:2}]        ], 
		["a.b c", o,                            [{d:2}, 1]     ],
		["a b c", o,                            [{d:2}, 1, 3]  ],
		["a b c", {a:{b:{b:{c:1}}}},            [1]            ],
		["a b c", {a:{b:{b:{c:{z:1}}}}},        [{z:1}]        ],

		[".*", {a:1, b:2, c:{d:3}},             [1,2,{d:3}]    ],

		["a * c", {a:{b:{c:[1,2]},c:3}},        [3,1,2]        ],
		[".a.*.c", {a:{b:{c:[1,2]},c:3}},       [1,2]          ]
	]);
});

describe('pseudo', function(){
	test_get([
		[':empty',  {a:{b:1}},  [1]   ]
	]);
});

describe('filter', function(){
	let o = {
		x:{
			a:[
{'fraction_non_alphanumeric': 0.26823030834429346, 'fraction_numerical': 0.023308490428174778, 'mean_word_length': 1.1306039850560399, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 90, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5761232', 'n_tokens_mistral': 5239, 'n_tokens_neox': 4697, 'n_words': 773}