-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add equality functions for comparing message definitions / -fields (#1)
### Public-Facing Changes Add equality functions for comparing message definitions / -fields ### Description - Changes the build to use typescript - Adds equality functions for comparing message definitions / -fields - Add some test cases
- Loading branch information
Showing
12 changed files
with
2,083 additions
and
19 deletions.
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,6 @@ | ||
{ | ||
"testMatch": ["<rootDir>/src/**/*.test.ts"], | ||
"transform": { | ||
"^.+\\.ts$": "ts-jest" | ||
} | ||
} |
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 |
---|---|---|
|
@@ -25,26 +25,36 @@ | |
"email": "[email protected]" | ||
}, | ||
"homepage": "https://github.com/foxglove/message-definition", | ||
"main": "src/index.js", | ||
"typings": "src/index.d.ts", | ||
"main": "dist/index.js", | ||
"typings": "dist/index.d.ts", | ||
"files": [ | ||
"dist", | ||
"src" | ||
], | ||
"scripts": { | ||
"build": "yarn clean && tsc -b ./tsconfig.build.json", | ||
"clean": "rm -rf dist tsconfig.build.tsbuildinfo", | ||
"lint:ci": "eslint --report-unused-disable-directives .", | ||
"lint": "eslint --report-unused-disable-directives --fix .", | ||
"prepublishOnly": "yarn lint:ci" | ||
"prepack": "yarn build", | ||
"prepublishOnly": "yarn lint:ci", | ||
"test": "jest" | ||
}, | ||
"devDependencies": { | ||
"@foxglove/eslint-plugin": "0.21.0", | ||
"@types/jest": "^29.5.3", | ||
"@typescript-eslint/eslint-plugin": "5.54.0", | ||
"@typescript-eslint/parser": "5.54.0", | ||
"eslint": "8.35.0", | ||
"eslint-config-prettier": "8.6.0", | ||
"eslint-plugin-es": "4.1.0", | ||
"eslint-plugin-filenames": "1.3.2", | ||
"eslint-plugin-import": "2.27.5", | ||
"eslint-plugin-jest": "^27.2.3", | ||
"eslint-plugin-prettier": "4.2.1", | ||
"prettier": "2.8.4" | ||
"jest": "^29.6.1", | ||
"prettier": "2.8.4", | ||
"ts-jest": "^29.1.1", | ||
"typescript": "^5.1.6" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,2 @@ | ||
export * from "./types"; | ||
export * from "./utils"; |
File renamed without changes.
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,63 @@ | ||
import { MessageDefinition } from "./types"; | ||
import { isMsgDefEqual } from "./utils"; | ||
|
||
describe("isMsgDefEqual", () => { | ||
it("Should return true for definition for same msg parsed from .msg and .idl", () => { | ||
const definition1: MessageDefinition = { | ||
definitions: [ | ||
{ | ||
name: "sec", | ||
type: "int32", | ||
isComplex: false, | ||
isArray: false, | ||
}, | ||
{ | ||
name: "nanosec", | ||
type: "uint32", | ||
isComplex: false, | ||
isArray: false, | ||
}, | ||
], | ||
}; | ||
const definition2: MessageDefinition = { | ||
name: "builtin_interfaces/msg/Time", | ||
definitions: [ | ||
{ | ||
type: "int32", | ||
isComplex: false, | ||
name: "sec", | ||
}, | ||
{ | ||
type: "uint32", | ||
isComplex: false, | ||
name: "nanosec", | ||
}, | ||
], | ||
}; | ||
expect(isMsgDefEqual(definition1, definition2)).toBe(true); | ||
}); | ||
|
||
it("should return true for identical definitions", () => { | ||
const definition1: MessageDefinition = { | ||
name: "definition", | ||
definitions: [ | ||
{ type: "string", name: "field1" }, | ||
{ type: "number", name: "field2" }, | ||
], | ||
}; | ||
const definition2: MessageDefinition = { ...definition1 }; | ||
expect(isMsgDefEqual(definition1, definition2)).toBe(true); | ||
}); | ||
|
||
it("should return false for different definitions", () => { | ||
const definition1: MessageDefinition = { | ||
name: "definition", | ||
definitions: [{ type: "string", name: "field1" }], | ||
}; | ||
const definition2: MessageDefinition = { | ||
...definition1, | ||
definitions: [{ type: "string", name: "fieldWithDifferentName" }], | ||
}; | ||
expect(isMsgDefEqual(definition1, definition2)).toBe(false); | ||
}); | ||
}); |
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,28 @@ | ||
import { MessageDefinition, MessageDefinitionField } from "./types"; | ||
|
||
export function isMsgDefFieldEqual( | ||
lhs: MessageDefinitionField, | ||
rhs: MessageDefinitionField, | ||
): boolean { | ||
return ( | ||
lhs.type === rhs.type && | ||
lhs.name === rhs.name && | ||
(lhs.isComplex ?? false) === (rhs.isComplex ?? false) && | ||
(lhs.isArray ?? false) === (rhs.isArray ?? false) && | ||
lhs.arrayLength === rhs.arrayLength && | ||
(lhs.isConstant ?? false) === (rhs.isConstant ?? false) && | ||
lhs.value === rhs.value && | ||
lhs.valueText === rhs.valueText && | ||
lhs.upperBound === rhs.upperBound && | ||
lhs.arrayUpperBound === rhs.arrayUpperBound && | ||
lhs.defaultValue === rhs.defaultValue | ||
); | ||
} | ||
|
||
export function isMsgDefEqual(lhs: MessageDefinition, rhs: MessageDefinition): boolean { | ||
return ( | ||
(lhs.name == undefined || rhs.name == undefined || lhs.name === rhs.name) && | ||
lhs.definitions.length === rhs.definitions.length && | ||
lhs.definitions.every((def, i) => isMsgDefFieldEqual(def, rhs.definitions[i]!)) | ||
); | ||
} |
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,4 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"exclude": ["**/**.test.ts"] | ||
} |
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
Oops, something went wrong.