mirror of https://github.com/on4kjm/FLEcli.git
parent
9b4b9a7dda
commit
8443c27b18
@ -0,0 +1,35 @@
|
||||
package cmd
|
||||
/*
|
||||
Copyright © 2020 Jean-Marc Meessen, ON4KJM <on4kjm@gmail.com>
|
||||
|
||||
Licensed 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
|
||||
|
||||
http://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.
|
||||
*/
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
||||
var validSotaRegexp = regexp.MustCompile(`^[0-9A-Z]{1,3}/[A-Z]{2}-[\d]{3}$`)
|
||||
|
||||
// ValidateSota verifies whether the supplied string is a valid SOTA reference.
|
||||
// The syntax is: AA/NN-CCC: Association/Name-3-digit numeric Code (e.g. G/CE-001).
|
||||
func ValidateSota(inputStr string) (ref, errorMsg string) {
|
||||
inputStr = strings.ToUpper(strings.TrimSpace(inputStr))
|
||||
wrongInputStr := "*" + inputStr
|
||||
if validSotaRegexp.MatchString(inputStr) {
|
||||
return inputStr, ""
|
||||
}
|
||||
return wrongInputStr, "Invalid SOTA reference"
|
||||
}
|
@ -0,0 +1,82 @@
|
||||
package cmd
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestValidateSota(t *testing.T) {
|
||||
type args struct {
|
||||
inputStr string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantRef string
|
||||
wantErrorMsg string
|
||||
}{
|
||||
{
|
||||
"Good ref (simple)",
|
||||
args{ inputStr: "on/ON-001", },
|
||||
"ON/ON-001", "",
|
||||
},
|
||||
{
|
||||
"Good ref (single digit prefix)",
|
||||
args{ inputStr: "g/ON-001", },
|
||||
"G/ON-001", "",
|
||||
},
|
||||
{
|
||||
"Good ref (numerical prefix)",
|
||||
args{ inputStr: "4x/ON-001", },
|
||||
"4X/ON-001", "",
|
||||
},
|
||||
{
|
||||
"Good ref (american style)",
|
||||
args{ inputStr: "w4z/ON-001", },
|
||||
"W4Z/ON-001", "",
|
||||
},
|
||||
{
|
||||
"Bad ref (long prefix)",
|
||||
args{ inputStr: "xxxx/ON-001", },
|
||||
"*XXXX/ON-001", "Invalid SOTA reference",
|
||||
},
|
||||
{
|
||||
"Bad ref (missing slash)",
|
||||
args{ inputStr: "on ON-001", },
|
||||
"*ON ON-001", "Invalid SOTA reference",
|
||||
},
|
||||
{
|
||||
"Bad ref (numerical region)",
|
||||
args{ inputStr: "on/9N-001", },
|
||||
"*ON/9N-001", "Invalid SOTA reference",
|
||||
},
|
||||
{
|
||||
"Bad ref (too long region)",
|
||||
args{ inputStr: "on/ONA-001", },
|
||||
"*ON/ONA-001", "Invalid SOTA reference",
|
||||
},
|
||||
{
|
||||
"Bad ref (no dash)",
|
||||
args{ inputStr: "on/ON/001", },
|
||||
"*ON/ON/001", "Invalid SOTA reference",
|
||||
},
|
||||
{
|
||||
"Bad ref (number too short)",
|
||||
args{ inputStr: "on/ON-01", },
|
||||
"*ON/ON-01", "Invalid SOTA reference",
|
||||
},
|
||||
{
|
||||
"Bad ref (Number too long)",
|
||||
args{ inputStr: "on/ON-9001", },
|
||||
"*ON/ON-9001", "Invalid SOTA reference",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
gotRef, gotErrorMsg := ValidateSota(tt.args.inputStr)
|
||||
if gotRef != tt.wantRef {
|
||||
t.Errorf("ValidateSota() gotRef = %v, want %v", gotRef, tt.wantRef)
|
||||
}
|
||||
if gotErrorMsg != tt.wantErrorMsg {
|
||||
t.Errorf("ValidateSota() gotErrorMsg = %v, want %v", gotErrorMsg, tt.wantErrorMsg)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package cmd
|
||||
/*
|
||||
Copyright © 2020 Jean-Marc Meessen, ON4KJM <on4kjm@gmail.com>
|
||||
|
||||
Licensed 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
|
||||
|
||||
http://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.
|
||||
*/
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
||||
var validWwffRegexp = regexp.MustCompile(`^[\d]{0,1}[A-Z]{1,2}FF-[\d]{4}$`)
|
||||
|
||||
// ValidateWwff verifies whether the supplied string is a valid WWFF reference.
|
||||
// The syntax is: AAFF-CCCC: AA = national prefix, CCCC = 4-digit numeric code (e.g. ONFF-0001).
|
||||
func ValidateWwff(inputStr string) (ref, errorMsg string) {
|
||||
inputStr = strings.ToUpper(strings.TrimSpace(inputStr))
|
||||
wrongInputStr := "*" + inputStr
|
||||
if validWwffRegexp.MatchString(inputStr) {
|
||||
return inputStr, ""
|
||||
}
|
||||
return wrongInputStr, "Invalid WWFF reference"
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package cmd
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestValidateWwff(t *testing.T) {
|
||||
type args struct {
|
||||
inputStr string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
wantRef string
|
||||
wantErrorMsg string
|
||||
}{
|
||||
{
|
||||
"Good call (simple)",
|
||||
args{ inputStr: "onff-0258", },
|
||||
"ONFF-0258", "",
|
||||
},
|
||||
{
|
||||
"Good call (simple)",
|
||||
args{ inputStr: "fff-0258", },
|
||||
"FFF-0258", "",
|
||||
},
|
||||
{
|
||||
"Good call (simple)",
|
||||
args{ inputStr: "4xff-0258", },
|
||||
"4XFF-0258", "",
|
||||
},
|
||||
//TODO: add the invalid cases
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
gotRef, gotErrorMsg := ValidateWwff(tt.args.inputStr)
|
||||
if gotRef != tt.wantRef {
|
||||
t.Errorf("ValidateWwff() gotRef = %v, want %v", gotRef, tt.wantRef)
|
||||
}
|
||||
if gotErrorMsg != tt.wantErrorMsg {
|
||||
t.Errorf("ValidateWwff() gotErrorMsg = %v, want %v", gotErrorMsg, tt.wantErrorMsg)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in new issue