-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add file with implementation of const, remove const parts from let.ts extract const and assign from let.ts, extract tests for const and assign from let tests
- Loading branch information
1 parent
58a8383
commit 7dd2b2b
Showing
7 changed files
with
169 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { runUntyped } from '../../..'; | ||
import { parse } from '../../../../askcode'; | ||
import * as core from '../../core'; | ||
import { assignRes } from '../assign'; | ||
|
||
const values = {}; | ||
|
||
function ask(code: string) { | ||
return runUntyped( | ||
{ | ||
resources: { ...core, assignRes }, | ||
values, | ||
}, | ||
parse(code) | ||
); | ||
} | ||
|
||
describe(`assign`, function () { | ||
it(`should assign value with the assign resource`, async function () { | ||
const initialValue = 6; | ||
const assignedValue = 4; | ||
const resolvedValue = await ask( | ||
`ask(let('a',${initialValue}),assign('a',${assignedValue}))` | ||
); | ||
expect(resolvedValue).toEqual(assignedValue); | ||
}); | ||
}); |
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,43 @@ | ||
import { runUntyped } from '../../..'; | ||
import { parse } from '../../../../askcode'; | ||
import * as core from '../../core'; | ||
import { constRes } from '../const'; | ||
|
||
const values = {}; | ||
|
||
function ask(code: string) { | ||
return runUntyped( | ||
{ | ||
resources: { ...core, constRes }, | ||
values, | ||
}, | ||
parse(code) | ||
); | ||
} | ||
|
||
describe(`const`, function () { | ||
it(`should assign value to a const variable on initialization`, async function () { | ||
const value = 6; | ||
await expect( | ||
ask(`ask(const('a',${value}),call(get('get'), 'a'))`) | ||
).resolves.toEqual(value); | ||
}); | ||
|
||
it(`should throw when trying to assign to const`, async function () { | ||
const value = 6; | ||
await expect( | ||
ask(`ask(const('a',${value}),call(get('get'),assign('a',4),'a'))`) | ||
).rejects.toThrow(`Cannot assign to a constant variable "a"`); | ||
}); | ||
|
||
const reservedWords = ['resources']; | ||
|
||
it(`should not allow reserved words as constant name`, async function () { | ||
for (let i = 0; i < reservedWords.length; i++) { | ||
const reservedWord = reservedWords[i]; | ||
await expect(ask(`ask(const('resources',6))`)).rejects.toThrow( | ||
`Key "${reservedWord}" cannot be redeclared` | ||
); | ||
} | ||
}); | ||
}); |
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,52 @@ | ||
import { | ||
any, | ||
resource, | ||
run, | ||
runUntyped, | ||
Options, | ||
TypedValue, | ||
JSONable, | ||
} from '../../lib'; | ||
|
||
function assignValue( | ||
valueObject: { [key: string]: any }, | ||
key: string, | ||
assignedValue: any | ||
) { | ||
if (Object.isFrozen(valueObject[key])) | ||
throw new Error(`Cannot assign to a constant variable "${key}"`); | ||
valueObject[key] = assignedValue; | ||
} | ||
|
||
export const assignRes = resource({ | ||
type: any, | ||
async compute(options, code, args): Promise<TypedValue<JSONable>> { | ||
const { params: children = [] } = code; | ||
|
||
const key: any = await runUntyped(options, children[0]); // FIXME any | ||
const value = await run(options, children[1]); | ||
|
||
if (key === 'resources') { | ||
throw new Error(`Key "resources" cannot be redeclared`); | ||
} | ||
|
||
if (code.name === 'assign') { | ||
for ( | ||
let prototype: Options | undefined = options; | ||
prototype; | ||
prototype = prototype?.prototype | ||
) { | ||
const { values = {} } = prototype; | ||
if (Object.prototype.hasOwnProperty.call(values, key)) { | ||
assignValue(values, key, value); | ||
return value; | ||
} | ||
} | ||
throw new Error(`Cannot assign to an unknown variable "${key}"`); | ||
} | ||
|
||
options.values![key] = value; | ||
|
||
return value; | ||
}, | ||
}); |
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,42 @@ | ||
import { | ||
any, | ||
resource, | ||
run, | ||
runUntyped, | ||
Options, | ||
TypedValue, | ||
JSONable, | ||
} from '../../lib'; | ||
|
||
function assignValue( | ||
valueObject: { [key: string]: any }, | ||
key: string, | ||
assignedValue: any | ||
) { | ||
if (Object.isFrozen(valueObject[key])) | ||
throw new Error(`Cannot assign to a constant variable "${key}"`); | ||
valueObject[key] = assignedValue; | ||
} | ||
|
||
export const constRes = resource({ | ||
type: any, | ||
async compute(options, code, args): Promise<TypedValue<JSONable>> { | ||
console.log(options); | ||
console.log(code); | ||
const { params: children = [] } = code; | ||
|
||
const key: any = await runUntyped(options, children[0]); // FIXME any | ||
const value = await run(options, children[1]); | ||
|
||
if (key === 'resources') { | ||
throw new Error(`Key "resources" cannot be redeclared`); | ||
} | ||
|
||
options.values![key] = value; | ||
|
||
if (code.name === 'const') { | ||
Object.freeze(options.values![key]); | ||
} | ||
return value; | ||
}, | ||
}); |
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