apg-go/mode_test.go
Winni Neessen 79f921f9ad
Add .gitgnore and SPDX headers in several files
This commit introduces the .gitignore configuration file and adds SPDX headers to several files including test and documentation files. The headers provide license information in a standardized format which can be easily picked up by automated tools for license compliance checks. Additionally, it deleted a .idea/.gitignore file, which is a project specific IDE configuration file not necessary for the repository. It also introduced a README.md file providing more insightful information about the project.
2024-03-12 20:59:07 +01:00

119 lines
2.9 KiB
Go

// SPDX-FileCopyrightText: 2021-2024 Winni Neessen <wn@neessen.dev>
//
// SPDX-License-Identifier: MIT
package apg
import (
"testing"
)
func TestSetClearHasToggleMode(t *testing.T) {
tt := []struct {
name string
mode Mode
}{
{"ModeHumanReadable", ModeHumanReadable},
{"ModeLowerCase", ModeLowerCase},
{"ModeNumeric", ModeNumeric},
{"ModeSpecial", ModeSpecial},
{"ModeUpperCase", ModeUpperCase},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
var m ModeMask
m = MaskSetMode(m, tc.mode)
if !MaskHasMode(m, tc.mode) {
t.Errorf("MaskSetMode() failed, mode not found in bitmask")
}
m = MaskToggleMode(m, tc.mode)
if MaskHasMode(m, tc.mode) {
t.Errorf("MaskToggleMode() failed, mode found in bitmask")
}
m = MaskToggleMode(m, tc.mode)
if !MaskHasMode(m, tc.mode) {
t.Errorf("MaskToggleMode() failed, mode not found in bitmask")
}
m = MaskClearMode(m, tc.mode)
if MaskHasMode(m, tc.mode) {
t.Errorf("MaskClearMode() failed, mode found in bitmask")
}
})
}
}
func TestModesFromFlags(t *testing.T) {
tt := []struct {
name string
ms string
mode []Mode
}{
{"ModeComplex", "C", []Mode{
ModeLowerCase, ModeNumeric, ModeSpecial,
ModeUpperCase,
}},
{"ModeHumanReadable", "H", []Mode{ModeHumanReadable}},
{"ModeLowerCase", "L", []Mode{ModeLowerCase}},
{"ModeNumeric", "N", []Mode{ModeNumeric}},
{"ModeUpperCase", "U", []Mode{ModeUpperCase}},
{"ModeSpecial", "S", []Mode{ModeSpecial}},
{"ModeLowerSpecialUpper", "LSUH", []Mode{
ModeHumanReadable,
ModeLowerCase, ModeSpecial, ModeUpperCase,
}},
{"ModeComplexNoHumanReadable", "Ch", []Mode{
ModeLowerCase,
ModeNumeric, ModeSpecial, ModeUpperCase,
}},
{"ModeComplexNoLower", "Cl", []Mode{
ModeNumeric, ModeSpecial,
ModeUpperCase,
}},
{"ModeComplexNoNumber", "Cn", []Mode{
ModeLowerCase, ModeSpecial,
ModeUpperCase,
}},
{"ModeComplexNoSpecial", "Cs", []Mode{
ModeLowerCase, ModeNumeric,
ModeUpperCase,
}},
{"ModeComplexNoUpper", "Cu", []Mode{
ModeLowerCase, ModeNumeric,
ModeSpecial,
}},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
mm := ModesFromFlags(tc.ms)
for _, tm := range tc.mode {
if !MaskHasMode(mm, tm) {
t.Errorf("ModesFromFlags() failed, expected mode %q not found", tm)
}
}
})
}
}
func TestMode_String(t *testing.T) {
tt := []struct {
name string
m Mode
e string
}{
{"ModeHumanReadable", ModeHumanReadable, "Human-readable"},
{"ModeLowerCase", ModeLowerCase, "Lower-case"},
{"ModeNumeric", ModeNumeric, "Numeric"},
{"ModeSpecial", ModeSpecial, "Special"},
{"ModeUpperCase", ModeUpperCase, "Upper-case"},
{"ModeUnknown", 255, "Unknown"},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
if tc.m.String() != tc.e {
t.Errorf("Mode.String() failed, expected: %s, got: %s", tc.e,
tc.m.String())
}
})
}
}