Skip to content

Commit

Permalink
Always clear Setup and FEN when calling .clear()
Browse files Browse the repository at this point in the history
  • Loading branch information
jhlywa committed Dec 17, 2023
1 parent 4160ef2 commit be03fe0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
26 changes: 26 additions & 0 deletions __tests__/clear.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Chess } from '../src/chess'

test('clear', () => {
const chess = new Chess()
chess.header('White', 'Magnus Carlsen')
chess.header('Black', 'Viswanathan Anand')

chess.clear()
console.log(chess.header())
expect(chess.fen()).toEqual('8/8/8/8/8/8/8/8 w - - 0 1')
expect(chess.header()).toEqual({})
})

test('clear - preserveHeaders = true', () => {
const chess = new Chess()
chess.header('White', 'Magnus Carlsen')
chess.header('Black', 'Viswanathan Anand')

chess.clear({ preserveHeaders: true })

expect(chess.fen()).toEqual('8/8/8/8/8/8/8/8 w - - 0 1')
expect(chess.header()).toEqual({
White: 'Magnus Carlsen',
Black: 'Viswanathan Anand',
})
})
14 changes: 5 additions & 9 deletions __tests__/regression.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe('Regression Tests', () => {
expect(chess.header()).toEqual(expected)
})

it('Github Issue #129 clear() should clear the board and delete all headers with the exception of SetUp and FEN', () => {
it('Github Issue #129 clear() should clear the board and delete all headers', () => {
const pgn = [
'[Event "Test Olympiad"]',
'[Site "Earth"]',
Expand All @@ -121,11 +121,7 @@ describe('Regression Tests', () => {
const chess = new Chess()
chess.loadPgn(pgn.join('\n'))
chess.clear()
const expected = {
FEN: '8/8/8/8/8/8/8/8 w - - 0 1',
SetUp: '1',
}
expect(chess.header()).toEqual(expected)
expect(chess.header()).toEqual({})
})

it('Github Issue #191 - whitespace before closing bracket', () => {
Expand Down Expand Up @@ -266,7 +262,7 @@ describe('Regression Tests', () => {
const pgn = `
[white "player a"]
[black "player b"]
[note "whitespace after right bracket"]
[note "whitespace after right bracket"]
1. e4 e5`

Expand All @@ -278,8 +274,8 @@ describe('Regression Tests', () => {
const pgn = `
[white "player a"]
[black "player b"]
[note "whitespace after right bracket and in empty line below"]
[note "whitespace after right bracket and in empty line below"]
1. e4 e5`

chess.loadPgn(pgn)
Expand Down
10 changes: 9 additions & 1 deletion src/chess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,15 @@ export class Chess {
this._history = []
this._comments = {}
this._header = preserveHeaders ? this._header : {}
this._updateSetup(this.fen())

/*
* Delete the SetUp and FEN headers (if preserved), the board is empty and
* these headers don't make sense in this state. They'll get added later
* via .load() or .put()
*/
delete this._header['SetUp']
delete this._header['FEN']

/*
* Instantiate a proxy that keeps track of position occurrence counts for the purpose
* of repetition checking. The getter and setter methods automatically handle trimming
Expand Down

0 comments on commit be03fe0

Please sign in to comment.