From d465ee5f629cfa96462e34e61ed0c0c201b38cb0 Mon Sep 17 00:00:00 2001 From: Mateusz Baginski Date: Sat, 30 Dec 2023 15:17:26 +0100 Subject: [PATCH] fix: more test fixes --- .../tests/analyze/access.test.ts | 23 +++++++-- .../tests/analyze/initializer.test.ts | 8 +-- .../tests/analyze/utils/analyzeMatcher.ts | 2 +- .../tests/ir/declarations/scope.test.ts | 10 ++++ .../tests/ir/utils/irMatcher.ts | 51 +++++++++++++++++++ 5 files changed, 85 insertions(+), 9 deletions(-) diff --git a/packages/compiler-pico-c/tests/analyze/access.test.ts b/packages/compiler-pico-c/tests/analyze/access.test.ts index a425329f..aedf29f5 100644 --- a/packages/compiler-pico-c/tests/analyze/access.test.ts +++ b/packages/compiler-pico-c/tests/analyze/access.test.ts @@ -14,19 +14,23 @@ describe('Variable access', () => { test('access nested array struct item to int', () => { expect(/* cpp */ ` - struct Vec2 { - int x, y; - struct Rect { int z; } nested[2]; - } abc = { .x = 5 }; + struct Vec2 { + int x, y; + struct Rect { int z; } nested[2]; + } abc = { .x = 5 }; + void main() { int acc = abc.nested[0].z + 4; + } `).not.toHaveCompilerError(); }); test('array like access to array variables', () => { expect(/* cpp */ ` + void main() { int numbers[] = { 1, 2, 3 }; int item = numbers[1]; + } `).not.toHaveCompilerError(); }); @@ -53,10 +57,19 @@ describe('Variable access', () => { `).toHaveCompilerError(CTypeCheckErrorCode.WRONG_NON_STRUCT_FIELD_ACCESS); }); - test('array like access to pointer variables', () => { + test('array like access to pointer local variables', () => { expect(/* cpp */ ` + void main() { const char* str = "Hello world"; char item = str[1]; + } `).not.toHaveCompilerError(); }); + + test('array like access to pointer global variables', () => { + expect(/* cpp */ ` + const char* str = "Hello world"; + char item = str[1]; + `).toHaveCompilerError(); + }); }); diff --git a/packages/compiler-pico-c/tests/analyze/initializer.test.ts b/packages/compiler-pico-c/tests/analyze/initializer.test.ts index 93f844c1..07d1e6d9 100644 --- a/packages/compiler-pico-c/tests/analyze/initializer.test.ts +++ b/packages/compiler-pico-c/tests/analyze/initializer.test.ts @@ -280,10 +280,12 @@ describe('Initializer typecheck', () => { `).not.toHaveCompilerError(); }); - test('dynamic variable initializer', () => { + test('dynamic variable initializer in local variables', () => { expect(/* cpp */ ` - int d = 5; - int acc = d + 4; + void main() { + int d = 5; + int acc = d + 4; + } `).not.toHaveCompilerError(); }); diff --git a/packages/compiler-pico-c/tests/analyze/utils/analyzeMatcher.ts b/packages/compiler-pico-c/tests/analyze/utils/analyzeMatcher.ts index 0c47b96c..8900dd18 100644 --- a/packages/compiler-pico-c/tests/analyze/utils/analyzeMatcher.ts +++ b/packages/compiler-pico-c/tests/analyze/utils/analyzeMatcher.ts @@ -13,7 +13,7 @@ declare global { namespace jest { // eslint-disable-next-line @typescript-eslint/no-unused-vars interface Matchers { - toHaveCompilerError(errorCode?: number): MatcherResult; + toHaveCompilerError(errorCode?: number | string): MatcherResult; } } } diff --git a/packages/compiler-pico-c/tests/ir/declarations/scope.test.ts b/packages/compiler-pico-c/tests/ir/declarations/scope.test.ts index a1f017d3..b06c15aa 100644 --- a/packages/compiler-pico-c/tests/ir/declarations/scope.test.ts +++ b/packages/compiler-pico-c/tests/ir/declarations/scope.test.ts @@ -1,6 +1,16 @@ +import { IRErrorCode } from '../../../src/frontend/ir/errors/IRError'; import '../utils'; describe('Declaration scope', () => { + test('dynamic variable initializer', () => { + expect(/* cpp */ ` + int d = 5; + int acc = d + 4; + `).toHaveIRError( + IRErrorCode.GLOBAL_INITIALIZER_MUST_HAVE_ONLY_CONSTANT_EXPRESSIONS, + ); + }); + test('should be possible to shadow variable name', () => { expect(/* cpp */ ` void main() { diff --git a/packages/compiler-pico-c/tests/ir/utils/irMatcher.ts b/packages/compiler-pico-c/tests/ir/utils/irMatcher.ts index 50f63b14..1b5056f4 100644 --- a/packages/compiler-pico-c/tests/ir/utils/irMatcher.ts +++ b/packages/compiler-pico-c/tests/ir/utils/irMatcher.ts @@ -1,5 +1,6 @@ import stripAnsi from 'strip-ansi'; import * as E from 'fp-ts/Either'; +import * as R from 'ramda'; import { stripNonPrintableCharacters, trimLines } from '@ts-c-compiler/core'; @@ -16,6 +17,7 @@ declare global { // eslint-disable-next-line @typescript-eslint/no-unused-vars interface Matchers { toCompiledIRBeEqual(ir: string): MatcherResult; + toHaveIRError(errorCode?: number | string): MatcherResult; } } } @@ -61,6 +63,55 @@ function toCompiledIRBeEqual( }; } +function toHaveIRError(received: string, code?: string): MatcherResult { + const parseResult = cIRCompiler()(received as string); + + if (E.isRight(parseResult)) { + return { + pass: false, + message: () => + `expected err code to be equal ${ + this.utils.printExpected(code) || '' + } but result is ok!`, + }; + } + + const err = parseResult.left; + const pass = (() => { + if (R.isNil(code)) { + return true; + } + + return this.equals( + err, + expect.arrayContaining([ + expect.objectContaining({ + code, + }), + ]), + ); + })(); + + if (pass) { + return { + pass, + message: () => + `expected err code ${this.utils.printReceived( + err[0].code, + )} to be equal ${this.utils.printExpected(code)}`, + }; + } + + return { + pass, + message: () => + `expected err code ${this.utils.printReceived( + err[0].code, + )} to not be equal ${this.utils.printExpected(code)}`, + }; +} + expect.extend({ toCompiledIRBeEqual, + toHaveIRError, });