Merge pull request #171 from quicktype/big-list-of-naughty-strings
Big list of naughty strings. Fixes #168
This commit is contained in:
Коммит
d51e6e9d99
|
@ -67,7 +67,6 @@ isPartCharacter c =
|
|||
Just ConnectorPunctuation -> true
|
||||
Just NonSpacingMark -> true
|
||||
Just SpacingCombiningMark -> true
|
||||
Just Format -> true
|
||||
_ -> isStartCharacter c
|
||||
|
||||
renderNullableToCSharp :: IRType -> Doc String
|
||||
|
|
|
@ -2,16 +2,16 @@ module Language.Java
|
|||
( renderer
|
||||
) where
|
||||
|
||||
import Doc (Doc, Renderer, blank, combineNames, forEachProperty_, forEachTopLevel_, getForSingleOrMultipleTopLevels, getTypeNameForUnion, indent, line, lookupClassName, lookupUnionName, noForbidNamer, renderRenderItems, simpleNamer, transformPropertyNames, unionIsNotSimpleNullable, unionNameIntercalated)
|
||||
import IRGraph (IRClassData(..), IRType(..), IRUnionRep, forUnion_, isUnionMember, nullableFromUnion, removeNullFromUnion, unionHasArray, unionHasClass, unionHasMap)
|
||||
import Prelude
|
||||
|
||||
import Data.Char.Unicode (GeneralCategory(..), generalCategory, isSpace)
|
||||
import Data.Char.Unicode (isAscii, isDigit, isLetter)
|
||||
import Data.Foldable (for_)
|
||||
import Data.Map (Map)
|
||||
import Data.Map as M
|
||||
import Data.Maybe (Maybe(..))
|
||||
import Data.String.Util (camelCase, capitalize, isLetterOrLetterNumber, legalizeCharacters, startWithLetter, stringEscape)
|
||||
import Data.String.Util (camelCase, capitalize, legalizeCharacters, startWithLetter, stringEscape)
|
||||
import Doc (Doc, Renderer, blank, combineNames, forEachProperty_, forEachTopLevel_, getForSingleOrMultipleTopLevels, getTypeNameForUnion, indent, line, lookupClassName, lookupUnionName, noForbidNamer, renderRenderItems, simpleNamer, transformPropertyNames, unionIsNotSimpleNullable, unionNameIntercalated)
|
||||
import IRGraph (IRClassData(..), IRType(..), IRUnionRep, forUnion_, isUnionMember, nullableFromUnion, removeNullFromUnion, unionHasArray, unionHasClass, unionHasMap)
|
||||
|
||||
forbiddenNames :: Array String
|
||||
forbiddenNames =
|
||||
|
@ -29,7 +29,7 @@ forbiddenNames =
|
|||
, "char", "final", "interface", "static", "void"
|
||||
, "class", "finally", "long", "strictfp", "volatile"
|
||||
, "const", "float", "native", "super", "while"
|
||||
, "null"
|
||||
, "null", "false", "true"
|
||||
]
|
||||
|
||||
renderer :: Renderer
|
||||
|
@ -57,20 +57,11 @@ nameForClass (IRClassData { names }) = javaNameStyle true $ combineNames names
|
|||
|
||||
isStartCharacter :: Char -> Boolean
|
||||
isStartCharacter c =
|
||||
case generalCategory c of
|
||||
Just CurrencySymbol -> true
|
||||
Just ConnectorPunctuation -> true
|
||||
_ -> isLetterOrLetterNumber c
|
||||
(isAscii c && isLetter c) || c == '_'
|
||||
|
||||
isPartCharacter :: Char -> Boolean
|
||||
isPartCharacter c =
|
||||
case generalCategory c of
|
||||
Just DecimalNumber -> true
|
||||
Just SpacingCombiningMark -> true
|
||||
Just NonSpacingMark -> true
|
||||
Just Format -> true
|
||||
Just Control -> not $ isSpace c
|
||||
_ -> isStartCharacter c
|
||||
isStartCharacter c || (isAscii c && isDigit c)
|
||||
|
||||
legalize :: String -> String
|
||||
legalize = legalizeCharacters isPartCharacter
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
import argparse
|
||||
import random
|
||||
import json
|
||||
import sys
|
||||
|
||||
with open('/usr/share/dict/words') as f:
|
||||
words = f.read().splitlines()
|
||||
|
@ -26,9 +28,18 @@ def main():
|
|||
parser.add_argument('--array-elements', nargs=1, type=int, default=[3])
|
||||
parser.add_argument('--object-size', nargs=1, type=int, default=None)
|
||||
parser.add_argument('--class-count', nargs=1, type=int, default=None)
|
||||
parser.add_argument('--with-string-list', nargs=1, default=None)
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.class_count:
|
||||
if args.with_string_list:
|
||||
with open(args.with_string_list[0]) as f:
|
||||
strings = [x[:-1] for x in f.readlines()]
|
||||
obj = {}
|
||||
for s in strings:
|
||||
obj[s] = s
|
||||
obj["dontMakeAMap"] = True
|
||||
json.dump(obj, sys.stdout)
|
||||
elif args.class_count:
|
||||
print('{')
|
||||
for i in range(args.class_count[0] - 1):
|
||||
print(' "class%d":' % i)
|
||||
|
|
Различия файлов скрыты, потому что одна или несколько строк слишком длинны
|
@ -9,9 +9,6 @@ declare module Math {
|
|||
|
||||
// https://stackoverflow.com/questions/1068834/object-comparison-in-javascript
|
||||
export default function deepEquals(x: any, y: any, path: string[] = []): boolean {
|
||||
var i;
|
||||
var p;
|
||||
|
||||
// remember that NaN === NaN returns false
|
||||
// and isNaN(undefined) returns true
|
||||
if (typeof x === 'number' && typeof y === 'number') {
|
||||
|
@ -60,8 +57,8 @@ export default function deepEquals(x: any, y: any, path: string[] = []): boolean
|
|||
console.error(`Arrays don't have the same length at path ${pathToString(path)}.`);
|
||||
return false;
|
||||
}
|
||||
for (i = 0; i < x.length; i++) {
|
||||
path.push(i)
|
||||
for (let i = 0; i < x.length; i++) {
|
||||
path.push(i.toString());
|
||||
if (!deepEquals(x[i], y[i], path))
|
||||
return false;
|
||||
path.pop();
|
||||
|
@ -69,10 +66,15 @@ export default function deepEquals(x: any, y: any, path: string[] = []): boolean
|
|||
return true;
|
||||
}
|
||||
|
||||
for (p in y) {
|
||||
// FIXMEL The way we're looking up properties with `indexOf` makes this
|
||||
// quadratic. So far no problem, so meh.
|
||||
const xKeys = Object.keys(x);
|
||||
const yKeys = Object.keys(y);
|
||||
|
||||
for (const p of yKeys) {
|
||||
// We allow properties in y that aren't present in x
|
||||
// so long as they're null.
|
||||
if (y.hasOwnProperty(p) && !x.hasOwnProperty(p)) {
|
||||
if (xKeys.indexOf(p) < 0) {
|
||||
if (y[p] !== null) {
|
||||
console.error(`Non-null property ${p} is not expected at path ${pathToString(path)}.`);
|
||||
return false;
|
||||
|
@ -85,8 +87,8 @@ export default function deepEquals(x: any, y: any, path: string[] = []): boolean
|
|||
}
|
||||
}
|
||||
|
||||
for (p in x) {
|
||||
if (x.hasOwnProperty(p) && !y.hasOwnProperty(p)) {
|
||||
for (const p of xKeys) {
|
||||
if (yKeys.indexOf(p) < 0) {
|
||||
console.error(`Expected property ${p} not found at path ${pathToString(path)}.`);
|
||||
return false;
|
||||
}
|
||||
|
|
44
test/test.ts
44
test/test.ts
|
@ -73,7 +73,8 @@ const FIXTURES: Fixture[] = [
|
|||
test: testJava,
|
||||
skip: [
|
||||
"identifiers.json",
|
||||
"simple-identifiers.json"
|
||||
"simple-identifiers.json",
|
||||
"blns-object.json"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -85,7 +86,8 @@ const FIXTURES: Fixture[] = [
|
|||
test: testGo,
|
||||
skip: [
|
||||
"identifiers.json",
|
||||
"simple-identifiers.json"
|
||||
"simple-identifiers.json",
|
||||
"blns-object.json"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -97,7 +99,8 @@ const FIXTURES: Fixture[] = [
|
|||
test: testJsonSchema,
|
||||
skip: [
|
||||
"identifiers.json",
|
||||
"simple-identifiers.json"
|
||||
"simple-identifiers.json",
|
||||
"blns-object.json"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -110,7 +113,8 @@ const FIXTURES: Fixture[] = [
|
|||
test: testElm,
|
||||
skip: [
|
||||
"identifiers.json",
|
||||
"simple-identifiers.json"
|
||||
"simple-identifiers.json",
|
||||
"blns-object.json"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -122,7 +126,8 @@ const FIXTURES: Fixture[] = [
|
|||
test: testSwift,
|
||||
skip: [
|
||||
"identifiers.json",
|
||||
"no-classes.json"
|
||||
"no-classes.json",
|
||||
"blns-object.json"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -297,6 +302,14 @@ function exec(
|
|||
return result;
|
||||
}
|
||||
|
||||
function callAndReportFailure<T>(message: string, f: () => T): T {
|
||||
try {
|
||||
return f();
|
||||
} catch (e) {
|
||||
failWith(message, { error: e });
|
||||
}
|
||||
}
|
||||
|
||||
type ComparisonArgs = {
|
||||
expectedFile: string;
|
||||
jsonFile?: string;
|
||||
|
@ -309,16 +322,17 @@ function compareJsonFileToJson(args: ComparisonArgs) {
|
|||
|
||||
let { expectedFile, jsonFile, jsonCommand, strict } = args;
|
||||
|
||||
let jsonString = jsonFile
|
||||
? fs.readFileSync(jsonFile, "utf8")
|
||||
: exec(jsonCommand).stdout;
|
||||
const jsonString = jsonFile
|
||||
? callAndReportFailure("Could not read JSON output file", () => fs.readFileSync(jsonFile, "utf8"))
|
||||
: callAndReportFailure("Could not run command for JSON output", () => exec(jsonCommand).stdout);
|
||||
|
||||
let givenJSON = JSON.parse(jsonString);
|
||||
let expectedJSON = JSON.parse(fs.readFileSync(expectedFile, "utf8"));
|
||||
const givenJSON = callAndReportFailure("Could not parse output JSON", () => JSON.parse(jsonString));
|
||||
const expectedJSON = callAndReportFailure("Could not read or parse expected JSON file",
|
||||
() => JSON.parse(fs.readFileSync(expectedFile, "utf8")));
|
||||
|
||||
let jsonAreEqual = strict
|
||||
? strictDeepEquals(givenJSON, expectedJSON)
|
||||
: deepEquals(expectedJSON, givenJSON);
|
||||
? callAndReportFailure("Failed to strictly compare objects", () => strictDeepEquals(givenJSON, expectedJSON))
|
||||
: callAndReportFailure("Failed to compare objects.", () => deepEquals(expectedJSON, givenJSON));
|
||||
|
||||
if (!jsonAreEqual) {
|
||||
failWith("Error: Output is not equivalent to input.", {
|
||||
|
@ -372,7 +386,11 @@ async function runFixtureWithSample(fixture: Fixture, sample: string, index: num
|
|||
// Generate code from the sample
|
||||
await quicktype({ src: [sampleFile], out: fixture.output, topLevel: fixture.topLevel});
|
||||
|
||||
await fixture.test(sampleFile);
|
||||
try {
|
||||
await fixture.test(sampleFile);
|
||||
} catch (e) {
|
||||
failWith("Fixture threw an exception", { error: e });
|
||||
}
|
||||
|
||||
if (fixture.diffViaSchema) {
|
||||
debug("* Diffing with code generated via JSON Schema");
|
||||
|
|
Загрузка…
Ссылка в новой задаче