New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Increase test coverage #4742
Merged
Merged
Increase test coverage #4742
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5bb1a91
Add tests for path type inferers
motiz88 32ba023
Add test for babel-types.valueToNode
motiz88 1e328d6
Add tests for babel-types.toKeyAlias
motiz88 336aad4
Add tests for babel-types.toStatement
motiz88 3934d56
Add tests for babel-types.toExpression
motiz88 dfbd78f
Lint fixes in babel-traverse/test/inference
motiz88 07222d2
Merge branch 'master' into increase-coverage
danez File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
import * as t from "../lib"; | ||
import { assert } from "chai"; | ||
|
||
describe("converters", function () { | ||
describe("valueToNode", function () { | ||
it("number", function () { | ||
assert.deepEqual(t.valueToNode(Math.PI), t.numericLiteral(Math.PI)); | ||
assert.deepEqual(t.valueToNode(-Infinity), t.numericLiteral(-Infinity)); | ||
assert.deepEqual(t.valueToNode(NaN), t.numericLiteral(NaN)); | ||
}); | ||
it("string", function () { | ||
assert.deepEqual(t.valueToNode("This is a \"string\""), t.stringLiteral("This is a \"string\"")); | ||
}); | ||
it("boolean", function () { | ||
assert.deepEqual(t.valueToNode(true), t.booleanLiteral(true)); | ||
assert.deepEqual(t.valueToNode(false), t.booleanLiteral(false)); | ||
}); | ||
it("null", function () { | ||
assert.deepEqual(t.valueToNode(null), t.nullLiteral()); | ||
}); | ||
it("undefined", function () { | ||
assert.deepEqual(t.valueToNode(undefined), t.identifier("undefined")); | ||
}); | ||
it("RegExp", function () { | ||
assert.deepEqual(t.valueToNode(/abc.+/gm), t.regExpLiteral("abc.+", "gm")); | ||
}); | ||
it("array", function () { | ||
assert.deepEqual(t.valueToNode([1, "a"]), t.arrayExpression([t.numericLiteral(1), t.stringLiteral("a")])); | ||
}); | ||
it("object", function () { | ||
assert.deepEqual(t.valueToNode({ | ||
a: 1, | ||
"b c": 2 | ||
}), t.objectExpression([ | ||
t.objectProperty(t.identifier("a"), t.numericLiteral(1)), | ||
t.objectProperty(t.stringLiteral("b c"), t.numericLiteral(2)) | ||
])); | ||
}); | ||
it("throws if cannot convert", function () { | ||
assert.throws(function () { | ||
t.valueToNode(Object); | ||
}); | ||
assert.throws(function () { | ||
t.valueToNode(Symbol()); | ||
}); | ||
}); | ||
}); | ||
describe("toKeyAlias", function () { | ||
beforeEach(function () { | ||
// make tests deterministic | ||
t.toKeyAlias.uid = 0; | ||
}); | ||
it("doesn't change string literals", function () { | ||
assert.equal(t.toKeyAlias(t.objectProperty(t.stringLiteral("a"), t.nullLiteral())), "\"a\""); | ||
}); | ||
it("wraps around at Number.MAX_SAFE_INTEGER", function () { | ||
assert.equal(t.toKeyAlias(t.objectMethod("method", t.identifier("a"), [], t.blockStatement([]))), "0"); | ||
}); | ||
}); | ||
describe("toStatement", function () { | ||
it("noop on statements", function () { | ||
const node = t.emptyStatement(); | ||
assert.equal(t.toStatement(node), node); | ||
t.assertEmptyStatement(node); | ||
}); | ||
it("mutate class expression to declaration", function () { | ||
const node = t.classExpression(t.identifier("A"), null, t.classBody([]), []); | ||
t.toStatement(node); | ||
t.assertClassDeclaration(node); | ||
}); | ||
it("fail if class expression has no id", function () { | ||
const node = t.classExpression(null, null, t.classBody([]), []); | ||
assert.throws(function() { | ||
t.toStatement(node); | ||
}); | ||
assert.strictEqual(t.toStatement(node, /* ignore = */ true), false); | ||
t.assertClassExpression(node); | ||
}); | ||
it("mutate function expression to declaration", function () { | ||
const node = t.functionExpression(t.identifier("A"), [], t.blockStatement([])); | ||
t.toStatement(node); | ||
t.assertFunctionDeclaration(node); | ||
}); | ||
it("fail if function expression has no id", function () { | ||
const node = t.functionExpression(null, [], t.blockStatement([])); | ||
assert.throws(function() { | ||
t.toStatement(node); | ||
}); | ||
assert.strictEqual(t.toStatement(node, /* ignore = */ true), false); | ||
t.assertFunctionExpression(node); | ||
}); | ||
it("assignment expression", function () { | ||
const node = t.assignmentExpression("+=", t.identifier("x"), t.numericLiteral(1)); | ||
t.assertExpressionStatement(t.toStatement(node)); | ||
t.assertAssignmentExpression(node); | ||
}); | ||
it("fail if cannot convert node type", function () { | ||
const node = t.yieldExpression(t.identifier("foo")); | ||
assert.throws(function() { | ||
t.toStatement(node); | ||
}); | ||
assert.strictEqual(t.toStatement(node, /* ignore = */ true), false); | ||
t.assertYieldExpression(node); | ||
}); | ||
}); | ||
describe("toExpression", function () { | ||
it("noop on expressions", function () { | ||
const node = t.identifier("a"); | ||
assert.equal(t.toExpression(node), node); | ||
t.assertIdentifier(node); | ||
}); | ||
it("mutate class declaration to expression", function () { | ||
const node = t.classDeclaration(t.identifier("A"), null, t.classBody([]), []); | ||
t.toExpression(node); | ||
t.assertClassExpression(node); | ||
}); | ||
it("mutate function declaration to expression", function () { | ||
const node = t.functionDeclaration(t.identifier("A"), [], t.blockStatement([])); | ||
t.toExpression(node); | ||
t.assertFunctionExpression(node); | ||
}); | ||
it("mutate object method to expression", function () { | ||
const node = t.objectMethod("method", t.identifier("A"), [], t.blockStatement([])); | ||
t.toExpression(node); | ||
t.assertFunctionExpression(node); | ||
}); | ||
it("mutate class method to expression", function () { | ||
const node = t.classMethod("constructor", t.identifier("A"), [], t.blockStatement([])); | ||
t.toExpression(node); | ||
t.assertFunctionExpression(node); | ||
}); | ||
it("expression statement", function () { | ||
const inner = t.yieldExpression(t.identifier("foo")); | ||
const node = t.expressionStatement(inner); | ||
t.assertYieldExpression(t.toExpression(node)); | ||
assert.equal(t.toExpression(node), inner); | ||
t.assertExpressionStatement(node); | ||
}); | ||
it("fail if cannot convert node type", function () { | ||
const node = t.program([]); | ||
assert.throws(function() { | ||
t.toExpression(node); | ||
}); | ||
t.assertProgram(node); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this be breaking if we remove
Func as Function, Func as Class
? (although maybe it's not used at all)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I interpreted it to be an implementation detail, where
getTypeAnnotation()
,baseTypeStrictlyMatches()
etc are the actual API.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And IIRC this was actually broken the old way - i.e. whoever wrote this was clearly expecting some code to expand
Function
andClass
a la visitor method names, and it wasn't happening. (But not tested so it only came up when I tackled coverage)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 yeah we have a lot that might not be used anywhere