...

Source file src/code.rocketnine.space/tslocum/joker/card_test.go

Documentation: code.rocketnine.space/tslocum/joker

     1  package joker
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  )
     7  
     8  type cardTestCase struct {
     9  	Card     Card
    10  	Expected string
    11  }
    12  
    13  var cardTestCases = []*cardTestCase{
    14  	{Card: Card{FaceAce, SuitSpades}, Expected: `"AS"`},
    15  	{Card: Card{Face7, SuitDiamonds}, Expected: `"7D"`},
    16  	{Card: Card{Face10, SuitHearts}, Expected: `"10H"`},
    17  	{Card: Card{FaceQueen, SuitHearts}, Expected: `"QH"`},
    18  	{Card: Card{FaceJoker, SuitClubs}, Expected: `"!C"`},
    19  	{Card: Card{FaceJoker, SuitJoker}, Expected: `"!!"`},
    20  	{Card: Card{FaceKing, SuitJoker}, Expected: `"K!"`},
    21  }
    22  
    23  func TestMarshalCard(t *testing.T) {
    24  	for _, c := range cardTestCases {
    25  		buf, err := json.Marshal(c.Card)
    26  		if err != nil {
    27  			t.Error(err)
    28  		}
    29  
    30  		if string(buf) != c.Expected {
    31  			t.Errorf("failed to marshal card: expected %s, got %s", c.Expected, string(buf))
    32  		}
    33  	}
    34  }
    35  
    36  func TestUnmarshalCard(t *testing.T) {
    37  	for _, c := range cardTestCases {
    38  		var cd Card
    39  
    40  		err := json.Unmarshal([]byte(c.Expected), &cd)
    41  		if err != nil {
    42  			t.Error(err)
    43  		}
    44  
    45  		if !cd.Equal(c.Card) {
    46  			t.Errorf("failed to unmarshal card: expected %s, got %s", c.Card, cd)
    47  		}
    48  	}
    49  }
    50  

View as plain text