package main import ( "fmt" "testing" ) func TestTypeFromValidation(t *testing.T) { for i, c := range []struct { expectedType string expectedComment string expectedOmitEmpty bool validation interface{} }{ {"string", "", true, ""}, {"string", "default|custom", true, "default|custom"}, {"string", ".{0,32}", true, ".{0,32}"}, {"string", "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^$", false, "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^$"}, {"int", "^([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^$", false, "^([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^$"}, {"int", "", true, "^[0-9]*$"}, {"float64", "", true, "[-+]?[0-9]*\\.?[0-9]+"}, // this one is really an error as the . is not escaped {"float64", "", true, "^([-]?[\\d]+[.]?[\\d]*)$"}, {"float64", "", true, "^([\\d]+[.]?[\\d]*)$"}, {"bool", "", false, "false|true"}, {"bool", "", false, "true|false"}, } { t.Run(fmt.Sprintf("%d %s %s", i, c.expectedType, c.validation), func(t *testing.T) { actualType, actualComment, actualOmitEmpty, err := typeFromValidation(c.validation) if err != nil { t.Fatal(err) } if actualType != c.expectedType { t.Fatalf("expected type %q got %q", c.expectedType, actualType) } if actualComment != c.expectedComment { t.Fatalf("expected comment %q got %q", c.expectedComment, actualComment) } if actualOmitEmpty != c.expectedOmitEmpty { t.Fatalf("expected omitempty %t got %t", c.expectedOmitEmpty, actualOmitEmpty) } }) } }