PWGen/pwgenerator/funcs_test.go
brent saner cbecd9caec
v0.1.3
FIXED:
* XML, YAML marshaling/unmarshaling of pwgenerator.CharSet.
2024-07-17 04:03:31 -04:00

149 lines
2.6 KiB
Go

package pwgenerator
import (
`bytes`
`embed`
`encoding/json`
`encoding/xml`
`fmt`
"testing"
`github.com/BurntSushi/toml`
`github.com/goccy/go-yaml`
)
var (
//go:embed "_testdata"
testFiles embed.FS
)
var (
testGen *GenOpts = &GenOpts{
Alpha: true,
Numeric: true,
Symbols: true,
ExtendedSymbols: false,
CountUpper: 0,
CountLower: 0,
CountNumbers: 0,
CountSymbols: 0,
CountExtended: 0,
DisabledChars: CharSet{
Char('a'),
Char('b'),
Char('c'),
Char('😊'),
},
LengthMin: 0,
LengthMax: 0,
Count: 0,
}
testGenNoDisabled *GenOpts = &GenOpts{
Alpha: true,
Numeric: true,
Symbols: true,
ExtendedSymbols: false,
CountUpper: 0,
CountLower: 0,
CountNumbers: 0,
CountSymbols: 0,
CountExtended: 0,
DisabledChars: nil,
LengthMin: 0,
LengthMax: 0,
Count: 0,
}
)
func TestParse(t *testing.T) {
var cmpB []byte
var err error
var g *GenOpts
var mB map[string][]byte = make(map[string][]byte)
if cmpB, err = json.Marshal(testGen); err != nil {
t.Fatal(err)
}
for _, i := range []string{
"json",
"xml",
"yml",
"toml",
} {
g = new(GenOpts)
if mB[i], err = testFiles.ReadFile(fmt.Sprintf("_testdata/test.%s", i)); err != nil {
t.Fatal(err)
}
switch i {
case "json":
if err = json.Unmarshal(mB[i], g); err != nil {
t.Fatal(err)
}
case "xml":
if err = xml.Unmarshal(mB[i], g); err != nil {
t.Fatal(err)
}
case "yml":
if err = yaml.Unmarshal(mB[i], g); err != nil {
t.Fatal(err)
}
case "toml":
if err = toml.Unmarshal(mB[i], g); err != nil {
t.Fatal(err)
}
}
if mB[i], err = json.Marshal(g); err != nil {
t.Fatal(err)
}
if !bytes.Equal(mB[i], cmpB) {
t.Fatalf("Syntax/testfile %s failed comparison.", i)
}
}
}
func TestRender(t *testing.T) {
var b []byte
var err error
var buf *bytes.Buffer = new(bytes.Buffer)
// JSON
if b, err = json.MarshalIndent(testGen, "", " "); err != nil {
t.Fatal(err)
}
buf.WriteString("# JSON\n")
buf.Write(b)
buf.WriteString("\n\n")
// XML
if b, err = xml.MarshalIndent(testGen, "", " "); err != nil {
t.Fatal(err)
}
buf.WriteString("# XML\n")
buf.Write(b)
buf.WriteString("\n\n")
// YAML
if b, err = yaml.Marshal(testGen); err != nil {
t.Fatal(err)
}
buf.WriteString("# YAML\n")
buf.Write(b)
buf.WriteString("\n\n")
// TOML
if b, err = toml.Marshal(testGen); err != nil {
t.Fatal(err)
}
buf.WriteString("# TOML\n")
buf.Write(b)
buf.WriteString("\n\n")
t.Log(buf.String())
}