Adding Product Code, tests and documentation

This commit is contained in:
Nev Wylie 2019-11-11 17:05:20 -08:00
Родитель 1ecb8c1095
Коммит 3964328a92
48 изменённых файлов: 41549 добавлений и 313 удалений

329
.gitignore поставляемый
Просмотреть файл

@ -12,319 +12,42 @@
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
# Visual Studio 2015/2017 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# Visual Studio 2017 auto generated files
Generated\ Files/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUNIT
*.VisualState.xml
TestResult.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# Benchmark Results
BenchmarkDotNet.Artifacts/
# .NET Core
project.lock.json
project.fragment.lock.json
artifacts/
**/Properties/launchSettings.json
# StyleCop
StyleCopReport.xml
# Files built by Visual Studio
*_i.c
*_p.c
*_i.h
*.ilk
*.meta
*.obj
*.iobj
*.pch
*.pdb
*.ipdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
# Build log files
*.err
*.prf
*.wrn
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# Visual Studio Trace Files
*.e2e
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# JustCode is a .NET coding add-in
.JustCode
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# AxoCover is a Code Coverage Tool
.axoCover/*
!.axoCover/settings.json
# Visual Studio code coverage results
*.coverage
*.coveragexml
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# The packages folder can be ignored because of Package Restore
**/[Pp]ackages/*
# except build/, which is used as an MSBuild target.
!**/[Pp]ackages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/[Pp]ackages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
*.appx
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
orleans.codegen.cs
# Including strong name files can present a security risk
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
#*.snk
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
ServiceFabricBackup/
*.rptproj.bak
# SQL Server files
*.mdf
*.ldf
*.ndf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
*.rptproj.rsuser
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
# Temp directories
bundle/
node_modules/
out/
dist-esm/
dist/
# Visual Studio 6 build log
*.plg
lib/bundle/
lib/dist-esm/
lib/dist/
# Visual Studio 6 workspace options file
*.opt
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw
# Grunt
.tscache
**/Test/**/*tests.js
**/Test/**/*tests.d.ts
**/Test/**/*tests.js.map
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Output of 'npm pack'
*.tgz
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# Rush files
common/temp/**
package-deps.json
package-lock.json
# JetBrains Rider
.idea/
*.sln.iml
# CodeRush
.cr/
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
# Cake - Uncomment if you are using it
# tools/**
# !tools/packages.config
# Tabs Studio
*.tss
# Telerik's JustMock configuration file
*.jmconfig
# BizTalk build output
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs
# OpenCover UI analysis results
OpenCover/
# Azure Stream Analytics local run output
ASALocalRun/
# MSBuild Binary and Structured Log
*.binlog
# NVidia Nsight GPU debugger configuration file
*.nvuser
# MFractors (Xamarin productivity tool) working folder
.mfractor/
# test output

18
.npmignore Normal file
Просмотреть файл

@ -0,0 +1,18 @@
# NPM Ignore
# ignore everything
*
# ... but these files
!/package.json
!/README.md
!/CONTRIBUTING.md
!/CODE_OF_CONDUCT.md
!/LICENSE
!/SECURITY
!/lib/bundle/dynamicproto-js.*
!/lib/dynamicproto-js.js
!/lib/dynamicproto-js.js.map
!/lib/dynamicproto-js.min.js
!/lib/dynamicproto-js.min.js.map
!/lib/**/*

15
.travis.yml Normal file
Просмотреть файл

@ -0,0 +1,15 @@
language: node_js
node_js:
- "10.9"
before_install:
- npm i -g npm@4.5.0
- npm i -g @microsoft/rush
- npm i -g grunt-cli
jobs:
include:
- stage: "Build and Test: DynamicProto JS"
name: "Build and Test: DynamicProto JS"
script:
- npm install
- npm run build
- npm run test

39
CONTRIBUTING.md Normal file
Просмотреть файл

@ -0,0 +1,39 @@
## Contributing
Welcome and thank you for your interest in contributing to DynamicProto-JS.
This project welcomes contributions and suggestions. Most contributions require you to agree to a
Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
When you submit a pull request, a CLA bot will automatically determine whether you need to provide
a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
## Clone, Setup, Build & Test this repo
1. Clone the repository and create a new branch
2. Install all dependencies
```sh
npm install
npm install -g @microsoft/rush
```
3. Navigate to the root folder and update rush dependencies
```sh
rush update
```
4. Build, lint, create docs and run tests
```sh
rush build
npm run test
```
If you are changing package versions or adding/removing any package dependencies, run<br>**```rush update --purge --recheck --full```**<br>before building. Please check-in any files that change under common\ folder.

136
README.md
Просмотреть файл

@ -1,14 +1,130 @@
# Dynamic Proto JavaScript
# Contributing
Generates dynamic prototype methods for JavaScript objects (classes) by supporting method definition within their "class" constructor (like an instance version), this removes the need to expose internal properties on the instance (this) and the usage of ```ClassName.prototype.funcName()``` both of which result in better code minfication (smaller output) and therefore improved load times for your users.
This project welcomes contributions and suggestions. Most contributions require you to agree to a
Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.
The dynamically generated prototype methods support class inheritance of any type, which means you can extend from base classes that use instance or prototype defined methods, you also don't need to add the normal boiler plate code to handle detecting, saving and calling any previous instance methods that you are overriding as support for this is provided automatically.
When you submit a pull request, a CLA bot will automatically determine whether you need to provide
a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions
provided by the bot. You will only need to do this once across all repos using our CLA.
So whether creating a new class or extending some other class/code, your resulting code, can be successfully extended via TypeScript or JavaScript.
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
## Removing / Hiding internal properties from instance
By defining the properties / methods within the constructors closure, each instance can contain or define internal state in the form of properties which it does not have to expose publically as each defined "public" instance method has direct access to this define state within the context/scope of the closure method.
While this does require some additional CPU and memory at the point of creating each instance object this is designed to be as minimal as possible and should be outwayed by the following advantages :-
* Avoids polluting the instance (this) namespace with internal values that can cause issues with inheritence for base/super classes or even derived classes that extend your class.
* Smaller code as the internal properties and methods when defined within the instance can be minified.
* As the resulting generated code can be better minified this *should* result in a smaller minified result and therefore better load times for your users.
## When to use
While this helper was originally created to support better minification for generated code via TypeScript, it is not limited to only being used from within TypeScript.
And as with including any additional code into your project there is a trade off that you need to make before using this helper which is the size of the additional code of this utility vs the minification gains that *may* be obtained. As in most cases of creating JavaScript code for better minfication if your code doesn't expose or provide a lot of public methods or only uses un-minifiable "names" less than 2 times then the potential gains may not be worth the additional bytes.
And yes at the end of the day, if you are creating JS classes directly in Javascript you *should* be able to create a simplier one-off solution that would result in smaller output as this project needs to be generic to be able to support all use-cases.
## Basic Usage
```typescript
import dynamicProto from "@microsoft/dynamicproto-js";
class ExampleClass extends BaseClass {
constructor() {
dynamicProto(ExampleClass, this, (_self, base) => {
// This will define a function that will be converted to a prototype function
_self.newFunc = () => {
// Access any "this" instance property
if (_self.someProperty) {
...
}
}
// This will define a function that will be converted to a prototype function
_self.myFunction = () => {
// Access any "this" instance property
if (_self.someProperty) {
// Call the base version of the function that we are overriding
base.myFunction();
}
...
}
_self.initialize = () => {
...
}
// Warnings: While the following will work as _self is simply a ference to
// this, if anyone overrides myFunction() the overridden will be called first
// as the normal JavaScript method resolution will occur and the defined
// _self.initialize() function is actually gets removed from the instance and
// a proxy prototype version is created to reference the created method.
_self.initialize();
});
}
}
```
## Build & Test this repo
1. Install all dependencies
```sh
npm install
npm install -g @microsoft/rush
```
2. Navigate to the root folder and update rush dependencies
```sh
rush update
```
3. Build, lint, create docs and run tests
```sh
rush build
npm run test
```
If you are changing package versions or adding/removing any package dependencies, run<br>**```rush update --purge --recheck --full```**<br>before building. Please check-in any files that change under common\ folder.
## Performance
The minified version of this adds a negligible amount of code and loadtime to your source code and by using this library your generated code can be better minified as it removes most references of Classname.prototype.XXX methods from the generated code.
> Summary:
>
> - **~2 KB** minified (uncompressed)
## Browser Support
![Chrome](https://raw.githubusercontent.com/alrra/browser-logos/master/src/chrome/chrome_48x48.png) | ![Firefox](https://raw.githubusercontent.com/alrra/browser-logos/master/src/firefox/firefox_48x48.png) | ![IE8](https://raw.githubusercontent.com/hotoo/browser-logos/master/ie9-10/ie9-10_48x48.png) | ![Edge](https://raw.githubusercontent.com/alrra/browser-logos/master/src/edge/edge_48x48.png) | ![Opera](https://raw.githubusercontent.com/alrra/browser-logos/master/src/opera/opera_48x48.png) | ![Safari](https://raw.githubusercontent.com/alrra/browser-logos/master/src/safari/safari_48x48.png)
--- | --- | --- | --- | --- | --- |
Latest ✔ | Latest ✔ | 8+ Full ✔ | Latest ✔ | Latest ✔ | Latest ✔ |
## Contributing
Read our [contributing guide](./CONTRIBUTING.md) to learn about our development process, how to propose bugfixes and improvements, and how to build and test your changes to Application Insights.
## ES3/IE8 Compatibility
As an library there are numerous users which cannot control the browsers that their customers use. As such we need to ensure that this library continues to "work" and does not break the JS execution when loaded by an older browser. While it would be ideal to just not support IE8 and older generation (ES3) browsers there are numerous large customers/users that continue to require pages to "work" and as noted they may or cannot control which browser that their end users choose to use.
As part of enabling ES3/IE8 support we have set the ```tsconfig.json``` to ES3 and ```uglify``` settings in ```rollup.config.js``` transformations to support ie8. This provides a first level of support which blocks anyone from adding unsupported ES3 features to the code and enables the generated javascript to be validily parsed in an ES3+ environment.
Ensuring that the generated code is compatible with ES3 is only the first step, JS parsers will still parse the code when an unsupport core function is used, it will just fail or throw an exception at runtime. Therefore, we also need to require/use polyfil implementations or helper functions to handle those scenarios.
### ES3/IE8 Features, Solutions, Workarounds and Polyfil style helper functions
This table does not attempt to include ALL of the ES3 unsuported features, just the currently known functions that where being used at the time or writing. You are welcome to contribute to provide additional helpers, workarounds or documentation of values that should not be used.
| Feature | Description | Usage |
|-----------|-----------------|------|
| ```Object.keys()``` | Not provided by ES3 and not used | N/A |
| ES5+ getters/setters<br>```Object.defineProperty(...)``` | Not provided by ES3 and not used | N/A |
| ```Object.create(protoObj, [descriptorSet]?)``` | Not provided by ES3 and not used | N/A |
| ```Object.defineProperties()``` | Not provided by ES3 and not used | N/A |
| ```Object.getOwnPropertyNames(obj)``` | Not provided by ES3 and not used | N/A |
| ```Object.getPrototypeOf(obj)``` | Not provided by ES3 and not used | ```_getObjProto(target:any)``` |
| ```Object.getOwnPropertyDescriptor(obj)``` | Not provided by ES3 and not used | N/A |
| ```Object.preventExtensions(obj)``` | Not provided by ES3 and not used | N/A |
| ```Object.isExtensible(obj)``` | Not provided by ES3 and not used | N/A |
| ```Object.seal(obj)``` | Not provided by ES3 and not used | N/A |
| ```Object.isSealed(obj)``` | Not provided by ES3 and not used | N/A |
| ```Object.freeze(obj)``` | Not provided by ES3 and not used | N/A |
| ```Object.isFrozen(obj)``` | Not provided by ES3 and not used | N/A |

2106
common/config/rush/npm-shrinkwrap.json сгенерированный Normal file

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -0,0 +1,51 @@
"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See the @microsoft/rush package's LICENSE file for license information.
Object.defineProperty(exports, "__esModule", { value: true });
// THIS FILE WAS GENERATED BY A TOOL. ANY MANUAL MODIFICATIONS WILL GET OVERWRITTEN WHENEVER RUSH IS UPGRADED.
//
// This script is intended for usage in an automated build environment where the Rush command may not have
// been preinstalled, or may have an unpredictable version. This script will automatically install the version of Rush
// specified in the rush.json configuration file (if not already installed), and then pass a command-line to it.
// An example usage would be:
//
// node common/scripts/install-run-rush.js install
//
// For more information, see: https://rushjs.io/pages/maintainer/setup_new_repo/
const path = require("path");
const fs = require("fs");
const install_run_1 = require("./install-run");
const PACKAGE_NAME = '@microsoft/rush';
function getRushVersion() {
const rushJsonFolder = install_run_1.findRushJsonFolder();
const rushJsonPath = path.join(rushJsonFolder, install_run_1.RUSH_JSON_FILENAME);
try {
const rushJsonContents = fs.readFileSync(rushJsonPath, 'utf-8');
// Use a regular expression to parse out the rushVersion value because rush.json supports comments,
// but JSON.parse does not and we don't want to pull in more dependencies than we need to in this script.
const rushJsonMatches = rushJsonContents.match(/\"rushVersion\"\s*\:\s*\"([0-9a-zA-Z.+\-]+)\"/);
return rushJsonMatches[1];
}
catch (e) {
throw new Error(`Unable to determine the required version of Rush from rush.json (${rushJsonFolder}). ` +
'The \'rushVersion\' field is either not assigned in rush.json or was specified ' +
'using an unexpected syntax.');
}
}
function run() {
const [nodePath, /* Ex: /bin/node */ // tslint:disable-line:no-unused-variable
scriptPath, /* /repo/common/scripts/install-run-rush.js */ // tslint:disable-line:no-unused-variable
...packageBinArgs /* [build, --to, myproject] */] = process.argv;
if (process.argv.length < 3) {
console.log('Usage: install-run-rush.js <command> [args...]');
console.log('Example: install-run-rush.js build --to myproject');
process.exit(1);
}
install_run_1.runWithErrorAndStatusCode(() => {
const version = getRushVersion();
console.log(`The rush.json configuration requests Rush version ${version}`);
return install_run_1.installAndRun(PACKAGE_NAME, version, 'rush', packageBinArgs);
});
}
run();
//# sourceMappingURL=install-run-rush.js.map

Просмотреть файл

@ -0,0 +1,397 @@
"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See the @microsoft/rush package's LICENSE file for license information.
Object.defineProperty(exports, "__esModule", { value: true });
// THIS FILE WAS GENERATED BY A TOOL. ANY MANUAL MODIFICATIONS WILL GET OVERWRITTEN WHENEVER RUSH IS UPGRADED.
//
// This script is intended for usage in an automated build environment where a Node tool may not have
// been preinstalled, or may have an unpredictable version. This script will automatically install the specified
// version of the specified tool (if not already installed), and then pass a command-line to it.
// An example usage would be:
//
// node common/scripts/install-run.js rimraf@2.6.2 rimraf -f project1/lib
//
// For more information, see: https://rushjs.io/pages/maintainer/setup_new_repo/
const childProcess = require("child_process");
const fs = require("fs");
const os = require("os");
const path = require("path");
exports.RUSH_JSON_FILENAME = 'rush.json';
const INSTALLED_FLAG_FILENAME = 'installed.flag';
const NODE_MODULES_FOLDER_NAME = 'node_modules';
const PACKAGE_JSON_FILENAME = 'package.json';
/**
* Parse a package specifier (in the form of name\@version) into name and version parts.
*/
function parsePackageSpecifier(rawPackageSpecifier) {
rawPackageSpecifier = (rawPackageSpecifier || '').trim();
const separatorIndex = rawPackageSpecifier.lastIndexOf('@');
let name;
let version = undefined;
if (separatorIndex === 0) {
// The specifier starts with a scope and doesn't have a version specified
name = rawPackageSpecifier;
}
else if (separatorIndex === -1) {
// The specifier doesn't have a version
name = rawPackageSpecifier;
}
else {
name = rawPackageSpecifier.substring(0, separatorIndex);
version = rawPackageSpecifier.substring(separatorIndex + 1);
}
if (!name) {
throw new Error(`Invalid package specifier: ${rawPackageSpecifier}`);
}
return { name, version };
}
/**
* Resolve a package specifier to a static version
*/
function resolvePackageVersion(rushCommonFolder, { name, version }) {
if (!version) {
version = '*'; // If no version is specified, use the latest version
}
if (version.match(/^[a-zA-Z0-9\-\+\.]+$/)) {
// If the version contains only characters that we recognize to be used in static version specifiers,
// pass the version through
return version;
}
else {
// version resolves to
try {
const rushTempFolder = ensureAndJoinPath(rushCommonFolder, 'temp');
const sourceNpmrcFolder = path.join(rushCommonFolder, 'config', 'rush');
syncNpmrc(sourceNpmrcFolder, rushTempFolder);
const npmPath = getNpmPath();
// This returns something that looks like:
// @microsoft/rush@3.0.0 '3.0.0'
// @microsoft/rush@3.0.1 '3.0.1'
// ...
// @microsoft/rush@3.0.20 '3.0.20'
// <blank line>
const npmVersionSpawnResult = childProcess.spawnSync(npmPath, ['view', `${name}@${version}`, 'version', '--no-update-notifier'], {
cwd: rushTempFolder,
stdio: []
});
if (npmVersionSpawnResult.status !== 0) {
throw new Error(`"npm view" returned error code ${npmVersionSpawnResult.status}`);
}
const npmViewVersionOutput = npmVersionSpawnResult.stdout.toString();
const versionLines = npmViewVersionOutput.split('\n').filter((line) => !!line);
const latestVersion = versionLines[versionLines.length - 1];
if (!latestVersion) {
throw new Error('No versions found for the specified version range.');
}
const versionMatches = latestVersion.match(/^.+\s\'(.+)\'$/);
if (!versionMatches) {
throw new Error(`Invalid npm output ${latestVersion}`);
}
return versionMatches[1];
}
catch (e) {
throw new Error(`Unable to resolve version ${version} of package ${name}: ${e}`);
}
}
}
let _npmPath = undefined;
/**
* Get the absolute path to the npm executable
*/
function getNpmPath() {
if (!_npmPath) {
try {
if (os.platform() === 'win32') {
// We're on Windows
const whereOutput = childProcess.execSync('where npm', { stdio: [] }).toString();
const lines = whereOutput.split(os.EOL).filter((line) => !!line);
// take the last result, we are looking for a .cmd command
// see https://github.com/Microsoft/web-build-tools/issues/759
_npmPath = lines[lines.length - 1];
}
else {
// We aren't on Windows - assume we're on *NIX or Darwin
_npmPath = childProcess.execSync('which npm', { stdio: [] }).toString();
}
}
catch (e) {
throw new Error(`Unable to determine the path to the NPM tool: ${e}`);
}
_npmPath = _npmPath.trim();
if (!fs.existsSync(_npmPath)) {
throw new Error('The NPM executable does not exist');
}
}
return _npmPath;
}
exports.getNpmPath = getNpmPath;
let _rushJsonFolder;
/**
* Find the absolute path to the folder containing rush.json
*/
function findRushJsonFolder() {
if (!_rushJsonFolder) {
let basePath = __dirname;
let tempPath = __dirname;
do {
const testRushJsonPath = path.join(basePath, exports.RUSH_JSON_FILENAME);
if (fs.existsSync(testRushJsonPath)) {
_rushJsonFolder = basePath;
break;
}
else {
basePath = tempPath;
}
} while (basePath !== (tempPath = path.dirname(basePath))); // Exit the loop when we hit the disk root
if (!_rushJsonFolder) {
throw new Error('Unable to find rush.json.');
}
}
return _rushJsonFolder;
}
exports.findRushJsonFolder = findRushJsonFolder;
/**
* Create missing directories under the specified base directory, and return the resolved directory.
*
* Does not support "." or ".." path segments.
* Assumes the baseFolder exists.
*/
function ensureAndJoinPath(baseFolder, ...pathSegments) {
let joinedPath = baseFolder;
try {
for (let pathSegment of pathSegments) {
pathSegment = pathSegment.replace(/[\\\/]/g, '+');
joinedPath = path.join(joinedPath, pathSegment);
if (!fs.existsSync(joinedPath)) {
fs.mkdirSync(joinedPath);
}
}
}
catch (e) {
throw new Error(`Error building local installation folder (${path.join(baseFolder, ...pathSegments)}): ${e}`);
}
return joinedPath;
}
/**
* As a workaround, _syncNpmrc() copies the .npmrc file to the target folder, and also trims
* unusable lines from the .npmrc file. If the source .npmrc file not exist, then _syncNpmrc()
* will delete an .npmrc that is found in the target folder.
*
* Why are we trimming the .npmrc lines? NPM allows environment variables to be specified in
* the .npmrc file to provide different authentication tokens for different registry.
* However, if the environment variable is undefined, it expands to an empty string, which
* produces a valid-looking mapping with an invalid URL that causes an error. Instead,
* we'd prefer to skip that line and continue looking in other places such as the user's
* home directory.
*
* IMPORTANT: THIS CODE SHOULD BE KEPT UP TO DATE WITH Utilities._syncNpmrc()
*/
function syncNpmrc(sourceNpmrcFolder, targetNpmrcFolder) {
const sourceNpmrcPath = path.join(sourceNpmrcFolder, '.npmrc');
const targetNpmrcPath = path.join(targetNpmrcFolder, '.npmrc');
try {
if (fs.existsSync(sourceNpmrcPath)) {
let npmrcFileLines = fs.readFileSync(sourceNpmrcPath).toString().split('\n');
npmrcFileLines = npmrcFileLines.map((line) => (line || '').trim());
const resultLines = [];
// Trim out lines that reference environment variables that aren't defined
for (const line of npmrcFileLines) {
// This finds environment variable tokens that look like "${VAR_NAME}"
const regex = /\$\{([^\}]+)\}/g;
const environmentVariables = line.match(regex);
let lineShouldBeTrimmed = false;
if (environmentVariables) {
for (const token of environmentVariables) {
// Remove the leading "${" and the trailing "}" from the token
const environmentVariableName = token.substring(2, token.length - 1);
if (!process.env[environmentVariableName]) {
lineShouldBeTrimmed = true;
break;
}
}
}
if (lineShouldBeTrimmed) {
// Example output:
// "; MISSING ENVIRONMENT VARIABLE: //my-registry.com/npm/:_authToken=${MY_AUTH_TOKEN}"
resultLines.push('; MISSING ENVIRONMENT VARIABLE: ' + line);
}
else {
resultLines.push(line);
}
}
fs.writeFileSync(targetNpmrcPath, resultLines.join(os.EOL));
}
else if (fs.existsSync(targetNpmrcPath)) {
// If the source .npmrc doesn't exist and there is one in the target, delete the one in the target
fs.unlinkSync(targetNpmrcPath);
}
}
catch (e) {
throw new Error(`Error syncing .npmrc file: ${e}`);
}
}
/**
* Detects if the package in the specified directory is installed
*/
function isPackageAlreadyInstalled(packageInstallFolder) {
try {
const flagFilePath = path.join(packageInstallFolder, INSTALLED_FLAG_FILENAME);
if (!fs.existsSync(flagFilePath)) {
return false;
}
const fileContents = fs.readFileSync(flagFilePath).toString();
return fileContents.trim() === process.version;
}
catch (e) {
return false;
}
}
/**
* Removes the following files and directories under the specified folder path:
* - installed.flag
* -
* - node_modules
*/
function cleanInstallFolder(rushCommonFolder, packageInstallFolder) {
try {
const flagFile = path.resolve(packageInstallFolder, INSTALLED_FLAG_FILENAME);
if (fs.existsSync(flagFile)) {
fs.unlinkSync(flagFile);
}
const packageLockFile = path.resolve(packageInstallFolder, 'package-lock.json');
if (fs.existsSync(packageLockFile)) {
fs.unlinkSync(packageLockFile);
}
const nodeModulesFolder = path.resolve(packageInstallFolder, NODE_MODULES_FOLDER_NAME);
if (fs.existsSync(nodeModulesFolder)) {
const rushRecyclerFolder = ensureAndJoinPath(rushCommonFolder, 'temp', 'rush-recycler', `install-run-${Date.now().toString()}`);
fs.renameSync(nodeModulesFolder, rushRecyclerFolder);
}
}
catch (e) {
throw new Error(`Error cleaning the package install folder (${packageInstallFolder}): ${e}`);
}
}
function createPackageJson(packageInstallFolder, name, version) {
try {
const packageJsonContents = {
'name': 'ci-rush',
'version': '0.0.0',
'dependencies': {
[name]: version
},
'description': 'DON\'T WARN',
'repository': 'DON\'T WARN',
'license': 'MIT'
};
const packageJsonPath = path.join(packageInstallFolder, PACKAGE_JSON_FILENAME);
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJsonContents, undefined, 2));
}
catch (e) {
throw new Error(`Unable to create package.json: ${e}`);
}
}
/**
* Run "npm install" in the package install folder.
*/
function installPackage(packageInstallFolder, name, version) {
try {
console.log(`Installing ${name}...`);
const npmPath = getNpmPath();
const result = childProcess.spawnSync(npmPath, ['install'], {
stdio: 'inherit',
cwd: packageInstallFolder,
env: process.env
});
if (result.status !== 0) {
throw new Error('"npm install" encountered an error');
}
console.log(`Successfully installed ${name}@${version}`);
}
catch (e) {
throw new Error(`Unable to install package: ${e}`);
}
}
/**
* Get the ".bin" path for the package.
*/
function getBinPath(packageInstallFolder, binName) {
const binFolderPath = path.resolve(packageInstallFolder, NODE_MODULES_FOLDER_NAME, '.bin');
const resolvedBinName = (os.platform() === 'win32') ? `${binName}.cmd` : binName;
return path.resolve(binFolderPath, resolvedBinName);
}
/**
* Write a flag file to the package's install directory, signifying that the install was successful.
*/
function writeFlagFile(packageInstallFolder) {
try {
const flagFilePath = path.join(packageInstallFolder, INSTALLED_FLAG_FILENAME);
fs.writeFileSync(flagFilePath, process.version);
}
catch (e) {
throw new Error(`Unable to create installed.flag file in ${packageInstallFolder}`);
}
}
function installAndRun(packageName, packageVersion, packageBinName, packageBinArgs) {
const rushJsonFolder = findRushJsonFolder();
const rushCommonFolder = path.join(rushJsonFolder, 'common');
const packageInstallFolder = ensureAndJoinPath(rushCommonFolder, 'temp', 'install-run', `${packageName}@${packageVersion}`);
if (!isPackageAlreadyInstalled(packageInstallFolder)) {
// The package isn't already installed
cleanInstallFolder(rushCommonFolder, packageInstallFolder);
const sourceNpmrcFolder = path.join(rushCommonFolder, 'config', 'rush');
syncNpmrc(sourceNpmrcFolder, packageInstallFolder);
createPackageJson(packageInstallFolder, packageName, packageVersion);
installPackage(packageInstallFolder, packageName, packageVersion);
writeFlagFile(packageInstallFolder);
}
const statusMessage = `Invoking "${packageBinName} ${packageBinArgs.join(' ')}"`;
const statusMessageLine = new Array(statusMessage.length + 1).join('-');
console.log(os.EOL + statusMessage + os.EOL + statusMessageLine + os.EOL);
const binPath = getBinPath(packageInstallFolder, packageBinName);
const result = childProcess.spawnSync(binPath, packageBinArgs, {
stdio: 'inherit',
cwd: process.cwd(),
env: process.env
});
return result.status;
}
exports.installAndRun = installAndRun;
function runWithErrorAndStatusCode(fn) {
process.exitCode = 1;
try {
const exitCode = fn();
process.exitCode = exitCode;
}
catch (e) {
console.error(os.EOL + os.EOL + e.toString() + os.EOL + os.EOL);
}
}
exports.runWithErrorAndStatusCode = runWithErrorAndStatusCode;
function run() {
const [nodePath, /* Ex: /bin/node */ // tslint:disable-line:no-unused-variable
scriptPath, /* /repo/common/scripts/install-run-rush.js */ rawPackageSpecifier, /* rimraf@^2.0.0 */ packageBinName, /* rimraf */ ...packageBinArgs /* [-f, myproject/lib] */] = process.argv;
if (path.basename(scriptPath).toLowerCase() !== 'install-run.js') {
// If install-run.js wasn't directly invoked, don't execute the rest of this function. Return control
// to the script that (presumably) imported this file
return;
}
if (process.argv.length < 4) {
console.log('Usage: install-run.js <package>@<version> <command> [args...]');
console.log('Example: install-run.js rimraf@2.6.2 rimraf -f project1/lib');
process.exit(1);
}
runWithErrorAndStatusCode(() => {
const rushJsonFolder = findRushJsonFolder();
const rushCommonFolder = ensureAndJoinPath(rushJsonFolder, 'common');
const packageSpecifier = parsePackageSpecifier(rawPackageSpecifier);
const name = packageSpecifier.name;
const version = resolvePackageVersion(rushCommonFolder, packageSpecifier);
if (packageSpecifier.version !== version) {
console.log(`Resolved to ${name}@${version}`);
}
return installAndRun(name, version, packageBinName, packageBinArgs);
});
}
run();
//# sourceMappingURL=install-run.js.map

62
gruntfile.js Normal file
Просмотреть файл

@ -0,0 +1,62 @@
module.exports = function (grunt) {
grunt.initConfig({
tslint: {
options: {
rulesDirectory: 'node_modules/tslint-microsoft-contrib',
},
files: {
src: [
'./lib/src/**/*.ts',
'!./**/node_modules/**',
'!./**/Tests/**',
'!./**/dist-esm/**',
'!./**/Generated/**'
],
}
},
ts: {
options: {
comments: true
},
default: {
tsconfig: './lib/tsconfig.json'
},
dynamicproto: {
tsconfig: './lib/tsconfig.json'
},
dynamicprototest: {
tsconfig: './lib/test/tsconfig.json',
src: [
'./lib/test/Selenium/DynamicProtoTests.ts'
],
out: 'lib/test/Selenium/dynamicprototests.js'
}
},
qunit: {
dynamicproto: {
options: {
urls: [
'./lib/test/Selenium/Tests.html'
],
timeout: 300 * 1000, // 5 min
console: false,
summaryOnly: true,
'--web-security': 'false' // we need this to allow CORS requests in PhantomJS
}
}
}
});
grunt.event.on('qunit.testStart', function (name) {
grunt.log.ok('Running test: ' + name);
});
grunt.loadNpmTasks("grunt-ts");
grunt.loadNpmTasks('grunt-tslint');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-qunit');
grunt.loadNpmTasks('grunt-run');
grunt.registerTask("default", ["ts:dynamicproto", "ts:dynamicprototest", "qunit:dynamicproto"])
grunt.registerTask("dynamicproto", ["ts:dynamicproto"]);
grunt.registerTask("dynamicprototest", ["ts:dynamicproto", "ts:dynamicprototest", "qunit:dynamicproto"]);
};

2333
lib/docs/assets/css/main.css Normal file

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Двоичные данные
lib/docs/assets/images/icons.png Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 9.4 KiB

Двоичные данные
lib/docs/assets/images/icons@2x.png Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 28 KiB

Двоичные данные
lib/docs/assets/images/widgets.png Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 480 B

Двоичные данные
lib/docs/assets/images/widgets@2x.png Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 855 B

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Просмотреть файл

@ -0,0 +1,3 @@
var typedoc = typedoc || {};
typedoc.search = typedoc.search || {};
typedoc.search.data = {"kinds":{"1":"External module","64":"Function","65536":"Type literal","4194304":"Type alias"},"rows":[{"id":0,"kind":1,"name":"\"DynamicProto\"","url":"modules/_dynamicproto_.html","classes":"tsd-kind-external-module"},{"id":1,"kind":4194304,"name":"DynamicProtoDelegate","url":"modules/_dynamicproto_.html#dynamicprotodelegate","classes":"tsd-kind-type-alias tsd-parent-kind-external-module tsd-has-type-parameter","parent":"\"DynamicProto\""},{"id":2,"kind":65536,"name":"__type","url":"modules/_dynamicproto_.html#dynamicprotodelegate.__type","classes":"tsd-kind-type-literal tsd-parent-kind-type-alias tsd-is-not-exported","parent":"\"DynamicProto\".DynamicProtoDelegate"},{"id":3,"kind":64,"name":"dynamicProto","url":"modules/_dynamicproto_.html#dynamicproto","classes":"tsd-kind-function tsd-parent-kind-external-module tsd-has-type-parameter","parent":"\"DynamicProto\""}]};

2663
lib/docs/globals.html Normal file

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

2664
lib/docs/index.html Normal file

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

61
lib/package.json Normal file
Просмотреть файл

@ -0,0 +1,61 @@
{
"name": "@microsoft/dynamicproto-js",
"author": "Microsoft Application Insights Team",
"version": "0.0.1",
"description": "Microsoft Dynamic Proto Utility",
"keywords": [
"javascript",
"dynamic prototype",
"microsoft",
"typescript",
"inheritence",
"minification",
"application insights"
],
"main": "./dist/dynamicproto-js.js",
"module": "./dist/dynamicproto-js.js",
"types": "./dist/dynamicproto-js.d.ts",
"directories": {
"doc": "./docs"
},
"scripts": {
"postinstall": "node common/scripts/install-run-rush.js update --recheck --full",
"clean": "grunt clean",
"build": "npm run build:esm && npm run build:bundle && npm run lint && npm run docs",
"build:esm": "grunt dynamicproto",
"build:bundle": "rollup -c rollup.config.js",
"test": "grunt dynamicprototest",
"lint": "tslint -p tsconfig.json",
"docs": "typedoc --out ./docs src --excludePrivate --excludeProtected --tsconfig tsconfig.json --theme minimal"
},
"repository": {
"type": "git",
"url": "git+https://github.com/microsoft/DynamicProto-JS.git"
},
"bugs": {
"url": "https://github.com/microsoft/DynamicProto-JS/issues"
},
"homepage": "https://github.com/microsoft/DynamicProto-JS#readme",
"license": "MIT",
"sideEffects": false,
"devDependencies": {
"grunt": "^1.0.1",
"grunt-cli": "^1.3.2",
"grunt-contrib-qunit": "^2.0.0",
"grunt-contrib-uglify": "^3.1.0",
"grunt-run": "^0.8.1",
"grunt-ts": "^6.0.0-beta.22",
"grunt-tslint": "^5.0.2",
"tslint": "^5.19.0",
"tslint-microsoft-contrib": "^5.2.1",
"tslint-config-prettier": "^1.18.0",
"typescript": "2.5.3",
"rollup-plugin-node-resolve": "^3.4.0",
"rollup-plugin-replace": "^2.1.0",
"rollup-plugin-uglify": "^6.0.0",
"rollup": "^0.66.0"
},
"dependencies": {
"tslib": "^1.9.3"
}
}

109
lib/rollup.config.js Normal file
Просмотреть файл

@ -0,0 +1,109 @@
import nodeResolve from "rollup-plugin-node-resolve";
import {uglify} from "rollup-plugin-uglify";
import replace from "rollup-plugin-replace";
const version = require("./package.json").version;
const outputName = "dynamicproto-js";
const banner = [
"/*!",
` * Microsoft Dynamic Proto Utility, ${version}`,
" * Copyright (c) Microsoft and contributors. All rights reserved.",
" */"
].join("\n");
const bundleRollupConfigFactory = isProduction => {
const bundleRollupConfig = {
input: `./dist-esm/${outputName}.js`,
output: {
file: `./bundle/${outputName}.js`,
banner: banner,
format: "umd",
name: "Microsoft.DynamicProto-JS",
sourcemap: true
},
plugins: [
replace({
delimiters: ["", ""],
values: {
"// Copyright (c) Microsoft Corporation. All rights reserved.": "",
"// Licensed under the MIT License.": ""
}
}),
nodeResolve({
module: true,
browser: true,
preferBuiltins: false
})
]
};
if (isProduction) {
bundleRollupConfig.output.file = `./bundle/${outputName}.min.js`;
bundleRollupConfig.plugins.push(
uglify({
ie8: true,
toplevel: true,
compress: {
passes:3,
unsafe: true
},
output: {
preamble: banner,
webkit:true
}
})
);
}
return bundleRollupConfig;
};
const nodeUmdRollupConfigFactory = (isProduction) => {
const nodeRollupConfig = {
input: `./dist-esm/${outputName}.js`,
output: {
file: `./dist/${outputName}.js`,
banner: banner,
format: "umd",
name: "Microsoft.DynamicProto-JS",
sourcemap: true
},
plugins: [
replace({
delimiters: ["", ""],
values: {
"// Copyright (c) Microsoft Corporation. All rights reserved.": "",
"// Licensed under the MIT License.": ""
}
}),
nodeResolve()
]
};
if (isProduction) {
nodeRollupConfig.output.file = `./dist/${outputName}.min.js`;
nodeRollupConfig.plugins.push(
uglify({
ie8: true,
toplevel: true,
compress: {
passes:3,
unsafe: true
},
output: {
preamble: banner,
webkit:true
}
})
);
}
return nodeRollupConfig;
}
export default [
nodeUmdRollupConfigFactory(true),
nodeUmdRollupConfigFactory(false),
bundleRollupConfigFactory(true),
bundleRollupConfigFactory(false)
];

408
lib/src/DynamicProto.ts Normal file
Просмотреть файл

@ -0,0 +1,408 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
/**
* Constant string defined to support minimization
* @ignore
*/
const Constructor = 'constructor';
/**
* Constant string defined to support minimization
* @ignore
*/
const Prototype = 'prototype';
/**
* Constant string defined to support minimization
* @ignore
*/
const strFunction = 'function';
/**
* Constant string defined to support minimization
* @ignore
*/
const GetPrototypeOf = 'getPrototypeOf';
/**
* Used to define the name of the instance function lookup table
* @ignore
*/
const DynInstFuncTable = '_dynInstFuncs';
/**
* Name used to tag the dynamic prototype function
* @ignore
*/
const DynProxyTag = '_isDynProxy';
/**
* Name added to a prototype to define the dynamic prototype "class" name used to lookup the function table
* @ignore
*/
const DynClassName = '_dynClass';
/**
* Prefix added to the classname to avoid any name clashes with other instance level properties
* @ignore
*/
const DynClassNamePrefix = '_dynCls$';
/**
* Value used as the name of a class when it cannot be determined
* @ignore
*/
const UnknownValue = '_unknown_';
/**
* Internal Global used to generate a unique dynamic class name, every new class will increase this value
* @ignore
*/
let _dynamicNames = 0;
/**
* Helper to check if the object contains a property of the name
* @ignore
*/
function _hasOwnProperty(obj:any, prop:string) {
return obj && Object[Prototype].hasOwnProperty.call(obj, prop);
}
/**
* Checks if the passed of value is a function.
* @param {any} value - Value to be checked.
* @return {boolean} True if the value is a boolean, false otherwise.
* @ignore
*/
function _isFunction(value: any): boolean {
return typeof value === strFunction;
}
/**
* Helper used to check whether the target is an Object prototype or Array prototype
* @ignore
*/
function _isObjectOrArrayPrototype(target:any) {
return target && (target === Object[Prototype] || target === Array[Prototype]);
}
/**
* Helper used to check whether the target is an Object prototype, Array prototype or Function prototype
* @ignore
*/
function _isObjectArrayOrFunctionPrototype(target:any)
{
return _isObjectOrArrayPrototype(target) || target === Function[Prototype];
}
/**
* Helper used to get the prototype of the target object as getPrototypeOf is not available in an ES3 environment.
* @ignore
*/
function _getObjProto(target:any) {
if (target) {
// This method doesn't existing in older browsers (e.g. IE8)
if (Object[GetPrototypeOf]) {
return Object[GetPrototypeOf](target);
}
var proto = "__proto__"; // using indexed lookup to assist with minification
if(_isObjectOrArrayPrototype(target[proto])) {
return target[proto];
}
var construct = target[Constructor];
if (construct) {
// May break if the constructor has been changed or removed
return construct[Prototype];
}
}
return null;
}
/**
* Helper function to check whether the provided function name is a potential candidate for dynamic
* callback and prototype generation.
* @param target The target object, may be a prototpe or class object
* @param funcName The function name
* @ignore
*/
function _isDynamicCandidate(target:any, funcName:string) {
return (funcName !== Constructor && _isFunction(target[funcName]) && _hasOwnProperty(target, funcName));
}
/**
* Helper to throw a TypeError exception
* @param message the message
* @ignore
*/
function _throwTypeError(message:string) {
throw new TypeError("DynamicProto: " + message);
}
/**
* Returns a collection of the instance functions that are defined directly on the thisTarget object, it does
* not return any inherited functions
* @param thisTarget The object to get the instance functions from
* @ignore
*/
function _getInstanceFuncs(thisTarget:any): any {
// Get the base proto
var instFuncs = {};
// Save any existing instance functions
for (var name in thisTarget) {
// Don't include any dynamic prototype instances - as we only want the real functions
if (!instFuncs[name] && _isDynamicCandidate(thisTarget, name)) {
// Create an instance callback for pasing the base function to the caller
instFuncs[name] = thisTarget[name];
}
}
return instFuncs;
}
/**
* Returns an object that contains callback functions for all "base/super" functions, this is used to "save"
* enabling calling super.xxx() functions without requiring that the base "class" has defined a prototype references
* @param target The current instance
* @ignore
*/
function _getBaseFuncs(classProto:any, thisTarget:any, instFuncs:any): any {
function _instFuncProxy(target:any, theFunc:any) {
return function() {
return theFunc.apply(target, arguments);
};
}
// Start creating a new baseFuncs by creating proxies for the instance functions (as they may get replaced)
var baseFuncs = {};
for (var name in instFuncs) {
// Create an instance callback for pasing the base function to the caller
baseFuncs[name] = _instFuncProxy(thisTarget, instFuncs[name]);
}
// Get the base prototype functions
var baseProto = _getObjProto(classProto);
// Don't include base object functions for Object, Array or Function
while (baseProto && !_isObjectArrayOrFunctionPrototype(baseProto)) {
// look for prototype functions
for (var name in baseProto) {
// Don't include any dynamic prototype instances - as we only want the real functions
if (!baseFuncs[name] && _isDynamicCandidate(baseProto, name)) {
// Create an instance callback for pasing the base function to the caller
baseFuncs[name] = _instFuncProxy(thisTarget, baseProto[name]);
}
}
// We need to find all possible functions that might be overloaded by walking the entire prototype chain
// This avoids the caller from needing to check whether it's direct base class implements the function or not
// by walking the entire chain it simplifies the usage and issues from upgrading any of the base classes.
baseProto = _getObjProto(baseProto);
}
return baseFuncs;
}
/**
* Add the required dynamic prototype methods to the the class prototype
* @param proto The class prototype
* @param className The instance classname
* @param target The target instance
* @param baseInstFuncs The base instance functions
* @ignore
*/
function _populatePrototype(proto:any, className:string, target:any, baseInstFuncs:any) {
function _createDynamicPrototype(proto:any, funcName:string) {
var dynProtoProxy = function() {
let _this = this;
// We need to check whether the class name is defined directly on this prototype otherwise
// it will walk the proto chain and return any parent proto classname.
if (_this && _hasOwnProperty(proto, DynClassName)) {
let instFunc = ((_this[DynInstFuncTable] || {})[proto[DynClassName]] || {})[funcName];
if (instFunc) {
// Used the instance function property
return instFunc.apply(_this, arguments);
}
// Avoid stack overflow from recursive calling the same function
_throwTypeError("Missing [" + funcName + "] " + strFunction);
}
let protoFunc = proto[funcName];
// Check that the prototype function is not a self reference -- try to avoid stack overflow!
if (protoFunc === dynProtoProxy) {
// It is so lookup the base prototype
protoFunc = _getObjProto(proto)[funcName];
}
if (!_isFunction(protoFunc)) {
_throwTypeError("[" + funcName + "] is not a " + strFunction);
}
return protoFunc.apply(_this, arguments);
};
// Tag this function as a proxy to support replacing dynamic proxy elements (primary use case is for unit testing
// via which can dynamically replace the prototype function reference)
(dynProtoProxy as any)[DynProxyTag] = 1;
return dynProtoProxy;
}
if (!_isObjectOrArrayPrototype(proto)) {
let instFuncTable = target[DynInstFuncTable] = target[DynInstFuncTable] || {};
let instFuncs = instFuncTable[className] = (instFuncTable[className] || {}); // fetch and assign if as it may not exist yet
for (var name in target) {
// Only add overriden functions
if (_isDynamicCandidate(target, name) && target[name] !== baseInstFuncs[name] ) {
// Save the instance Function to the lookup table and remove it from the instance as it's not a dynamic proto function
instFuncs[name] = target[name];
delete target[name];
// Add a dynamic proto if one doesn't exist or if a prototype function exists and it's not a dynamic one
if (!_hasOwnProperty(proto, name) || (proto[name] && !proto[name][DynProxyTag])) {
proto[name] = _createDynamicPrototype(proto, name);
}
}
}
}
}
/**
* Checks whether the passed prototype object appears to be correct by walking the prototype heirarchy of the instance
* @param classProto The class prototype instance
* @param thisTarget The current instance that will be checked whther the passed prototype instance is in the heirarchy
* @ignore
*/
function _checkPrototype(classProto:any, thisTarget:any) {
let thisProto = _getObjProto(thisTarget);
while (thisProto && !_isObjectArrayOrFunctionPrototype(thisProto)) {
if (thisProto === classProto) {
return true;
}
thisProto = _getObjProto(thisProto);
}
return false;
}
/**
* Gets the current prototype name using the ES6 name if available otherwise falling back to a use unknown as the name.
* It's not critical for this to return a name, it's used to decorate the generated unique name for easier debugging only.
* @param target
* @param unknownValue
* @ignore
*/
function _getObjName(target:any, unknownValue?:string) {
if (_hasOwnProperty(target, Prototype)) {
// Look like a prototype
return target.name || unknownValue || UnknownValue
}
return (((target || {})[Constructor]) || {}).name || unknownValue || UnknownValue;
}
/**
* The delegate signature for the function used as the callback for dynamicProto()
* @typeparam DPType This is the generic type of the class, used to keep intellisense valid for the proxy instance, even
* though it is only a proxy that only contains the functions
* @param theTarget This is the real "this" of the current target object
* @param baseFuncProxy The is a proxy object which ONLY contains this function that existed on the "this" instance before
* calling dynamicProto, it does NOT contain properties of this. This is basically equivalent to using the "super" keyword.
*/
export type DynamicProtoDelegate<DPType> = (theTarget:DPType, baseFuncProxy?:DPType) => void;
/**
* Helper function when creating dynamic (inline) functions for classes, this helper performs the following tasks :-
* - Saves references to all defined base class functions
* - Calls the delegateFunc with the current target (this) and a base object reference that can be used to call all "super" functions.
* - Will populate the class prototype for all overridden functions to support class extension that call the prototype instance.
* Callers should use this helper when declaring all function within the constructor of a class, as mentioned above the delegateFunc is
* passed both the target "this" and an object that can be used to call any base (super) functions, using this based object in place of
* super.XXX() (which gets expanded to _super.prototype.XXX()) provides a better minification outcome and also ensures the correct "this"
* context is maintained as TypeScript creates incorrect references using super.XXXX() for dynamically defined functions i.e. Functions
* defined in the constructor or some other function (rather than declared as complete typescript functions).
* ### Usage
* ```typescript
* import dynamicProto from "@microsoft/dynamicproto-js";
* class ExampleClass extends BaseClass {
* constructor() {
* dynamicProto(ExampleClass, this, (_self, base) => {
* // This will define a function that will be converted to a prototype function
* _self.newFunc = () => {
* // Access any "this" instance property
* if (_self.someProperty) {
* ...
* }
* }
* // This will define a function that will be converted to a prototype function
* _self.myFunction = () => {
* // Access any "this" instance property
* if (_self.someProperty) {
* // Call the base version of the function that we are overriding
* base.myFunction();
* }
* ...
* }
* _self.initialize = () => {
* ...
* }
* // Warnings: While the following will work as _self is simply a reference to
* // this, if anyone overrides myFunction() the overridden will be called first
* // as the normal JavaScript method resolution will occur and the defined
* // _self.initialize() function is actually gets removed from the instance and
* // a proxy prototype version is created to reference the created method.
* _self.initialize();
* });
* }
* }
* ```
* @typeparam DPType This is the generic type of the class, used to keep intellisense valid
* @typeparam DPCls The type that contains the prototype of the current class
* @param theClass This is the current class instance which contains the prototype for the current class
* @param target The current "this" (target) reference, when the class has been extended this.prototype will not be the 'theClass' value.
* @param delegateFunc The callback function (closure) that will create the dynamic function
*/
export default function dynamicProto<DPType, DPCls>(theClass:DPCls, target:DPType, delegateFunc: DynamicProtoDelegate<DPType>) {
// Make sure that the passed theClass argument looks correct
if (!_hasOwnProperty(theClass, Prototype)) {
_throwTypeError("theClass is an invalid class definition.");
}
// Quick check to make sure that the passed theClass argument looks correct (this is a common copy/paste error)
let classProto = theClass[Prototype];
if (!_checkPrototype(classProto, target)) {
_throwTypeError("[" + _getObjName(theClass) + "] is not in class heirarchy of [" + _getObjName(target) + "]");
}
let className = null;
if (_hasOwnProperty(classProto, DynClassName)) {
// Only grab the class name if it's defined on this prototype (i.e. don't walk the prototype chain)
className = classProto[DynClassName];
} else {
// As not all browser support name on the prototype creating a unique dynamic one if we have not already
// assigned one, so we can use a simple string as the lookup rather than an object for the dynamic instance
// function table lookup.
className = DynClassNamePrefix + _getObjName(theClass, "_") + "$" + _dynamicNames ;
_dynamicNames++;
classProto[DynClassName] = className;
}
// Get the current instance functions
let instFuncs = _getInstanceFuncs(target);
// Get all of the functions for any base instance (before they are potentially overriden)
let baseFuncs = _getBaseFuncs(classProto, target, instFuncs);
// Execute the delegate passing in both the current target "this" and "base" function references
// Note casting the same type as we don't actually have the base class here and this will provide some intellisense support
delegateFunc(target, baseFuncs as DPType);
// Populate the Prototype for any overidden instance functions
_populatePrototype(classProto, className, target, instFuncs);
}

Просмотреть файл

@ -0,0 +1,542 @@
/// <reference path="./TestFramework/Common.ts" />
import dynamicProto from "../src/DynamicProto";
//import dynamicProto from '../src/DynamicProto';
//import * as sinon from 'sinon';
interface IInheritTest {
executionOrder:string[];
testFunction?(): void;
}
class InheritTest1 implements IInheritTest {
public executionOrder:string[] = [];
constructor() {
this.executionOrder.push("InheritTest1()");
}
public testFunction() {
this.executionOrder.push("InheritTest1.test()");
}
}
class InheritTest2 extends InheritTest1 {
constructor() {
super();
this.executionOrder.push("InheritTest2()");
}
public testFunction() {
super.testFunction();
this.executionOrder.push("InheritTest2.test()");
}
}
class InheritTest3 extends InheritTest2 {
constructor() {
super();
this.executionOrder.push("InheritTest3()");
}
public testFunction() {
super.testFunction();
this.executionOrder.push("InheritTest3.test()");
}
}
class DynInheritTest1 implements IInheritTest {
public executionOrder:string[] = [];
public testFunction?(): void;
constructor() {
this.executionOrder.push("DynInheritTest1()");
dynamicProto(DynInheritTest1, this, (_self, base) => {
_self.testFunction = () => {
this.executionOrder.push("DynInheritTest1.test()");
}
});
}
}
class InheritTest4 extends DynInheritTest1 {
constructor() {
super();
this.executionOrder.push("InheritTest4()");
}
public testFunction() {
super.testFunction();
this.executionOrder.push("InheritTest4.test()");
}
}
class InheritTest5 extends InheritTest4 {
constructor() {
super();
this.executionOrder.push("InheritTest5()");
}
public testFunction() {
super.testFunction();
this.executionOrder.push("InheritTest5.test()");
}
}
class DynInheritTest2 extends InheritTest1 {
constructor() {
super();
this.executionOrder.push("DynInheritTest2()");
dynamicProto(DynInheritTest2, this, (_self, base) => {
_self.testFunction = () => {
base.testFunction();
this.executionOrder.push("DynInheritTest2.test()");
}
});
}
}
class DynInheritTest3 extends DynInheritTest2 {
constructor() {
super();
this.executionOrder.push("DynInheritTest3()");
dynamicProto(DynInheritTest3, this, (_self, base) => {
_self.testFunction = () => {
base.testFunction();
this.executionOrder.push("DynInheritTest3.test()");
}
});
}
}
class InheritTest6 extends DynInheritTest2 {
constructor() {
super();
this.executionOrder.push("InheritTest6()");
}
public testFunction() {
super.testFunction();
this.executionOrder.push("InheritTest6.test()");
}
}
class DynInheritTest4 extends InheritTest6 {
constructor() {
super();
this.executionOrder.push("DynInheritTest4()");
dynamicProto(DynInheritTest4, this, (_self, base) => {
_self.testFunction = () => {
base.testFunction();
this.executionOrder.push("DynInheritTest4.test()");
}
});
}
}
class DynInheritTest5 extends DynInheritTest1 {
constructor() {
super();
this.executionOrder.push("DynInheritTest5()");
dynamicProto(DynInheritTest5, this, (_self, base) => {
_self.testFunction = () => {
base.testFunction();
this.executionOrder.push("DynInheritTest5.test()");
}
});
}
}
class DynInheritTest6 extends DynInheritTest5 {
constructor() {
super();
this.executionOrder.push("DynInheritTest6()");
dynamicProto(DynInheritTest6, this, (_self, base) => {
_self.testFunction = () => {
base.testFunction();
this.executionOrder.push("DynInheritTest6.test()");
}
});
}
}
class InstInherit1 implements IInheritTest {
public executionOrder:string[] = [];
public testFunction?():void;
constructor() {
this.executionOrder.push("InstInherit1()");
this.testFunction = () => {
this.executionOrder.push("InstInherit1.test()");
}
}
}
class InstInherit2 extends InheritTest2 {
constructor() {
super();
this.executionOrder.push("InstInherit2()");
this.testFunction = () => {
super.testFunction();
this.executionOrder.push("InstInherit2.test()");
}
}
}
class InheritTest7 extends InstInherit1 {
constructor() {
super();
this.executionOrder.push("InheritTest7()");
}
public testFunction() {
super.testFunction();
this.executionOrder.push("InheritTest7.test()");
}
}
class DynInheritTest7 extends InstInherit1 {
constructor() {
super();
this.executionOrder.push("DynInheritTest7()");
dynamicProto(DynInheritTest7, this, (_self, base) => {
_self.testFunction = () => {
base.testFunction();
this.executionOrder.push("DynInheritTest7.test()");
}
});
}
}
class InstInherit3 extends DynInheritTest7 {
constructor() {
super();
this.executionOrder.push("InstInherit3()");
this.testFunction = () => {
super.testFunction();
this.executionOrder.push("InstInherit3.test()");
}
}
}
class DynInheritTest8 extends InstInherit3 {
constructor() {
super();
this.executionOrder.push("DynInheritTest8()");
dynamicProto(DynInheritTest8, this, (_self, base) => {
_self.testFunction = () => {
base.testFunction();
this.executionOrder.push("DynInheritTest8.test()");
}
});
}
}
class BadInstInherit1 extends InstInherit1 {
constructor() {
super();
this.executionOrder.push("BadInstInherit1()");
this.testFunction = () => {
try {
super.testFunction();
} catch (e) {
this.executionOrder.push("BadInstInherit1.throw()");
}
this.executionOrder.push("BadInstInherit1.test()");
}
}
}
class DynInheritTest9 extends BadInstInherit1 {
constructor() {
super();
this.executionOrder.push("DynInheritTest9()");
dynamicProto(DynInheritTest9, this, (_self, base) => {
_self.testFunction = () => {
base.testFunction();
this.executionOrder.push("DynInheritTest9.test()");
}
});
}
}
class GoodInstInherit1 extends InstInherit1 {
constructor() {
super();
this.executionOrder.push("GoodInstInherit1()");
let prevTestFunc = this.testFunction;
this.testFunction = () => {
prevTestFunc.call(this);
this.executionOrder.push("GoodInstInherit1.test()");
}
}
}
class DynInheritTest10 extends GoodInstInherit1 {
constructor() {
super();
this.executionOrder.push("DynInheritTest10()");
dynamicProto(DynInheritTest10, this, (_self, base) => {
_self.testFunction = () => {
base.testFunction();
this.executionOrder.push("DynInheritTest10.test()");
}
});
}
}
class GoodInstInherit2 extends DynInheritTest10 {
constructor() {
super();
this.executionOrder.push("GoodInstInherit2()");
let prevTestFunc = this.testFunction;
this.testFunction = () => {
prevTestFunc.call(this);
this.executionOrder.push("GoodInstInherit2.test()");
}
}
}
export class DynamicProtoTests extends TestClass {
public testInitialize() {
}
private _validateOrder(message:string, actual:string[], expected:string[]) {
QUnit.assert.equal(actual.length, expected.length, message + ": Checking the length");
let passed = true;
let error = "";
for (let lp = 0; lp < expected.length; lp++) {
if (lp < actual.length) {
if (actual[lp] !== expected[lp]) {
passed = false
error += " **[" + actual[lp] + "!=" + expected[lp] + "]**;"
} else {
error += " " + expected[lp] + ";";
}
} else {
passed = false;
error += " --[" + expected[lp] + "]--;"
}
}
// Fail test and log any extra unexpected calls
for (let lp = expected.length; lp < actual.length; lp++) {
passed = false;
error += " ++[" + actual[lp] + "]++;"
}
QUnit.assert.ok(passed, message + ":" + error);
}
private doTest(message:string, theTest:IInheritTest, expectedOrder:string[])
{
theTest.testFunction();
this._validateOrder(message, theTest.executionOrder, expectedOrder);
}
public registerTests() {
this.testCase({
name: "Inheritance tests",
test: () => {
this.doTest("InheritTest1", new InheritTest1(), [
"InheritTest1()",
"InheritTest1.test()"
]);
this.doTest("InheritTest2", new InheritTest2(), [
"InheritTest1()",
"InheritTest2()",
"InheritTest1.test()",
"InheritTest2.test()"
]);
this.doTest("InheritTest3", new InheritTest3(), [
"InheritTest1()",
"InheritTest2()",
"InheritTest3()",
"InheritTest1.test()",
"InheritTest2.test()",
"InheritTest3.test()"
]);
this.doTest("InheritTest4", new InheritTest4(), [
"DynInheritTest1()",
"InheritTest4()",
"DynInheritTest1.test()",
"InheritTest4.test()"
]);
this.doTest("InheritTest5", new InheritTest5(), [
"DynInheritTest1()",
"InheritTest4()",
"InheritTest5()",
"DynInheritTest1.test()",
"InheritTest4.test()",
"InheritTest5.test()"
]);
this.doTest("DynInheritTest1", new DynInheritTest1(), [
"DynInheritTest1()",
"DynInheritTest1.test()"
]);
this.doTest("DynInheritTest2", new DynInheritTest2(), [
"InheritTest1()",
"DynInheritTest2()",
"InheritTest1.test()",
"DynInheritTest2.test()"
]);
this.doTest("DynInheritTest3", new DynInheritTest3(), [
"InheritTest1()",
"DynInheritTest2()",
"DynInheritTest3()",
"InheritTest1.test()",
"DynInheritTest2.test()",
"DynInheritTest3.test()"
]);
this.doTest("InheritTest6", new InheritTest6(), [
"InheritTest1()",
"DynInheritTest2()",
"InheritTest6()",
"InheritTest1.test()",
"DynInheritTest2.test()",
"InheritTest6.test()"
]);
this.doTest("DynInheritTest4", new DynInheritTest4(), [
"InheritTest1()",
"DynInheritTest2()",
"InheritTest6()",
"DynInheritTest4()",
"InheritTest1.test()",
"DynInheritTest2.test()",
"InheritTest6.test()",
"DynInheritTest4.test()"
]);
this.doTest("DynInheritTest5", new DynInheritTest5(), [
"DynInheritTest1()",
"DynInheritTest5()",
"DynInheritTest1.test()",
"DynInheritTest5.test()"
]);
this.doTest("DynInheritTest6", new DynInheritTest6(), [
"DynInheritTest1()",
"DynInheritTest5()",
"DynInheritTest6()",
"DynInheritTest1.test()",
"DynInheritTest5.test()",
"DynInheritTest6.test()"
]);
this.doTest("InstInherit1", new InstInherit1(), [
"InstInherit1()",
"InstInherit1.test()"
]);
this.doTest("InstInherit2", new InstInherit2(), [
"InheritTest1()",
"InheritTest2()",
"InstInherit2()",
"InheritTest1.test()",
"InheritTest2.test()",
"InstInherit2.test()"
]);
// NOTE: Notice that InheritTest7.test() was not called -- this is because TS doesn't handle this
this.doTest("InheritTest7", new InheritTest7(), [
"InstInherit1()",
"InheritTest7()",
"InstInherit1.test()"
]);
// NOTE: Notice that DynInheritTest7.test() IS called -- this is because dynamicProto handles this scenario
this.doTest("DynInheritTest7", new DynInheritTest7(), [
"InstInherit1()",
"DynInheritTest7()",
"InstInherit1.test()",
"DynInheritTest7.test()"
]);
this.doTest("InstInherit3", new InstInherit3(), [
"InstInherit1()",
"DynInheritTest7()",
"InstInherit3()",
"InstInherit1.test()",
"DynInheritTest7.test()",
"InstInherit3.test()"
]);
this.doTest("DynInheritTest8", new DynInheritTest8(), [
"InstInherit1()",
"DynInheritTest7()",
"InstInherit3()",
"DynInheritTest8()",
"InstInherit1.test()",
"DynInheritTest7.test()",
"InstInherit3.test()",
"DynInheritTest8.test()"
]);
// Note: Bad inherit as with InheritTest7 fails to call base instance and actually throws in this case
this.doTest("BadInstInherit1", new BadInstInherit1(), [
"InstInherit1()",
"BadInstInherit1()",
"BadInstInherit1.throw()",
"BadInstInherit1.test()"
]);
// Note: dynamicProto doesn't fix broken base classes, but it still calls them in the correct order
this.doTest("DynInheritTest9", new DynInheritTest9(), [
"InstInherit1()",
"BadInstInherit1()",
"DynInheritTest9()",
"BadInstInherit1.throw()",
"BadInstInherit1.test()",
"DynInheritTest9.test()"
]);
this.doTest("GoodInstInherit1", new GoodInstInherit1(), [
"InstInherit1()",
"GoodInstInherit1()",
"InstInherit1.test()",
"GoodInstInherit1.test()"
]);
this.doTest("DynInheritTest10", new DynInheritTest10(), [
"InstInherit1()",
"GoodInstInherit1()",
"DynInheritTest10()",
"InstInherit1.test()",
"GoodInstInherit1.test()",
"DynInheritTest10.test()"
]);
this.doTest("GoodInstInherit2", new GoodInstInherit2(), [
"InstInherit1()",
"GoodInstInherit1()",
"DynInheritTest10()",
"GoodInstInherit2()",
"InstInherit1.test()",
"GoodInstInherit1.test()",
"DynInheritTest10.test()",
"GoodInstInherit2.test()",
]);
}
});
}
}

61
lib/test/External/blanket-1.2.2.js поставляемый Normal file

Различия файлов скрыты, потому что одна или несколько строк слишком длинны

44
lib/test/External/blanket-reporter.js поставляемый Normal file
Просмотреть файл

@ -0,0 +1,44 @@
(function myReporter() {
var reported = false;
var a = document.createElement("a");
document.body.appendChild(a);
//your reporter code
blanket.customReporter = function (coverage) {
blanket.defaultReporter(coverage);
var styleTags = document.getElementsByTagName("style");
var styles = "";
for (var i = 0; i < styleTags.length; i++) {
styles += styleTags[i].outerHTML;
}
var scriptTags = document.getElementsByTagName("body")[0].getElementsByTagName("script");
var scripts = "";
for (var i = 0; i < scriptTags.length; i++) {
scripts += scriptTags[i].outerHTML;
}
var title = document.getElementsByTagName("title")[0].text;
var documentName = title.replace(/[^\w]/ig, '') + 'Coverage.html';
var coverageReport = '';
coverageReport += '<html>' + '<head>' + styles + "</head><body><h1>" + title.replace(/[^\w\s]/ig, '') + "</h1>" + scripts + document.getElementById("blanket-main").outerHTML + "</body></html>";
var file = new Blob([coverageReport], { type: 'text/plain' });
a.href = URL.createObjectURL(file);
a.download = documentName;
if (!reported) {
a.click();
reported = true;
}
};
})();

10308
lib/test/External/jquery-1.11.1.js поставляемый Normal file

Разница между файлами не показана из-за своего большого размера Загрузить разницу

3926
lib/test/External/jquery.d.ts поставляемый Normal file

Разница между файлами не показана из-за своего большого размера Загрузить разницу

4334
lib/test/External/qunit-1.23.1.js поставляемый Normal file

Разница между файлами не показана из-за своего большого размера Загрузить разницу

723
lib/test/External/qunit.d.ts поставляемый Normal file
Просмотреть файл

@ -0,0 +1,723 @@
// Type definitions for QUnit 1.10
// Project: http://qunitjs.com/
// Definitions by: Diullei Gomes <https://github.com/diullei>
// DefinitelyTyped: https://github.com/borisyankov/DefinitelyTyped
interface DoneCallbackObject {
/**
* The number of failed assertions
*/
failed: number;
/**
* The number of passed assertions
*/
passed: number;
/**
* The total number of assertions
*/
total: number;
/**
* The time in milliseconds it took tests to run from start to finish.
*/
runtime: number;
}
interface LogCallbackObject {
/**
* The boolean result of an assertion, true means passed, false means failed.
*/
result: boolean;
/**
* One side of a comparision assertion. Can be undefined when ok() is used.
*/
actual: Object;
/**
* One side of a comparision assertion. Can be undefined when ok() is used.
*/
expected: Object;
/**
* A string description provided by the assertion.
*/
message: string;
/**
* The associated stacktrace, either from an exception or pointing to the source
* of the assertion. Depends on browser support for providing stacktraces, so can be
* undefined.
*/
source: string;
}
interface ModuleStartCallbackObject {
/**
* Name of the next module to run
*/
name: string;
}
interface ModuleDoneCallbackObject {
/**
* Name of this module
*/
name: string;
/**
* The number of failed assertions
*/
failed: number;
/**
* The number of passed assertions
*/
passed: number;
/**
* The total number of assertions
*/
total: number;
}
interface TestDoneCallbackObject {
/**
* TName of the next test to run
*/
name: string;
/**
* Name of the current module
*/
module: string;
/**
* The number of failed assertions
*/
failed: number;
/**
* The number of passed assertions
*/
passed: number;
/**
* The total number of assertions
*/
total: number;
/**
* The total runtime, including setup and teardown
*/
duration: number;
}
interface TestStartCallbackObject {
/**
* Name of the next test to run
*/
name: string;
/**
* Name of the current module
*/
module: string;
}
interface Config {
altertitle: boolean;
autostart: boolean;
current: Object;
reorder: boolean;
requireExpects: boolean;
testTimeout: number;
urlConfig: Array<URLConfigItem>;
done: any;
}
interface URLConfigItem {
id: string;
label: string;
tooltip: string;
}
interface LifecycleObject {
/**
* Runs before each test
*/
setup?: () => any;
/**
* Runs after each test
*/
teardown?: () => any;
}
interface QUnitAssert {
/* ASSERT */
assert: any;
current_testEnvironment: any;
jsDump: any;
/**
* A deep recursive comparison assertion, working on primitive types, arrays, objects,
* regular expressions, dates and functions.
*
* The deepEqual() assertion can be used just like equal() when comparing the value of
* objects, such that { key: value } is equal to { key: value }. For non-scalar values,
* identity will be disregarded by deepEqual.
*
* @param actual Object or Expression being tested
* @param expected Known comparison value
* @param message A short description of the assertion
*/
deepEqual(actual: any, expected: any, message?: string): any;
/**
* A non-strict comparison assertion, roughly equivalent to JUnit assertEquals.
*
* The equal assertion uses the simple comparison operator (==) to compare the actual
* and expected arguments. When they are equal, the assertion passes: any; otherwise, it fails.
* When it fails, both actual and expected values are displayed in the test result,
* in addition to a given message.
*
* @param actual Expression being tested
* @param expected Known comparison value
* @param message A short description of the assertion
*/
equal(actual: any, expected: any, message?: string): any;
/**
* An inverted deep recursive comparison assertion, working on primitive types,
* arrays, objects, regular expressions, dates and functions.
*
* The notDeepEqual() assertion can be used just like equal() when comparing the
* value of objects, such that { key: value } is equal to { key: value }. For non-scalar
* values, identity will be disregarded by notDeepEqual.
*
* @param actual Object or Expression being tested
* @param expected Known comparison value
* @param message A short description of the assertion
*/
notDeepEqual(actual: any, expected: any, message?: string): any;
/**
* A non-strict comparison assertion, checking for inequality.
*
* The notEqual assertion uses the simple inverted comparison operator (!=) to compare
* the actual and expected arguments. When they aren't equal, the assertion passes: any;
* otherwise, it fails. When it fails, both actual and expected values are displayed
* in the test result, in addition to a given message.
*
* @param actual Expression being tested
* @param expected Known comparison value
* @param message A short description of the assertion
*/
notEqual(actual: any, expected: any, message?: string): any;
notPropEqual(actual: any, expected: any, message?: string): any;
propEqual(actual: any, expected: any, message?: string): any;
/**
* A non-strict comparison assertion, checking for inequality.
*
* The notStrictEqual assertion uses the strict inverted comparison operator (!==)
* to compare the actual and expected arguments. When they aren't equal, the assertion
* passes: any; otherwise, it fails. When it fails, both actual and expected values are
* displayed in the test result, in addition to a given message.
*
* @param actual Expression being tested
* @param expected Known comparison value
* @param message A short description of the assertion
*/
notStrictEqual(actual: any, expected: any, message?: string): any;
/**
* A boolean assertion, equivalent to CommonJSs assert.ok() and JUnits assertTrue().
* Passes if the first argument is truthy.
*
* The most basic assertion in QUnit, ok() requires just one argument. If the argument
* evaluates to true, the assertion passes; otherwise, it fails. If a second message
* argument is provided, it will be displayed in place of the result.
*
* @param state Expression being tested
* @param message A short description of the assertion
*/
ok(state: any, message?: string): any;
/**
* A strict type and value comparison assertion.
*
* The strictEqual() assertion provides the most rigid comparison of type and value with
* the strict equality operator (===)
*
* @param actual Expression being tested
* @param expected Known comparison value
* @param message A short description of the assertion
*/
strictEqual(actual: any, expected: any, message?: string): any;
/**
* Assertion to test if a callback throws an exception when run.
*
* When testing code that is expected to throw an exception based on a specific set of
* circumstances, use throws() to catch the error object for testing and comparison.
*
* @param block Function to execute
* @param expected Error Object to compare
* @param message A short description of the assertion
*/
throws(block: () => any, expected: any, message?: string): any;
/**
* @param block Function to execute
* @param message A short description of the assertion
*/
throws(block: () => any, message?: string): any;
}
interface QUnitStatic extends QUnitAssert{
/* ASYNC CONTROL */
/**
* Start running tests again after the testrunner was stopped. See stop().
*
* When your async test has multiple exit points, call start() for the corresponding number of stop() increments.
*
* @param decrement Optional argument to merge multiple start() calls into one. Use with multiple corrsponding stop() calls.
*/
start(decrement?: number): any;
/**
* Stop the testrunner to wait for async tests to run. Call start() to continue.
*
* When your async test has multiple exit points, call stop() with the increment argument, corresponding to the number of start() calls you need.
*
* On Blackberry 5.0, window.stop is a native read-only function. If you deal with that browser, use QUnit.stop() instead, which will work anywhere.
*
* @param decrement Optional argument to merge multiple stop() calls into one. Use with multiple corrsponding start() calls.
*/
stop(increment? : number): any;
/* CALLBACKS */
/**
* Register a callback to fire whenever the test suite begins.
*
* QUnit.begin() is called once before running any tests. (a better would've been QUnit.start,
* but thats already in use elsewhere and can't be changed.)
*
* @param callback Callback to execute
*/
begin(callback: () => any): any;
/**
* Register a callback to fire whenever the test suite ends.
*
* @param callback Callback to execute.
*/
done(callback: (details: DoneCallbackObject) => any): any;
/**
* Register a callback to fire whenever an assertion completes.
*
* This is one of several callbacks QUnit provides. Its intended for integration scenarios like
* PhantomJS or Jenkins. The properties of the details argument are listed below as options.
*
* @param callback Callback to execute.
*/
log(callback: (details: LogCallbackObject) => any): any;
/**
* Register a callback to fire whenever a module ends.
*
* @param callback Callback to execute.
*/
moduleDone(callback: (details: ModuleDoneCallbackObject) => any): any;
/**
* Register a callback to fire whenever a module begins.
*
* @param callback Callback to execute.
*/
moduleStart(callback: (details: ModuleStartCallbackObject) => any): any;
/**
* Register a callback to fire whenever a test ends.
*
* @param callback Callback to execute.
*/
testDone(callback: (details: TestDoneCallbackObject) => any): any;
/**
* Register a callback to fire whenever a test begins.
*
* @param callback Callback to execute.
*/
testStart(callback: (details: TestStartCallbackObject) => any): any;
/* CONFIGURATION */
/**
* QUnit has a bunch of internal configuration defaults, some of which are
* useful to override. Check the description for each option for details.
*/
config: Config;
/* TEST */
/**
* Add an asynchronous test to run. The test must include a call to start().
*
* For testing asynchronous code, asyncTest will automatically stop the test runner
* and wait for your code to call start() to continue.
*
* @param name Title of unit being tested
* @param expected Number of assertions in this test
* @param test Function to close over assertions
*/
asyncTest(name: string, expected: number, test: () => any): any;
/**
* Add an asynchronous test to run. The test must include a call to start().
*
* For testing asynchronous code, asyncTest will automatically stop the test runner
* and wait for your code to call start() to continue.
*
* @param name Title of unit being tested
* @param test Function to close over assertions
*/
asyncTest(name: string, test: () => any): any;
/**
* Specify how many assertions are expected to run within a test.
*
* To ensure that an explicit number of assertions are run within any test, use
* expect( number ) to register an expected count. If the number of assertions
* run does not match the expected count, the test will fail.
*
* @param amount Number of assertions in this test.
*/
expect(amount: number): any;
/**
* Group related tests under a single label.
*
* All tests that occur after a call to module() will be grouped into that module.
* The test names will all be preceded by the module name in the test results.
* You can then use that module name to select tests to run.
*
* @param name Label for this group of tests
* @param lifecycle Callbacks to run before and after each test
*/
module(name: string, lifecycle?: LifecycleObject): any;
/**
* Add a test to run.
*
* When testing the most common, synchronous code, use test().
* The assert argument to the callback contains all of QUnit's assertion methods.
* If you are avoiding using any of QUnit's globals, you can use the assert
* argument instead.
*
* @param title Title of unit being tested
* @param expected Number of assertions in this test
* @param test Function to close over assertions
*/
test(title: string, expected: number, test: (assert: QUnitAssert) => any): any;
/**
* @param title Title of unit being tested
* @param test Function to close over assertions
*/
test(title: string, test: (assert: QUnitAssert) => any): any;
/**
* https://github.com/jquery/qunit/blob/master/qunit/qunit.js#L1568
*/
equiv(a: any, b: any): any;
// https://github.com/jquery/qunit/blob/master/qunit/qunit.js#L661
raises: any;
/**
* https://github.com/jquery/qunit/blob/master/qunit/qunit.js#L897
*/
push(result: any, actual: any, expected: any, message: string): any;
/**
* https://github.com/jquery/qunit/blob/master/qunit/qunit.js#L839
*/
reset(): any;
}
/* ASSERT */
/**
* A deep recursive comparison assertion, working on primitive types, arrays, objects,
* regular expressions, dates and functions.
*
* The deepEqual() assertion can be used just like equal() when comparing the value of
* objects, such that { key: value } is equal to { key: value }. For non-scalar values,
* identity will be disregarded by deepEqual.
*
* @param actual Object or Expression being tested
* @param expected Known comparison value
* @param message A short description of the assertion
*/
declare function deepEqual(actual: any, expected: any, message?: string): any;
/**
* A non-strict comparison assertion, roughly equivalent to JUnit assertEquals.
*
* The equal assertion uses the simple comparison operator (==) to compare the actual
* and expected arguments. When they are equal, the assertion passes: any; otherwise, it fails.
* When it fails, both actual and expected values are displayed in the test result,
* in addition to a given message.
*
* @param actual Expression being tested
* @param expected Known comparison value
* @param message A short description of the assertion
*/
declare function equal(actual: any, expected: any, message?: string): any;
/**
* An inverted deep recursive comparison assertion, working on primitive types,
* arrays, objects, regular expressions, dates and functions.
*
* The notDeepEqual() assertion can be used just like equal() when comparing the
* value of objects, such that { key: value } is equal to { key: value }. For non-scalar
* values, identity will be disregarded by notDeepEqual.
*
* @param actual Object or Expression being tested
* @param expected Known comparison value
* @param message A short description of the assertion
*/
declare function notDeepEqual(actual: any, expected: any, message?: string): any;
/**
* A non-strict comparison assertion, checking for inequality.
*
* The notEqual assertion uses the simple inverted comparison operator (!=) to compare
* the actual and expected arguments. When they aren't equal, the assertion passes;
* otherwise, it fails. When it fails, both actual and expected values are displayed
* in the test result, in addition to a given message.
*
* @param actual Expression being tested
* @param expected Known comparison value
* @param message A short description of the assertion
*/
declare function notEqual(actual: any, expected: any, message?: string): any;
/**
* A non-strict comparison assertion, checking for inequality.
*
* The notStrictEqual assertion uses the strict inverted comparison operator (!==)
* to compare the actual and expected arguments. When they aren't equal, the assertion
* passes; otherwise, it fails. When it fails, both actual and expected values are
* displayed in the test result, in addition to a given message.
*
* @param actual Expression being tested
* @param expected Known comparison value
* @param message A short description of the assertion
*/
declare function notStrictEqual(actual: any, expected: any, message?: string): any;
/**
* A boolean assertion, equivalent to CommonJSs assert.ok() and JUnits assertTrue().
* Passes if the first argument is truthy.
*
* The most basic assertion in QUnit, ok() requires just one argument. If the argument
* evaluates to true, the assertion passes; otherwise, it fails. If a second message
* argument is provided, it will be displayed in place of the result.
*
* @param state Expression being tested
* @param message A short description of the assertion
*/
declare function ok(state: any, message?: string): any;
/**
* A strict type and value comparison assertion.
*
* The strictEqual() assertion provides the most rigid comparison of type and value with
* the strict equality operator (===)
*
* @param actual Expression being tested
* @param expected Known comparison value
* @param message A short description of the assertion
*/
declare function strictEqual(actual: any, expected: any, message?: string): any;
/**
* Assertion to test if a callback throws an exception when run.
*
* When testing code that is expected to throw an exception based on a specific set of
* circumstances, use throws() to catch the error object for testing and comparison.
*
* @param block Function to execute
* @param expected Error Object to compare
* @param message A short description of the assertion
*/
declare function throws(block: () => any, expected: any, message?: string): any;
/**
* @param block Function to execute
* @param message A short description of the assertion
*/
declare function throws(block: () => any, message?: string): any;
/* ASYNC CONTROL */
/**
* Start running tests again after the testrunner was stopped. See stop().
*
* When your async test has multiple exit points, call start() for the corresponding number of stop() increments.
*
* @param decrement Optional argument to merge multiple start() calls into one. Use with multiple corrsponding stop() calls.
*/
declare function start(decrement?: number): any;
/**
* Stop the testrunner to wait for async tests to run. Call start() to continue.
*
* When your async test has multiple exit points, call stop() with the increment argument, corresponding to the number of start() calls you need.
*
* On Blackberry 5.0, window.stop is a native read-only function. If you deal with that browser, use QUnit.stop() instead, which will work anywhere.
*
* @param decrement Optional argument to merge multiple stop() calls into one. Use with multiple corrsponding start() calls.
*/
declare function stop(increment? : number): any;
/* CALLBACKS */
/**
* Register a callback to fire whenever the test suite begins.
*
* QUnit.begin() is called once before running any tests. (a better would've been QUnit.start,
* but thats already in use elsewhere and can't be changed.)
*
* @param callback Callback to execute
*/
declare function begin(callback: () => any): any;
/**
* Register a callback to fire whenever the test suite ends.
*
* @param callback Callback to execute.
*/
declare function done(callback: (details: DoneCallbackObject) => any): any;
/**
* Register a callback to fire whenever an assertion completes.
*
* This is one of several callbacks QUnit provides. Its intended for integration scenarios like
* PhantomJS or Jenkins. The properties of the details argument are listed below as options.
*
* @param callback Callback to execute.
*/
declare function log(callback: (details: LogCallbackObject) => any): any;
/**
* Register a callback to fire whenever a module ends.
*
* @param callback Callback to execute.
*/
declare function moduleDone(callback: (details: ModuleDoneCallbackObject) => any): any;
/**
* Register a callback to fire whenever a module begins.
*
* @param callback Callback to execute.
*/
declare function moduleStart(callback: (name: string) => any): any;
/**
* Register a callback to fire whenever a test ends.
*
* @param callback Callback to execute.
*/
declare function testDone(callback: (details: TestDoneCallbackObject) => any): any;
/**
* Register a callback to fire whenever a test begins.
*
* @param callback Callback to execute.
*/
declare function testStart(callback: (details: TestStartCallbackObject) => any): any;
/* TEST */
/**
* Add an asynchronous test to run. The test must include a call to start().
*
* For testing asynchronous code, asyncTest will automatically stop the test runner
* and wait for your code to call start() to continue.
*
* @param name Title of unit being tested
* @param expected Number of assertions in this test
* @param test Function to close over assertions
*/
declare function asyncTest(name: string, expected?: any, test?: () => any): any;
/**
* Add an asynchronous test to run. The test must include a call to start().
*
* For testing asynchronous code, asyncTest will automatically stop the test runner
* and wait for your code to call start() to continue.
*
* @param name Title of unit being tested
* @param test Function to close over assertions
*/
declare function asyncTest(name: string, test: () => any): any;
/**
* Specify how many assertions are expected to run within a test.
*
* To ensure that an explicit number of assertions are run within any test, use
* expect( number ) to register an expected count. If the number of assertions
* run does not match the expected count, the test will fail.
*
* @param amount Number of assertions in this test.
*/
declare function expect(amount: number): any;
// ** conflict with TypeScript module keyword. Must be used on QUnit namespace
//declare var module: (name: string, lifecycle?: LifecycleObject) => any;
/**
* Add a test to run.
*
* When testing the most common, synchronous code, use test().
* The assert argument to the callback contains all of QUnit's assertion methods.
* If you are avoiding using any of QUnit's globals, you can use the assert
* argument instead.
*
* @param title Title of unit being tested
* @param expected Number of assertions in this test
* @param test Function to close over assertions
*/
declare function test(title: string, expected: number, test: (assert?: QUnitAssert) => any): any;
/**
* @param title Title of unit being tested
* @param test Function to close over assertions
*/
declare function test(title: string, test: (assert?: QUnitAssert) => any): any;
declare function notPropEqual(actual: any, expected: any, message?: string): any;
declare function propEqual(actual: any, expected: any, message?: string): any;
// https://github.com/jquery/qunit/blob/master/qunit/qunit.js#L1568
declare function equiv(a: any, b: any): any;
// https://github.com/jquery/qunit/blob/master/qunit/qunit.js#L661
declare var raises: any;
/* QUNIT */
declare var QUnit: QUnitStatic;

6389
lib/test/External/sinon-1.17.2.js поставляемый Normal file

Разница между файлами не показана из-за своего большого размера Загрузить разницу

434
lib/test/External/sinon.d.ts поставляемый Normal file
Просмотреть файл

@ -0,0 +1,434 @@
// Type definitions for Sinon 1.16.0
// Project: http://sinonjs.org/
// Definitions by: William Sears <https://github.com/mrbigdog2u>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
interface SinonSpyCallApi {
// Properties
thisValue: any;
args: any[];
exception: any;
returnValue: any;
// Methods
calledOn(obj: any): boolean;
calledWith(...args: any[]): boolean;
calledWithExactly(...args: any[]): boolean;
calledWithMatch(...args: any[]): boolean;
notCalledWith(...args: any[]): boolean;
notCalledWithMatch(...args: any[]): boolean;
returned(value: any): boolean;
threw(): boolean;
threw(type: string): boolean;
threw(obj: any): boolean;
callArg(pos: number): void;
callArgOn(pos: number, obj: any, ...args: any[]): void;
callArgWith(pos: number, ...args: any[]): void;
callArgOnWith(pos: number, obj: any, ...args: any[]): void;
yield(...args: any[]): void;
yieldOn(obj: any, ...args: any[]): void;
yieldTo(property: string, ...args: any[]): void;
yieldToOn(property: string, obj: any, ...args: any[]): void;
}
interface SinonSpyCall extends SinonSpyCallApi {
calledBefore(call: SinonSpyCall): boolean;
calledAfter(call: SinonSpyCall): boolean;
calledWithNew(call: SinonSpyCall): boolean;
}
interface SinonSpy extends SinonSpyCallApi {
// Properties
callCount: number;
called: boolean;
notCalled: boolean;
calledOnce: boolean;
calledTwice: boolean;
calledThrice: boolean;
firstCall: SinonSpyCall;
secondCall: SinonSpyCall;
thirdCall: SinonSpyCall;
lastCall: SinonSpyCall;
thisValues: any[];
args: any[][];
exceptions: any[];
returnValues: any[];
// Methods
(...args: any[]): any;
calledBefore(anotherSpy: SinonSpy): boolean;
calledAfter(anotherSpy: SinonSpy): boolean;
calledWithNew(spy: SinonSpy): boolean;
withArgs(...args: any[]): SinonSpy;
alwaysCalledOn(obj: any): boolean;
alwaysCalledWith(...args: any[]): boolean;
alwaysCalledWithExactly(...args: any[]): boolean;
alwaysCalledWithMatch(...args: any[]): boolean;
neverCalledWith(...args: any[]): boolean;
neverCalledWithMatch(...args: any[]): boolean;
alwaysThrew(): boolean;
alwaysThrew(type: string): boolean;
alwaysThrew(obj: any): boolean;
alwaysReturned(): boolean;
invokeCallback(...args: any[]): void;
getCall(n: number): SinonSpyCall;
reset(): void;
printf(format: string, ...args: any[]): string;
restore(): void;
}
interface SinonSpyStatic {
(): SinonSpy;
(func: any): SinonSpy;
(obj: any, method: string): SinonSpy;
}
interface SinonStatic {
spy: SinonSpyStatic;
}
interface SinonStub extends SinonSpy {
resetBehavior(): void;
returns(obj: any): SinonStub;
returnsArg(index: number): SinonStub;
throws(type?: string): SinonStub;
throws(obj: any): SinonStub;
callsArg(index: number): SinonStub;
callsArgOn(index: number, context: any): SinonStub;
callsArgWith(index: number, ...args: any[]): SinonStub;
callsArgOnWith(index: number, context: any, ...args: any[]): SinonStub;
callsArgAsync(index: number): SinonStub;
callsArgOnAsync(index: number, context: any): SinonStub;
callsArgWithAsync(index: number, ...args: any[]): SinonStub;
callsArgOnWithAsync(index: number, context: any, ...args: any[]): SinonStub;
onCall(n: number): SinonStub;
onFirstCall(): SinonStub;
onSecondCall(): SinonStub;
onThirdCall(): SinonStub;
yields(...args: any[]): SinonStub;
yieldsOn(context: any, ...args: any[]): SinonStub;
yieldsTo(property: string, ...args: any[]): SinonStub;
yieldsToOn(property: string, context: any, ...args: any[]): SinonStub;
yieldsAsync(...args: any[]): SinonStub;
yieldsOnAsync(context: any, ...args: any[]): SinonStub;
yieldsToAsync(property: string, ...args: any[]): SinonStub;
yieldsToOnAsync(property: string, context: any, ...args: any[]): SinonStub;
withArgs(...args: any[]): SinonStub;
}
interface SinonStubStatic {
(): SinonStub;
(obj: any): SinonStub;
(obj: any, method: string): SinonStub;
(obj: any, method: string, func: any): SinonStub;
}
interface SinonStatic {
stub: SinonStubStatic;
}
interface SinonExpectation extends SinonStub {
atLeast(n: number): SinonExpectation;
atMost(n: number): SinonExpectation;
never(): SinonExpectation;
once(): SinonExpectation;
twice(): SinonExpectation;
thrice(): SinonExpectation;
exactly(n: number): SinonExpectation;
withArgs(...args: any[]): SinonExpectation;
withExactArgs(...args: any[]): SinonExpectation;
on(obj: any): SinonExpectation;
verify(): SinonExpectation;
restore(): void;
}
interface SinonExpectationStatic {
create(methodName?: string): SinonExpectation;
}
interface SinonMock {
expects(method: string): SinonExpectation;
restore(): void;
verify(): void;
}
interface SinonMockStatic {
(): SinonExpectation;
(obj: any): SinonMock;
}
interface SinonStatic {
expectation: SinonExpectationStatic;
mock: SinonMockStatic;
}
interface SinonFakeTimers {
now: number;
create(now: number): SinonFakeTimers;
setTimeout(callback: (...args: any[]) => void, timeout: number, ...args: any[]): number;
clearTimeout(id: number): void;
setInterval(callback: (...args: any[]) => void, timeout: number, ...args: any[]): number;
clearInterval(id: number): void;
tick(ms: number): number;
reset(): void;
Date(): Date;
Date(year: number): Date;
Date(year: number, month: number): Date;
Date(year: number, month: number, day: number): Date;
Date(year: number, month: number, day: number, hour: number): Date;
Date(year: number, month: number, day: number, hour: number, minute: number): Date;
Date(year: number, month: number, day: number, hour: number, minute: number, second: number): Date;
Date(year: number, month: number, day: number, hour: number, minute: number, second: number, ms: number): Date;
restore(): void;
/**
* Simulate the user changing the system clock while your program is running. It changes the 'now' timestamp
* without affecting timers, intervals or immediates.
* @param now The new 'now' in unix milliseconds
*/
setSystemTime(now: number): void;
/**
* Simulate the user changing the system clock while your program is running. It changes the 'now' timestamp
* without affecting timers, intervals or immediates.
* @param now The new 'now' as a JavaScript Date
*/
setSystemTime(date: Date): void;
}
interface SinonFakeTimersStatic {
(): SinonFakeTimers;
(...timers: string[]): SinonFakeTimers;
(now: number, ...timers: string[]): SinonFakeTimers;
}
interface SinonStatic {
useFakeTimers: SinonFakeTimersStatic;
clock: SinonFakeTimers;
}
interface SinonFakeUploadProgress {
eventListeners: {
progress: any[];
load: any[];
abort: any[];
error: any[];
};
addEventListener(event: string, listener: (e: Event) => any): void;
removeEventListener(event: string, listener: (e: Event) => any): void;
dispatchEvent(event: Event): void;
}
interface SinonFakeXMLHttpRequest {
// Properties
onCreate: (xhr: SinonFakeXMLHttpRequest) => void;
url: string;
method: string;
requestHeaders: any;
requestBody: string;
status: number;
statusText: string;
async: boolean;
username: string;
password: string;
withCredentials: boolean;
upload: SinonFakeUploadProgress;
responseXML: Document;
getResponseHeader(header: string): string;
getAllResponseHeaders(): any;
// Methods
restore(): void;
useFilters: boolean;
addFilter(filter: (method: string, url: string, async: boolean, username: string, password: string) => boolean): void;
setResponseHeaders(headers: any): void;
setResponseBody(body: string): void;
respond(status: number, headers: any, body: string): void;
autoRespond(ms: number): void;
}
interface SinonFakeXMLHttpRequestStatic {
(): SinonFakeXMLHttpRequest;
}
interface SinonStatic {
useFakeXMLHttpRequest: SinonFakeXMLHttpRequestStatic;
FakeXMLHttpRequest: SinonFakeXMLHttpRequest;
}
interface SinonFakeServer {
// Properties
autoRespond: boolean;
autoRespondAfter: number;
fakeHTTPMethods: boolean;
getHTTPMethod: (request: SinonFakeXMLHttpRequest) => string;
requests: SinonFakeXMLHttpRequest[];
respondImmediately: boolean;
// Methods
respondWith(body: string): void;
respondWith(response: any[]): void;
respondWith(fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
respondWith(url: string, body: string): void;
respondWith(url: string, response: any[]): void;
respondWith(url: string, fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
respondWith(method: string, url: string, body: string): void;
respondWith(method: string, url: string, response: any[]): void;
respondWith(method: string, url: string, fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
respondWith(url: RegExp, body: string): void;
respondWith(url: RegExp, response: any[]): void;
respondWith(url: RegExp, fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
respondWith(method: string, url: RegExp, body: string): void;
respondWith(method: string, url: RegExp, response: any[]): void;
respondWith(method: string, url: RegExp, fn: (xhr: SinonFakeXMLHttpRequest) => void): void;
respond(): void;
restore(): void;
}
interface SinonFakeServerStatic {
create(): SinonFakeServer;
}
interface SinonStatic {
fakeServer: SinonFakeServerStatic;
fakeServerWithClock: SinonFakeServerStatic;
}
interface SinonExposeOptions {
prefix?: string;
includeFail?: boolean;
}
interface SinonAssert {
// Properties
failException: string;
fail: (message?: string) => void; // Overridable
pass: (assertion: any) => void; // Overridable
// Methods
notCalled(spy: SinonSpy): void;
called(spy: SinonSpy): void;
calledOnce(spy: SinonSpy): void;
calledTwice(spy: SinonSpy): void;
calledThrice(spy: SinonSpy): void;
callCount(spy: SinonSpy, count: number): void;
callOrder(...spies: SinonSpy[]): void;
calledOn(spy: SinonSpy, obj: any): void;
alwaysCalledOn(spy: SinonSpy, obj: any): void;
calledWith(spy: SinonSpy, ...args: any[]): void;
alwaysCalledWith(spy: SinonSpy, ...args: any[]): void;
neverCalledWith(spy: SinonSpy, ...args: any[]): void;
calledWithExactly(spy: SinonSpy, ...args: any[]): void;
alwaysCalledWithExactly(spy: SinonSpy, ...args: any[]): void;
calledWithMatch(spy: SinonSpy, ...args: any[]): void;
alwaysCalledWithMatch(spy: SinonSpy, ...args: any[]): void;
neverCalledWithMatch(spy: SinonSpy, ...args: any[]): void;
threw(spy: SinonSpy): void;
threw(spy: SinonSpy, exception: string): void;
threw(spy: SinonSpy, exception: any): void;
alwaysThrew(spy: SinonSpy): void;
alwaysThrew(spy: SinonSpy, exception: string): void;
alwaysThrew(spy: SinonSpy, exception: any): void;
expose(obj: any, options?: SinonExposeOptions): void;
}
interface SinonStatic {
assert: SinonAssert;
}
interface SinonMatcher {
and(expr: SinonMatcher): SinonMatcher;
or(expr: SinonMatcher): SinonMatcher;
}
interface SinonMatch {
(value: number): SinonMatcher;
(value: string): SinonMatcher;
(expr: RegExp): SinonMatcher;
(obj: any): SinonMatcher;
(callback: (value: any) => boolean): SinonMatcher;
any: SinonMatcher;
defined: SinonMatcher;
truthy: SinonMatcher;
falsy: SinonMatcher;
bool: SinonMatcher;
number: SinonMatcher;
string: SinonMatcher;
object: SinonMatcher;
func: SinonMatcher;
array: SinonMatcher;
regexp: SinonMatcher;
date: SinonMatcher;
same(obj: any): SinonMatcher;
typeOf(type: string): SinonMatcher;
instanceOf(type: any): SinonMatcher;
has(property: string, expect?: any): SinonMatcher;
hasOwn(property: string, expect?: any): SinonMatcher;
}
interface SinonStatic {
match: SinonMatch;
}
interface SinonSandboxConfig {
injectInto?: any;
properties?: string[];
useFakeTimers?: any;
useFakeServer?: any;
}
interface SinonSandbox {
clock: SinonFakeTimers;
requests: SinonFakeXMLHttpRequest;
server: SinonFakeServer;
spy: SinonSpyStatic;
stub: SinonStubStatic;
mock: SinonMockStatic;
useFakeTimers: SinonFakeTimersStatic;
useFakeXMLHttpRequest: SinonFakeXMLHttpRequestStatic;
useFakeServer(): SinonFakeServer;
restore(): void;
}
interface SinonSandboxStatic {
create(): SinonSandbox;
create(config: SinonSandboxConfig): SinonSandbox;
}
interface SinonStatic {
sandbox: SinonSandboxStatic;
}
interface SinonTestConfig {
injectIntoThis?: boolean;
injectInto?: any;
properties?: string[];
useFakeTimers?: boolean;
useFakeServer?: boolean;
}
interface SinonTestWrapper extends SinonSandbox {
(...args: any[]): any;
}
interface SinonStatic {
config: SinonTestConfig;
test(fn: (...args: any[]) => any): SinonTestWrapper;
testCase(tests: any): any;
}
// Utility overridables
interface SinonStatic {
createStubInstance(constructor: any): SinonStub;
format(obj: any): string;
log(message: string): void;
restore(object: any): void;
}
declare var sinon: SinonStatic;
declare module "sinon" {
export = sinon;
}

Просмотреть файл

@ -0,0 +1,5 @@
import { DynamicProtoTests } from '../DynamicProto.Tests';
export function runTests() {
new DynamicProtoTests().registerTests();
}

Просмотреть файл

@ -0,0 +1,48 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Cache-control" content="no-Cache" />
<title>Tests for Dynamic Proto</title>
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-1.23.1.css">
<script src="http://sinonjs.org/releases/sinon-2.3.8.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/require.js/2.2.0/require.js"></script>
<script src="dynamicprototests.js"></script>
<script>
require.config({
baseUrl: '../',
paths: {
qunit: "./External/qunit-1.23.1"
}
});
// Load core & common before channel tests
require([
'qunit' // Load qunit here instead of with tests, otherwise will not work
], function (QUnit, aicore) {
require.config({
baseUrl: './'
});
require([
"test/Selenium/DynamicProtoTests"
], function (tests) {
QUnit.start();
tests.runTests();
}, function (err) {
console.log('REQUIRE ERROR:', err.toString());
});
})
</script>
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<div id="error-message"></div>
</body>
</html>

Просмотреть файл

@ -0,0 +1,147 @@
/// <reference path="../External/qunit.d.ts" />
/**
* Wrapper around QUnit asserts. This class has two purposes:
* - Make Assertion methods easy to discover.
* - Make them consistent with XUnit assertions in the order of the actual and expected parameter values.
*/
class Assert {
/**
* A deep recursive comparison assertion, working on primitive types, arrays, objects,
* regular expressions, dates and functions.
*
* The deepEqual() assertion can be used just like equal() when comparing the value of
* objects, such that { key: value } is equal to { key: value }. For non-scalar values,
* identity will be disregarded by deepEqual.
*
* @param expected Known comparison value
* @param actual Object or Expression being tested
* @param message A short description of the assertion
*/
public static deepEqual(expected: any, actual: any, message?: string): any {
return deepEqual(actual, expected, message);
}
/**
* A non-strict comparison assertion, roughly equivalent to JUnit assertEquals.
*
* The equal assertion uses the simple comparison operator (==) to compare the actual
* and expected arguments. When they are equal, the assertion passes: any; otherwise, it fails.
* When it fails, both actual and expected values are displayed in the test result,
* in addition to a given message.
*
* @param expected Known comparison value
* @param actual Expression being tested
* @param message A short description of the assertion
*/
public static equal(expected: any, actual: any, message?: string): any {
return equal(actual, expected, message);
}
/**
* An inverted deep recursive comparison assertion, working on primitive types,
* arrays, objects, regular expressions, dates and functions.
*
* The notDeepEqual() assertion can be used just like equal() when comparing the
* value of objects, such that { key: value } is equal to { key: value }. For non-scalar
* values, identity will be disregarded by notDeepEqual.
*
* @param expected Known comparison value
* @param actual Object or Expression being tested
* @param message A short description of the assertion
*/
public static notDeepEqual(expected: any, actual: any, message?: string): any {
return notDeepEqual(actual, expected, message);
}
/**
* A non-strict comparison assertion, checking for inequality.
*
* The notEqual assertion uses the simple inverted comparison operator (!=) to compare
* the actual and expected arguments. When they aren't equal, the assertion passes: any;
* otherwise, it fails. When it fails, both actual and expected values are displayed
* in the test result, in addition to a given message.
*
* @param expected Known comparison value
* @param actual Expression being tested
* @param message A short description of the assertion
*/
public static notEqual(expected: any, actual: any, message?: string): any {
return notEqual(actual, expected, message);
}
public static notPropEqual(expected: any, actual: any, message?: string): any {
return notPropEqual(actual, expected, message);
}
public static propEqual(expected: any, actual: any, message?: string): any {
return propEqual(actual, expected, message);
}
/**
* A non-strict comparison assertion, checking for inequality.
*
* The notStrictEqual assertion uses the strict inverted comparison operator (!==)
* to compare the actual and expected arguments. When they aren't equal, the assertion
* passes: any; otherwise, it fails. When it fails, both actual and expected values are
* displayed in the test result, in addition to a given message.
*
* @param expected Known comparison value
* @param actual Expression being tested
* @param message A short description of the assertion
*/
public static notStrictEqual(expected: any, actual: any, message?: string): any {
return notStrictEqual(actual, expected, message);
}
/**
* A boolean assertion, equivalent to CommonJS's assert.ok() and JUnit's assertTrue().
* Passes if the first argument is truthy.
*
* The most basic assertion in QUnit, ok() requires just one argument. If the argument
* evaluates to true, the assertion passes; otherwise, it fails. If a second message
* argument is provided, it will be displayed in place of the result.
*
* @param state Expression being tested
* @param message A short description of the assertion
*/
public static ok(state: any, message?: string): any {
return ok(state, message);
}
/**
* A strict type and value comparison assertion.
*
* The strictEqual() assertion provides the most rigid comparison of type and value with
* the strict equality operator (===)
*
* @param expected Known comparison value
* @param actual Expression being tested
* @param message A short description of the assertion
*/
public static strictEqual(expected: any, actual: any, message?: string): any {
return strictEqual(actual, expected, message);
}
/**
* Assertion to test if a callback throws an exception when run.
*
* When testing code that is expected to throw an exception based on a specific set of
* circumstances, use throws() to catch the error object for testing and comparison.
*
* @param block Function to execute
* @param expected Error Object to compare
* @param message A short description of the assertion
*/
public static throws(block: () => any, expected: any, message?: string): any;
/**
* @param block Function to execute
* @param message A short description of the assertion
*/
public static throws(block: () => any, message?: string): any;
public static throws(block: () => any, expected?: any, message?: string): any {
return throws(block, expected, message);
}
}

Просмотреть файл

@ -0,0 +1,6 @@
/// <reference path="../External/sinon.d.ts" />
/// <reference path="../External/qunit.d.ts" />
/// <reference path="Assert.ts" />
/// <reference path="PollingAssert.ts" />
/// <reference path="TestClass.ts" />
/// <reference path="TestCase.ts" />

Просмотреть файл

@ -0,0 +1,146 @@
/// <reference path="../../JavaScriptSDK/Serializer.ts" />
/// <reference path="./TestClass.ts"/>
class ContractTestHelper extends TestClass {
public name: string;
private initializer: () => Microsoft.ApplicationInsights.ISerializable;
constructor(initializer: () => Microsoft.ApplicationInsights.ISerializable, name: string) {
super();
this.name = name;
this.initializer = initializer;
}
/** Method called before the start of each test method */
public testInitialize() {
}
/** Method called after each test method has completed */
public testCleanup() {
}
public registerTests() {
const name = this.name + ": ";
this.testCase({
name: name + "constructor does not throw errors",
test: () => {
this.getSubject(this.initializer, this.name);
}
});
this.testCase({
name: name + "serialization does not throw errors",
test: () => {
const subject = this.getSubject(this.initializer, this.name);
this.serialize(subject, this.name);
}
});
this.testCase({
name: name + "all required fields are constructed",
test: () => {
this.allRequiredFieldsAreConstructed(this.initializer, this.name);
}
});
this.testCase({
name: name + "extra fields are removed upon serialization",
test: () => {
this.extraFieldsAreRemovedBySerializer(this.initializer, this.name);
}
});
this.testCase({
name: this.name + "optional fields are not required by the back end",
test: () => {
this.optionalFieldsAreNotRequired(this.initializer, this.name);
}
});
this.testCase({
name: this.name + "all fields are serialized if included",
test: () => {
this.allFieldsAreIncludedIfSpecified(this.initializer, this.name);
}
});
}
public checkSerializableObject(initializer: () => any, name: string) {
this.allRequiredFieldsAreConstructed(initializer, name);
this.extraFieldsAreRemovedBySerializer(initializer, name);
this.allFieldsAreIncludedIfSpecified(initializer, name);
}
private allRequiredFieldsAreConstructed(initializer: () => any, name: string) {
const subject = this.getSubject(initializer, name);
for (const field in subject.aiDataContract) {
if (subject.aiDataContract[field] & Microsoft.ApplicationInsights.FieldType.Required) {
Assert.ok(subject[field] != null, "The required field '" + field + "' is constructed for: '" + name + "'");
}
}
}
private extraFieldsAreRemovedBySerializer(initializer: () => any, name: string) {
const subject = this.getSubject(initializer, name);
const extra = "extra";
subject[extra + 0] = extra;
subject[extra + 1] = extra;
subject[extra + 3] = extra;
const serializedSubject = this.serialize(subject, name);
for (const field in serializedSubject) {
Assert.ok(subject.aiDataContract[field] != null, "The field '" + field + "' exists in the contract for '" + name + "' and was serialized");
}
}
private optionalFieldsAreNotRequired(initializer: () => any, name: string) {
const subject = this.getSubject(this.initializer, this.name);
for (const field in subject.aiDataContract) {
if (!subject.aiDataContract[field]) {
delete subject[field];
}
}
}
private allFieldsAreIncludedIfSpecified(initializer: () => any, name: string) {
const subject = this.getSubject(this.initializer, this.name);
for (const field in subject.aiDataContract) {
subject[field] = field;
}
const serializedSubject = this.serialize(subject, this.name);
for (field in subject.aiDataContract) {
Assert.ok(serializedSubject[field] === field, "Field '" + field + "' was not serialized" + this.name);
}
for (field in serializedSubject) {
Assert.ok(subject.aiDataContract[field] !== undefined, "Field '" + field + "' was included but is not specified in the contract " + this.name);
}
}
private serialize(subject: Microsoft.ApplicationInsights.ISerializable, name: string) {
let serialized = "";
try {
serialized = Microsoft.ApplicationInsights.Serializer.serialize(subject);
} catch (e) {
Assert.ok(false, "Failed to serialize '" + name + "'\r\n" + e);
}
return JSON.parse(serialized);
}
private getSubject(construction: () => Microsoft.ApplicationInsights.ISerializable, name: string): any {
const subject = construction();
Assert.ok(!!subject, "can construct " + name);
return subject;
}
}

Просмотреть файл

@ -0,0 +1,227 @@
/// <reference path="../TestFramework/Common.ts" />
/// <reference path="../External/jquery.d.ts" />
/// <reference path="../../JavaScriptSDK/AppInsights.ts" />
/// <reference path="../../JavaScriptSDK/Telemetry/Common/Data.ts" />
interface IJSLitmus {
test: (name: string, f: Function) => void;
stop: () => void;
runAll: (e?: Event) => JQueryDeferred<void>;
_tests: any[];
}
interface IPerfResult {
operationCount: number;
timeInMs: number;
name: string;
opsPerSec: number;
period: number;
date: number;
platform: string;
os: string;
oneHrDate: number;
friendlyDate: string;
group: string;
millisecondsPerOp: number;
microsecondsPerOp: number;
secondsPerOp: number;
browser: string;
}
declare var JSLitmus: IJSLitmus;
class PerformanceTestHelper extends TestClass {
public testCount;
public appInsights;
public testProperties;
public testMeasurements;
public results: IPerfResult[];
private isDone;
constructor(timeout?: number) {
super();
this.testCount = 0;
this.synchronouslyLoadJquery();
this.results = [];
}
/** Method called before the start of each test method */
public testInitialize() {
this.useFakeServer = false;
sinon.fakeServer["restore"]();
this.useFakeTimers = false;
this.clock.restore();
this.appInsights = new Microsoft.ApplicationInsights.AppInsights({
instrumentationKey: "3e6a441c-b52b-4f39-8944-f81dd6c2dc46",
url: "file:///C:/src/sdk/src/JavaScript/JavaScriptSDK.Tests//E2ETests/ai.js",
endpointUrl: "https://dc.services.visualstudio.com/v2/track",
maxBatchInterval: 0
} as any);
this.appInsights.context._sender._sender = () => null;
this.testProperties = { p1: "val", p2: "val", p3: "val", p4: "val", p5: "val", p6: "val", p7: "val" };
this.testMeasurements = { m1: 1, m2: 1, m3: 1, m4: 1, m5: 1, m6: 1, m7: 1, m8: 1, m9: 1 };
}
/** Method called after each test method has completed */
public testCleanup() {
this.useFakeServer = true;
this.useFakeTimers = true;
const serializedPerfResults: string = window["perfResults"] || "[]";
let perfResults: IPerfResult[] = (JSON.parse(serializedPerfResults)) as any;
perfResults = perfResults.concat(this.results);
window["perfResults"] = JSON.stringify(perfResults);
window["perfResultsCsv"] = this.toCsv(perfResults).csv;
window["perfResultsCsvHeaders"] = this.toCsv(perfResults).headers;
}
private toCsv(array: any[]) {
let headers = "";
if (array.length > 0) {
const names = [];
for (const name in array[0]) {
names.push(name);
}
headers = names.join(",");
}
const csv = [];
for (let i = 0; i < array.length; i++) {
const datum = array[i];
const values = [];
for (let j = 0; j < names.length; j++) {
values.push(datum[names[j]]);
}
csv.push(values.join(","));
}
return { headers, csv: csv.join("\r\n") };
}
public enqueueTest(name: string, action: () => void) {
JSLitmus.test(name, (count) => {
while (count--) {
action();
}
});
}
public runTests() {
JSLitmus.runAll().done(() => this.onTestsComplete());
}
public onTestsComplete() {
const perfLogging = new Microsoft.ApplicationInsights.AppInsights({
instrumentationKey: "1a6933ad-f260-447f-a2b0-e2233f6658eb",
url: "file:///C:/src/sdk/src/JavaScript/JavaScriptSDK.Tests//E2ETests/ai.js",
endpointUrl: "http://prodintdataforker.azurewebsites.net/dcservices?intKey=4d93aad0-cf1d-45b7-afc9-14f55504f6d5",
sessionRenewalMs: 30 * 60 * 1000,
sessionExpirationMs: 24 * 60 * 60 * 1000,
maxBatchSizeInBytes: 1000000,
maxBatchInterval: 0
} as any);
perfLogging.context._sender._sender = (payload) => {
const xhr = new sinon["xhr"].workingXHR();
xhr.open("POST", perfLogging.config.endpointUrl, true);
xhr.setRequestHeader("Content-type", "application/json");
xhr.send(payload);
}
JSLitmus.stop();
for (let i = 0; i < JSLitmus._tests.length; i++) {
const test = JSLitmus._tests[i];
const opsPerSec = test.count / test.time;
Assert.ok(true, test.name + " operations per sec:" + opsPerSec);
const timeInMs = test.time as number;
const date = +new Date;
const oneHr = 60 * 60 * 1000;
const oneHrDate = Math.floor(date / oneHr) * oneHr;
const friendlyDate = new Date(oneHrDate).toISOString();
const platform = test.platform as string;
let browser = "internetExplorer";
const name = test.name as string;
const group = name.split(".")[0];
if (platform.toLowerCase().indexOf("chrome") >= 0) {
browser = "chrome";
} else if (platform.toLowerCase().indexOf("firefox") >= 0) {
browser = "firefox";
} else if (platform.toLowerCase().indexOf("safari") >= 0) {
browser = "safari";
}
const result: IPerfResult = {
name,
timeInMs,
operationCount: 1,
opsPerSec: 1 / (timeInMs / 1000),
period: 1,
date,
oneHrDate,
friendlyDate,
group,
platform,
browser,
os: test.os as string,
millisecondsPerOp: (timeInMs / 1),
microsecondsPerOp: (timeInMs / 1) * 1000,
secondsPerOp: (timeInMs / 1) / 1000
};
perfLogging.trackMetric(result.name, opsPerSec);
const event = new Microsoft.ApplicationInsights.Telemetry.Event(result.name, opsPerSec, result);
const data = new Microsoft.ApplicationInsights.Telemetry.Common.Data<Microsoft.ApplicationInsights.Telemetry.Event>(
Microsoft.ApplicationInsights.Telemetry.Event.dataType, event);
const envelope = new Microsoft.ApplicationInsights.Telemetry.Common.Envelope(data, Microsoft.ApplicationInsights.Telemetry.Event.envelopeType);
perfLogging.context.track(envelope);
this.results.push(result);
}
JSLitmus._tests.length = 0;
this.isDone = true;
this.testCleanup();
}
public onTimeout() {
if (!this.isDone) {
Assert.ok(false, "timeout reached");
this.onTestsComplete();
}
}
/**
* Synchronously loads jquery
* we could regress the test suite and develop sublte jquery dependencies in the product code
* if jquery were added to all tests as it hides a lot of cross browser weirdness. However,
* for these tests it is useful to manipulate the dom to display performance results.
*/
private synchronouslyLoadJquery() {
if (!window["$"]) {
// get some kind of XMLHttpRequest
let xhrObj = false as any;
if (window["ActiveXObject"]) {
xhrObj = (new ActiveXObject("Microsoft.XMLHTTP") as any);
} else if (window["XMLHttpRequest"]) {
xhrObj = (new XMLHttpRequest() as any);
} else {
alert("Please upgrade your browser! Your browser does not support AJAX!");
}
// open and send a synchronous request
xhrObj.open('GET', "http://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.js", false);
xhrObj.send('');
// add the returned content to a newly created script tag
const script = document.createElement('script');
script.type = "text/javascript";
script.text = xhrObj.responseText;
document.getElementsByTagName('head')[0].appendChild(script);
}
}
}

Просмотреть файл

@ -0,0 +1,34 @@
/// <reference path="../External/qunit.d.ts" />
/// <reference path="TestClass.ts" />
class PollingAssert {
/**
* Starts polling assertion function for a period of time after which it's considered failed.
* @param {() => boolean} assertionFunctionReturnsBoolean - funciton returning true if condition passes and false if condition fails. Assertion will be done on this function's result.
* @param {string} assertDescription - message shown with the assertion
* @param {number} timeoutSeconds - timeout in seconds after which assertion fails
* @param {number} pollIntervalMs - polling interval in milliseconds
* @returns {(nextTestStep) => void} callback which will be invoked by the TestClass
*/
public static createPollingAssert(assertionFunctionReturnsBoolean: () => boolean, assertDescription: string, timeoutSeconds: number = 30, pollIntervalMs: number = 500): (nextTestStep) => void {
const pollingAssert = (nextTestStep) => {
const timeout = new Date(new Date().getTime() + timeoutSeconds * 1000);
const polling = () => {
if (assertionFunctionReturnsBoolean.apply(this)) {
Assert.ok(true, assertDescription);
nextTestStep();
} else if (timeout < new Date()) {
Assert.ok(false, "assert didn't succeed for " + timeout + " seconds: " + assertDescription);
nextTestStep();
} else {
setTimeout(polling, pollIntervalMs);
}
}
setTimeout(polling, pollIntervalMs);
}
pollingAssert[TestClass.isPollingStepFlag] = true;
return pollingAssert;
}
}

Просмотреть файл

@ -0,0 +1,22 @@

/** Defines a test case */
class TestCase {
/** Name to use for the test case */
public name: string;
/** Test case method */
public test: () => void;
}
/** Defines a test case */
interface TestCaseAsync {
/** Name to use for the test case */
name: string;
/** time to wait after pre before invoking post and calling start() */
stepDelay: number;
/** async steps */
steps: Array<() => void>;
}

Просмотреть файл

@ -0,0 +1,250 @@
/// <reference path="../External/sinon.d.ts" />
/// <reference path="../External/qunit.d.ts" />
/// <reference path="Assert.ts" />
/// <reference path="./TestCase.ts"/>
class TestClass {
constructor(name?: string) {
QUnit.module(name);
}
public static isPollingStepFlag = "isPollingStep";
/** The instance of the currently running suite. */
public static currentTestClass: TestClass;
/** Turns on/off sinon's syncronous implementation of setTimeout. On by default. */
public useFakeTimers: boolean = true;
/** Turns on/off sinon's fake implementation of XMLHttpRequest. On by default. */
public useFakeServer: boolean = true;
/** Method called before the start of each test method */
public testInitialize() {
}
/** Method called after each test method has completed */
public testCleanup() {
}
/** Method in which test class intances should call this.testCase(...) to register each of this suite's tests. */
public registerTests() {
}
/** Register an async Javascript unit testcase. */
public testCaseAsync(testInfo: TestCaseAsync) {
if (!testInfo.name) {
throw new Error("Must specify name in testInfo context in registerTestcase call");
}
if (isNaN(testInfo.stepDelay)) {
throw new Error("Must specify 'stepDelay' period between pre and post");
}
if (!testInfo.steps) {
throw new Error("Must specify 'steps' to take asynchronously");
}
// Create a wrapper around the test method so we can do test initilization and cleanup.
const testMethod = (assert) => {
const done = assert.async();
// Save off the instance of the currently running suite.
TestClass.currentTestClass = this;
// Run the test.
try {
this._testStarting();
const steps = testInfo.steps;
const trigger = () => {
if (steps.length) {
const step = steps.shift();
// The callback which activates the next test step.
const nextTestStepTrigger = () => {
setTimeout(() => {
trigger();
}, testInfo.stepDelay);
};
// There 2 types of test steps - simple and polling.
// Upon completion of the simple test step the next test step will be called.
// In case of polling test step the next test step is passed to the polling test step, and
// it is responsibility of the polling test step to call the next test step.
try {
if (step[TestClass.isPollingStepFlag]) {
step.call(this, nextTestStepTrigger);
} else {
step.call(this);
nextTestStepTrigger.call(this);
}
} catch (e) {
this._testCompleted();
Assert.ok(false, e.toString());
// done is QUnit callback indicating the end of the test
done();
return;
}
} else {
this._testCompleted();
// done is QUnit callback indicating the end of the test
done();
}
};
trigger();
} catch (ex) {
Assert.ok(false, "Unexpected Exception: " + ex);
this._testCompleted(true);
// done is QUnit callback indicating the end of the test
done();
}
};
// Register the test with QUnit
QUnit.test(testInfo.name, testMethod);
}
/** Register a Javascript unit testcase. */
public testCase(testInfo: TestCase) {
if (!testInfo.name) {
throw new Error("Must specify name in testInfo context in registerTestcase call");
}
if (!testInfo.test) {
throw new Error("Must specify 'test' method in testInfo context in registerTestcase call");
}
// Create a wrapper around the test method so we can do test initilization and cleanup.
const testMethod = () => {
// Save off the instance of the currently running suite.
TestClass.currentTestClass = this;
// Run the test.
try {
this._testStarting();
testInfo.test.call(this);
this._testCompleted();
}
catch (ex) {
Assert.ok(false, "Unexpected Exception: " + ex);
this._testCompleted(true);
}
};
// Register the test with QUnit
test(testInfo.name, testMethod);
}
/** Called when the test is starting. */
private _testStarting() {
// Initialize the sandbox similar to what is done in sinon.js "test()" override. See note on class.
const config = (sinon as any).getConfig(sinon.config);
config.useFakeTimers = this.useFakeTimers;
config.useFakeServer = this.useFakeServer;
config.injectInto = config.injectIntoThis && this || config.injectInto;
this.sandbox = sinon.sandbox.create(config);
this.server = this.sandbox.server;
// Allow the derived class to perform test initialization.
this.testInitialize();
}
/** Called when the test is completed. */
private _testCompleted(failed?: boolean) {
if (failed) {
// Just cleanup the sandbox since the test has already failed.
this.sandbox.restore();
}
else {
// Verify the sandbox and restore.
(this.sandbox as any).verifyAndRestore();
}
this.testCleanup();
// Clear the instance of the currently running suite.
TestClass.currentTestClass = null;
}
/**** Sinon methods and properties ***/
// These methods and properties are injected by Sinon and will override the implementation here.
// These are here purely to make typescript happy.
public clock: SinonFakeTimers;
public server: SinonFakeServer;
public sandbox: SinonSandbox;
/** Creates an anonymous function that records arguments, this value, exceptions and return values for all calls. */
public spy(): SinonSpy;
/** Spies on the provided function */
public spy(funcToWrap: Function): SinonSpy;
/** Creates a spy for object.methodName and replaces the original method with the spy. The spy acts exactly like the original method in all cases. The original method can be restored by calling object.methodName.restore(). The returned spy is the function object which replaced the original method. spy === object.method. */
public spy(object: any, methodName: string, func?: Function): SinonSpy;
public spy(...args: any[]): SinonSpy { return null; }
/** Creates an anonymous stub function. */
public stub(): SinonStub;
/** Stubs all the object's methods. */
public stub(object: any): SinonStub;
/** Replaces object.methodName with a func, wrapped in a spy. As usual, object.methodName.restore(); can be used to restore the original method. */
public stub(object: any, methodName: string, func?: Function): SinonStub;
public stub(...args: any[]): SinonStub { return null; }
/** Creates a mock for the provided object.Does not change the object, but returns a mock object to set expectations on the object's methods. */
public mock(object: any): SinonMock { return null; }
/**** end: Sinon methods and properties ***/
/** Sends a JSON response to the provided request.
* @param request The request to respond to.
* @param data Data to respond with.
* @param errorCode Optional error code to send with the request, default is 200
*/
public sendJsonResponse(request: SinonFakeXMLHttpRequest, data: any, errorCode?: number) {
if (errorCode === undefined) {
errorCode = 200;
}
request.respond(
errorCode,
{ "Content-Type": "application/json" },
JSON.stringify(data));
}
protected setUserAgent(userAgent: string) {
Object.defineProperty(window.navigator, 'userAgent',
{
configurable: true,
get () {
return userAgent;
}
});
}
}
// Configure Sinon
sinon.assert.fail = function (msg?) {
Assert.ok(false, msg);
};
sinon.assert.pass = function (assertion) {
Assert.ok(assertion, "sinon assert");
};
sinon.config = {
injectIntoThis: true,
injectInto: null,
properties: ["spy", "stub", "mock", "clock", "sandbox"],
useFakeTimers: true,
useFakeServer: true
};

14
lib/test/tsconfig.json Normal file
Просмотреть файл

@ -0,0 +1,14 @@
{
"compilerOptions": {
"sourceMap": true,
"inlineSources": true,
"noImplicitAny": false,
"module": "amd",
"moduleResolution": "Node",
"target": "es5",
"alwaysStrict": true,
"declaration": true,
"lib": [ "dom", "es2015" ]
},
"files": []
}

25
lib/tsconfig.json Normal file
Просмотреть файл

@ -0,0 +1,25 @@
{
"compilerOptions": {
"sourceMap": true,
"inlineSources": true,
"noImplicitAny": false,
"module": "amd",
"moduleResolution": "node",
"target": "es3",
"forceConsistentCasingInFileNames": true,
"importHelpers": true,
"noEmitHelpers": true,
"alwaysStrict": true,
"declaration": true,
//"declarationDir": "lib/types",
"outFile": "./dist-esm/dynamicproto-js.js",
"rootDir": "lib/src",
"suppressImplicitAnyIndexErrors": true,
"allowSyntheticDefaultImports": true,
"lib": [ "dom", "es2015" ]
},
"include": [
"src/DynamicProto.ts"
],
"exclude": [ "node_modules/" ]
}

5
lib/tslint.json Normal file
Просмотреть файл

@ -0,0 +1,5 @@
{
"extends": [
"../tslint-base.json"
]
}

54
package.json Normal file
Просмотреть файл

@ -0,0 +1,54 @@
{
"name": "@microsoft/dynamicproto-js",
"description": "Microsoft Dynamic Proto Utility",
"version": "0.0.1",
"keywords": [
"javascript",
"dynamic prototype",
"microsoft",
"typescript",
"inheritence",
"minification",
"application insights"
],
"main": "./lib/dist/dynamicproto-js.js",
"module": "./lib/dist/dynamicproto-js.js",
"types": "./lib/dist/dynamicproto-js.d.ts",
"directories": {
"doc": "lib/docs"
},
"scripts": {
"postinstall": "node common/scripts/install-run-rush.js update --recheck --full",
"build": "node common/scripts/install-run-rush.js rebuild",
"test": "grunt dynamicprototest",
"docs": "typedoc --out docs lib/src --excludePrivate --excludeProtected --tsconfig lib/tsconfig.json --theme minimal"
},
"repository": {
"type": "git",
"url": "git+https://github.com/microsoft/DynamicProto-JS.git"
},
"author": "Microsoft Application Insights Team",
"license": "MIT",
"bugs": {
"url": "https://github.com/microsoft/DynamicProto-JS/issues"
},
"homepage": "https://github.com/microsoft/DynamicProto-JS#readme",
"devDependencies": {
"grunt": "^1.0.1",
"grunt-cli": "^1.3.2",
"grunt-contrib-qunit": "^2.0.0",
"grunt-contrib-uglify": "^3.1.0",
"grunt-run": "^0.8.1",
"grunt-ts": "^6.0.0-beta.22",
"grunt-tslint": "^5.0.2",
"tslint": "^5.19.0",
"tslint-microsoft-contrib": "^5.2.1",
"tslint-config-prettier": "^1.18.0",
"typescript": "2.5.3",
"rollup": "^0.66.0",
"typedoc": "^0.15.0"
},
"dependencies": {
"tslib": "^1.9.3"
}
}

15
rush.json Normal file
Просмотреть файл

@ -0,0 +1,15 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/rush/v5/rush.schema.json",
"npmVersion": "4.5.0",
"rushVersion": "5.4.0",
"projectFolderMaxDepth": 4,
"projects": [
{
"packageName": "@microsoft/dynamicproto-js",
"projectFolder": "lib",
"shouldPublish": true
}
]
}

12
tsconfig.json Normal file
Просмотреть файл

@ -0,0 +1,12 @@
{
"compilerOptions": {
"sourceMap": true,
"inlineSources": true,
"noImplicitAny": false,
"module": "amd",
"target": "es3",
"alwaysStrict": true,
"declaration": true
},
"files": []
}

11
tsconfigmodule.json Normal file
Просмотреть файл

@ -0,0 +1,11 @@
{
"compilerOptions": {
"sourceMap": true,
"inlineSources": true,
"noImplicitAny": false,
"module": "amd",
"target": "es3",
"alwaysStrict": true
},
"files": []
}

37
tslint-base.json Normal file
Просмотреть файл

@ -0,0 +1,37 @@
{
"defaultSeverity": "error",
"extends": [
"tslint:latest",
"tslint-config-prettier"
],
"rules": {
"no-eval": true,
"ordered-imports": false,
"variable-name": false,
"member-access": false,
"object-literal-sort-keys": false,
"no-bitwise": false,
"one-variable-per-declaration": false,
"max-classes-per-file": false,
"no-console": false,
"prefer-for-of": false,
"class-name": false,
"interface-name": false,
"no-empty-interface": false,
"no-string-literal": false,
"no-reference": false,
"no-empty": false,
"forin": false,
"ban-types": false,
"no-shadowed-variable": false,
"no-implicit-dependencies": false,
"no-object-literal-type-assertion": false,
"no-this-assignment":false,
"unified-signatures": false,
"no-unused-expression": false,
"no-var-keyword": false,
"no-duplicate-variable": false,
"prefer-const": false,
"only-arrow-functions": [false]
}
}