Skip to content

Commit

Permalink
chore: added tests for MakeMatchMap
Browse files Browse the repository at this point in the history
  • Loading branch information
retr0h committed Jun 23, 2024
1 parent ca01f62 commit 87eb206
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions internal/repositories/repositories_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright (c) 2024 John Dewey

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

package repositories

import (
"regexp"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

type RepositoriesTestSuite struct {
suite.Suite
}

func (suite *RepositoriesTestSuite) SetupTest() {}

func (suite *RepositoriesTestSuite) TestMakeMatchMap() {
type input struct {
pattern string
str string
}

type test struct {
input *input
want map[string]string
}

tests := []test{
{
input: &input{
pattern: `^(?P<alpha>[a-zA-Z]*)(?P<numeric>[0-9]*)(?P<special>[^a-zA-Z0-9]*)$`,
str: "abc123!@#",
},
want: map[string]string{
"alpha": "abc",
"numeric": "123",
"special": "!@#",
},
},
{
input: &input{
pattern: `^(?P<alpha>[a-zA-Z]*)(?P<numeric>[0-9]*)(?P<special>[^a-zA-Z0-9]*)?$`,
str: "abc123",
},
want: map[string]string{
"alpha": "abc",
"numeric": "123",
"special": "",
},
},
// failure cases
{
input: &input{
pattern: `^(?P<alpha>[a-zA-Z]*)(?P<numeric>[0-9]*)(?P<special>[^a-zA-Z0-9]*)$`,
str: "abc123abc",
},
want: map[string]string{},
},
}

for _, tc := range tests {
re := regexp.MustCompile(tc.input.pattern)
matches := re.FindStringSubmatch(tc.input.str)
got := MakeMatchMap(re, matches)

assert.Equal(suite.T(), tc.want, got)
}
}

// In order for `go test` to run this suite, we need to create
// a normal test function and pass our suite to suite.Run.
func TestRepositoriesTestSuite(t *testing.T) {
suite.Run(t, new(RepositoriesTestSuite))
}

0 comments on commit 87eb206

Please sign in to comment.