Skip to content

Commit 8818424

Browse files
committed
Fix generator test cases
1 parent 407fdf6 commit 8818424

File tree

4 files changed

+25
-26
lines changed

4 files changed

+25
-26
lines changed

src/parser.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,10 @@ export default class Parser {
228228
* A particularly hard to detect situation is when the struct is really
229229
* a wrapper around either a primitive or a primitive array that should be
230230
* elided. Examples of such types are
231-
* LDKStr, LDKBigEndianScalar, LDKu5, LDKTransaction, LDKu8slice, LDKBigEndianScalar,
231+
* LDKStr, LDKBigEndianScalar, LDKU5, LDKTransaction, LDKu8slice, LDKBigEndianScalar,
232232
* and many, many, more.
233233
* The indications are really varied, because there are multiple breakdowns:
234-
* a) is it a wrapper over one primitive (LDKu5, LDKWitnessVersion), or over an
234+
* a) is it a wrapper over one primitive (LDKU5, LDKWitnessVersion), or over an
235235
* array of primitives (all other instances)
236236
* b) if it's a wrapper over an array, is it fixed-length or variable-length?
237237
* Fixed-length examples are LDKRecoverableSignature, LDKBigEndianScalar.

test/fixtures/parsing/array_wrapper_test_01.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ typedef struct LDKThirtyTwoBytes {
4141
/**
4242
* Integer in the range `0..32`
4343
*/
44-
typedef struct LDKu5 {
44+
typedef struct LDKU5 {
4545
uint8_t _0;
46-
} LDKu5;
46+
} LDKU5;
4747

4848
/**
4949
* A serialized transaction, in (pointer, length) form.

test/generator_tests.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import ResultGenerator from '../src/generation/result_generator.mjs';
1919
import PrimitiveWrapperGenerator from '../src/generation/primitive_wrapper_generator.mjs';
2020
import * as chai from 'chai';
2121
import PrimitiveEnumGenerator from '../src/generation/primitive_enum_generator.mjs';
22-
import Generator, {AuxiliaryArtifacts} from '../src/generation/index.mjs';
22+
import {AuxiliaryArtifacts} from '../src/generation/index.mjs';
2323
import TraitGenerator from '../src/generation/trait_generator.mjs';
2424

2525
class TestConfig extends Config {
@@ -56,7 +56,7 @@ describe('Generator Tests', () => {
5656
chai.expect(output).contains('public func getClaimableBalances() -> [UInt8] {');
5757
chai.expect(output).contains('internal func free() {');
5858

59-
chai.expect(output).contains('let chainSourceOption = Option_FilterZ(value: chainSource)');
59+
chai.expect(output).contains('let chainSourceOption = Option_FilterZ(some: chainSource)');
6060
chai.expect(output).contains('let nativeCallResult = ChainMonitor_free(self.cType!)');
6161
});
6262

@@ -105,9 +105,6 @@ describe('Generator Tests', () => {
105105
const vectorVectorOutput = generator.generateFileContents(routeVectorVector);
106106

107107
chai.expect(vectorVectorOutput).includes('public init(array: [[RouteHop]]) {');
108-
chai.expect(vectorVectorOutput).includes('let rustArray = array.map { (currentValueDepth1) in');
109-
chai.expect(vectorVectorOutput).includes('currentValueDepth1.map { (currentValueDepth2) in');
110-
chai.expect(vectorVectorOutput).includes('currentValueDepth2.danglingClone().cType!');
111108
chai.expect(vectorVectorOutput)
112109
.includes('let dataContainer = UnsafeMutablePointer<LDKCVec_RouteHopZ>.allocate(capacity: array.count)');
113110
chai.expect(vectorVectorOutput)
@@ -122,24 +119,24 @@ describe('Generator Tests', () => {
122119
chai.expect(vectorVectorOutput).includes('var convertedEntry1 = [LDKRouteHop]()');
123120
chai.expect(vectorVectorOutput).includes('let currentEntry2 = currentEntry1.data[index2]');
124121
chai.expect(vectorVectorOutput).includes('convertedEntry1.append(currentEntry2)');
125-
chai.expect(vectorVectorOutput).includes('let swiftArray = array.map { (currentCType) in');
126-
chai.expect(vectorVectorOutput).includes('currentCType.map { (currentCType) in');
127-
chai.expect(vectorVectorOutput).includes('RouteHop(pointer: currentCType)');
122+
chai.expect(vectorVectorOutput).includes('let swiftArray = array.map { (currentCType: [LDKRouteHop]) -> [RouteHop] in');
123+
chai.expect(vectorVectorOutput).includes('currentCType.map { (currentCType: LDKRouteHop) -> RouteHop in');
124+
chai.expect(vectorVectorOutput).includes('RouteHop(cType: currentCType, anchor: self).dangle()');
128125

129126
const routeVector = <RustVector>glossary['LDKCVec_RouteHopZ'];
130127
const vectorOutput = generator.generateFileContents(routeVector);
131128
chai.expect(vectorOutput).includes('public init(array: [RouteHop]) {');
132-
chai.expect(vectorOutput).includes('let rustArray = array.map { (currentValueDepth1) in');
133-
chai.expect(vectorOutput).includes('currentValueDepth1.danglingClone().cType!');
129+
chai.expect(vectorOutput).includes('let rustArray = array.map { (currentValueDepth1: RouteHop) -> LDKRouteHop in');
130+
chai.expect(vectorOutput).includes('return currentValueDepth1.cType!');
134131
chai.expect(vectorOutput)
135132
.includes('let dataContainer = UnsafeMutablePointer<LDKRouteHop>.allocate(capacity: array.count)');
136133
chai.expect(vectorOutput)
137134
.includes('let vector = LDKCVec_RouteHopZ(data: dataContainer, datalen: UInt(array.count))');
138135

139136
chai.expect(vectorOutput).includes('public func getValue() -> [RouteHop] {');
140137
chai.expect(vectorOutput).includes('var array = [LDKRouteHop]()');
141-
chai.expect(vectorOutput).includes('let swiftArray = array.map { (currentCType) in');
142-
chai.expect(vectorOutput).includes('RouteHop(pointer: currentCType)');
138+
chai.expect(vectorOutput).includes('let swiftArray = array.map { (currentCType: LDKRouteHop) -> RouteHop in');
139+
chai.expect(vectorOutput).includes('RouteHop(cType: currentCType, anchor: self).dangle()');
143140
});
144141

145142
it('should generate Vec<u8>', () => {
@@ -191,15 +188,15 @@ describe('Generator Tests', () => {
191188
const accessErrorOutput = generator.generateFileContents(accessError);
192189
chai.expect(accessErrorOutput).includes('internal init (value: LDKAccessError) {');
193190
chai.expect(accessErrorOutput).includes('self = .UnknownTx');
194-
chai.expect(accessErrorOutput).includes('if value == LDKAccessError_UnknownChain {');
191+
chai.expect(accessErrorOutput).includes('case LDKAccessError_UnknownTx:');
195192
chai.expect(accessErrorOutput).includes('internal func getCValue() -> LDKAccessError {');
196193
chai.expect(accessErrorOutput).includes('case .UnknownChain:');
197194
chai.expect(accessErrorOutput).includes('return LDKAccessError_UnknownChain');
198195

199196
const monitorUpdateStatusOutput = generator.generateFileContents(monitorUpdateStatus);
200197
chai.expect(monitorUpdateStatusOutput).includes('internal init (value: LDKChannelMonitorUpdateStatus) {');
201198
chai.expect(monitorUpdateStatusOutput).includes('self = .PermanentFailure');
202-
chai.expect(monitorUpdateStatusOutput).includes('if value == LDKChannelMonitorUpdateStatus_Completed {');
199+
chai.expect(monitorUpdateStatusOutput).includes('case LDKChannelMonitorUpdateStatus_Completed:');
203200
chai.expect(monitorUpdateStatusOutput).includes('internal func getCValue() -> LDKChannelMonitorUpdateStatus {');
204201
chai.expect(monitorUpdateStatusOutput).includes('case .Completed:');
205202
chai.expect(monitorUpdateStatusOutput).includes('return LDKChannelMonitorUpdateStatus_Completed');
@@ -238,7 +235,7 @@ describe('Generator Tests', () => {
238235
});
239236

240237
describe('Primitive Wrapper Generation Tests', () => {
241-
it('should generate LDKu5', () => {
238+
it('should generate LDKU5', () => {
242239
// only the new and get_claimable_balances methods are present
243240
// some parameters have been removed
244241
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
@@ -248,7 +245,7 @@ describe('Generator Tests', () => {
248245

249246
const generator = new PrimitiveWrapperGenerator(config, new AuxiliaryArtifacts());
250247

251-
const ldku5 = <RustPrimitiveWrapper>parser.glossary['LDKu5'];
248+
const ldku5 = <RustPrimitiveWrapper>parser.glossary['LDKU5'];
252249
const ldkStr = <RustPrimitiveWrapper>parser.glossary['LDKStr'];
253250
const thirtyTwoBytes = <RustPrimitiveWrapper>parser.glossary['LDKThirtyTwoBytes'];
254251
const transaction = <RustPrimitiveWrapper>parser.glossary['LDKTransaction'];
@@ -261,24 +258,26 @@ describe('Generator Tests', () => {
261258
chai.expect(transactionOutput).includes('public init(value: [UInt8]) {');
262259
chai.expect(transactionOutput).includes('public func getValue() -> [UInt8] {');
263260
chai.expect(transactionOutput)
264-
.includes('self.cType = LDKTransaction(data: dataContainer, datalen: UInt(value.count), data_is_owned: false)');
261+
.includes('self.cType = LDKTransaction(data: dataContainer, datalen: UInt(value.count), data_is_owned: true)');
265262

266263
chai.expect(thirtyTwoBytesOutput).includes('public init(value: [UInt8]) {');
267264
chai.expect(thirtyTwoBytesOutput).includes('public func getValue() -> [UInt8] {');
268265
chai.expect(thirtyTwoBytesOutput)
269-
.includes('self.cType = LDKThirtyTwoBytes(data: (value[0], value[1], value[2], value[3], value[4], value[5], value[6], value[7], value[8], value[9], value[10], value[11], value[12], value[13], value[14], value[15], value[16], value[17], value[18], value[19], value[20], value[21], value[22], value[23], value[24], value[25], value[26], value[27], value[28], value[29], value[30], value[31]))');
266+
.includes('self.cType = LDKThirtyTwoBytes(data: Bindings.arrayToUInt8Tuple32(array: value))');
267+
270268
chai.expect(thirtyTwoBytesOutput)
271-
.includes('return [self.cType!.data.0, self.cType!.data.1, self.cType!.data.2, self.cType!.data.3, self.cType!.data.4, self.cType!.data.5, self.cType!.data.6, self.cType!.data.7, self.cType!.data.8, self.cType!.data.9, self.cType!.data.10, self.cType!.data.11, self.cType!.data.12, self.cType!.data.13, self.cType!.data.14, self.cType!.data.15, self.cType!.data.16, self.cType!.data.17, self.cType!.data.18, self.cType!.data.19, self.cType!.data.20, self.cType!.data.21, self.cType!.data.22, self.cType!.data.23, self.cType!.data.24, self.cType!.data.25, self.cType!.data.26, self.cType!.data.27, self.cType!.data.28, self.cType!.data.29, self.cType!.data.30, self.cType!.data.31]');
269+
.includes('return Bindings.UInt8Tuple32ToArray(tuple: self.cType!.data)');
272270

273271
chai.expect(strOutput).includes('public init(value: String) {');
274272
chai.expect(strOutput).includes('public func getValue() -> String {');
275273
chai.expect(strOutput)
276-
.includes('self.cType = LDKStr(chars: Bindings.string_to_unsafe_uint8_pointer(string: value), len: UInt(value.count), chars_is_owned: false)');
274+
.includes('self.cType = LDKStr(chars: Bindings.string_to_unsafe_uint8_pointer(string: value), len: UInt(value.count), chars_is_owned: true)');
275+
277276
chai.expect(strOutput).includes('return String(data: data, encoding: .utf8)!');
278277

279278
chai.expect(u5Output).includes('public init(value: UInt8) {');
280279
chai.expect(u5Output).includes('public func getValue() -> UInt8 {');
281-
chai.expect(u5Output).includes('self.cType = LDKu5(_0: value)');
280+
chai.expect(u5Output).includes('self.cType = LDKU5(_0: value)');
282281
chai.expect(u5Output).includes('return self.cType!._0');
283282
});
284283
});

test/parser_tests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ describe('Parser Tests', () => {
392392
chai.assert(!thirtyTwoBytes.lengthField);
393393
chai.assert(!thirtyTwoBytes.ownershipField);
394394

395-
const u5 = glossary['LDKu5'];
395+
const u5 = glossary['LDKU5'];
396396
chai.assert(u5 instanceof RustPrimitiveWrapper);
397397
chai.expect(u5.dataField.contextualName).equals('_0');
398398
chai.assert(!u5.lengthField);

0 commit comments

Comments
 (0)