github.com/vpayno/adventofcode-2022-golang-workspace@v0.0.0-20230605190011-dbafed5593de/internal/day02/app_test.go (about)

     1  package day02
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/vpayno/adventofcode-2022-golang-workspace/internal/aocshared"
     9  )
    10  
    11  /*
    12    Stdout testing code borrowed from Jon Calhoun's FizzBuzz example.
    13    https://courses.calhoun.io/lessons/les_algo_m01_08
    14    https://github.com/joncalhoun/algorithmswithgo.com/blob/master/module01/fizz_buzz_test.go
    15  */
    16  
    17  // This is the main test function. This is the gatekeeper of all the tests in the appwc package.
    18  func TestMain(m *testing.M) {
    19  	exitCode := m.Run()
    20  	os.Exit(exitCode)
    21  }
    22  
    23  func TestRun(t *testing.T) {
    24  	conf := Setup("day02")
    25  
    26  	err := Run(conf)
    27  	var cause string
    28  	if err != nil {
    29  		cause = err.Error()
    30  	}
    31  	assert.Nil(t, err, cause)
    32  }
    33  
    34  func TestRun_fileError(t *testing.T) {
    35  	// this tests fails because it can't find the file
    36  	conf := Setup("day00")
    37  
    38  	err := Run(conf)
    39  	assert.NotNil(t, err, "Run() didn't fail with a can't find file error")
    40  }
    41  
    42  func TestRun_badData(t *testing.T) {
    43  	// this tests fails because it can't find the file
    44  	conf := Setup("day02")
    45  
    46  	// Give it bad data.
    47  	conf.inputFileName = "data/day02/day02-input-bad_data.txt"
    48  
    49  	err := Run(conf)
    50  	assert.NotNil(t, err, "Run() didn't fail with a bad data error")
    51  }
    52  
    53  func TestLoadData(t *testing.T) {
    54  	wantLen := 2500
    55  
    56  	fileName := "data/day02/day02-input.txt"
    57  
    58  	file, err := aocshared.GetFile(fileName)
    59  	assert.Nil(t, err, err)
    60  
    61  	scanner := aocshared.GetScanner(file)
    62  
    63  	got, err := loadData(scanner)
    64  	assert.Nil(t, err, err)
    65  
    66  	gotLen := len(got)
    67  
    68  	assert.Equal(t, wantLen, gotLen)
    69  
    70  	for _, r := range got {
    71  		assert.IsType(t, rock, r.them, "their move isn't of type move")
    72  		assert.IsType(t, win, r.goal, "your move isn't of type move")
    73  	}
    74  }
    75  
    76  func TestLoadData_badFile(t *testing.T) {
    77  	fileName := "data/day02/day02-input-bad_data.txt"
    78  
    79  	file, err := aocshared.GetFile(fileName)
    80  	assert.Nil(t, err, err)
    81  
    82  	scanner := aocshared.GetScanner(file)
    83  
    84  	_, err = loadData(scanner)
    85  	assert.NotNil(t, err, err)
    86  }
    87  
    88  func TestGetTotalScore(t *testing.T) {
    89  	input := rounds{
    90  		round{them: string2move["A"], goal: string2outcome["Y"]},
    91  		round{them: string2move["B"], goal: string2outcome["X"]},
    92  		round{them: string2move["C"], goal: string2outcome["Z"]},
    93  	}
    94  
    95  	want := 12
    96  
    97  	got := getTotalScore(input)
    98  
    99  	assert.Equal(t, want, got)
   100  }