#53 Add tests and refactor Algorithm constants

Test suite `TestIntToAlgo` was added to `algo_test.go` to validate `IntToAlgo` function. Additionally, Algorithm constants in `algo.go` have been prefixed with "Algo". These changes were made in order to make the code clearer and ensure the function returns the expected values for the given inputs.
This commit is contained in:
Winni Neessen 2023-08-04 20:35:31 +02:00
parent 3cccc65498
commit 69bb1e4cb7
Signed by: wneessen
GPG key ID: 5F3AF39B820C119D
2 changed files with 32 additions and 9 deletions

18
algo.go
View file

@ -5,14 +5,14 @@ package apg
type Algorithm int type Algorithm int
const ( const (
// Pronouncable represents the algorithm for pronouncable passwords // AlgoPronouncable represents the algorithm for pronouncable passwords
// (koremutake syllables) // (koremutake syllables)
Pronouncable Algorithm = iota AlgoPronouncable Algorithm = iota
// Random represents the algorithm for purely random passwords according // AlgoRandom represents the algorithm for purely random passwords according
// to the provided password modes/flags // to the provided password modes/flags
Random AlgoRandom
// Unsupported represents an unsupported algorithm // AlgoUnsupported represents an unsupported algorithm
Unsupported AlgoUnsupported
) )
// IntToAlgo takes an int value as input and returns the corresponding // IntToAlgo takes an int value as input and returns the corresponding
@ -20,10 +20,10 @@ const (
func IntToAlgo(a int) Algorithm { func IntToAlgo(a int) Algorithm {
switch a { switch a {
case 0: case 0:
return Pronouncable return AlgoPronouncable
case 1: case 1:
return Random return AlgoRandom
default: default:
return Unsupported return AlgoUnsupported
} }
} }

23
algo_test.go Normal file
View file

@ -0,0 +1,23 @@
package apg
import "testing"
func TestIntToAlgo(t *testing.T) {
tt := []struct {
name string
a int
e Algorithm
}{
{"AlgoPronouncable", 0, AlgoPronouncable},
{"AlgoRandom", 1, AlgoRandom},
{"AlgoUnsupported", 2, AlgoUnsupported},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
a := IntToAlgo(tc.a)
if a != tc.e {
t.Errorf("IntToAlgo() failed, expected: %d, got: %d", tc.e, a)
}
})
}
}