Card and Pile tests

This commit is contained in:
Github Readme Stats Bot 2026-01-17 18:10:52 -07:00
parent 59a0995c29
commit 63ea3ff952
3 changed files with 98 additions and 2 deletions

46
card.test.ts Normal file
View file

@ -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')
})
})

14
card.ts
View file

@ -31,14 +31,24 @@ export default class Card {
this.suit = suit this.suit = suit
} }
// 000, 001, 010...
get decimalIndex() { get decimalIndex() {
const base = 2 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() { get decimalSuit() {
const base = 2 const base = 2
return this.suit.toString(base) const digits = 2
return this
.suit
.toString(base)
.padStart(digits, '0')
} }
get pretty() { get pretty() {

40
pile.test.ts Normal file
View file

@ -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 })