Skip to content

Commit

Permalink
Refactor variable selection functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Tatehito committed Feb 20, 2024
1 parent 83b4f9a commit 6025e33
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
14 changes: 10 additions & 4 deletions src/app/lib/solveProblem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ export function executeEval(command: string): (CharacterVariable | Variable)[] {
return result;
}

export function selectCharacterVariables(variables: (CharacterVariable | Variable)[]): CharacterVariable[] {
return variables.filter((variable) => variable.value instanceof CharacterClass) as CharacterVariable[];
}

export function selectOtherVariables(variables: (CharacterVariable | Variable)[]): Variable[] {
return variables.filter((variable) => !(variable.value instanceof CharacterClass)) as Variable[];
}

export function extractVariableNames(command: string): string[] {
// 'const' 'let' 'var' で始まる変数名を comand から抽出する
const regex = /(?:const|let|var)\s+(\w+)\s*=\s*(.*?);/g;
Expand Down Expand Up @@ -57,10 +65,8 @@ export function solveProblem(program: string): SolveProblemResult {
}

const variables = executeEval(mergedCommand);
const characterVariables = variables.filter(
(variable) => variable.value instanceof CharacterClass
) as CharacterVariable[];
const otherVariables = variables.filter((variable) => !(variable.value instanceof CharacterClass)) as Variable[];
const characterVariables = selectCharacterVariables(variables);
const otherVariables = selectOtherVariables(variables);

const board = new BoardClass();
for (const history of histories) {
Expand Down
33 changes: 20 additions & 13 deletions tests/solveProblem.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { expect, test } from 'vitest';

import { parseProgram, extractVariables, executeEval, solveProblem } from '../src/app/lib/solveProblem';
import {
parseProgram,
extractVariableNames,
executeEval,
solveProblem,
selectCharacterVariables,
} from '../src/app/lib/solveProblem';

test('Parse a program', () => {
const program = `
Expand All @@ -25,7 +31,6 @@ test('Parse a program', () => {
});

test('Extract variables', () => {
const variableName = 'character';
const command = `
const character1 = new Character();
const character2 = new Character();
Expand All @@ -38,7 +43,7 @@ test('Extract variables', () => {
character4.moveForward();
character5.moveForward();
`;
const characterVariables = extractVariables(variableName, command);
const characterVariables = extractVariableNames(command);

expect(characterVariables).not.toBeFalsy();
expect(characterVariables).toEqual(['character1', 'character2', 'character3', 'character4', 'character5']);
Expand All @@ -53,11 +58,12 @@ test('Execute eval (1character)', () => {
character1.moveForward();
character1.moveForward();
`;
const characters = executeEval(command);
const variables = executeEval(command);
const characterVariables = selectCharacterVariables(variables);

expect(characters).not.toBeFalsy();
expect(characters[0].value.x).toEqual(1);
expect(characters[0].value.y).toEqual(6);
expect(characterVariables).not.toBeFalsy();
expect(characterVariables[0].value.x).toEqual(1);
expect(characterVariables[0].value.y).toEqual(6);
});

test('Execute eval (2characters)', () => {
Expand All @@ -69,13 +75,14 @@ test('Execute eval (2characters)', () => {
character2.moveForward();
character2.moveForward();
`;
const characters = executeEval(command);
const variables = executeEval(command);
const characterVariables = selectCharacterVariables(variables);

expect(characters).not.toBeFalsy();
expect(characters[0].value.x).toEqual(1);
expect(characters[0].value.y).toEqual(3);
expect(characters[1].value.x).toEqual(2);
expect(characters[1].value.y).toEqual(3);
expect(characterVariables).not.toBeFalsy();
expect(characterVariables[0].value.x).toEqual(1);
expect(characterVariables[0].value.y).toEqual(3);
expect(characterVariables[1].value.x).toEqual(2);
expect(characterVariables[1].value.y).toEqual(3);
});

test('Solve a problem (1character)', () => {
Expand Down

0 comments on commit 6025e33

Please sign in to comment.