Add resources tests
This commit is contained in:
parent
fb4ac6424c
commit
6bd04ee6e2
@ -16,7 +16,7 @@ export class GatheringTimings {
|
|||||||
this.crop = crop;
|
this.crop = crop;
|
||||||
}
|
}
|
||||||
|
|
||||||
get common(): GatheringTime {
|
private get common(): GatheringTime {
|
||||||
const xs = [this.lumber, this.clay, this.iron, this.crop];
|
const xs = [this.lumber, this.clay, this.iron, this.crop];
|
||||||
return xs.reduce((m, t) => (m === 'never' || t === 'never' ? 'never' : Math.max(m, t)), 0);
|
return xs.reduce((m, t) => (m === 'never' || t === 'never' ? 'never' : Math.max(m, t)), 0);
|
||||||
}
|
}
|
||||||
|
@ -27,11 +27,11 @@ function calcNeedResources(
|
|||||||
totalRequired: Resources,
|
totalRequired: Resources,
|
||||||
storage: ResourceStorage
|
storage: ResourceStorage
|
||||||
): Resources {
|
): Resources {
|
||||||
if (!current.gt(required)) {
|
if (current.lt(required)) {
|
||||||
return required.sub(current);
|
return required.sub(current);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!current.gt(totalRequired)) {
|
if (current.lt(totalRequired)) {
|
||||||
return totalRequired.sub(current);
|
return totalRequired.sub(current);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,21 +86,13 @@ export class Resources implements ResourcesInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
lt(other: ResourcesInterface): boolean {
|
lt(other: ResourcesInterface): boolean {
|
||||||
return this.lumber < other.lumber && this.clay < other.clay && this.iron < other.iron && this.crop < other.crop;
|
return this.lumber < other.lumber || this.clay < other.clay || this.iron < other.iron || this.crop < other.crop;
|
||||||
}
|
}
|
||||||
|
|
||||||
gt(other: ResourcesInterface): boolean {
|
gt(other: ResourcesInterface): boolean {
|
||||||
return this.lumber > other.lumber && this.clay > other.clay && this.iron > other.iron && this.crop > other.crop;
|
return this.lumber > other.lumber && this.clay > other.clay && this.iron > other.iron && this.crop > other.crop;
|
||||||
}
|
}
|
||||||
|
|
||||||
lte(other: ResourcesInterface): boolean {
|
|
||||||
return !this.gt(other);
|
|
||||||
}
|
|
||||||
|
|
||||||
gte(other: ResourcesInterface): boolean {
|
|
||||||
return !this.lt(other);
|
|
||||||
}
|
|
||||||
|
|
||||||
min(other: ResourcesInterface): Resources {
|
min(other: ResourcesInterface): Resources {
|
||||||
return new Resources(
|
return new Resources(
|
||||||
Math.min(this.lumber, other.lumber),
|
Math.min(this.lumber, other.lumber),
|
||||||
|
@ -2,25 +2,21 @@ import { it, describe } from 'mocha';
|
|||||||
import { expect } from 'chai';
|
import { expect } from 'chai';
|
||||||
|
|
||||||
import { Resources } from '../../src/Core/Resources';
|
import { Resources } from '../../src/Core/Resources';
|
||||||
import { ResourceType } from '../../src/Core/ResourceType';
|
|
||||||
import { ResourceStorage } from '../../src/Core/ResourceStorage';
|
|
||||||
import { calcGatheringTimings, GatheringTimings } from '../../src/Core/GatheringTimings';
|
import { calcGatheringTimings, GatheringTimings } from '../../src/Core/GatheringTimings';
|
||||||
|
|
||||||
describe('Gathering timings', function() {
|
describe('Gathering timings', function() {
|
||||||
it('Can calc common from numbers', function() {
|
it('Can calc common from numbers', function() {
|
||||||
const timings = new GatheringTimings(10, 20, 15, 5);
|
const timings = new GatheringTimings(10, 20, 15, 5);
|
||||||
expect(20).to.equals(timings.common);
|
expect(20).to.equals(timings.hours);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Can calc common with never', function() {
|
it('Can calc common with never', function() {
|
||||||
const timings = new GatheringTimings(10, 20, 'never', 5);
|
const timings = new GatheringTimings(10, 20, 'never', 5);
|
||||||
expect('never').to.equals(timings.common);
|
|
||||||
expect(true).to.equals(timings.never);
|
expect(true).to.equals(timings.never);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Can calc common with all never', function() {
|
it('Can calc common with all never', function() {
|
||||||
const timings = new GatheringTimings('never', 'never', 'never', 'never');
|
const timings = new GatheringTimings('never', 'never', 'never', 'never');
|
||||||
expect('never').to.equals(timings.common);
|
|
||||||
expect(true).to.equals(timings.never);
|
expect(true).to.equals(timings.never);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -29,7 +25,7 @@ describe('Gathering timings', function() {
|
|||||||
const desired = new Resources(60, 60, 60, 60);
|
const desired = new Resources(60, 60, 60, 60);
|
||||||
const speed = new Resources(5, 5, 5, 5);
|
const speed = new Resources(5, 5, 5, 5);
|
||||||
const timings = calcGatheringTimings(resources, desired, speed);
|
const timings = calcGatheringTimings(resources, desired, speed);
|
||||||
expect(10).to.equals(timings.common);
|
expect(10).to.equals(timings.hours);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Can calc timings with different speed', function() {
|
it('Can calc timings with different speed', function() {
|
||||||
@ -41,7 +37,7 @@ describe('Gathering timings', function() {
|
|||||||
expect(5).to.equals(timings.clay);
|
expect(5).to.equals(timings.clay);
|
||||||
expect(2).to.equals(timings.iron);
|
expect(2).to.equals(timings.iron);
|
||||||
expect(10).to.equals(timings.crop);
|
expect(10).to.equals(timings.crop);
|
||||||
expect(10).to.equals(timings.common);
|
expect(10).to.equals(timings.hours);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Can calc timings with negative speed', function() {
|
it('Can calc timings with negative speed', function() {
|
||||||
@ -53,7 +49,6 @@ describe('Gathering timings', function() {
|
|||||||
expect(5).to.equals(timings.clay);
|
expect(5).to.equals(timings.clay);
|
||||||
expect(2).to.equals(timings.iron);
|
expect(2).to.equals(timings.iron);
|
||||||
expect('never').to.equals(timings.crop);
|
expect('never').to.equals(timings.crop);
|
||||||
expect('never').to.equals(timings.common);
|
|
||||||
expect(true).to.equals(timings.never);
|
expect(true).to.equals(timings.never);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
30
tests/Core/ResourcesTest.ts
Normal file
30
tests/Core/ResourcesTest.ts
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import { it, describe } from 'mocha';
|
||||||
|
import { expect } from 'chai';
|
||||||
|
|
||||||
|
import { Resources } from '../../src/Core/Resources';
|
||||||
|
|
||||||
|
describe('Resources', function() {
|
||||||
|
it('Can compare with lt', function() {
|
||||||
|
const x = new Resources(0, 0, 0, 0);
|
||||||
|
const y = new Resources(5, 5, 5, 5);
|
||||||
|
expect(true).is.equals(x.lt(y));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Can compare with lt (mixed)', function() {
|
||||||
|
const x = new Resources(20, 20, 5, 20);
|
||||||
|
const y = new Resources(10, 10, 10, 10);
|
||||||
|
expect(true).is.equals(x.lt(y));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Can compare with gt', function() {
|
||||||
|
const x = new Resources(5, 5, 5, 5);
|
||||||
|
const y = new Resources(0, 0, 0, 0);
|
||||||
|
expect(true).is.equals(x.gt(y));
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Can compare with gt (mixed)', function() {
|
||||||
|
const x = new Resources(30, 30, 10, 30);
|
||||||
|
const y = new Resources(20, 20, 20, 20);
|
||||||
|
expect(false).is.equals(x.gt(y));
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user