Skip to content

Commit

Permalink
Allow testing a negated directive selector
Browse files Browse the repository at this point in the history
Related to #983
  • Loading branch information
martijnversluis committed Dec 15, 2024
1 parent 32e6351 commit b67ff61
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 11 deletions.
22 changes: 18 additions & 4 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,30 @@ export function getKeys(key: Key | string): string[] {
return keyObj.isMinor() ? minorKeys[chordType] : majorKeys[chordType];
}

export function testSelector(selector: string, configuration: Configuration, metadata: Metadata) {
export function testSelector(
{
selector,
isNegated,
configuration,
metadata,
}:
{
selector: string,
isNegated: boolean,
configuration: Configuration,
metadata: Metadata
},
) {
if (selector === configuration.instrument?.type) {
return true;
return !isNegated;
}

if (selector === configuration.user?.name) {
return true;
return !isNegated;
}

const metadataValue = metadata.getSingle(selector);
const metadataValueTruthy = metadataValue && metadataValue !== '';

return !!(metadataValue && metadataValue !== '');
return isNegated ? !metadataValueTruthy : !!metadataValueTruthy;
}
163 changes: 156 additions & 7 deletions test/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,48 +37,197 @@ describe('testSelector', () => {
const configuration = new Configuration({ instrument: { type: 'guitar' } });
const metadata = new Metadata();

expect(testSelector('guitar', configuration, metadata)).toEqual(true);
expect(
testSelector({
selector: 'guitar',
isNegated: false,
configuration,
metadata,
}),
).toEqual(true);
});

it('returns false when the selector does not match the configured instrument type', () => {
const configuration = new Configuration({ instrument: { type: 'guitar' } });
const metadata = new Metadata();

expect(testSelector('piano', configuration, metadata)).toEqual(false);
expect(
testSelector({
selector: 'piano',
isNegated: false,
configuration,
metadata,
}),
).toEqual(false);
});

it('return true when the selector matches the configured user name', () => {
const configuration = new Configuration({ user: { name: 'john' } });
const metadata = new Metadata();

expect(testSelector('john', configuration, metadata)).toEqual(true);
expect(
testSelector({
selector: 'john',
isNegated: false,
configuration,
metadata,
}),
).toEqual(true);
});

it('returns false when the selector does not match the configured user name', () => {
const configuration = new Configuration({ user: { name: 'john' } });
const metadata = new Metadata();

expect(testSelector('jane', configuration, metadata)).toEqual(false);
expect(
testSelector({
selector: 'jane',
isNegated: false,
configuration,
metadata,
}),
).toEqual(false);
});

it('returns true when the selector matches a truthy metadata value', () => {
const configuration = new Configuration();
const metadata = new Metadata({ 'horns': 'true' });

expect(testSelector('horns', configuration, metadata)).toEqual(true);
expect(
testSelector({
selector: 'horns',
isNegated: false,
configuration,
metadata,
}),
).toEqual(true);
});

it('returns false when the selector matches an empty metadata value', () => {
const configuration = new Configuration();
const metadata = new Metadata({ 'horns': '' });

expect(testSelector('horns', configuration, metadata)).toEqual(false);
expect(
testSelector({
selector: 'horns',
isNegated: false,
configuration,
metadata,
}),
).toEqual(false);
});

it('returns false when the selector does not match a metadata value', () => {
const configuration = new Configuration();
const metadata = new Metadata();

expect(testSelector('horns', configuration, metadata)).toEqual(false);
expect(
testSelector({
selector: 'horns',
isNegated: false,
configuration,
metadata,
}),
).toEqual(false);
});

describe('when negated', () => {
it('returns false when the selector matches the configured instrument type', () => {
const configuration = new Configuration({ instrument: { type: 'guitar' } });
const metadata = new Metadata();

expect(
testSelector({
selector: 'guitar',
isNegated: true,
configuration,
metadata,
}),
).toEqual(false);
});

it('returns true when the selector does not match the configured instrument type', () => {
const configuration = new Configuration({ instrument: { type: 'guitar' } });
const metadata = new Metadata();

expect(
testSelector({
selector: 'piano',
isNegated: true,
configuration,
metadata,
}),
).toEqual(true);
});

it('returns false when the selector matches the configured user name', () => {
const configuration = new Configuration({ user: { name: 'john' } });
const metadata = new Metadata();

expect(
testSelector({
selector: 'john',
isNegated: true,
configuration,
metadata,
}),
).toEqual(false);
});

it('returns true when the selector does not match the configured user name', () => {
const configuration = new Configuration({ user: { name: 'john' } });
const metadata = new Metadata();

expect(
testSelector({
selector: 'jane',
isNegated: true,
configuration,
metadata,
}),
).toEqual(true);
});

it('returns false when the selector matches a truthy metadata value', () => {
const configuration = new Configuration();
const metadata = new Metadata({ 'horns': 'true' });

expect(
testSelector({
selector: 'horns',
isNegated: true,
configuration,
metadata,
}),
).toEqual(false);
});

it('returns true when the selector matches an empty metadata value', () => {
const configuration = new Configuration();
const metadata = new Metadata({ 'horns': '' });

expect(
testSelector({
selector: 'horns',
isNegated: true,
configuration,
metadata,
}),
).toEqual(true);
});

it('returns true when the selector does not match a metadata value', () => {
const configuration = new Configuration();
const metadata = new Metadata();

expect(
testSelector({
selector: 'horns',
isNegated: true,
configuration,
metadata,
}),
).toEqual(true);
});
});
});

0 comments on commit b67ff61

Please sign in to comment.