Skip to content

Commit

Permalink
2024/01/13 - Tests for JavaScript generators/iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
FadiShawki committed Jan 13, 2024
1 parent f49d87e commit 09741c6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
36 changes: 36 additions & 0 deletions src/@orbitmines/explorer/Ray.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,42 @@ describe("Ray", () => {
expect(current.next.previous.next.next.previous.self.any.js).toBe('B');
expect(current.next.previous.next.next.previous.next.self.any.js).toBe('C');
});
test("[A, B, C][.as_array, ...]", () => {
const A = Ray.vertex().o({ js: 'A' }).as_reference().o({ js: 'A.#' });
const B = Ray.vertex().o({ js: 'B' }).as_reference().o({ js: 'B.#' });
const C = Ray.vertex().o({ js: 'C' }).as_reference().o({ js: 'C.#' });

A.continues_with(B).continues_with(C);

expect(A.as_array().map(ref => ref.self.any.js)).toEqual(['A', 'B', 'C']);
expect(B.as_array().map(ref => ref.self.any.js)).toEqual(['B', 'C']); // TODO: This may or may not be expected behavior, you could make a case for saying it should render both sides for .as_array. ???
expect(C.as_array().map(ref => ref.self.any.js)).toEqual(['C']);

expect([...A].map(ref => ref.self.any.js)).toEqual(['A', 'B', 'C']);
expect([...A.traverse()].map(ref => ref.self.any.js)).toEqual(['A', 'B', 'C']);
expect([...A.as_generator()].map(ref => ref.self.any.js)).toEqual(['A', 'B', 'C']);
expect(A.as_string()).toBe('A,B,C');

{

const iterator = A.as_iterator();

let array: Ray[] = [];

while (true) {
let current = iterator.next();
if (current.done)
break;

array.push(current.value);
}

expect(array.map(ref => ref.self.any.js)).toEqual(['A', 'B', 'C']);

}

// TODO async_generator / async_iterator / for await *
});
// test(".next(ref => .continues_with(.vertex.#))", () => {
// let A = Ray.vertex().o({ js: 'A' }).as_reference();
// let B = Ray.vertex().o({ js: 'B'}).as_reference();
Expand Down
28 changes: 23 additions & 5 deletions src/@orbitmines/explorer/Ray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ export class Ray // Other possibly names: AbstractDirectionality, ..., ??
}),

// TODO: Possibly follow infintely
[RayType.REFERENCE]: () => { throw new NotImplementedError(); }
[RayType.REFERENCE]: (ref) => { throw new NotImplementedError(ref.self.type); }
}),

})
Expand Down Expand Up @@ -596,7 +596,25 @@ export class Ray // Other possibly names: AbstractDirectionality, ..., ??

// ___compute = ()

*traverse(): Generator<Ray> {}
*traverse(direction: ((ref: Ray) => Ray) = ((ref): Ray => ref.next)): Generator<Ray> {
// TODO: Also to ___func??

if (this.type !== RayType.VERTEX)
throw new NotImplementedError();

let current: Ray = this;

while (true) {
yield current;

try {
current = current.next;
} catch (e) {
console.error('stopped traversal through implementation error...', e)
break; // TODO: HACKY FOR NOW
}
}
}

/**
* JavaScript, possible compilations - TODO: Could have enumeratd possibilities, but just ignore that for now.
Expand All @@ -614,10 +632,10 @@ export class Ray // Other possibly names: AbstractDirectionality, ..., ??
// JS.AsyncIterator
as_iterator = (): Iterator<Ray> => this.as_generator();
// JS.Array
as_array = (): any[] => [...this];
as_array = (): Ray[] => [...this];
// JS.String
toString = (): string => this.as_array().toString();
as_string = () => this.toString();
toString = (): string => this.as_string();
as_string = (): string => this.as_array().map(ref => ref.self.any.js).join(','); // TODO: PROPER

as_int = (): number => { throw new NotImplementedError(); }
as_number = this.as_int;
Expand Down

0 comments on commit 09741c6

Please sign in to comment.