Skip to content

Commit

Permalink
fix: more test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mati365 committed Dec 30, 2023
1 parent 1e604e6 commit d465ee5
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 9 deletions.
23 changes: 18 additions & 5 deletions packages/compiler-pico-c/tests/analyze/access.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand All @@ -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();
});
});
8 changes: 5 additions & 3 deletions packages/compiler-pico-c/tests/analyze/initializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ declare global {
namespace jest {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Matchers<R = void> {
toHaveCompilerError(errorCode?: number): MatcherResult;
toHaveCompilerError(errorCode?: number | string): MatcherResult;
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions packages/compiler-pico-c/tests/ir/declarations/scope.test.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
51 changes: 51 additions & 0 deletions packages/compiler-pico-c/tests/ir/utils/irMatcher.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -16,6 +17,7 @@ declare global {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface Matchers<R = void, T = {}> {
toCompiledIRBeEqual(ir: string): MatcherResult;
toHaveIRError(errorCode?: number | string): MatcherResult;
}
}
}
Expand Down Expand Up @@ -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) || '<any>'
} 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,
});

0 comments on commit d465ee5

Please sign in to comment.