Skip to content

Commit

Permalink
test: add jest for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
pengx17 committed Dec 14, 2018
1 parent a478bef commit 0229f02
Show file tree
Hide file tree
Showing 97 changed files with 15,181 additions and 4,061 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Load with ESM is added, but not yet tested.
* `git clone https://github.com/pengx17/monaco-yaml`
* `cd monaco-yaml`
* `yarn`
* open `$/monaco-yaml/test/index.html` in your favorite browser.
* open `$/monaco-yaml/demo/index.html` in your favorite browser.

A running example:
![demo-image](test-demo.png)
Expand Down
156 changes: 156 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link
rel="stylesheet"
data-name="vs/editor/editor.main"
href="../node_modules/monaco-editor-core/dev/vs/editor/editor.main.css"
/>
</head>

<body>
<h2>Monaco Editor YAML test page</h2>
<code id="path"></code>
<div
id="container"
style="width:800px;height:600px;border:1px solid grey"
></div>

<style>
.x-highlight-range {
background-color: lightblue;
}
</style>

<script>
// Loading basic-languages to get the YAML language definition
var paths = {
'vs/basic-languages': '../node_modules/monaco-languages/release/dev',
'vs/language/yaml': '../release/dev',
vs: '../node_modules/monaco-editor-core/dev/vs',
};
if (document.location.protocol === 'http:') {
// Add support for running local http server
let testIndex = document.location.pathname.indexOf('/test/');
if (testIndex !== -1) {
let prefix = document.location.pathname.substr(0, testIndex);
paths['vs/language/yaml'] = prefix + '/release/dev';
}
}
var require = {
paths: paths,
};
</script>
<script src="../node_modules/monaco-editor-core/dev/vs/loader.js"></script>
<script src="../node_modules/monaco-editor-core/dev/vs/editor/editor.main.nls.js"></script>
<script src="../node_modules/monaco-editor-core/dev/vs/editor/editor.main.js"></script>

<script>
require([
'vs/basic-languages/monaco.contribution',
'vs/language/yaml/monaco.contribution',
], function() {
const yaml = `p1: `;
const modelUri = monaco.Uri.parse('a://b/foo.json');
const editor = monaco.editor.create(
document.getElementById('container'),
{
language: 'yaml',
showFoldingControls: 'always',
model: monaco.editor.createModel(yaml, 'yaml', modelUri),
}
);

monaco.languages.yaml.yamlDefaults.setDiagnosticsOptions({
enableSchemaRequest: true,
validate: true,
schemas: [
{
uri: 'http://myserver/foo-schema.json', // id of the first schema
fileMatch: [modelUri.toString()], // associate with our model
schema: {
type: 'object',
properties: {
p1: {
enum: ['v1', 'v2'],
},
p2: {
$ref: 'http://myserver/bar-schema.json', // reference the second schema
},
},
},
},
{
uri: 'http://myserver/bar-schema.json', // id of the first schema
schema: {
type: 'object',
properties: {
q1: {
enum: ['x1', 'x2'],
},
},
},
},
],
});

require(['vs/editor/contrib/quickOpen/quickOpen'], async quickOpen => {
const NEVER_CANCEL_TOKEN = {
isCancellationRequested: false,
onCancellationRequested: () => Event.NONE,
};

let oldDecorations = [];

async function _getSymbolForPosition(model, position) {
const symbols = await quickOpen.getDocumentSymbols(
model,
false,
NEVER_CANCEL_TOKEN
);

function _recur(symbol) {
let target = symbol;
if (symbol && symbol.children && symbol.children.length) {
target =
_recur(
symbol.children.find(child =>
child.range.containsPosition(position)
)
) || symbol;
}

return target;
}

return _recur({ children: symbols });
}

editor.onDidChangeCursorSelection(async ({ selection }) => {
const model = editor.getModel();
const position = selection.getPosition();
const symbol = await _getSymbolForPosition(model, position);

console.log(`${symbol.name}: ${symbol.range}`);
if (symbol && symbol.range) {
const decoration = {
range: symbol.range,
options: {
isWholeLine: false,
className: 'x-highlight-range',
},
};

oldDecorations = editor.deltaDecorations(
oldDecorations,
decoration ? [decoration] : []
);
}
});
});
});
</script>
</body>
</html>
52 changes: 48 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
{
"name": "monaco-yaml",
"version": "2.0.0",
"version": "2.1.0",
"description": "YAML plugin for the Monaco Editor",
"scripts": {
"compile": "rimraf ./out && yarn compile:umd && yarn compile:esm",
"compile:umd": "tsc -p ./src/tsconfig.json",
"compile:esm": "tsc -p ./src/tsconfig.esm.json",
"watch": "tsc -p ./src --watch",
"prepublish": "rimraf ./out && rimraf ./release && yarn compile:umd && node ./scripts/bundle && mcopy ./src/monaco.d.ts ./release/monaco.d.ts"
"prepublish": "rimraf ./out && rimraf ./release && yarn compile:umd && node ./scripts/bundle && mcopy ./src/monaco.d.ts ./release/monaco.d.ts",
"lint": "prettier \"{src,test}/**/*.{json,scss,html,ts}\" --write",
"test": "jest"
},
"author": "Kevin Decker <[email protected]> (http://incaseofstairs.com)",
"maintainers": [
Expand All @@ -23,19 +25,61 @@
"url": "https://github.com/pengx17/monaco-yaml/issues"
},
"devDependencies": {
"@types/chai": "^4.1.4",
"@types/mocha": "^5.2.5",
"@types/jest": "^23.3.10",
"@types/node": "^10.9.3",
"husky": "^1.2.1",
"jest": "^23.6.0",
"js-yaml": "^3.12.0",
"jsonc-parser": "^2.0.2",
"lint-staged": "^8.1.0",
"monaco-editor-core": "0.15.5",
"monaco-languages": "1.6.0",
"monaco-plugin-helpers": "^1.0.2",
"prettier": "^1.15.3",
"requirejs": "^2.3.5",
"rimraf": "^2.6.2",
"ts-jest": "^23.10.5",
"typescript": "^3.1.6",
"uglify-es": "^3.3.9",
"vscode-languageserver-types": "3.12.0",
"yaml-language-server": "^0.1.0"
},
"prettier": {
"singleQuote": true,
"trailingComma": "es5",
"semi": true
},
"lint-staged": {
"linters": {
"*.{json,scss,html}": [
"prettier --write",
"git add"
]
},
"ignore": [
"src/backend/vendor/**/*"
]
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"jest": {
"globals": {
"ts-jest": {
"tsConfig": "./test/tsconfig.json"
}
},
"moduleFileExtensions": [
"js",
"ts"
],
"transform": {
"^.+\\.(ts|tsx)$": "ts-jest"
},
"testMatch": [
"**/test/*.test.+(ts|js)"
]
}
}
2 changes: 1 addition & 1 deletion src/fillers/os.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const EOL = '\n';
export const EOL = '\n';
52 changes: 27 additions & 25 deletions src/fillers/vscode-nls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,48 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import {LocalizeInfo, LocalizeFunc, Options, LoadFunc} from 'vscode-nls';
import { LoadFunc, LocalizeFunc, LocalizeInfo, Options } from 'vscode-nls';

export interface Options {
locale?: string;
cacheLanguageResolution?: boolean;
locale?: string;
cacheLanguageResolution?: boolean;
}
export interface LocalizeInfo {
key: string;
comment: string[];
key: string;
comment: string[];
}
export interface LocalizeFunc {
(info: LocalizeInfo, message: string, ...args: any[]): string;
(key: string, message: string, ...args: any[]): string;
}
export interface LoadFunc {
(file?: string): LocalizeFunc;
(info: LocalizeInfo, message: string, ...args: any[]): string;
(key: string, message: string, ...args: any[]): string;
}
export type LoadFunc = (file?: string) => LocalizeFunc;

function format(message: string, args: any[]): string {
let result:string;
let result: string;

if (args.length === 0) {
result = message;
} else {
result = message.replace(/\{(\d+)\}/g, (match, rest) => {
let index = rest[0];
return typeof args[index] !== 'undefined' ? args[index] : match;
});
}
return result;
if (args.length === 0) {
result = message;
} else {
result = message.replace(/\{(\d+)\}/g, (match, rest) => {
const index = rest[0];
return typeof args[index] !== 'undefined' ? args[index] : match;
});
}
return result;
}

function localize(key: string | LocalizeInfo, message: string, ...args: any[]): string {
return format(message, args);
function localize(
key: string | LocalizeInfo,
message: string,
...args: any[]
): string {
return format(message, args);
}

export function loadMessageBundle(file?: string): LocalizeFunc {
return localize;
return localize;
}

export function config(opt?: Options | string): LoadFunc {
return loadMessageBundle;
}
return loadMessageBundle;
}
Loading

0 comments on commit 0229f02

Please sign in to comment.