Skip to content

Commit

Permalink
Fix + adapt unit tests to broken changes in Game.date() + fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
yo35 committed Jul 19, 2022
1 parent e1ac592 commit 06d532f
Show file tree
Hide file tree
Showing 26 changed files with 125 additions and 108 deletions.
8 changes: 4 additions & 4 deletions src/date_value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ export class DateValue {
}
this.#type = type;
this.#year = dateOrYear;
this.#month = month;
this.#day = day;
this.#month = month === null ? undefined : month;
this.#day = day === null ? undefined : day;
}
}

Expand Down Expand Up @@ -187,10 +187,10 @@ function toStringImpl(year: number, month: number | undefined, day: number | und


function computeType(year: number, month?: number, day?: number): false | 'y' | 'ym' | 'ymd' {
if (day !== undefined) {
if (day !== undefined && day !== null) {
return isValidYear(year) && isValidMonth(month) && Number.isInteger(day) && day! >= 1 && day! <= daysInMonth(year, month!) ? 'ymd' : false;
}
else if (month != undefined) {
else if (month !== undefined && month !== null) {
return isValidYear(year) && isValidMonth(month) ? 'ym' : false;
}
else {
Expand Down
47 changes: 30 additions & 17 deletions src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,23 +203,36 @@ export class Game {
date(year: number, month?: number, day?: number): void;

date(valueOrYear?: DateValue | Date | undefined | number, month?: number, day?: number) {
if (arguments.length === 0) {
return this._date;
}
else if (valueOrYear === undefined || valueOrYear === null) {
this._date = undefined;
}
else if (valueOrYear instanceof DateValue) {
this._date = valueOrYear;
}
else if (valueOrYear instanceof Date) {
this._date = new DateValue(valueOrYear);
}
else if (typeof valueOrYear === 'number' && DateValue.isValid(valueOrYear, month, day)) {
this._date = new DateValue(valueOrYear, month, day);
}
else {
throw new IllegalArgument('Game.date()');
switch (arguments.length) {
case 0:
return this._date;

case 1:
if (valueOrYear === undefined || valueOrYear === null) {
this._date = undefined;
}
else if (valueOrYear instanceof DateValue) {
this._date = valueOrYear;
}
else if (valueOrYear instanceof Date) {
this._date = new DateValue(valueOrYear);
}
else if (DateValue.isValid(valueOrYear)) {
this._date = new DateValue(valueOrYear);
}
else {
throw new IllegalArgument('Game.date()');
}
break;

default:
if (DateValue.isValid(valueOrYear as number, month, day)) {
this._date = new DateValue(valueOrYear as number, month, day);
}
else {
throw new IllegalArgument('Game.date()');
}
break;
}
}

Expand Down
97 changes: 65 additions & 32 deletions test/09_game_base.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,86 +125,105 @@ describe('Color-dependant header', function() {

describe('Date header', function() {

function testDateIsUndefined(game) {
test.value(game.date()).is(undefined);
}

function testDateIs(game, value) {
test.value(game.date()).isNotFalse();
test.value(game.date().toString()).is(value);
}

it('Initial state', function() {
var game = new kokopu.Game();
test.value(game.date()).is(undefined);
test.value(game.dateAsString()).is(undefined);
testDateIsUndefined(game);
});

it('Set full date & get', function() {
it('Set JS Date & get', function() {
var game = new kokopu.Game();
game.date(new Date(2021, 8, 12));
test.value(game.date()).isInstanceOf(Date);
test.value(game.date().toDateString()).is('Sun Sep 12 2021');
testDateIs(game, '2021-09-12');
});

it('Set month+year 1 date & get', function() {
it('Set full date & get', function() {
var game = new kokopu.Game();
game.date(2021, 9, 12);
testDateIs(game, '2021-09-12');
});

it('Set month+year date 1 & get', function() {
var game = new kokopu.Game();
game.date({ year: 2021, month: 12 });
test.value(game.date()).is({ year: 2021, month: 12 });
game.date(2021, 12);
testDateIs(game, '2021-12-**');
});

it('Set month+year 2 date & get', function() {
it('Set month+year date 2 & get', function() {
var game = new kokopu.Game();
game.date({ year: 2021, month: 7.8 });
test.value(game.date()).is({ year: 2021, month: 8 });
game.date(2021, 2, undefined);
testDateIs(game, '2021-02-**');
});

it('Set month+year 3 date & get', function() {
it('Set month+year date 3 & get', function() {
var game = new kokopu.Game();
game.date({ year: 2021, month: 1.1 });
test.value(game.date()).is({ year: 2021, month: 1 });
game.date(2021, 2, null);
testDateIs(game, '2021-02-**');
});

it('Set year-only date 1 & get', function() {
var game = new kokopu.Game();
game.date({ year: 2021 });
test.value(game.date()).is({ year: 2021 });
game.date(2021);
testDateIs(game, '2021-**-**');
});

it('Set year-only date 2 & get', function() {
var game = new kokopu.Game();
game.date({ year: 2021.2, month: undefined });
test.value(game.date()).is({ year: 2021 });
game.date(1, undefined);
testDateIs(game, '0001-**-**');
});

it('Set year-only date 3 & get', function() {
var game = new kokopu.Game();
game.date({ year: 2020.7, month: null });
test.value(game.date()).is({ year: 2021 });
game.date(99, null);
testDateIs(game, '0099-**-**');
});

it('Set year-only date 4 & get', function() {
var game = new kokopu.Game();
game.date(1921, undefined, undefined);
testDateIs(game, '1921-**-**');
});

it('Erase with undefined', function() {
var game = new kokopu.Game();
game.date(new Date(2021, 8, 12));
game.date(2021, 9, 12);
game.date(undefined);
test.value(game.date()).is(undefined);
testDateIsUndefined(game);
});

it('Erase with null', function() {
var game = new kokopu.Game();
game.date(new Date(2021, 8, 12));
game.date(2021, 9, 12);
game.date(null);
test.value(game.date()).is(undefined);
testDateIsUndefined(game);
});

it('Get as string (full date)', function() {
var game = new kokopu.Game();
game.date(new Date(2021, 8, 12));
game.date(2021, 9, 12);
test.value(game.dateAsString('en-us')).is('September 12, 2021');
test.value(game.dateAsString('fr')).is('12 septembre 2021');
});

it('Get as string (month+year)', function() {
var game = new kokopu.Game();
game.date({ year: 2021, month: 12 });
game.date(2021, 12);
test.value(game.dateAsString('en-us')).is('December 2021');
test.value(game.dateAsString('fr')).is('décembre 2021');
});

it('Get as string (year only)', function() {
var game = new kokopu.Game();
game.date({ year: 2021 });
game.date(2021);
test.value(game.dateAsString('en-us')).is('2021');
test.value(game.dateAsString('fr')).is('2021');
});
Expand All @@ -219,11 +238,25 @@ describe('Date header', function() {
itInvalidValue('Set string', 'dummy');
itInvalidValue('Set boolean', false);
itInvalidValue('Set empty object', {});
itInvalidValue('Set object with non-number-valued fields', { year: 'blah' });
itInvalidValue('Set object undefined-valued fields', { year: undefined });
itInvalidValue('Set object null-valued fields', { year: null });
itInvalidValue('Set invalid month value 1', { year: 2021, month: 13 });
itInvalidValue('Set invalid month value 2', { year: 2021, month: 0 });
itInvalidValue('Set invalid year 1', -5);
itInvalidValue('Set invalid year 2', 1989.3);

function itInvalidYMD(label, year, month, day) {
it(label, function() {
var game = new kokopu.Game();
test.exception(function() { game.date(year, month, day); }).isInstanceOf(kokopu.exception.IllegalArgument);
});
}

itInvalidYMD('Set invalid month 1', 2021, 13, undefined);
itInvalidYMD('Set invalid month 2', 2021, 0, undefined);
itInvalidYMD('Set invalid month 3', 2021, 5.7, undefined);
itInvalidYMD('Set invalid day 1', 2021, 8, 0);
itInvalidYMD('Set invalid day 2', 2021, 6, 31);
itInvalidYMD('Set invalid day 3', 2021, 3, 3.1);
itInvalidYMD('Set day without month', 2021, undefined, 4);
itInvalidYMD('Set month without year', undefined, 8, undefined);
itInvalidYMD('Set all undefined', undefined, undefined, undefined);
});


Expand Down
6 changes: 3 additions & 3 deletions test/10_game_tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ var oneGamefactories = {
'all-headers': function() {
var game = new kokopu.Game();
game.annotator(' The Annotator ');
game.date(new Date(2021, 8, 4));
game.date(2021, 9, 4);
game.event('An event name\nspanning several lines');
game.playerElo('w', 1942);
game.playerElo('b', 2421);
Expand All @@ -80,7 +80,7 @@ var oneGamefactories = {

'missing-headers-1': function() {
var game = new kokopu.Game();
game.date({ year: 1998 });
game.date(1998);
game.playerElo('w', 2345);
game.playerName('w', 'John Doe');
game.playerTitle('b', 'GM');
Expand All @@ -91,7 +91,7 @@ var oneGamefactories = {

'missing-headers-2': function() {
var game = new kokopu.Game();
game.date({ year: 1955, month: 11 });
game.date(1955, 11);
game.round(3);
game.playerElo('w', '2299');
game.playerTitle('w', 'FM');
Expand Down
33 changes: 2 additions & 31 deletions test/common/dumpgame.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,38 +39,9 @@ module.exports = function(game) {
var res = '\n';

function dumpHeader(key, value) {
if(value === undefined) { return; }

res += key + ' = {';
if(value instanceof Date) {
res += value.toDateString();
if (value !== undefined) {
res += key + ' = {' + value + '}\n';
}
else if(typeof value === 'object') {

// Extract the subkeys of the object `value`.
var subkeys = [];
for(var subkey in value) {
subkeys.push(subkey);
}
subkeys.sort();

// Print the value of each subkey.
if(subkeys.length === 0) {
res += '{}';
}
else {
res += '{ ';
for(var i=0; i<subkeys.length; ++i) {
if(i !== 0) { res += ', '; }
res += subkeys[i] + ':' + value[subkeys[i]];
}
res += ' }';
}
}
else {
res += value;
}
res += '}\n';
}

function dumpResult(result) {
Expand Down
2 changes: 1 addition & 1 deletion test/resources/games/all-headers/dump-clean.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ BlackTitle = {IM}
Event = {An event name spanning several lines}
Round = {1}
Site = {Somewhere...}
Date = {Sat Sep 04 2021}
Date = {2021-09-04}
Annotator = {The Annotator}
[start] -+<LONG
{Black wins}
2 changes: 1 addition & 1 deletion test/resources/games/all-headers/dump.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Event = {An event name
spanning several lines}
Round = {1}
Site = {Somewhere...}
Date = {Sat Sep 04 2021}
Date = {2021-09-04}
Annotator = { The Annotator }
[start] -+<LONG
{Black wins}
Expand Down
2 changes: 1 addition & 1 deletion test/resources/games/missing-headers-1/dump.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ White = {John Doe}
WhiteElo = {2345}
BlackTitle = {GM}
Round = {3}
Date = {{ year:1998 }}
Date = {1998-**-**}
[start] -+<LONG
{Draw}
2 changes: 1 addition & 1 deletion test/resources/games/missing-headers-2/dump.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ WhiteElo = {2299}
WhiteTitle = {FM}
Black = {Mister No-Name}
Round = {3}
Date = {{ month:11, year:1955 }}
Date = {1955-11-**}
[start] -+<LONG
{Line}
2 changes: 1 addition & 1 deletion test/resources/pgns/antichess/0.log
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Black = {yo35}
BlackElo = {1558}
Event = {Rated Antichess game}
Site = {Lichess}
Date = {Sat May 22 2021}
Date = {2021-05-22}
Variant = {antichess}
+---+---+---+---+---+---+---+---+
| r | n | b | q | k | b | n | r |
Expand Down
2 changes: 1 addition & 1 deletion test/resources/pgns/antichess/1.log
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Black = {yo35}
BlackElo = {1500}
Event = {Rated Antichess game}
Site = {Lichess}
Date = {Sat May 22 2021}
Date = {2021-05-22}
Variant = {antichess}
+---+---+---+---+---+---+---+---+
| r | n | b | q | k | b | n | r |
Expand Down
2 changes: 1 addition & 1 deletion test/resources/pgns/bom/0.log
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Black = {Tasic, Branislav}
Event = {Olympiad-21 Final D}
Round = {1}
Site = {Nice}
Date = {Sat Jun 15 1974}
Date = {1974-06-15}
Annotator = {Hannes Langrock}
[start] -+<LONG
[1w] (1w) e4
Expand Down
2 changes: 1 addition & 1 deletion test/resources/pgns/chess960/0.log
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Black = {Nakamura, Hikaru}
Event = {Nakamura - Carlsen Chess960 Match}
Round = {9}
Site = {Høvikodden, Bærum NOR}
Date = {Tue Feb 13 2018}
Date = {2018-02-13}
Variant = {chess960}
+---+---+---+---+---+---+---+---+
| q | r | k | n | b | b | n | r |
Expand Down
2 changes: 1 addition & 1 deletion test/resources/pgns/chess960/1.log
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Black = {MagnusCarlsen}
BlackElo = {2878}
Event = {GM Blitz Battle Chp - Chess960}
Site = {Chess.com}
Date = {Tue Aug 23 2016}
Date = {2016-08-23}
Variant = {chess960}
+---+---+---+---+---+---+---+---+
| r | n | n | k | r | b | b | q |
Expand Down
2 changes: 1 addition & 1 deletion test/resources/pgns/headers/10.log
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Event = {Invalid month in date 2}
Date = {{ year:2000 }}
Date = {2000-**-**}
[start] -+<LONG
{Line}

0 comments on commit 06d532f

Please sign in to comment.