Commit 6f3f12eb authored by Omkara's avatar Omkara

Add typescript tests

parent 42f7c500
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
"scripts": { "scripts": {
"build": "tsc", "build": "tsc",
"lint": "standard", "lint": "standard",
"test": "standard && mocha tests/ -t 300000" "test": "standard && mocha --require ts-node/register tests/*.ts -t 300000"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
...@@ -35,8 +35,14 @@ ...@@ -35,8 +35,14 @@
"valid-url": "^1.0.9" "valid-url": "^1.0.9"
}, },
"devDependencies": { "devDependencies": {
"@types/chai": "^4.1.7",
"@types/mocha": "^5.2.5",
"@types/node": "^10.12.18",
"chai": "^4.2.0",
"mocha": "^5.1.0", "mocha": "^5.1.0",
"remix-plugin": "0.0.1-alpha.2",
"standard": "^12.0.1", "standard": "^12.0.1",
"ts-node": "^7.0.1",
"tslint": "^5.11.0", "tslint": "^5.11.0",
"typescript": "^3.1.6" "typescript": "^3.1.6"
} }
......
import axios from 'axios' import axios from 'axios'
interface Imported { export interface Imported {
content: string; content: string;
cleanURL: string; cleanURL: string;
type: string; type: string;
...@@ -96,7 +96,7 @@ export class ImportResolver { ...@@ -96,7 +96,7 @@ export class ImportResolver {
} }
] ]
} }
async resolve(filePath: string, customHandlers?: Handler[]) { async resolve(filePath: string, customHandlers?: Handler[]): Promise<Imported> {
var imported: Imported = this.previouslyHandled[filePath] var imported: Imported = this.previouslyHandled[filePath]
if(imported) { if(imported) {
return imported return imported
......
const rr = require('../dist/index.js') import { expect } from 'chai'
const assert = require('assert') import { ImportResolver, Imported } from '../src'
const fs = require('fs') import * as fs from 'fs'
const path = require('path') import * as path from 'path'
import * as assert from 'assert'
import { AppManager } from 'remix-plugin'
describe('testRunner', function () { describe('testRunner', () => {
describe('#resolve', function() { describe('#resolve', () => {
describe('test example_1 [local imports]]', function () { describe('test example_1 [local imports]]', () => {
const fileName = '../remix-resolve/tests/example_1/greeter.sol' const fileName = '../remix-resolve/tests/example_1/greeter.sol'
let results = {} let results = {}
before(function (done) { before(done => {
const resolver = new rr.ImportResolver() let resolver: ImportResolver = new ImportResolver()
var sources = []
function handleLocal(pathString, filePath) { function handleLocal(pathString: string, filePath: string) {
// if no relative/absolute path given then search in node_modules folder // if no relative/absolute path given then search in node_modules folder
if (pathString && pathString.indexOf('.') !== 0 && pathString.indexOf('/') !== 0) { if (pathString && pathString.indexOf('.') !== 0 && pathString.indexOf('/') !== 0) {
return handleNodeModulesImport(pathString, filePath, pathString) // return handleNodeModulesImport(pathString, filePath, pathString)
return
} else { } else {
const o = { encoding: 'UTF-8' } const o = { encoding: 'UTF-8' }
const p = pathString ? path.resolve(pathString, filePath) : path.resolve(pathString, filePath) const p = pathString ? path.resolve(pathString, filePath) : path.resolve(pathString, filePath)
...@@ -26,8 +29,8 @@ describe('testRunner', function () { ...@@ -26,8 +29,8 @@ describe('testRunner', function () {
const localFSHandler = [ const localFSHandler = [
{ {
type: 'local', type: 'local',
match: (url) => { return /(^(?!(?:http:\/\/)|(?:https:\/\/)?(?:www.)?(?:github.com)))(^\/*[\w+-_/]*\/)*?(\w+\.sol)/g.exec(url) }, match: (url: string) => { return /(^(?!(?:http:\/\/)|(?:https:\/\/)?(?:www.)?(?:github.com)))(^\/*[\w+-_/]*\/)*?(\w+\.sol)/g.exec(url) },
handle: (match) => { return handleLocal(match[2], match[3]) } handle: (match: Array<string>) => { return handleLocal(match[2], match[3]) }
} }
] ]
resolver.resolve(fileName, localFSHandler) resolver.resolve(fileName, localFSHandler)
...@@ -40,11 +43,11 @@ describe('testRunner', function () { ...@@ -40,11 +43,11 @@ describe('testRunner', function () {
}) })
}) })
it('should have 3 items', function () { it('should have 3 items', () => {
assert.equal(Object.keys(results).length, 3) assert.equal(Object.keys(results).length, 3)
}) })
it('should returns contract content of given local path', function () { it('should returns contract content of given local path', () => {
const expt = { const expt = {
content: 'pragma solidity ^0.5.0;\nimport "./mortal.sol";\n\ncontract Greeter is Mortal {\n /* Define variable greeting of the type string */\n string greeting;\n\n /* This runs when the contract is executed */\n constructor(string memory _greeting) public {\n greeting = _greeting;\n }\n\n /* Main function */\n function greet() public view returns (string memory) {\n return greeting;\n }\n}\n', content: 'pragma solidity ^0.5.0;\nimport "./mortal.sol";\n\ncontract Greeter is Mortal {\n /* Define variable greeting of the type string */\n string greeting;\n\n /* This runs when the contract is executed */\n constructor(string memory _greeting) public {\n greeting = _greeting;\n }\n\n /* Main function */\n function greet() public view returns (string memory) {\n return greeting;\n }\n}\n',
cleanURL: '../remix-resolve/tests/example_1/greeter.sol', cleanURL: '../remix-resolve/tests/example_1/greeter.sol',
...@@ -52,33 +55,34 @@ describe('testRunner', function () { ...@@ -52,33 +55,34 @@ describe('testRunner', function () {
} }
assert.deepEqual(results, expt) assert.deepEqual(results, expt)
}) })
})
// test IPFShandle
describe('test getting IPFS files', function() {
const fileName = 'ipfs://QmeKtwMBqz5Ac7oL8SyTD96mccEzw9X9d39jLb2kgnBYbn'
let results = []
before(async function() { // test IPFShandle
try { describe('test getting IPFS files', () => {
const resolver = new rr.ImportResolver() const fileName = 'ipfs://QmeKtwMBqz5Ac7oL8SyTD96mccEzw9X9d39jLb2kgnBYbn'
var sources = await resolver.resolve(fileName) let results:Imported
results = sources
return
} catch (e) {
throw e
}
})
it('should have 3 items', function () { before(async function() {
assert.equal(Object.keys(results).length, 3) try {
}) const resolver: ImportResolver = new ImportResolver()
it('should returns contract content of given local path', function () { var sources: Imported = await resolver.resolve(fileName)
const expt = { results = sources
content: 'pragma solidity ^0.5.0;\nimport "./mortal.sol";\n\ncontract Greeter is Mortal {\n /* Define variable greeting of the type string */\n string greeting;\n\n /* This runs when the contract is executed */\n constructor(string memory _greeting) public {\n greeting = _greeting;\n }\n\n /* Main function */\n function greet() public view returns (string memory) {\n return greeting;\n }\n}\n', return
cleanURL: 'ipfs://QmeKtwMBqz5Ac7oL8SyTD96mccEzw9X9d39jLb2kgnBYbn', } catch (e) {
type: 'ipfs' throw e
} }
assert.deepEqual(results, expt) })
it('should have 3 items', () => {
assert.equal(Object.keys(results).length, 3)
})
it('should returns contract content of given local path', () => {
const expt = {
content: 'pragma solidity ^0.5.0;\nimport "./mortal.sol";\n\ncontract Greeter is Mortal {\n /* Define variable greeting of the type string */\n string greeting;\n\n /* This runs when the contract is executed */\n constructor(string memory _greeting) public {\n greeting = _greeting;\n }\n\n /* Main function */\n function greet() public view returns (string memory) {\n return greeting;\n }\n}\n',
cleanURL: 'ipfs://QmeKtwMBqz5Ac7oL8SyTD96mccEzw9X9d39jLb2kgnBYbn',
type: 'ipfs'
}
assert.deepEqual(results, expt)
})
}) })
}) })
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment