You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by sr...@apache.org on 2023/05/22 15:15:25 UTC
[plc4x] 01/02: test(plc4go/spi): add test for regexp
This is an automated email from the ASF dual-hosted git repository.
sruehl pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git
commit 8c16ec0f60a5b18d37d017126e8fd502bcbe0211
Author: Sebastian Rühl <sr...@apache.org>
AuthorDate: Mon May 22 17:10:22 2023 +0200
test(plc4go/spi): add test for regexp
---
plc4go/spi/utils/Regexp.go | 13 +++++---
plc4go/spi/utils/Regexp_test.go | 73 +++++++++++++++++++++++++++++++++++++++++
2 files changed, 82 insertions(+), 4 deletions(-)
diff --git a/plc4go/spi/utils/Regexp.go b/plc4go/spi/utils/Regexp.go
index ab3a7722d8..41f55cfa6d 100644
--- a/plc4go/spi/utils/Regexp.go
+++ b/plc4go/spi/utils/Regexp.go
@@ -19,7 +19,10 @@
package utils
-import "regexp"
+import (
+ "regexp"
+ "strconv"
+)
func GetSubgroupMatches(r *regexp.Regexp, query string) map[string]string {
match := r.FindStringSubmatch(query)
@@ -27,10 +30,12 @@ func GetSubgroupMatches(r *regexp.Regexp, query string) map[string]string {
return nil
}
subMatchMap := make(map[string]string)
- for i, name := range r.SubexpNames() {
- if i != 0 {
- subMatchMap[name] = match[i]
+ for i, name := range r.SubexpNames()[1:] {
+ groupIndex := i + 1
+ if name == "" {
+ name = "_" + strconv.Itoa(groupIndex)
}
+ subMatchMap[name] = match[groupIndex]
}
return subMatchMap
}
diff --git a/plc4go/spi/utils/Regexp_test.go b/plc4go/spi/utils/Regexp_test.go
new file mode 100644
index 0000000000..e725d16d4c
--- /dev/null
+++ b/plc4go/spi/utils/Regexp_test.go
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package utils
+
+import (
+ "github.com/stretchr/testify/assert"
+ "regexp"
+ "testing"
+)
+
+func TestGetSubgroupMatches(t *testing.T) {
+ type args struct {
+ r *regexp.Regexp
+ query string
+ }
+ tests := []struct {
+ name string
+ args args
+ want map[string]string
+ }{
+ {
+ name: "plc4x",
+ args: args{
+ r: regexp.MustCompile("plc(?P<aNumber>4)x"),
+ query: "plc4x",
+ },
+ want: map[string]string{
+ "aNumber": "4",
+ },
+ },
+ {
+ name: "plc4x not found",
+ args: args{
+ r: regexp.MustCompile("plc(?P<aNumber>4)x"),
+ query: "plc5x",
+ },
+ want: nil,
+ },
+ {
+ name: "plc4x not named group",
+ args: args{
+ r: regexp.MustCompile("plc(4)(x)"),
+ query: "plc4x",
+ },
+ want: map[string]string{
+ "_1": "4",
+ "_2": "x",
+ },
+ },
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ assert.Equalf(t, tt.want, GetSubgroupMatches(tt.args.r, tt.args.query), "GetSubgroupMatches(%v, %v)", tt.args.r, tt.args.query)
+ })
+ }
+}