-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow record default properties to be overridden (#103)
Fixes #101 . According to [The Big O of Code Reviews](https://www.egorand.dev/the-big-o-of-code-reviews/), this is a O(_n_) change. This PR adds a test for the behaviour described in #101, and a fix.
- Loading branch information
Showing
3 changed files
with
69 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
*/ | ||
|
||
import { uniffiCreateRecord } from "../../src/records"; | ||
|
||
export type MyRecord = { | ||
string: string; | ||
number: number; | ||
optionalString: string | undefined; | ||
bool: boolean; | ||
optionalBool: boolean | undefined; | ||
}; | ||
|
||
export const MyRecord = (() => { | ||
const defaults = () => ({ | ||
string: "default", | ||
optionalString: undefined, | ||
}); | ||
|
||
const create = uniffiCreateRecord<MyRecord, ReturnType<typeof defaults>>( | ||
defaults, | ||
); | ||
|
||
return { | ||
create, | ||
}; | ||
})(); |
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,38 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
*/ | ||
|
||
import { Asserts, test, xtest } from "../testing/asserts"; | ||
import { console } from "../testing/hermes"; | ||
import { MyRecord } from "./playground/records"; | ||
|
||
test("Allow defaults to be missing", (t) => { | ||
const v: MyRecord = MyRecord.create({ | ||
number: 42, | ||
bool: true, | ||
optionalBool: undefined, | ||
}); | ||
t.assertEqual(v.string, "default"); | ||
t.assertEqual(v.optionalString, undefined); | ||
t.assertEqual(v.number, 42); | ||
t.assertEqual(v.bool, true); | ||
t.assertEqual(v.optionalBool, undefined); | ||
}); | ||
|
||
test("Allow defaults to be overridden", (t) => { | ||
const v: MyRecord = MyRecord.create({ | ||
string: "overridden", | ||
optionalString: "also overridden", | ||
number: 43, | ||
bool: false, | ||
optionalBool: true, | ||
}); | ||
|
||
t.assertEqual(v.string, "overridden"); | ||
t.assertEqual(v.optionalString, "also overridden"); | ||
t.assertEqual(v.number, 43); | ||
t.assertEqual(v.bool, false); | ||
t.assertEqual(v.optionalBool, true); | ||
}); |