bug(tests): Passes npm test

Fixes minor bugs that caused npm test to fail.
This commit is contained in:
Ian Riley 2015-06-06 18:31:44 -07:00
parent 5bec620cbb
commit b18b5eea13
4 changed files with 67 additions and 196 deletions

View File

@ -1,94 +0,0 @@
/// <reference path="collection.d.ts" />
// there are various instances in these tests when the <any> type
// is provided, this is to prevent the compiler from complaining
// when I violate the type enforcement posed by the various
// collection types.
var assortment: Assortment = {}
function testAssortment() {
// assortments can be indexed by either number or string
assortment[0] = 0;
assortment['1'] = 1;
// assortments can store any type
assortment['a'] = 'a';
assortment['true'] = true;
}
testAssortment();
var collection: Collection<number> = {};
function testCollection() {
// collections can be indexed by either number or string
collection[0] = 0;
collection['1'] = 1;
// a collection can always be expected to provide back its type
var zero: number = collection[0];
var one: number = collection[1];
// collections should not store values that aren't of their type
collection['b'] = <any>'b'; // would have thrown compiler error
// collections also should not return values that aren't their type
var b: string = <any>collection['b']; // would have thrown compiler error
}
testCollection();
var dictionary: Dictionary = {};
function testDictionary() {
// dictionarys can store any type and are indexed by string
dictionary['b'] = 'b';
dictionary['1'] = 1;
dictionary['true'] = true;
// also, hopefully in the future, Dictionarys will error with these expressions as well
// However, TypeScript currently does not enforce key types for arrays
// dictionarys should not use any index value, but string
var newDictionary: Dictionary = <any>{0: 'a', 'b': 'b'}; // should have thrown compiler error
var a: string = newDictionary[<any>0]; // should have thrown compiler error
newDictionary[<any>1] = '1'; // should have thrown compiler error
// dictionarys should be used with type checking because of this
if (typeof dictionary['b'] == 'string') {
var b_str: string = dictionary['b'];
} else if (typeof dictionary['b'] == 'number') {
var b_num: number = dictionary['b'];
}
// but we can also just take the value if we don't care about the type
var one: any = dictionary['1'];
}
testDictionary();
var thesaurus: Thesaurus<string> = {};
function testThesaurus() {
// a thesaurus is indexed by string
thesaurus['word'] = 'a combination of varters';
// it should not be indexed with any other value, but it can
// in the current build of TypeScript
var newThesaurus: Thesaurus<string> = <any>{0: '0', '1': '1'}; // should have thrown compiler
// error
var zero: string = newThesaurus[<any>0]; // should have thrown compiler error
newThesaurus[<any>1] = '1'; // should have thrown compile error
// a thesaurus will return its respective value
var word: string = thesaurus['word'];
// assigning any other type will throw a compiler error
thesaurus['two'] = <any>2; // would have thrown compiler error
// expecting any other type will throw a compiler error
var two: number = <any>thesaurus['two']; // would have thrown compiler error
}

View File

@ -1,34 +0,0 @@
// Type definitions for collections
// Project: https://github.com/ttowncompiled/DefinitelyTyped
// Definitions by: Ian Riley <https://github.com/ttowncompiled>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
// an Assortment allows a variety of types, and each object can be referenced
// by number or by string.
interface Assortment {
[key: number]: any;
[key: string]: any;
}
// a Collection only allows a single type, and each object can be referenced
// by number or by string.
interface Collection<T> {
[key: number]: T;
[key: string]: T;
}
// a Dictionary allows a variety of types, but each object must be referenced by string.
interface Dictionary {
[key: string]: any;
}
// a Thesaurus only allows a single type, but each object must be referenced by string.
interface Thesaurus<T> {
[key: string]: T;
}
// an object that allows a variety of types, but each object is referenced by number
// is: any[]
// an object that allows only a single type, but each object is referenced by number
// is: T[]

View File

@ -1,8 +1,8 @@
/// <reference path="../chai/chai.d.ts" />
/// <reference path="protobufjs.d.ts" />
import {assert} from 'chai';
import {readFileSync} from 'fs';
var assert = require('chai').assert;
var readFileSync = require('fs').readFileSync;
import ProtoBuf = require("protobufjs");
function testProtoBufJs() {
@ -23,7 +23,7 @@ function testProtoBufJs() {
var jsonFileProto: ProtoBuf.ProtoBuilder = ProtoBuf.loadJsonFile("test.json");
assertIsProtoBuilder(jsonFileProto, "loadJsonFile");
ProtoBuf.loadJsonFile("test.json", (error, builder: ProtoBuf.ProtoBuilder) => {
ProtoBuf.loadJsonFile("test.json", (error: any, builder: ProtoBuf.ProtoBuilder) => {
assertIsProtoBuilder(builder, "loadJsonFile callback");
});
@ -34,7 +34,7 @@ function testProtoBufJs() {
var protoFile: ProtoBuf.ProtoBuilder = ProtoBuf.loadProtoFile("test.proto");
assertIsProtoBuilder(protoFile, "loadProtoFile");
ProtoBuf.loadProtoFile("test.proto", (error, builder: ProtoBuf.ProtoBuilder) => {
ProtoBuf.loadProtoFile("test.proto", (error: any, builder: ProtoBuf.ProtoBuilder) => {
assertIsProtoBuilder(builder, "loadProtoFile callback");
});

View File

@ -3,7 +3,6 @@
// Definitions by: Panu Horsmalahti <https://github.com/panuhorsmalahti>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
/// <reference path="../collection/collection.d.ts" />
/// <reference path="../node/node.d.ts" />
declare module ProtoBuf {
@ -32,31 +31,28 @@ declare module ProtoBuf {
callback?: (error: any, builder: ProtoBuilder) => void,
builder?: ProtoBuilder): ProtoBuilder;
export function newBuilder(options?: Dictionary): ProtoBuilder;
export function newBuilder(options?: {[key: string]: any}): ProtoBuilder;
// ==========
// protobufjs/src/ProtoBuf/Builder.js
/**
* TODO: constructor returns type ProtoBuilder
*/
export interface Builder {
(options?: Dictionary): void; // returns ProtoBuilder
new(options?: {[key: string]: any}): ProtoBuilder;
Message: Message;
Service: Service;
isValidMessage(def: Dictionary): boolean;
isValidMessageField(def: Dictionary): boolean;
isValidEnum(def: Dictionary): boolean;
isValidService(def: Dictionary): boolean;
isValidExtend(def: Dictionary): boolean;
isValidMessage(def: {[key: string]: any}): boolean;
isValidMessageField(def: {[key: string]: any}): boolean;
isValidEnum(def: {[key: string]: any}): boolean;
isValidService(def: {[key: string]: any}): boolean;
isValidExtend(def: {[key: string]: any}): boolean;
}
/**
* TODO: Confirm that message needs no further implementation
*/
export interface Message {
(values?: Dictionary, var_args?: string[]): void;
new(values?: {[key: string]: any}, var_args?: string[]): Message;
[field: string]: any;
}
@ -64,7 +60,7 @@ declare module ProtoBuf {
* TODO: Implement service interface
*/
export interface Service {
(rpcImpl?: Function): void;
new(rpcImpl?: Function): Service;
}
@ -78,22 +74,22 @@ declare module ProtoBuf {
result: ProtoBuf;
files: string[];
importRoot: string;
options: Dictionary;
options: {[key: string]: any};
syntax: string;
reset(): void;
define(pkg: string, options?: Dictionary): ProtoBuilder;
create(defs?: Dictionary[]): ProtoBuilder;
define(pkg: string, options?: {[key: string]: any}): ProtoBuilder;
create(defs?: {[key: string]: any}[]): ProtoBuilder;
resolveAll(): void;
build(path?: string): ProtoBuf;
lookup(path?: string): ReflectT;
}
export interface ProtoBuf {
[package: string]: Thesaurus<MetaMessage | any>;
[package: string]: {[key: string]: MetaMessage | any};
}
export interface MetaMessage {
(values?: Dictionary, var_args?: string[]): void; // returns Message
new(values?: {[key: string]: any}, var_args?: string[]): Message;
decode(buffer?: Buffer, enc?: string): Message;
decodeDelimited(buffer?: Buffer, enc?: string): Message;
decode64(str: string): Message;
@ -109,14 +105,14 @@ declare module ProtoBuf {
}
export interface Parser {
(proto: string): void;
new(proto: string): Parser;
tn: Tokenizer;
parse(): MetaProto;
toString(): string;
}
export interface Tokenizer {
(proto: string): void;
new(proto: string): Tokenizer;
source: string;
index: number;
line: number;
@ -136,14 +132,14 @@ declare module ProtoBuf {
messages: ProtoMessage[];
enums: ProtoEnum[];
imports: string[];
options: Dictionary;
options: {[key: string]: any};
services: ProtoService[];
}
export interface ProtoEnum {
name: string;
values: ProtoEnumValue;
options: Dictionary;
options: {[key: string]: any};
}
export interface ProtoEnumValue {
@ -153,7 +149,7 @@ declare module ProtoBuf {
export interface ProtoField {
rule: string;
options: Dictionary;
options: {[key: string]: any};
type: string;
name: string;
id: number;
@ -166,20 +162,20 @@ declare module ProtoBuf {
fields: ProtoField[];
enums: ProtoEnum[];
messages: ProtoMessage[];
options: Dictionary;
oneofs: Thesaurus<number[]>;
options: {[key: string]: any};
oneofs: {[key: string]:number[]};
}
export interface ProtoRpcService {
request: string;
response: string;
options: Dictionary;
options: {[key: string]: any};
}
export interface ProtoService {
name: string;
rpc: Thesaurus<ProtoRpcService>;
options: Dictionary;
rpc: {[key: string]:ProtoRpcService};
options: {[key: string]: any};
}
@ -196,7 +192,7 @@ declare module ProtoBuf {
}
export interface ReflectT {
(builder?: ProtoBuilder, parent?: ReflectT, name?: string): void;
new(builder?: ProtoBuilder, parent?: ReflectT, name?: string): ReflectT;
builder: ProtoBuilder;
parent: ReflectT;
name: string;
@ -205,72 +201,74 @@ declare module ProtoBuf {
}
export interface ReflectNamespace extends ReflectT {
(builder?: ProtoBuilder, parent?: ReflectNamespace, name?: string,
options?: Dictionary): void;
new(builder?: ProtoBuilder, parent?: ReflectNamespace, name?: string,
options?: {[key: string]: any}): ReflectNamespace;
className: string;
children: ReflectT[];
options: Dictionary;
options: {[key: string]: any};
syntax: string;
getChildren(type?: ReflectT): ReflectT[];
addChild(child: ReflectT): void;
getChild(nameOrId?: string | number): ReflectT;
resolve(qn: string, excludeFields?: boolean): ReflectNamespace;
build(): any; // TODO: discover the return type of build
buildOpt(): Dictionary;
build(): ProtoBuf;
buildOpt(): {[key: string]: any};
getOption(name?: string): any;
}
export interface ReflectMessage extends ReflectNamespace {
(builder?: ProtoBuilder, parent?: ReflectNamespace, name?: string,
options?: Dictionary, isGroup?: boolean): void;
Field: ReflectField; // TODO: only for new ProtoBuf.Reflect.Message.Field();
ExtensionField: ReflectExtensionField; // TODO: only for
new(builder?: ProtoBuilder, parent?: ReflectNamespace, name?: string,
options?: {[key: string]: any}, isGroup?: boolean): ReflectMessage;
Field: ReflectField; // NOTE: only for new ProtoBuf.Reflect.Message.Field();
ExtensionField: ReflectExtensionField; // NOTE: only for
// new ProtoBuf.Reflect.Message.ExtensionField();
OneOf: ReflectOneOf; // TODO: only for new ProtoBuf.Reflect.Message.OneOf();
OneOf: ReflectOneOf; // NOTE: only for new ProtoBuf.Reflect.Message.OneOf();
extensions: number[];
clazz(): any; //TODO: discover type of clazz
clazz(): MetaMessage;
isGroup: boolean;
build(rebuild?: boolean): any; // TODO: discover the return type of build
build(rebuild?: boolean): MetaMessage|any;
encode(message: Message, buffer: Buffer, noVerify?: boolean): Buffer;
calculate(message: Message): number;
decode(buffer: Buffer, length?: number, expectedGroupEndId?: number): Message;
}
export interface ReflectEnum extends ReflectNamespace {
(builder?: ProtoBuilder, parent?: ReflectT, name?: string,
options?: Dictionary): void;
Value: ReflectValue; // TODO: only for new ProtoBuf.Reflect.Enum.Value();
object: Thesaurus<number>;
build(): any; // TODO: discover the return type of build
new(builder?: ProtoBuilder, parent?: ReflectT, name?: string,
options?: {[key: string]: any}): ReflectEnum;
Value: ReflectValue; // NOTE: only for new ProtoBuf.Reflect.Enum.Value();
object: {[key: string]:number};
build(): {[key: string]: any};
}
export interface ReflectExtension extends ReflectT {
(builder?: ProtoBuilder, parent?: ReflectT, name?: string, field?: ReflectField): void;
new(builder?: ProtoBuilder, parent?: ReflectT, name?: string,
field?: ReflectField): ReflectExtension;
field: ReflectField;
}
export interface ReflectService extends ReflectNamespace {
Method: ReflectMethod; // TODO: only for new ProtoBuf.Reflect.Service.Method();
RPCMethod: ReflectRPCMethod; // TODO: only for new ProtoBuf.Reflect.Service.RPCMethod();
clazz(): any; // TODO: discover type of clazz
build(rebuild?: boolean): any; // TODO: discover the return type of build
new(): ReflectService;
Method: ReflectMethod; // NOTE: only for new ProtoBuf.Reflect.Service.Method();
RPCMethod: ReflectRPCMethod; // NOTE: only for new ProtoBuf.Reflect.Service.RPCMethod();
clazz(): Function;
build(rebuild?: boolean): Function|any;
}
// TODO: check that the runtime instance of this type reflects this definition
export interface ReflectField extends ReflectT {
(builder: ProtoBuilder, message: ReflectMessage, rule: string, type: string,
name: string, id: number, options: Dictionary, oneof: ReflectOneOf): void;
new(builder: ProtoBuilder, message: ReflectMessage, rule: string, type: string,
name: string, id: number, options: {[key: string]: any}, oneof: ReflectOneOf): ReflectField;
className: string;
required: boolean;
repeated: boolean;
type: string | WireTuple;
resolvedType: ReflectT;
id: number;
options: Dictionary;
options: {[key: string]: any};
defaultValue: any;
oneof: ReflectOneOf;
originalName: string;
build(): any; // TODO: discover the return type of build
build(): {[key: string]: any};
mkLong(value: any, unsigned?: boolean): number;
verifyValue(value: any, skipRepeated?: boolean): any;
encode(value: any, buffer: Buffer): Buffer;
@ -287,32 +285,33 @@ declare module ProtoBuf {
// TODO: check that the runtime instance of this type reflects this definition
export interface ReflectExtensionField extends ReflectField {
(builder: ProtoBuilder, message: ReflectMessage, rule: string, type: string,
name: string, id: number, options: Dictionary): void;
new(builder: ProtoBuilder, message: ReflectMessage, rule: string, type: string,
name: string, id: number, options: {[key: string]: any}): ReflectExtensionField;
extension: ReflectExtension;
}
export interface ReflectOneOf extends ReflectT {
(builder: ProtoBuilder, message: ReflectMessage, name: string): void;
new(builder?: ProtoBuilder, message?: ReflectMessage, name?: string): ReflectOneOf;
fields: ReflectField[];
}
export interface ReflectValue extends ReflectT {
(builder?: ProtoBuilder, enm?: ReflectEnum, name?: string, id?: number): void;
new(builder?: ProtoBuilder, enm?: ReflectEnum, name?: string, id?: number): ReflectValue;
className: string;
id: number;
}
export interface ReflectMethod extends ReflectT {
(builder: ProtoBuilder, svc: ReflectService, name: string, options: Dictionary): void;
new(builder?: ProtoBuilder, svc?: ReflectService, name?: string,
options?: {[key: string]: any}): ReflectMethod;
className: string;
options: Dictionary;
buildOpt(): Dictionary;
options: {[key: string]: any};
buildOpt(): {[key: string]: any};
}
export interface ReflectRPCMethod extends ReflectMethod {
(builder: ProtoBuilder, svc: ReflectService, name: string, request: string,
response: string, options: Dictionary): void;
new(builder?: ProtoBuilder, svc?: ReflectService, name?: string, request?: string,
response?: string, options?: {[key: string]: any}): ReflectRPCMethod;
requestName: string;
responseName: string;
resolvedRequestType: ReflectMessage;