Skip to content

Commit

Permalink
tests and small fixes [patch]
Browse files Browse the repository at this point in the history
  • Loading branch information
mmisty committed Nov 2, 2023
1 parent dfbfb32 commit e59beaa
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 13 deletions.
10 changes: 10 additions & 0 deletions integration/e2e/env-should-keep-inline.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { registerTags } from 'cy-local/setup';

Cypress.env('cyTagsShowTagsInTitle', false);
registerTags();

describe('should keep tags in object when inline tags and show taga is false', () => {
it('one @tag("info1")', function () {
expect(this.test?.tags).to.deep.eq([{ tag: '@tag', info: ['info1'] }]);
});
});
35 changes: 35 additions & 0 deletions integration/e2e/info.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { tag } from 'cy-local/utils/tags';
import { registerTags } from 'cy-local/setup';

[true, false].forEach(cyTagsShowTagsInTitle => {
Cypress.env('cyTagsShowTagsInTitle', cyTagsShowTagsInTitle);
registerTags();

describe(`should have proper infos when cyTagsShowTagsInTitle is ${cyTagsShowTagsInTitle}`, () => {
it('one @tag("info1")', function () {
expect(this.test?.tags).to.deep.eq([{ tag: '@tag', info: ['info1'] }]);
});

it('several @tag("info1","info2")', function () {
expect(this.test?.tags).to.deep.eq([{ tag: '@tag', info: ['info1', 'info2'] }]);
});

it('several with spaces @tag("info1 one", "info2 two")', function () {
console.log(this.test?.tags);
expect(this.test?.tags).to.deep.eq([{ tag: '@tag', info: ['info1 one', 'info2 two'] }]);
});

it('one with spaces @tag("some info with spaces")', function () {
expect(this.test?.tags).to.deep.eq([{ tag: '@tag', info: ['some info with spaces'] }]);
});

it('one with special symbols', { tags: [{ tag: '@tag', info: ['Special symbol $%^@@$`'] }] }, function () {
console.log(this.test?.tags);
expect(this.test?.tags).to.deep.eq([{ tag: '@tag', info: ['Special symbol $%^@@$`'] }]);
});

it(`one with special symbols (helper for title) ${tag('tag', 'Special symbol $%^@@$`')} `, function () {
expect(this.test?.tags).to.deep.eq([{ tag: '@tag', info: ['Special symbol $%^@@$`'] }]);
});
});
});
35 changes: 27 additions & 8 deletions src/setup/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parseInlineTags, removeTagsFromTitle, uniqTags } from '../utils/tags';
import { encodeTagInfo, parseInlineTags, removeTagsFromTitle, uniqTags } from '../utils/tags';
import GrepTagObject = Mocha.GrepTagObject;

export const origins = () => ({
Expand Down Expand Up @@ -26,11 +26,20 @@ export const registerTags = () => {
const parseAll = (testOrSuite: Mocha.Suite | Mocha.Test, res: GrepTagObject[]): GrepTagObject[] => {
const objTags = (testOrSuite as unknown as TestConfig)._testConfig?.tags;

const tagsArr = objTags ? (typeof objTags === 'string' ? [objTags] : objTags ?? []) : [];
const tagsArr = objTags ? (typeof objTags === 'string' ? [objTags] : objTags) : [];

const fromConfig = tagsArr.flatMap(t => {
if (typeof t === 'string') {
return parseInlineTags(t);
}

if (t.tag && typeof t.tag === 'string') {
return [{ info: [], ...t }];
}

return [{ tag: 'unknown type of tag' }];
});

const fromConfig = tagsArr.flatMap(t =>
typeof t === 'string' ? parseInlineTags(t) : t.tag ? [{ info: [], ...t }] : [{ tag: 'unknown type of tag' }],
);
const inlineTagsTest = parseInlineTags(testOrSuite.title);
res = [...res, ...fromConfig, ...inlineTagsTest];

Expand All @@ -42,9 +51,19 @@ export const registerTags = () => {
};

const tagsLineForTitle = (tags: GrepTagObject[]): string => {
const info = (inf: string[] | undefined) => (inf && inf.length > 0 ? `("${inf.join('","')}")` : '');
const infoChange = (inf: string[] | undefined): string => (inf && inf.length > 0 ? `("${inf.join('","')}")` : '');

const encodeWhenSpecialChars = (info: string[] | undefined): string => {
return infoChange(
info?.map(t => {
const pattern = /[$^%@`]/;

return pattern.test(t) ? encodeTagInfo(t) : t;
}),
);
};

return tags.map(t => `${t.tag}${info(t.info)}`).join(' ');
return tags.map(t => `${t.tag}${encodeWhenSpecialChars(t.info)}`).join(' ');
};

/**
Expand All @@ -68,7 +87,7 @@ export const registerTags = () => {
};

const testProcess = (test: Mocha.Test) => {
test.tags = parseAll(test, []);
test.tags = uniqTags([...(test.tags ?? []), ...parseAll(test, [])]);

if (showTagsInTitle() === undefined) {
return;
Expand Down
18 changes: 13 additions & 5 deletions src/utils/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ const regexDiffQuotes = ['"', "'"].map(t => `(@[^@ ]+\\(${t}[^@${t}]+${t}(?:,\\s
const tagsRegex = new RegExp(`${regexDiffQuotes.join('|')}|${regexTagsNoReasons}`, 'g');

export const uniqTags = (arr: GrepTagObject[]): GrepTagObject[] => {
const stringifyTagObj = (o: GrepTagObject) => {
return JSON.stringify({
tags: o.tag,
info: o.info,
});
};

return arr.filter((obj, index, self) => {
const indexCurrent = self.map(s => JSON.stringify(s)).indexOf(JSON.stringify(obj));
const indexCurrent = self.map(s => stringifyTagObj(s)).indexOf(stringifyTagObj(obj));

return indexCurrent === index;
});
Expand Down Expand Up @@ -46,8 +53,8 @@ const encodeDecode = (str: string, isEncode: boolean) => {
return newStr;
};

const encodeFailReason = (str: string) => encodeDecode(encodeURIComponent(str), true);
const decodeFailReason = (str: string) => encodeDecode(decodeURIComponent(str), false);
export const encodeTagInfo = (str: string) => encodeDecode(encodeURIComponent(str), true);
export const decodeTagInfo = (str: string) => encodeDecode(decodeURIComponent(str), false);

export const parseOneTag = (tg: string): GrepTagObject => {
const reasons: string[] = [];
Expand All @@ -60,9 +67,10 @@ export const parseOneTag = (tg: string): GrepTagObject => {
reasons.push(
...reasonsMatch[1]
.split(',')
.map(p => p.trim())
.map(p => p.replace(/^"(.*)"$/, '$1'))
.map(p => p.replace(/^'(.*)'$/, '$1'))
.map(k => decodeFailReason(k)),
.map(k => decodeTagInfo(k)),
);
}
const tagMatch = tg.match(regexpTag);
Expand Down Expand Up @@ -104,7 +112,7 @@ export const parseTags = (str: string): GrepTagObject[] => {
* });
*/
export const tag = (name: string, ...info: string[]): string => {
const encodedReasons = info.map(inf => encodeFailReason(inf));
const encodedReasons = info.map(inf => encodeTagInfo(inf));
const reasonsSeparatedByComma = encodedReasons.map(r => `"${r}"`).join(',');
const reasonsStr = encodedReasons.length > 0 ? `(${reasonsSeparatedByComma})` : '';

Expand Down

0 comments on commit e59beaa

Please sign in to comment.