From 3a5d4dff959d14d862b8d41ac5ee1bba8221de2b Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Thu, 3 Jan 2019 00:29:25 +0200 Subject: [PATCH] Use 'TokenKind' enum inside lexer/parser tests --- src/language/__tests__/lexer-test.js | 33 ++++++++++++++------------- src/language/__tests__/parser-test.js | 8 +++++-- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/language/__tests__/lexer-test.js b/src/language/__tests__/lexer-test.js index f18695391c..b92483a88c 100644 --- a/src/language/__tests__/lexer-test.js +++ b/src/language/__tests__/lexer-test.js @@ -704,8 +704,8 @@ describe('Lexer', () => { }); it('lex reports useful information for dashes in names', () => { - const q = 'a-b'; - const lexer = createLexer(new Source(q)); + const source = new Source('a-b'); + const lexer = createLexer(source); const firstToken = lexer.advance(); expect(firstToken).to.contain({ kind: TokenKind.NAME, @@ -723,21 +723,22 @@ describe('Lexer', () => { }); it('produces double linked list of tokens, including comments', () => { - const lexer = createLexer( - new Source(`{ - #comment - field - }`), - ); + const source = new Source(` + { + #comment + field + } + `); + const lexer = createLexer(source); const startToken = lexer.token; let endToken; do { endToken = lexer.advance(); // Lexer advances over ignored comment tokens to make writing parsers // easier, but will include them in the linked list result. - expect(endToken.kind).not.to.equal('Comment'); - } while (endToken.kind !== ''); + expect(endToken.kind).not.to.equal(TokenKind.COMMENT); + } while (endToken.kind !== TokenKind.EOF); expect(startToken.prev).to.equal(null); expect(endToken.next).to.equal(null); @@ -752,12 +753,12 @@ describe('Lexer', () => { } expect(tokens.map(tok => tok.kind)).to.deep.equal([ - '', - '{', - 'Comment', - 'Name', - '}', - '', + TokenKind.SOF, + TokenKind.BRACE_L, + TokenKind.COMMENT, + TokenKind.NAME, + TokenKind.BRACE_R, + TokenKind.EOF, ]); }); }); diff --git a/src/language/__tests__/parser-test.js b/src/language/__tests__/parser-test.js index 48512ce73d..43f802196f 100644 --- a/src/language/__tests__/parser-test.js +++ b/src/language/__tests__/parser-test.js @@ -14,6 +14,7 @@ import { join } from 'path'; import { Kind } from '../kinds'; import { expect } from 'chai'; import { describe, it } from 'mocha'; +import { TokenKind } from '../lexer'; import { parse, parseValue, parseType } from '../parser'; import { Source } from '../source'; import dedent from '../../jsutils/dedent'; @@ -403,8 +404,11 @@ describe('Parser', () => { it('contains references to start and end tokens', () => { const result = parse('{ id }'); - expect(result).to.have.nested.property('loc.startToken.kind', ''); - expect(result).to.have.nested.property('loc.endToken.kind', ''); + expect(result).to.have.nested.property( + 'loc.startToken.kind', + TokenKind.SOF, + ); + expect(result).to.have.nested.property('loc.endToken.kind', TokenKind.EOF); }); describe('parseValue', () => {