From 63ea3ff95232a7164f8fea59321d320daf432058 Mon Sep 17 00:00:00 2001 From: Github Readme Stats Bot Date: Sat, 17 Jan 2026 18:10:52 -0700 Subject: [PATCH] Card and Pile tests --- card.test.ts | 46 ++++++++++++++++++++++++++++++++++++++++++++++ card.ts | 14 ++++++++++++-- pile.test.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 card.test.ts create mode 100644 pile.test.ts diff --git a/card.test.ts b/card.test.ts new file mode 100644 index 0000000..c7e5f9e --- /dev/null +++ b/card.test.ts @@ -0,0 +1,46 @@ +import { describe, expect, test } from "bun:test" +import Card, { Index, Suit } from "./card" + +describe('types', () => { + test('has expected indexes', () => { + [ + Index.Joker, + Index.One, + Index.Two, + Index.Three, + Index.Four, + Index.Five, + Index.Six, + Index.Seven, + Index.Eight, + Index.Nine, + Index.Ten, + Index.Jack, + Index.Queen, + Index.King, + Index.Ace + ].forEach((current, i) => expect(current).toBe(i)) + }) + + test('has expected suits', () => { + [Suit.Diamond, Suit.Club, Suit.Heart, Suit.Spade] + .forEach((current, i) => expect(current).toBe(i)) + }) +}) + +describe('Card', () => { + test('can read simple data from a Card', () => { + const card = new Card(Index.Five, Suit.Diamond) + + expect(card.prettyIndex).toBe('Five') + expect(card.prettySuit).toBe('♦') + expect(card.pretty).toBe('Five of ♦') + }) + + test('can read decimal data from a Card', () => { + const card = new Card(Index.Five, Suit.Club) + + expect(card.decimalIndex).toBe('101') + expect(card.decimalSuit).toBe('01') + }) +}) diff --git a/card.ts b/card.ts index 11d0967..aaafeb1 100644 --- a/card.ts +++ b/card.ts @@ -31,14 +31,24 @@ export default class Card { this.suit = suit } + // 000, 001, 010... get decimalIndex() { const base = 2 - return this.index.toString(base) + const digits = 3 + return this + .index + .toString(base) + .padStart(digits, '0') } + // 00, 01, 10, 11 get decimalSuit() { const base = 2 - return this.suit.toString(base) + const digits = 2 + return this + .suit + .toString(base) + .padStart(digits, '0') } get pretty() { diff --git a/pile.test.ts b/pile.test.ts new file mode 100644 index 0000000..5c1f4e2 --- /dev/null +++ b/pile.test.ts @@ -0,0 +1,40 @@ +import { expect, test, spyOn } from "bun:test" +import Pile from "./pile" +import Card, { Index, Suit } from "./card" + +test('can assign Cards to a Pile', () => { + const fiveOfClubs = new Card(Index.Five, Suit.Club) + const aceOfSpades = new Card(Index.Ace, Suit.Spade) + const pile = new Pile([fiveOfClubs, aceOfSpades]) + + expect(pile.cards[0]).toBe(fiveOfClubs) + expect(pile.cards[1]).toBe(aceOfSpades) +}) + +test('can pretty print all Cards in a Pile', () => { + const fiveOfClubs = new Card(Index.Five, Suit.Club) + const aceOfSpades = new Card(Index.Ace, Suit.Spade) + const pile = new Pile([fiveOfClubs, aceOfSpades]) + + expect(pile.pretty).toBe(`Five of ♣\nAce of ♠`) +}) + +test('can shuffle a Pile', () => { + spyOn(global.Math, 'random').mockReturnValue(0.123); + + const fiveOfClubs = new Card(Index.Five, Suit.Club) + const aceOfSpades = new Card(Index.Ace, Suit.Spade) + const queenOfHearts = new Card(Index.Queen, Suit.Heart) + const pile = new Pile([fiveOfClubs, aceOfSpades, queenOfHearts]) + + pile.shuffle() + + expect(pile.cards.includes(fiveOfClubs)).toBe(true) + expect(pile.cards.includes(aceOfSpades)).toBe(true) + expect(pile.cards.includes(queenOfHearts)).toBe(true) + expect(pile.cards[0]).not.toBe(fiveOfClubs) + expect(pile.cards[1]).not.toBe(aceOfSpades) + expect(pile.cards[2]).not.toBe(queenOfHearts) + + spyOn(global.Math, 'random').mockRestore(); +}, { repeats: 9 })