diff --git a/console/parse.go b/console/parse.go index ebce7b134..8e310c3cc 100644 --- a/console/parse.go +++ b/console/parse.go @@ -19,6 +19,8 @@ along with this program. If not, see package console import ( + "fmt" + "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/utils" ) @@ -77,6 +79,7 @@ func (self *CmdParse) LocalExecute() string { if rsrField, err := config.NewRSRParser(self.rpcParams.Expression); err != nil { return err.Error() } else if parsed, err := rsrField.ParseValue(self.rpcParams.Value); err != nil { + fmt.Println("yay") return err.Error() } else { return parsed diff --git a/console/parse_test.go b/console/parse_test.go new file mode 100644 index 000000000..e56d60dba --- /dev/null +++ b/console/parse_test.go @@ -0,0 +1,123 @@ +/* +Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments +Copyright (C) ITsysCOM GmbH + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see +*/ + +package console + +import ( + "fmt" + "reflect" + "testing" + + "github.com/cgrates/cgrates/utils" +) + +func TestCmdParse(t *testing.T) { + // commands map is initiated in init function + command := commands["parse"] + + // for coverage purpose + expected := command.Name() + if !reflect.DeepEqual(expected, "parse") { + fmt.Errorf("Expected <%+v>, Received <%+v>", "parse", expected) + } + + // for coverage purpose + expected = command.RpcMethod() + if !reflect.DeepEqual(expected, utils.EmptyString) { + fmt.Errorf("Expected <%+v>, Received <%+v>", utils.EmptyString, expected) + } + + // for coverage purpose + expected2 := command.RpcParams(true) + if !reflect.DeepEqual(expected, &AttrParse{}) { + fmt.Errorf("Expected <%+v>, Received <%+v>", &AttrParse{}, expected2) + } + + // for coverage purpose + if err := command.RpcResult(); err != nil { + t.Fatal(err) + } + + // for coverage purpose + if err := command.PostprocessRpcParams(); err != nil { + t.Fatal(err) + } + +} +func TestCmdParseLocalExecuteCase1(t *testing.T) { + // for coverage purpose + testStruct := &CmdParse{ + rpcParams: &AttrParse{ + Expression: "", + Value: "", + }, + } + + result := testStruct.LocalExecute() + expected := "Empty expression error" + if !reflect.DeepEqual(result, expected) { + t.Errorf("Expected <%+v>, Received <%+v>", expected, result) + } + +} +func TestCmdParseLocalExecuteCase2(t *testing.T) { + // for coverage purpose + testStruct := &CmdParse{ + rpcParams: &AttrParse{ + Expression: "test_exp", + Value: "", + }, + } + + result := testStruct.LocalExecute() + expected := "Empty value error" + if !reflect.DeepEqual(result, expected) { + t.Errorf("Expected <%+v>, Received <%+v>", expected, result) + } +} + +func TestCmdParseLocalExecuteCase3(t *testing.T) { + // for coverage purpose + testStruct := &CmdParse{ + rpcParams: &AttrParse{ + Expression: "test_exp", + Value: "test_val", + }, + } + + result := testStruct.LocalExecute() + expected := "test_exp" + if !reflect.DeepEqual(result, expected) { + t.Errorf("Expected <%+v>, Received <%+v>", expected, result) + } +} + +func TestCmdParseLocalExecuteCase4(t *testing.T) { + // for coverage purpose + testStruct := &CmdParse{ + rpcParams: &AttrParse{ + Expression: "~*req.Field{*}", + Value: "~*req.Field{*}", + }, + } + err := testStruct.LocalExecute() + expected := "invalid converter value in string: <*>, err: unsupported converter definition: <*>" + if !reflect.DeepEqual(err, expected) { + t.Errorf("Expected <%+v>, Received <%+v>", expected, err) + } +} diff --git a/console/ping_test.go b/console/ping_test.go index 59b9e2266..cb9ffb645 100644 --- a/console/ping_test.go +++ b/console/ping_test.go @@ -652,6 +652,6 @@ func TestCmdPingTestDefault(t *testing.T) { castCommand.item = "test_item" result2 := command.RpcMethod() if !reflect.DeepEqual(result2, "") { - t.Errorf("Expected <%T>, Received <%T>", "", result2) + t.Errorf("Expected <%+v>, Received <%+v>", "", result2) } }