Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
charredUtensil committed Dec 4, 2024
1 parent f96e4d9 commit 770c880
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 23 deletions.
169 changes: 168 additions & 1 deletion src/core/architects/utils/script.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { sanitizeString } from "./script";
import goldenTest from "../../test_utils/golden";
import { PreprogrammedCavern } from "../../transformers/04_ephemera/03_preprogram";
import { mkScriptBuilder, mkVars, sanitizeString } from "./script";

describe("escapeString", () => {
it("handles the empty string", () => {
Expand All @@ -23,3 +25,168 @@ describe("escapeString", () => {
);
});
});

describe("ScriptBuilder", () => {
const cavern = {} as PreprogrammedCavern;

it("declares variables", () => {
const sb = mkScriptBuilder(cavern);
sb.declareArrow('arw');
sb.declareBuilding('bld');
sb.declareCreature('cr');
sb.declareInt('num', 42);
sb.declareString('str', 'Message!');
expect(sb.build()).toEqual(`\
arrow arw
building bld
creature cr
int num=42
string str="Message!"`);
});

it("declares an event", () => {
const sb = mkScriptBuilder(cavern);
sb.event('foo', 'crystals+=1;');
expect(sb.build()).toEqual(`\
foo::;
crystals+=1;
`);
});

it("declares a simple if trigger", () => {
const sb = mkScriptBuilder(cavern);
sb.if('crystals>10', 'crystals-=5;');
expect(sb.build()).toEqual("if(crystals>10)[crystals-=5]");
});


it("declares a simple when trigger", () => {
const sb = mkScriptBuilder(cavern);
sb.when('crystals>50', 'crystals-=1;');
expect(sb.build()).toEqual("when(crystals>50)[crystals-=1]");
});

it("creates an anonymous event chain to handle multiple statements", () => {
const sb = mkScriptBuilder(cavern);
sb.if('crystals>10', 'wait:5;', 'crystals-=5;');
expect(sb.build()).toEqual(`\
if(crystals>10)[t0]
t0::;
wait:5;
crystals-=5;
`);
});

it("creates an anonymous event chain to handle repeated triggers", () => {
const sb = mkScriptBuilder(cavern);
sb.when('ore>50', 'ore-=20;');
sb.when('ore>50', 'crystals+=1;');
expect(sb.build()).toEqual(`\
when(ore>50)[tw0]
tw0::;
ore-=20;
crystals+=1;
`);
});

it("creates anonymous event chains to handle repeated triggers with multiple statements", () => {
const sb = mkScriptBuilder(cavern);
sb.when('ore>50', 'ore-=20;');
sb.when('ore>50', 'wait:5;', 'crystals+=1;');
sb.when('ore>50', 'wait:10;', 'crystals+=1;');
expect(sb.build()).toEqual(`\
when(ore>50)[tw2]
tw2::;
ore-=20;
t0;
t1;
t0::;
wait:5;
crystals+=1;
t1::;
wait:10;
crystals+=1;
`);
});

it("handles if and when triggers on the same condition 1", () => {
const sb = mkScriptBuilder(cavern);
sb.if('ore>50', 'msg:msgFirstAlchemy;');
sb.when('ore>50', 'ore-=20;', 'wait:5;', 'crystals+=1;');
expect(sb.build()).toEqual(`\
int tf0=0
if(tf0>0)[msg:msgFirstAlchemy]
when(ore>50)[tw2]
tw2::;
tf0=1;
t1;
t1::;
ore-=20;
wait:5;
crystals+=1;
`);
});

it("handles if and when triggers on the same condition 2", () => {
const sb = mkScriptBuilder(cavern);
sb.if('ore>50', 'shake:1;', 'msg:msgFirstAlchemy;');
sb.when('ore>50', 'ore-=20;');
expect(sb.build()).toEqual(`\
int tf0=0
if(tf0>0)[t1]
t1::;
shake:1;
msg:msgFirstAlchemy;
when(ore>50)[tw2]
tw2::;
tf0=1;
ore-=20;
`);
});

goldenTest("fizzbuzz", () => {
// This code is bad but idk - it's more about testing the builder.

const sb = mkScriptBuilder(cavern);
const v = mkVars('fbz', [
'n', 'lock', 'm', 'three', 'five', 'msgFizz', 'msgBuzz', 'msgFizzBuzz'
]);
sb.declareInt(v.n, 0);
sb.declareInt(v.m, 0);
sb.declareInt(v.lock, 0);
sb.declareInt(v.three, 0);
sb.declareInt(v.five, 0);

sb.when(
'enter:1,1',
`${v.lock}+=1;`,
);
sb.when(
`${v.lock}==1`,

`pan:${v.three},${v.five};`,

`${v.n}+=1;`,
`${v.m}=0;`,

`${v.three}+=1;`,
`((${v.three}==3))${v.m}=1;`,
`((${v.three}>=3))${v.three}-=3;`,

`${v.five}+=1;`,
`((${v.five}==5))${v.m}+=2;`,
`((${v.five}>=5))${v.five}-=5;`,

`((${v.m}==1))msg:${sb.declareString(v.msgFizz, 'Fizz')};`,
`((${v.m}==2))msg:${sb.declareString(v.msgBuzz, 'Buzz')};`,
`((${v.m}==3))msg:${sb.declareString(v.msgFizzBuzz, 'Fizz Buzz')};`,

`${v.lock}=0;`,
);
return sb.build();
})
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const updateGoldenFile = async (filePath: string, actual: string) => {
};

const goldenTest = async (name: string, fn: () => string) => {
const filePath = path.resolve(__dirname, `${GOLDEN_DIR}/${name}.dat`);
const filePath = path.resolve(__dirname, GOLDEN_DIR, name);
test(name, async () => {
const actual = fn();
if (process.env[UPDATE_GOLDENS]) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
comments{
Cavern generated by groundHog [VERSION]
https://github.com/charredUtensil/groundhog
initialContext: {}
{
"initialContext": {}
}
}
info{
rowcount:14
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
comments{
Cavern generated by groundHog [VERSION]
https://github.com/charredUtensil/groundhog
initialContext: {}
{
"initialContext": {}
}
}
info{
rowcount:14
Expand Down
24 changes: 24 additions & 0 deletions src/core/test_utils/goldens/fizzbuzz
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
int fbz_n=0
int fbz_m=0
int fbz_lock=0
int fbz_three=0
int fbz_five=0
string fbz_msgFizz="Fizz"
string fbz_msgBuzz="Buzz"
string fbz_msgFizzBuzz="Fizz Buzz"
when(enter:1,1)[fbz_lock+=1]
when(fbz_lock==1)[t0]
t0::;
pan:fbz_three,fbz_five;
fbz_n+=1;
fbz_m=0;
fbz_three+=1;
((fbz_three==3))fbz_m=1;
((fbz_three>=3))fbz_three-=3;
fbz_five+=1;
((fbz_five==5))fbz_m+=2;
((fbz_five>=5))fbz_five-=5;
((fbz_m==1))msg:fbz_msgFizz;
((fbz_m==2))msg:fbz_msgBuzz;
((fbz_m==3))msg:fbz_msgFizzBuzz;
fbz_lock=0;
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
comments{
Cavern generated by groundHog [VERSION]
https://github.com/charredUtensil/groundhog
initialContext: {}
{
"initialContext": {}
}
}
info{
rowcount:6
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EAST, NORTH, Point, SOUTH, WEST } from "../common/geometry";
import { MutableGrid } from "../common/grid";
import { EAST, NORTH, Point, SOUTH, WEST } from "../../common/geometry";
import { MutableGrid } from "../../common/grid";
import {
CANTEEN,
DOCKS,
Expand All @@ -12,8 +12,8 @@ import {
TELEPORT_PAD,
TOOL_STORE,
UPGRADE_STATION,
} from "../models/building";
import { Cavern } from "../models/cavern";
} from "../../models/building";
import { Cavern } from "../../models/cavern";
import {
BAT,
CreatureFactory,
Expand All @@ -22,16 +22,16 @@ import {
ROCK_MONSTER,
SLIMY_SLUG,
SMALL_SPIDER,
} from "../models/creature";
import { Erosion, Landslide } from "../models/hazards";
import { MinerFactory } from "../models/miner";
import { atCenterOfTile, position } from "../models/position";
import { Tile } from "../models/tiles";
import discover from "../transformers/03_plastic/01_discover";
import fence from "../transformers/03_plastic/00_fence";
import serialize from "../transformers/04_ephemera/05_serialize";
import goldenTest from "./golden";
import strataflux from "../transformers/03_plastic/03_strataflux";
} from "../../models/creature";
import { Erosion, Landslide } from "../../models/hazards";
import { MinerFactory } from "../../models/miner";
import { atCenterOfTile, position } from "../../models/position";
import { Tile } from "../../models/tiles";
import discover from "../03_plastic/01_discover";
import fence from "../03_plastic/00_fence";
import serialize from "./05_serialize";
import goldenTest from "../../test_utils/golden";
import strataflux from "../03_plastic/03_strataflux";
import {
CARGO_CARRIER,
CHROME_CRUSHER,
Expand All @@ -46,7 +46,7 @@ import {
TUNNEL_SCOUT,
TUNNEL_TRANSPORT,
VehicleFactory,
} from "../models/vehicle";
} from "../../models/vehicle";

function fill<T>(
grid: MutableGrid<T>,
Expand Down Expand Up @@ -107,7 +107,7 @@ function ds(args: Partial<Cavern>) {
).serialized.replace(/groundHog v\S+/, "groundHog [VERSION]");
}

goldenTest("mvp", () => {
goldenTest("mvp.dat", () => {
const crystals = new MutableGrid<number>();
const tiles = new MutableGrid<Tile>();
const openCaveFlags = new MutableGrid<true>();
Expand All @@ -133,7 +133,7 @@ goldenTest("mvp", () => {
});
});

goldenTest("building_zoo", () => {
goldenTest("building_zoo.dat", () => {
const tiles = new MutableGrid<Tile>();
const openCaveFlags = new MutableGrid<true>();

Expand Down Expand Up @@ -200,7 +200,7 @@ goldenTest("building_zoo", () => {
});
});

goldenTest("entity_zoo", () => {
goldenTest("entity_zoo.dat", () => {
const tiles = new MutableGrid<Tile>();
const openCaveFlags = new MutableGrid<true>();

Expand Down

0 comments on commit 770c880

Please sign in to comment.