Skip to content
Permalink
Browse files

Close #42

  • Loading branch information...
thiagodp committed May 28, 2019
1 parent ab05a04 commit 16a541f16c0b62e4bce04e575264ee5253374a37
@@ -5,31 +5,78 @@ import { ImportLexer } from '../../modules/lexer/ImportLexer';
*/
describe( 'ImportLexerTest', () => {

// IMPORTANT: since ImportLexer inherits from QuotedNodeLexer and does not add any
// behavior, the QuotedNodeLexerTest already covers most test cases.
// IMPORTANT: ImportLexer inherits from QuotedNodeLexer and the latter has tests.

let keyword = 'import';
let word = 'import';
let words = [ word ];
const keyword = 'import';
const word = 'import';

let lexer = new ImportLexer( words ); // under test
let lexer = new ImportLexer( [ word ] ); // under test

it( 'ignores a comment after the value', () => {
let line = " \t \t" + word + " \t " + '"Hello world"#comment';

let r = lexer.analyze( line, 1 );
function expectNodeWithValue( line: string, value: string ): void {
const r = lexer.analyze( line, 1 );
expect( r ).toBeDefined();
expect( r.errors ).toHaveLength( 0 );
expect( r.nodes ).toHaveLength( 1 );

let node = r.nodes[ 0 ];
const node = r.nodes[ 0 ];
expect( node ).toEqual(
{
nodeType: keyword,
location: { line: 1, column: 6 },
value: "Hello world"
location: { line: 1, column: 1 + line.length - line.trim().length },
value: value
}
);
}

it( 'ignores a comment after the value', () => {
const value = 'Hello world';
const line = ` \t \t${word} \t "${value}"#comment`;
expectNodeWithValue( line, value );
} );

describe( 'consider a unix file path as a valid name', () => {

it( 'full path', () => {
const value = "/foo/bar/foo.bar";
const line = `${word} "${value}"`;
expectNodeWithValue( line, value );
} );

it( 'current path', () => {
const value = "./foo.bar";
const line = `${word} "${value}"`;
expectNodeWithValue( line, value );
} );

it( 'parent path', () => {
const value = "../../bar/foo.bar";
const line = `${word} "${value}"`;
expectNodeWithValue( line, value );
} );

} );


describe( 'consider a windows file path as a valid name', () => {

it( 'full path', () => {
const value = "C:\\foo\\bar\\foo.bar";
const line = `${word} "${value}"`;
expectNodeWithValue( line, value );
} );

it( 'current path', () => {
const value = ".\\foo.bar";
const line = `${word} "${value}"`;
expectNodeWithValue( line, value );
} );

it( 'parent path', () => {
const value = "..\\..\\bar\\foo.bar";
const line = `${word} "${value}"`;
expectNodeWithValue( line, value );
} );

} );

} );
@@ -5,23 +5,59 @@ const ImportLexer_1 = require("../../modules/lexer/ImportLexer");
* @author Thiago Delgado Pinto
*/
describe('ImportLexerTest', () => {
// IMPORTANT: since ImportLexer inherits from QuotedNodeLexer and does not add any
// behavior, the QuotedNodeLexerTest already covers most test cases.
let keyword = 'import';
let word = 'import';
let words = [word];
let lexer = new ImportLexer_1.ImportLexer(words); // under test
it('ignores a comment after the value', () => {
let line = " \t \t" + word + " \t " + '"Hello world"#comment';
let r = lexer.analyze(line, 1);
// IMPORTANT: ImportLexer inherits from QuotedNodeLexer and the latter has tests.
const keyword = 'import';
const word = 'import';
let lexer = new ImportLexer_1.ImportLexer([word]); // under test
function expectNodeWithValue(line, value) {
const r = lexer.analyze(line, 1);
expect(r).toBeDefined();
expect(r.errors).toHaveLength(0);
expect(r.nodes).toHaveLength(1);
let node = r.nodes[0];
const node = r.nodes[0];
expect(node).toEqual({
nodeType: keyword,
location: { line: 1, column: 6 },
value: "Hello world"
location: { line: 1, column: 1 + line.length - line.trim().length },
value: value
});
}
it('ignores a comment after the value', () => {
const value = 'Hello world';
const line = ` \t \t${word} \t "${value}"#comment`;
expectNodeWithValue(line, value);
});
describe('consider a unix file path as a valid name', () => {
it('full path', () => {
const value = "/foo/bar/foo.bar";
const line = `${word} "${value}"`;
expectNodeWithValue(line, value);
});
it('current path', () => {
const value = "./foo.bar";
const line = `${word} "${value}"`;
expectNodeWithValue(line, value);
});
it('parent path', () => {
const value = "../../bar/foo.bar";
const line = `${word} "${value}"`;
expectNodeWithValue(line, value);
});
});
describe('consider a windows file path as a valid name', () => {
it('full path', () => {
const value = "C:\\foo\\bar\\foo.bar";
const line = `${word} "${value}"`;
expectNodeWithValue(line, value);
});
it('current path', () => {
const value = ".\\foo.bar";
const line = `${word} "${value}"`;
expectNodeWithValue(line, value);
});
it('parent path', () => {
const value = "..\\..\\bar\\foo.bar";
const line = `${word} "${value}"`;
expectNodeWithValue(line, value);
});
});
});
@@ -1,7 +1,8 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const QuotedNodeLexer_1 = require("./QuotedNodeLexer");
const isValidPath = require("is-valid-path");
const NodeTypes_1 = require("../req/NodeTypes");
const QuotedNodeLexer_1 = require("./QuotedNodeLexer");
/**
* Detects an Import.
*
@@ -15,5 +16,11 @@ class ImportLexer extends QuotedNodeLexer_1.QuotedNodeLexer {
suggestedNextNodeTypes() {
return [NodeTypes_1.NodeTypes.FEATURE, NodeTypes_1.NodeTypes.VARIANT];
}
/**
* @inheritdoc
*/
isValidName(name) {
return isValidPath(name);
}
}
exports.ImportLexer = ImportLexer;
@@ -1,10 +1,12 @@
import * as isValidPath from 'is-valid-path';

import { Import } from '../ast/Import';
import { QuotedNodeLexer } from './QuotedNodeLexer';
import { NodeTypes } from "../req/NodeTypes";
import { QuotedNodeLexer } from './QuotedNodeLexer';

/**
* Detects an Import.
*
*
* @author Thiago Delgado Pinto
*/
export class ImportLexer extends QuotedNodeLexer< Import > {
@@ -18,4 +20,11 @@ export class ImportLexer extends QuotedNodeLexer< Import > {
return [ NodeTypes.FEATURE, NodeTypes.VARIANT ];
}

/**
* @inheritdoc
*/
public isValidName( name: string ): boolean {
return isValidPath( name );
}

}

Some generated files are not rendered by default. Learn more.

@@ -102,6 +102,7 @@
"he": "^1.1.1",
"inquirer": "^6.2.2",
"is-my-json-valid": "^2.17.2",
"is-valid-path": "^0.1.1",
"jl-sql-api": "^2.8.1",
"js-joda": "^1.8.2",
"json-schema-generator": "^2.0.6",

0 comments on commit 16a541f

Please sign in to comment.
You can’t perform that action at this time.