Skip to content

Commit

Permalink
test: create tests for textlint-rule-allowed-uris.js
Browse files Browse the repository at this point in the history
and fix typo in tests/utils/getDefinitionNodeUriType.spec.js
  • Loading branch information
lumirlumir committed Aug 15, 2024
1 parent b441e4e commit 8aab2cf
Show file tree
Hide file tree
Showing 3 changed files with 330 additions and 1 deletion.
279 changes: 279 additions & 0 deletions tests/textlint-rule-allowed-uris.data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
/**
* `testCases` for `textlint-rule-allowed-uris.spec.js`
*/
module.exports = [
/* Totally empty */
{
options: {
// allowed: {
// links: [],
// images: [],
// },
// disallowed: {
// links: [],
// images: [],
// },
},
lines: [],
},
{
options: {
allowed: {
// links: [],
// images: [],
},
// disallowed: {
// links: [],
// images: [],
// },
},
lines: [],
},
{
options: {
// allowed: {
// links: [],
// images: [],
// },
disallowed: {
// links: [],
// images: [],
},
},
lines: [],
},
{
options: {
allowed: {
// links: [],
// images: [],
},
disallowed: {
// links: [],
// images: [],
},
},
lines: [],
},

/* Pass empty array */
{
options: {
allowed: {
links: [], // Act like an whitelist. i.e. Empty array allows nothing. Every `links` will be detected.
// images: [],
},
disallowed: {
// links: [],
// images: [],
},
},
lines: [7, 9, 13, 15, 19, 21, 23, 25, 31, 37, 43, 47, 86, 101, 103, 105],
},
{
options: {
allowed: {
// links: [],
images: [], // Act like an whitelist. i.e. Empty array allows nothing. Every `images` will be detected.
},
disallowed: {
// links: [],
// images: [],
},
},
lines: [53, 59, 65, 71, 75, 77, 79, 86, 105],
},
{
options: {
allowed: {
links: [], // Act like an whitelist. i.e. Empty array allows nothing. Every `links` will be detected.
images: [], // Act like an whitelist. i.e. Empty array allows nothing. Every `images` will be detected.
},
disallowed: {
// links: [],
// images: [],
},
},
lines: [
7, 9, 13, 15, 19, 21, 23, 25, 31, 37, 43, 47, 53, 59, 65, 71, 75, 77, 79, 86, 86,
101, 103, 105, 105,
], // `86` and `105` are same line number, but detected twice. one for `links` and the other for `images`
},
{
options: {
allowed: {
// links: [],
// images: [],
},
disallowed: {
links: [], // Act like a blacklist. i.e. Empty array allows everything. No `links` will be detected.
// images: [],
},
},
lines: [],
},
{
options: {
allowed: {
// links: [],
// images: [],
},
disallowed: {
// links: [],
images: [], // Act like a blacklist. i.e. Empty array allows everything. No `images` will be detected.
},
},
lines: [],
},
{
options: {
allowed: {
// links: [],
// images: [],
},
disallowed: {
links: [], // Act like a blacklist. i.e. Empty array allows everything. No `images` will be detected.
images: [], // Act like a blacklist. i.e. Empty array allows everything. No `images` will be detected.
},
},
lines: [],
},

/* Pass values */
// `allowed.links`
{
options: {
allowed: {
links: [/google/],
// images: [],
},
// disallowed: {
// links: [],
// images: [],
// },
},
lines: [9, 15, 21, 23, 25, 86, 101, 103],
},
{
options: {
allowed: {
links: [/google/, /gmail/],
// images: [],
},
// disallowed: {
// links: [],
// images: [],
// },
},
lines: [21, 23, 25, 86, 101, 103],
},
{
options: {
allowed: {
links: [/google/, /gmail/, /README.md/],
// images: [],
},
// disallowed: {
// links: [],
// images: [],
// },
},
lines: [21, 23, 86, 103],
},
// `allowed.images`
{
options: {
allowed: {
// links: [],
images: [/wiki/],
},
// disallowed: {
// links: [],
// images: [],
// },
},
lines: [],
},
// `disallowed.links`
{
options: {
// allowed: {
// links: [],
// images: [],
// },
disallowed: {
links: [/google/],
// images: [],
},
},
lines: [7, 13, 19, 31, 37, 43, 47, 105],
},
{
options: {
// allowed: {
// links: [],
// images: [],
// },
disallowed: {
links: [/google/, /gmail/],
// images: [],
},
},
lines: [7, 9, 13, 15, 19, 31, 37, 43, 47, 105],
},
{
options: {
// allowed: {
// links: [],
// images: [],
// },
disallowed: {
links: [/google/, /gmail/, /README.md/],
// images: [],
},
},
lines: [7, 9, 13, 15, 19, 25, 31, 37, 43, 47, 101, 105],
},
// `disallowed.images`
{
options: {
// allowed: {
// links: [],
// images: [],
// },
disallowed: {
// links: [],
images: [/wiki/],
},
},
lines: [53, 59, 65, 71, 75, 77, 79, 86, 105],
},

/* Pass same values to `allowed` and `disallowed` */
{
options: {
allowed: {
links: [/google/],
// images: [],
},
disallowed: {
links: [/google/],
// images: [],
},
},
lines: [7, 9, 13, 15, 19, 21, 23, 25, 31, 37, 43, 47, 86, 101, 103, 105], // Every `links` will be detected.
},
{
options: {
allowed: {
// links: [],
images: [/wiki/],
},
disallowed: {
// links: [],
images: [/wiki/],
},
},
lines: [53, 59, 65, 71, 75, 77, 79, 86, 105], // Every `images` will be detected.
},
];
50 changes: 50 additions & 0 deletions tests/textlint-rule-allowed-uris.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const fs = require('fs');
const path = require('path');
const TextLintTester = require('textlint-tester').default;
const allowedUris = require('../src/textlint-rule-allowed-uris');
const testCases = require('./textlint-rule-allowed-uris.data');

const tester = new TextLintTester();
const testCasesMarkdown = fs.readFileSync(
path.resolve(__dirname, 'textlint-rule-allowed-uris.data.md'),
'utf-8',
);

/**
* Tests for the `textlint-rule-allowed-uris.js`
*/
describe('textlint-rule-allowed-uris', () => {
testCases.forEach(({ options, lines }) => {
/* Initialization */
const testConfig = {
rules: [
{
ruleId: 'textlint-rule-allowed-uris',
rule: allowedUris,
options,
},
],
};
const invalid = [
{
text: testCasesMarkdown,
errors: lines.map(line => ({ line })),
},
];

/* Test */
tester.run(
`
allowed links: ${options?.allowed?.links?.join(' or ')}
allowed images: ${options?.allowed?.images?.join(' or ')}
disallowed links: ${options?.disallowed?.links?.join(' or ')}
disallowed images: ${options?.disallowed?.links?.join(' or ')}
lines: ${lines.join(', ')}
`,
testConfig,
{
invalid,
},
);
});
});
2 changes: 1 addition & 1 deletion tests/utils/getDefinitionNodeUriType.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const testCases = require('./getDefinitionNodeUriType.data');
const getDefinitionNodeUriType = require('../../src/utils/getDefinitionNodeUriType');

/**
* Test for the `getDefinitionNodeUriType.js`
* Tests for the `getDefinitionNodeUriType.js`
*/
describe('Util getDefinitionNodeUriType strictEqual testing', () => {
testCases.forEach(testCase => {
Expand Down

0 comments on commit 8aab2cf

Please sign in to comment.