Skip to content

Commit

Permalink
added test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
kurrx committed Oct 17, 2024
1 parent ef598e0 commit dfb2481
Showing 1 changed file with 334 additions and 1 deletion.
335 changes: 334 additions & 1 deletion packages/agtree/test/parser/misc/ubo-parameter-list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,121 @@ import { NodeExpectContext, type NodeExpectFn } from '../../helpers/node-utils';
import { AdblockSyntaxError } from '../../../src/errors/adblock-syntax-error';
import { UboParameterListParser } from '../../../src/parser/misc/ubo-parameter-list';
import { defaultParserOptions } from '../../../src/parser/options';
import { type ParameterList } from '../../../src/parser/common';

describe('UboParameterListParser', () => {
// valid cases are tested in `../cosmetic/body/ubo-scriptlet.test.ts`
describe('UboParameterListParser.parse - valid cases when requireQuotes enabled', () => {
test.each<{ actual: string; expected: ParameterList }>([
{
actual: String.raw`'abc'`,
expected: {
type: 'ParameterList',
start: 0,
end: 5,
children: [
{
type: 'Value',
start: 0,
end: 5,
value: String.raw`'abc'`,
},
],
},
},
{
actual: String.raw` '' `,
expected: {
type: 'ParameterList',
start: 0,
end: 5,
children: [
{
type: 'Value',
start: 1,
end: 3,
value: String.raw`''`,
},
],
},
},
{
actual: String.raw`'abc', 'cba'`,
expected: {
type: 'ParameterList',
start: 0,
end: 12,
children: [
{
type: 'Value',
start: 0,
end: 5,
value: String.raw`'abc'`,
},
{
type: 'Value',
start: 7,
end: 12,
value: String.raw`'cba'`,
},
],
},
},
{
actual: String.raw` '' , '' `,
expected: {
type: 'ParameterList',
start: 0,
end: 12,
children: [
{
type: 'Value',
start: 1,
end: 3,
value: String.raw`''`,
},
{
type: 'Value',
start: 8,
end: 10,
value: String.raw`''`,
},
],
},
},
{
actual: String.raw` "" , "", "" `,
expected: {
type: 'ParameterList',
start: 0,
end: 17,
children: [
{
type: 'Value',
start: 1,
end: 3,
value: String.raw`""`,
},
{
type: 'Value',
start: 8,
end: 10,
value: String.raw`""`,
},
{
type: 'Value',
start: 13,
end: 15,
value: String.raw`""`,
},
],
},
},
])("should parse correctly on input '$actual'", ({ actual, expected }) => {
const result = UboParameterListParser.parse(actual, defaultParserOptions, 0, COMMA, true);

expect(result).toMatchObject(expected);
});
});

describe('UboParameterListParser.parse - invalid cases when requireQuotes enabled', () => {
test.each<{ actual: string; expected: NodeExpectFn<AdblockSyntaxError> }>([
Expand Down Expand Up @@ -59,6 +171,16 @@ describe('UboParameterListParser', () => {
);
},
},
{
actual: String.raw`'abc' 'bbb'`,
// ~~~~~
expected: (context: NodeExpectContext): AdblockSyntaxError => {
return new AdblockSyntaxError(
"Expected separator, got: '''",
...context.toTuple(context.getRangeFor(String.raw`'bbb'`)),
);
},
},
])("should throw on input: '$actual'", ({ actual, expected: expectedFn }) => {
const fn = jest.fn(() => UboParameterListParser.parse(actual, defaultParserOptions, 0, COMMA, true));

Expand All @@ -75,4 +197,215 @@ describe('UboParameterListParser', () => {
expect(error).toHaveProperty('end', expected.end);
});
});

describe('UboParameterListParser.parse - valid cases when requireQuotes disabled', () => {
test.each<{ actual: string; expected: ParameterList; }>([
{
actual: String.raw`abc`,
expected: {
type: 'ParameterList',
start: 0,
end: 3,
children: [
{
type: 'Value',
start: 0,
end: 3,
value: String.raw`abc`,
},
],
},
},
{
actual: String.raw`abc, cba`,
expected: {
type: 'ParameterList',
start: 0,
end: 8,
children: [
{
type: 'Value',
start: 0,
end: 3,
value: String.raw`abc`,
},
{
type: 'Value',
start: 5,
end: 8,
value: String.raw`cba`,
},
],
},
},
{
actual: String.raw`abc, cba,`,
expected: {
type: 'ParameterList',
start: 0,
end: 9,
children: [
{
type: 'Value',
start: 0,
end: 3,
value: String.raw`abc`,
},
{
type: 'Value',
start: 5,
end: 8,
value: String.raw`cba`,
},
null,
],
},
},
{
actual: String.raw`abc, cba, `,
expected: {
type: 'ParameterList',
start: 0,
end: 11,
children: [
{
type: 'Value',
start: 0,
end: 3,
value: String.raw`abc`,
},
{
type: 'Value',
start: 5,
end: 8,
value: String.raw`cba`,
},
null,
],
},
},
{
actual: String.raw`abc, , cba,`,
expected: {
type: 'ParameterList',
start: 0,
end: 11,
children: [
{
type: 'Value',
start: 0,
end: 3,
value: String.raw`abc`,
},
null,
{
type: 'Value',
start: 7,
end: 10,
value: String.raw`cba`,
},
null,
],
},
},
{
actual: String.raw`'abc `,
expected: {
type: 'ParameterList',
start: 0,
end: 7,
children: [
{
type: 'Value',
start: 0,
end: 4,
value: String.raw`'abc`,
},
],
},
},
{
actual: String.raw`abc, ').cba='1'`,
expected: {
type: 'ParameterList',
start: 0,
end: 15,
children: [
{
type: 'Value',
start: 0,
end: 3,
value: String.raw`abc`,
},
{
type: 'Value',
start: 5,
end: 15,
value: String.raw`').cba='1'`,
},
],
},
},
{
actual: String.raw`abc, ').cba, '1'`,
expected: {
type: 'ParameterList',
start: 0,
end: 16,
children: [
{
type: 'Value',
start: 0,
end: 3,
value: String.raw`abc`,
},
{
type: 'Value',
start: 5,
end: 11,
value: String.raw`').cba`,
},
{
type: 'Value',
start: 13,
end: 16,
value: String.raw`'1'`,
},
],
},
},
{
actual: String.raw`abc, ').cba='1', cba`,
expected: {
type: 'ParameterList',
start: 0,
end: 20,
children: [
{
type: 'Value',
start: 0,
end: 3,
value: String.raw`abc`,
},
{
type: 'Value',
start: 5,
end: 15,
value: String.raw`').cba='1'`,
},
{
type: 'Value',
start: 17,
end: 20,
value: String.raw`cba`,
},
],
},
},
])("should parse correctly on input '$actual'", ({ actual, expected }) => {
const result = UboParameterListParser.parse(actual, defaultParserOptions, 0, COMMA, false);

expect(result).toMatchObject(expected);
});
});
});

0 comments on commit dfb2481

Please sign in to comment.