Skip to content

Commit 3810280

Browse files
committed
Separate type declarations, run_tests(areaLog, tests) for simulation, and run_all_tests() for testcases into separate files
1 parent 631fd23 commit 3810280

File tree

5 files changed

+104
-76
lines changed

5 files changed

+104
-76
lines changed

src/bundles/robot_minigame/functions.ts

Lines changed: 11 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,14 @@ import context from 'js-slang/context';
55
// type List
66
// } from 'js-slang/dist/stdlib/list';
77

8-
// A point (x, y)
9-
interface Point {
10-
x: number
11-
y: number
12-
}
13-
14-
// A point (x, y) with rotation
15-
interface PointWithRotation extends Point {
16-
rotation: number
17-
}
18-
19-
export interface Robot extends PointWithRotation {
20-
radius: number
21-
}
8+
import { run_tests } from './tests';
9+
import type {
10+
Point, PointWithRotation, Robot,
11+
Action,
12+
AreaFlags, Area,
13+
AreaTest,
14+
RobotMinigame
15+
} from './types';
2216

2317
// A line segment between p1 and p2
2418
interface LineSegment {
@@ -32,44 +26,6 @@ interface Ray {
3226
target: Point
3327
}
3428

35-
// A stored action
36-
export interface Action {
37-
type: 'begin' | 'move' | 'rotate' | 'sensor'
38-
position: PointWithRotation
39-
}
40-
41-
interface AreaFlags {
42-
[name: string]: any
43-
}
44-
45-
export interface Area {
46-
vertices: Point[]
47-
isObstacle: boolean
48-
flags: AreaFlags
49-
}
50-
51-
interface Test {
52-
type: string
53-
test: Function
54-
}
55-
56-
interface AreaTest extends Test {
57-
type: 'area'
58-
test: (areas: Area[]) => boolean
59-
}
60-
61-
export interface RobotMinigame {
62-
isInit: boolean
63-
width: number
64-
height: number
65-
robot: Robot
66-
areas: Area[]
67-
areaLog: Area[]
68-
actionLog: Action[]
69-
tests: Test[]
70-
message: string
71-
}
72-
7329
// Default state before initialisation
7430
const state: RobotMinigame = {
7531
isInit: false,
@@ -405,26 +361,8 @@ export function turn_right() {
405361
*
406362
* @returns if all tests pass
407363
*/
408-
export function run_tests() : boolean {
409-
// Run each test in order
410-
for (const test of state.tests) {
411-
// Store status in a variable
412-
let success: boolean;
413-
414-
switch(test.type) {
415-
case 'area':
416-
success = test.test(state.areaLog);
417-
break;
418-
default:
419-
success = true;
420-
}
421-
422-
// If the test fails, return false
423-
if (!success) return false;
424-
}
425-
426-
// If all tests pass, return true
427-
return true;
364+
export function run_all_tests() : boolean {
365+
return run_tests(state);
428366
}
429367

430368
// ==================
@@ -690,7 +628,7 @@ function logAction(
690628
type: 'begin' | 'move' | 'rotate' | 'sensor',
691629
position: PointWithRotation
692630
) {
693-
state.actionLog.push({type, position});
631+
state.actionLog.push({type, position} as Action);
694632
}
695633

696634
/**

src/bundles/robot_minigame/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ export {
1010
init, create_area, create_rect_area, create_obstacle, create_rect_obstacle, complete_init,
1111
get_distance, get_color,
1212
move_forward, move_forward_to_wall, rotate, turn_left, turn_right,
13-
should_enter_colors, run_tests
13+
should_enter_colors, run_all_tests
1414
} from './functions';
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type { Area, Test } from './types';
2+
3+
/**
4+
* Run the stored tests in state
5+
*
6+
* @returns if all tests pass
7+
*/
8+
export function run_tests({
9+
areaLog,
10+
tests
11+
}: {
12+
areaLog: Area[],
13+
tests: Test[]
14+
}) : boolean {
15+
// Run each test in order
16+
for (const test of tests) {
17+
// Store status in a variable
18+
let success: boolean;
19+
20+
switch(test.type) {
21+
case 'area':
22+
success = test.test(areaLog);
23+
break;
24+
default:
25+
success = true;
26+
}
27+
28+
// If the test fails, return false
29+
if (!success) return false;
30+
}
31+
32+
// If all tests pass, return true
33+
return true;
34+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// A point (x, y)
2+
export interface Point {
3+
x: number
4+
y: number
5+
}
6+
7+
// A point (x, y) with rotation
8+
export interface PointWithRotation extends Point {
9+
rotation: number
10+
}
11+
12+
// The robot with position and radius
13+
export interface Robot extends PointWithRotation {
14+
radius: number
15+
}
16+
17+
// A stored action
18+
export interface Action {
19+
type: 'begin' | 'move' | 'rotate' | 'sensor'
20+
position: PointWithRotation
21+
}
22+
23+
export interface AreaFlags {
24+
[name: string]: any
25+
}
26+
27+
export interface Area {
28+
vertices: Point[]
29+
isObstacle: boolean
30+
flags: AreaFlags
31+
}
32+
33+
export interface Test {
34+
type: string
35+
test: Function
36+
}
37+
38+
export interface AreaTest extends Test {
39+
type: 'area'
40+
test: (areas: Area[]) => boolean
41+
}
42+
43+
export interface RobotMinigame {
44+
isInit: boolean
45+
width: number
46+
height: number
47+
robot: Robot
48+
areas: Area[]
49+
areaLog: Area[]
50+
actionLog: Action[]
51+
tests: Test[]
52+
message: string
53+
}

src/tabs/RobotMaze/components/RobotSimulation.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { useEffect, useRef, useState } from 'react';
2-
import type { Area, Action, Robot, RobotMinigame } from '../../../bundles/robot_minigame/functions';
2+
import { run_tests } from '../../../bundles/robot_minigame/tests';
3+
import type { Area, Action, Robot, RobotMinigame } from '../../../bundles/robot_minigame/types';
34

45
/**
56
* Calculate the acute angle between 2 angles
@@ -134,6 +135,8 @@ const RobotSimulation : React.FC<MapProps> = ({
134135
robot: {radius: robotSize},
135136
areas,
136137
actionLog,
138+
areaLog,
139+
tests,
137140
message
138141
}
139142
}) => {
@@ -273,7 +276,7 @@ const RobotSimulation : React.FC<MapProps> = ({
273276
: animationStatus === 2
274277
? <button onClick={() => {setAnimationStatus(1);}}>Resume</button>
275278
: <button onClick={() => {setAnimationStatus(0);}}>Reset</button>}
276-
{animationStatus === 3 && <span style={{marginLeft: '5px'}}>{message}</span>}
279+
{animationStatus === 3 && <span style={{marginLeft: '5px'}}>{run_tests({tests, areaLog}) ? 'Success! 🎉' : message}</span>}
277280
</div>
278281
<div style={{display: 'flex', justifyContent: 'center'}}>
279282
<canvas ref={canvasRef}/>

0 commit comments

Comments
 (0)