diff --git a/cmd/spectre/main_test.go b/cmd/spectre/main_test.go index 2f28f8d..9c563e2 100644 --- a/cmd/spectre/main_test.go +++ b/cmd/spectre/main_test.go @@ -2,39 +2,29 @@ package main import ( _ "embed" - "encoding/xml" - "fmt" + "go.jolheiser.com/go-spectre/testdata" "testing" ) // These are the exact same tests as spectre_test.go // These are here just to make sure the CLI is giving the same outputs func TestCLI(t *testing.T) { - var tests TestCases - if err := xml.Unmarshal(testsXML, &tests); err != nil { - t.Log("could not load test data") + cases, err := testdata.Cases() + if err != nil { + t.Log(err) t.FailNow() } - dc := tests.Cases[0] - for _, tc := range tests.Cases[1:] { + for _, tc := range cases { t.Run(tc.ID, func(t *testing.T) { - user := def(dc.UserName, tc.UserName) - secret := def(dc.UserSecret, tc.UserSecret) - siteName := def(dc.SiteName, tc.SiteName) - template := def(dc.ResultType, tc.ResultType) - counter := def(dc.KeyCounter, tc.KeyCounter) - scope := def(dc.KeyPurpose, tc.KeyPurpose) - args := []string{ - "--username", user, - "--secret", secret, - "--template", template, - "--counter", counter, - "--scope", scope, - siteName, + "--username", tc.UserName, + "--secret", tc.UserSecret, + "--template", tc.ResultType, + "--counter", tc.KeyCounter, + "--scope", tc.KeyPurpose, + tc.SiteName, } - fmt.Println(args) pw, err := doMain(args) if err != nil { @@ -49,28 +39,3 @@ func TestCLI(t *testing.T) { }) } } - -//go:embed spectre_tests.xml -var testsXML []byte - -type TestCases struct { - Cases []TestCase `xml:"case"` -} - -type TestCase struct { - ID string `xml:"id,attr"` - UserName string `xml:"userName"` - UserSecret string `xml:"userSecret"` - SiteName string `xml:"siteName"` - ResultType string `xml:"resultType"` - KeyCounter string `xml:"keyCounter"` - KeyPurpose string `xml:"keyPurpose"` - Result string `xml:"result"` -} - -func def(def, alt string) string { - if alt != "" { - return alt - } - return def -} diff --git a/spectre_test.go b/spectre_test.go index cf888ad..51bc572 100644 --- a/spectre_test.go +++ b/spectre_test.go @@ -2,8 +2,8 @@ package spectre_test import ( _ "embed" - "encoding/xml" "fmt" + "go.jolheiser.com/go-spectre/testdata" "strconv" "testing" @@ -11,38 +11,29 @@ import ( ) func TestSpectre(t *testing.T) { - var tests TestCases - if err := xml.Unmarshal(testsXML, &tests); err != nil { - t.Log("could not load test data") + cases, err := testdata.Cases() + if err != nil { + t.Log(err) t.FailNow() } - dc := tests.Cases[0] - for _, tc := range tests.Cases[1:] { + for _, tc := range cases { t.Run(tc.ID, func(t *testing.T) { - user := def(dc.UserName, tc.UserName) - secret := def(dc.UserSecret, tc.UserSecret) - - s, err := spectre.New(user, secret) + s, err := spectre.New(tc.UserName, tc.UserSecret) if err != nil { t.Logf("could not initialize spectre: %v", err) t.Fail() } - - siteName := def(dc.SiteName, tc.SiteName) - template := def(dc.ResultType, tc.ResultType) - counterStr := def(dc.KeyCounter, tc.KeyCounter) - counter, err := strconv.Atoi(counterStr) + counter, err := strconv.Atoi(tc.KeyCounter) if err != nil { t.Log("could not convert counter") t.Fail() } - scope := def(dc.KeyPurpose, tc.KeyPurpose) - pass := s.Site(siteName, - spectre.WithTemplate(spectre.Template(template)), + pass := s.Site(tc.SiteName, + spectre.WithTemplate(spectre.Template(tc.ResultType)), spectre.WithCounter(counter), - spectre.WithScope(spectre.Scope(scope)), + spectre.WithScope(spectre.Scope(tc.KeyPurpose)), ) if pass != tc.Result { @@ -83,28 +74,3 @@ func Example_second() { fmt.Println(pw) // Output: Ig^JIcxD!*)TbefJBi6- } - -//go:embed cmd/spectre/spectre_tests.xml -var testsXML []byte - -type TestCases struct { - Cases []TestCase `xml:"case"` -} - -type TestCase struct { - ID string `xml:"id,attr"` - UserName string `xml:"userName"` - UserSecret string `xml:"userSecret"` - SiteName string `xml:"siteName"` - ResultType string `xml:"resultType"` - KeyCounter string `xml:"keyCounter"` - KeyPurpose string `xml:"keyPurpose"` - Result string `xml:"result"` -} - -func def(def, alt string) string { - if alt != "" { - return alt - } - return def -} diff --git a/cmd/spectre/spectre_tests.xml b/testdata/spectre_tests.xml similarity index 100% rename from cmd/spectre/spectre_tests.xml rename to testdata/spectre_tests.xml diff --git a/testdata/testdata.go b/testdata/testdata.go new file mode 100644 index 0000000..5fa9a0a --- /dev/null +++ b/testdata/testdata.go @@ -0,0 +1,54 @@ +package testdata + +import ( + _ "embed" + "encoding/xml" +) + +func Cases() ([]*TestCase, error) { + var tests TestCases + if err := xml.Unmarshal(testsXML, &tests); err != nil { + return nil, err + } + defaultCase := tests.Cases[0] + cases := make([]*TestCase, 0, len(tests.Cases[1:])) + for _, tc := range tests.Cases[1:] { + tc = &TestCase{ + ID: tc.ID, + UserName: def(defaultCase.UserName, tc.UserName), + UserSecret: def(defaultCase.UserSecret, tc.UserSecret), + SiteName: def(defaultCase.SiteName, tc.SiteName), + ResultType: def(defaultCase.ResultType, tc.ResultType), + KeyCounter: def(defaultCase.KeyCounter, tc.KeyCounter), + KeyPurpose: def(defaultCase.KeyPurpose, tc.KeyPurpose), + Result: tc.Result, + } + cases = append(cases, tc) + } + return cases, nil +} + +//go:embed spectre_tests.xml +var testsXML []byte + +type TestCases struct { + Cases []*TestCase `xml:"case"` +} + +type TestCase struct { + ID string `xml:"id,attr"` + UserName string `xml:"userName"` + UserSecret string `xml:"userSecret"` + SiteName string `xml:"siteName"` + ResultType string `xml:"resultType"` + KeyCounter string `xml:"keyCounter"` + KeyPurpose string `xml:"keyPurpose"` + Result string `xml:"result"` +} + +func def(def, alt string) string { + if alt != "" { + return alt + } + return def +}