|
1 | 1 | import * as assert from 'assert'; |
2 | 2 | import { describe, expect, test, vi, beforeEach, afterEach } from 'vitest'; |
3 | 3 | import { Parser, Interpreter, values, errors, utils, Ast } from '../src'; |
| 4 | +import { FALSE, NUM, OBJ, STR, TRUE, Value } from '../src/interpreter/value'; |
4 | 5 |
|
5 | 6 | let { FN_NATIVE } = values; |
6 | 7 | let { AiScriptRuntimeError, AiScriptIndexOutOfRangeError, AiScriptHostsideError } = errors; |
@@ -324,3 +325,61 @@ describe('pause', () => { |
324 | 325 | }); |
325 | 326 | }); |
326 | 327 | }); |
| 328 | + |
| 329 | +describe('Attribute', () => { |
| 330 | + const getAttr = async (name: string, script: string): Promise<Value['attr']> => { |
| 331 | + const parser = new Parser(); |
| 332 | + const interpreter = new Interpreter({}); |
| 333 | + const ast = parser.parse(script); |
| 334 | + await interpreter.exec(ast); |
| 335 | + const value = interpreter.scope.get(name); |
| 336 | + return value.attr; |
| 337 | + }; |
| 338 | + |
| 339 | + test.concurrent('no attribute', async () => { |
| 340 | + const attr = await getAttr('f', ` |
| 341 | + @f() {} |
| 342 | + `); |
| 343 | + expect(attr).toBeUndefined(); |
| 344 | + }); |
| 345 | + |
| 346 | + test.concurrent('single attribute', async () => { |
| 347 | + const attr = await getAttr('f', ` |
| 348 | + #[x 42] |
| 349 | + @f() {} |
| 350 | + `); |
| 351 | + expect(attr).toStrictEqual([{ name: 'x', value: NUM(42) }]); |
| 352 | + }); |
| 353 | + |
| 354 | + test.concurrent('multiple attributes', async () => { |
| 355 | + const attr = await getAttr('f', ` |
| 356 | + #[o { a: 1, b: 2 }] |
| 357 | + #[s "ai"] |
| 358 | + #[b false] |
| 359 | + @f() {} |
| 360 | + `); |
| 361 | + expect(attr).toStrictEqual([ |
| 362 | + { name: 'o', value: OBJ(new Map([['a', NUM(1)], ['b', NUM(2)]])) }, |
| 363 | + { name: 's', value: STR('ai') }, |
| 364 | + { name: 'b', value: FALSE }, |
| 365 | + ]); |
| 366 | + }); |
| 367 | + |
| 368 | + test.concurrent('single attribute without value', async () => { |
| 369 | + const attr = await getAttr('f', ` |
| 370 | + #[x] |
| 371 | + @f() {} |
| 372 | + `); |
| 373 | + expect(attr).toStrictEqual([{ name: 'x', value: TRUE }]); |
| 374 | + }); |
| 375 | + |
| 376 | + test.concurrent('attribute under namespace', async () => { |
| 377 | + const attr = await getAttr('Ns:f', ` |
| 378 | + :: Ns { |
| 379 | + #[x 42] |
| 380 | + @f() {} |
| 381 | + } |
| 382 | + `); |
| 383 | + expect(attr).toStrictEqual([{ name: 'x', value: NUM(42) }]); |
| 384 | + }); |
| 385 | +}); |
0 commit comments