Skip to content
This repository has been archived by the owner on Jan 5, 2019. It is now read-only.

Commit

Permalink
Fixed test cases for skip_if_zero fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Mar 10, 2018
1 parent fde465f commit 7dae662
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 31 deletions.
69 changes: 45 additions & 24 deletions src/format/__tests__/formatStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,34 +177,55 @@ it('should support skipping fallback', () => {
).toEqual([]);
});

it('should support skip if zero fallback', () => {
const stats = [{ id: 'from_armour_movement_speed_+%', value: 0 }];
describe('skip_if_zero fallback', () => {
it('ignores stats with 0 value', () => {
const stats = [{ id: 'weapon_physical_damage_+%', value: 0 }];

expect(() =>
formatStats(stats, {
datas
})
).toThrow('no descriptions found for from_armour_movement_speed_+%');
expect(
formatStats(stats, {
datas,
fallback: Fallback.skip_if_zero
})
).toEqual([]);
expect(() =>
formatStats(stats, {
datas
})
).toThrow("matching translation not found for 'weapon_physical_damage_+%'");
expect(
formatStats(stats, {
datas,
fallback: Fallback.skip_if_zero
})
).toEqual([]);
});

// only warn about non zero stats for which no desc was found
expect(() =>
formatStats(
[
{ id: 'from_armour_movement_speed_+%', value: 0 },
{ id: 'non_existing_non_zero', value: [0, 1] }
],
{
it('still throws for stats which are not 0 and no matching translation was found', () => {
expect(() =>
formatStats(
[
{ id: 'weapon_physical_damage_+%', value: 0 },
{ id: 'local_energy_shield_+%', value: [0, 1] }
],
{
datas,
fallback: Fallback.skip_if_zero
}
)
).toThrow("matching translation not found for 'local_energy_shield_+%'");
});

it('ignores 0 stats for which no description was found', () => {
expect(
formatStats([{ id: 'non_existing_zero', value: 0 }], {
datas,
fallback: Fallback.skip_if_zero
}
)
).toThrow('no descriptions found for non_existing_non_zero');
})
).toEqual([]);
});

it('still throws if stats are non zero and have no description', () => {
expect(() =>
formatStats([{ id: 'non_existing_zero', value: [0, 1] }], {
datas,
fallback: Fallback.skip_if_zero
})
).toThrow('no descriptions found for non_existing_zero');
});
});

it('should throw if we provide an unrecognize fallback', () => {
Expand Down
10 changes: 3 additions & 7 deletions src/format/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,8 @@ interface FormatWithFinderOptions {
function formatWithFinder(
stats: Map<string, Stat>,
find: (stat: Stat) => Description | undefined,
options: Partial<FormatWithFinderOptions> = {}
options: FormatWithFinderOptions
): string[] {
const {
ignore_if_zero = false,
getFormatters = (t: Translation) => t.formatters
} = options;
const lines: string[] = [];
const translated: Set<string> = new Set();

Expand All @@ -136,7 +132,7 @@ function formatWithFinder(

if (description !== undefined) {
const translation = translate(description, stats, (t: Translation, n) =>
getFormatters(t, stat, n)
options.getFormatters(t, stat, n)
);

if (translation === undefined) {
Expand All @@ -145,7 +141,7 @@ function formatWithFinder(
stats
).every(({ value }) => isZero(value));

if (!ignore_if_zero || !requiredStatsAreZero) {
if (!options.ignore_if_zero || !requiredStatsAreZero) {
throw new Error(`matching translation not found for '${stat.id}'`);
}
} else {
Expand Down

0 comments on commit 7dae662

Please sign in to comment.