Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
apendua committed Aug 21, 2024
1 parent d49eed5 commit b819a75
Show file tree
Hide file tree
Showing 30 changed files with 1,577 additions and 0 deletions.
131 changes: 131 additions & 0 deletions tests/baselines/reference/referencesForSpecializeTag.baseline.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// === findAllReferences ===
// === /a.js ===
// --- (line: 3) skipped ---
// class Collection {}
//
// /**
// * <|@typedef {object} [|{| isWriteAccess: true, isDefinition: true |}U/*FIND ALL REFS*/serData|]
// * @property {string} id
// * @property {string} name
// |>*/
//
// /** @specialize <[|UserData|]> */
// const users = new Collection('users');

// === Definitions ===
// === /a.js ===
// --- (line: 3) skipped ---
// class Collection {}
//
// /**
// * <|@typedef {object} [|U/*FIND ALL REFS*/serData|]
// * @property {string} id
// * @property {string} name
// |>*/
//
// /** @specialize <UserData> */
// const users = new Collection('users');

// === Details ===
[
{
"containerKind": "",
"containerName": "",
"kind": "type",
"name": "type UserData = {\n id: string;\n name: string;\n}",
"displayParts": [
{
"text": "type",
"kind": "keyword"
},
{
"text": " ",
"kind": "space"
},
{
"text": "UserData",
"kind": "aliasName"
},
{
"text": " ",
"kind": "space"
},
{
"text": "=",
"kind": "operator"
},
{
"text": " ",
"kind": "space"
},
{
"text": "{",
"kind": "punctuation"
},
{
"text": "\n",
"kind": "lineBreak"
},
{
"text": " ",
"kind": "space"
},
{
"text": "id",
"kind": "propertyName"
},
{
"text": ":",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "string",
"kind": "keyword"
},
{
"text": ";",
"kind": "punctuation"
},
{
"text": "\n",
"kind": "lineBreak"
},
{
"text": " ",
"kind": "space"
},
{
"text": "name",
"kind": "propertyName"
},
{
"text": ":",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "string",
"kind": "keyword"
},
{
"text": ";",
"kind": "punctuation"
},
{
"text": "\n",
"kind": "lineBreak"
},
{
"text": "}",
"kind": "punctuation"
}
]
}
]
50 changes: 50 additions & 0 deletions tests/baselines/reference/specializeTag1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//// [tests/cases/conformance/jsdoc/specializeTag1.ts] ////

//// [specializeTag1.js]
/**
* @template T
* @param {object} _jsonSchema
* @returns {(x: unknown) => x is T}
*/
function createValidator(_jsonSchema) {
/**
* @param {unknown} _x
* @returns {_x is T}
*/
return (_x) => true;
}

/** @specialize <number> */
const isNumber = createValidator({ type: 'number' });

const isString = /** @specialize <string> */(createValidator({ type: 'string' }));


//// [specializeTag1.js]
/**
* @template T
* @param {object} _jsonSchema
* @returns {(x: unknown) => x is T}
*/
function createValidator(_jsonSchema) {
/**
* @param {unknown} _x
* @returns {_x is T}
*/
return function (_x) { return true; };
}
/** @specialize <number> */
var isNumber = createValidator({ type: 'number' });
var isString = /** @specialize <string> */ (createValidator({ type: 'string' }));


//// [specializeTag1.d.ts]
/**
* @template T
* @param {object} _jsonSchema
* @returns {(x: unknown) => x is T}
*/
declare function createValidator<T>(_jsonSchema: object): (x: unknown) => x is T;
/** @specialize <number> */
declare const isNumber: (x: unknown) => x is number;
declare const isString: (x: unknown) => x is string;
31 changes: 31 additions & 0 deletions tests/baselines/reference/specializeTag1.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//// [tests/cases/conformance/jsdoc/specializeTag1.ts] ////

=== specializeTag1.js ===
/**
* @template T
* @param {object} _jsonSchema
* @returns {(x: unknown) => x is T}
*/
function createValidator(_jsonSchema) {
>createValidator : Symbol(createValidator, Decl(specializeTag1.js, 0, 0))
>_jsonSchema : Symbol(_jsonSchema, Decl(specializeTag1.js, 5, 25))

/**
* @param {unknown} _x
* @returns {_x is T}
*/
return (_x) => true;
>_x : Symbol(_x, Decl(specializeTag1.js, 10, 12))
}

/** @specialize <number> */
const isNumber = createValidator({ type: 'number' });
>isNumber : Symbol(isNumber, Decl(specializeTag1.js, 14, 5))
>createValidator : Symbol(createValidator, Decl(specializeTag1.js, 0, 0))
>type : Symbol(type, Decl(specializeTag1.js, 14, 34))

const isString = /** @specialize <string> */(createValidator({ type: 'string' }));
>isString : Symbol(isString, Decl(specializeTag1.js, 16, 5))
>createValidator : Symbol(createValidator, Decl(specializeTag1.js, 0, 0))
>type : Symbol(type, Decl(specializeTag1.js, 16, 62))

57 changes: 57 additions & 0 deletions tests/baselines/reference/specializeTag1.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//// [tests/cases/conformance/jsdoc/specializeTag1.ts] ////

=== specializeTag1.js ===
/**
* @template T
* @param {object} _jsonSchema
* @returns {(x: unknown) => x is T}
*/
function createValidator(_jsonSchema) {
>createValidator : <T>(_jsonSchema: object) => (x: unknown) => x is T
> : ^ ^^ ^^ ^^^^^
>_jsonSchema : any

/**
* @param {unknown} _x
* @returns {_x is T}
*/
return (_x) => true;
>(_x) => true : (_x: unknown) => _x is T
> : ^ ^^ ^^^^^
>_x : unknown
> : ^^^^^^^
>true : true
> : ^^^^
}

/** @specialize <number> */
const isNumber = createValidator({ type: 'number' });
>isNumber : (x: unknown) => x is number
> : ^ ^^ ^^^^^ ^^^^^^
>createValidator({ type: 'number' }) : (x: unknown) => x is number
> : ^ ^^ ^^^^^ ^^^^^^
>createValidator : <T>(_jsonSchema: object) => (x: unknown) => x is T
> : ^ ^^ ^^ ^^^^^
>{ type: 'number' } : { type: string; }
> : ^^^^^^^^^^^^^^^^^
>type : string
> : ^^^^^^
>'number' : "number"
> : ^^^^^^^^

const isString = /** @specialize <string> */(createValidator({ type: 'string' }));
>isString : (x: unknown) => x is string
> : ^ ^^ ^^^^^ ^^^^^^
>(createValidator({ type: 'string' })) : (x: unknown) => x is string
> : ^ ^^ ^^^^^ ^^^^^^
>createValidator({ type: 'string' }) : (x: unknown) => x is string
> : ^ ^^ ^^^^^ ^^^^^^
>createValidator : <T>(_jsonSchema: object) => (x: unknown) => x is T
> : ^ ^^ ^^ ^^^^^
>{ type: 'string' } : { type: string; }
> : ^^^^^^^^^^^^^^^^^
>type : string
> : ^^^^^^
>'string' : "string"
> : ^^^^^^^^

36 changes: 36 additions & 0 deletions tests/baselines/reference/specializeTag2.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
specializeTag2.js(19,15): error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
specializeTag2.js(23,26): error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.


==== specializeTag2.js (2 errors) ====
/**
* @template T
* @param {TemplateStringsArray} strings
* @param {...T} values
* @returns {Record<string, T>}
*/
function parse(strings, ...values) {
/** @type {Record<string, T>} */
const result = {};
strings.forEach((key, i) => {
if (i < values.length) {
result[key] = values[i];
}
})
return result;
}

const query1 = /** @specialize {string} */(
parse`a=${1}b=${2}`
~
!!! error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.
)

/** @specialize {string} */
const query2 = parse`a=${1}b=${2}`; // Type error
~
!!! error TS2345: Argument of type 'number' is not assignable to parameter of type 'string'.

/** @specialize <`${number}`> */
const query3 = parse`a=${"1"}b=${"2"}`;

76 changes: 76 additions & 0 deletions tests/baselines/reference/specializeTag2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//// [tests/cases/conformance/jsdoc/specializeTag2.ts] ////

//// [specializeTag2.js]
/**
* @template T
* @param {TemplateStringsArray} strings
* @param {...T} values
* @returns {Record<string, T>}
*/
function parse(strings, ...values) {
/** @type {Record<string, T>} */
const result = {};
strings.forEach((key, i) => {
if (i < values.length) {
result[key] = values[i];
}
})
return result;
}

const query1 = /** @specialize {string} */(
parse`a=${1}b=${2}`
)

/** @specialize {string} */
const query2 = parse`a=${1}b=${2}`; // Type error

/** @specialize <`${number}`> */
const query3 = parse`a=${"1"}b=${"2"}`;


//// [specializeTag2.js]
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
return cooked;
};
/**
* @template T
* @param {TemplateStringsArray} strings
* @param {...T} values
* @returns {Record<string, T>}
*/
function parse(strings) {
var values = [];
for (var _i = 1; _i < arguments.length; _i++) {
values[_i - 1] = arguments[_i];
}
/** @type {Record<string, T>} */
var result = {};
strings.forEach(function (key, i) {
if (i < values.length) {
result[key] = values[i];
}
});
return result;
}
var query1 = /** @specialize {string} */ (parse(__makeTemplateObject(["a=", "b=", ""], ["a=", "b=", ""]), 1, 2));
/** @specialize {string} */
var query2 = parse(__makeTemplateObject(["a=", "b=", ""], ["a=", "b=", ""]), 1, 2); // Type error
/** @specialize <`${number}`> */
var query3 = parse(__makeTemplateObject(["a=", "b=", ""], ["a=", "b=", ""]), "1", "2");


//// [specializeTag2.d.ts]
/**
* @template T
* @param {TemplateStringsArray} strings
* @param {...T} values
* @returns {Record<string, T>}
*/
declare function parse<T>(strings: TemplateStringsArray, ...values: T[]): Record<string, T>;
declare const query1: Record<string, string>;
/** @specialize {string} */
declare const query2: Record<string, string>;
/** @specialize <`${number}`> */
declare const query3: Record<string, `${number}`>;
Loading

0 comments on commit b819a75

Please sign in to comment.