-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: remove-enum-prefix for nested enums (#903)
- Loading branch information
Showing
10 changed files
with
488 additions
and
43 deletions.
There are no files selected for viewing
52 changes: 43 additions & 9 deletions
52
integration/remove-enum-prefix-string-enums/remove-enum-prefix-string-enums-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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,46 @@ | ||
import { EnumFields, Foo, Bar } from "./remove-enum-prefix-string-enums"; | ||
import { | ||
Foo, | ||
Bar, | ||
fooFromJSON, | ||
fooToJSON, | ||
barFromJSON, | ||
barToJSON, | ||
WithNestedEnum_Baz, | ||
WithNestedEnum_Qux, | ||
withNestedEnum_BazFromJSON, | ||
withNestedEnum_BazToJSON, | ||
withNestedEnum_QuxFromJSON, | ||
withNestedEnum_QuxToJSON, | ||
} from "./remove-enum-prefix-string-enums"; | ||
|
||
describe("nestjs-metadata-test", () => { | ||
it("compiles", () => { | ||
const msg: EnumFields = { | ||
foo: Foo.BAR, | ||
bar: Bar.BAZ, | ||
}; | ||
const out = EnumFields.toJSON(msg); | ||
expect(out).not.toBeUndefined(); | ||
function testEnumFromJSONAndToJSON<ENUM>( | ||
fromJSON: (s: string) => ENUM, | ||
toJSON: (e: ENUM) => string, | ||
valueMap: Record<string, ENUM>, | ||
) { | ||
for (const [jsonValue, enumValue] of Object.entries(valueMap)) { | ||
expect(fromJSON(jsonValue)).toBe(enumValue); | ||
expect(toJSON(enumValue)).toBe(jsonValue); | ||
} | ||
} | ||
|
||
describe("remove-enum-prefix-string-enums", () => { | ||
it("encode and decode correctly", () => { | ||
testEnumFromJSONAndToJSON(fooFromJSON, fooToJSON, { | ||
FOO_UNSPECIFIED: Foo.UNSPECIFIED, | ||
FOO_BAR: Foo.BAR, | ||
}); | ||
testEnumFromJSONAndToJSON(barFromJSON, barToJSON, { | ||
BAR_UNSPECIFIED: Bar.UNSPECIFIED, | ||
BAZ: Bar.BAZ, | ||
}); | ||
testEnumFromJSONAndToJSON(withNestedEnum_BazFromJSON, withNestedEnum_BazToJSON, { | ||
BAZ_UNSPECIFIED: WithNestedEnum_Baz.UNSPECIFIED, | ||
BAZ_ONE: WithNestedEnum_Baz.ONE, | ||
}); | ||
testEnumFromJSONAndToJSON(withNestedEnum_QuxFromJSON, withNestedEnum_QuxToJSON, { | ||
QUX_UNSPECIFIED: WithNestedEnum_Qux.UNSPECIFIED, | ||
ONE: WithNestedEnum_Qux.ONE, | ||
}); | ||
}); | ||
}); |
Binary file modified
BIN
+631 Bytes
(190%)
integration/remove-enum-prefix-string-enums/remove-enum-prefix-string-enums.bin
Binary file not shown.
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,46 @@ | ||
import { | ||
Foo, | ||
Bar, | ||
fooFromJSON, | ||
fooToJSON, | ||
barFromJSON, | ||
barToJSON, | ||
withNestedEnum_BazFromJSON, | ||
WithNestedEnum_Baz, | ||
withNestedEnum_BazToJSON, | ||
withNestedEnum_QuxFromJSON, | ||
withNestedEnum_QuxToJSON, | ||
WithNestedEnum_Qux, | ||
} from "./remove-enum-prefix"; | ||
|
||
function testEnumFromJSONAndToJSON<ENUM>( | ||
fromJSON: (s: string) => ENUM, | ||
toJSON: (e: ENUM) => string, | ||
valueMap: Record<string, ENUM>, | ||
) { | ||
for (const [jsonValue, enumValue] of Object.entries(valueMap)) { | ||
expect(fromJSON(jsonValue)).toBe(enumValue); | ||
expect(toJSON(enumValue)).toBe(jsonValue); | ||
} | ||
} | ||
|
||
describe("remove-enum-prefix", () => { | ||
it("encode and decode correctly", () => { | ||
testEnumFromJSONAndToJSON(fooFromJSON, fooToJSON, { | ||
FOO_UNSPECIFIED: Foo.UNSPECIFIED, | ||
FOO_BAR: Foo.BAR, | ||
}); | ||
testEnumFromJSONAndToJSON(barFromJSON, barToJSON, { | ||
BAR_UNSPECIFIED: Bar.UNSPECIFIED, | ||
BAZ: Bar.BAZ, | ||
}); | ||
testEnumFromJSONAndToJSON(withNestedEnum_BazFromJSON, withNestedEnum_BazToJSON, { | ||
BAZ_UNSPECIFIED: WithNestedEnum_Baz.UNSPECIFIED, | ||
BAZ_ONE: WithNestedEnum_Baz.ONE, | ||
}); | ||
testEnumFromJSONAndToJSON(withNestedEnum_QuxFromJSON, withNestedEnum_QuxToJSON, { | ||
QUX_UNSPECIFIED: WithNestedEnum_Qux.UNSPECIFIED, | ||
ONE: WithNestedEnum_Qux.ONE, | ||
}); | ||
}); | ||
}); |
Binary file not shown.
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.