mirror of
https://github.com/wneessen/apg-go.git
synced 2024-11-22 13:50:49 +01:00
#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:
parent
3cccc65498
commit
69bb1e4cb7
2 changed files with 32 additions and 9 deletions
18
algo.go
18
algo.go
|
@ -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
23
algo_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue