add ut for yaml convertor 81/58181/1
authorLvbo163 <lv.bo163@zte.com.cn>
Tue, 31 Jul 2018 07:28:24 +0000 (15:28 +0800)
committerLvbo163 <lv.bo163@zte.com.cn>
Tue, 31 Jul 2018 07:28:24 +0000 (15:28 +0800)
Issue-ID: MSB-236

Change-Id: Ib6899928d7c41b05cdfaae617dd3ab1c41352df2
Signed-off-by: Lvbo163 <lv.bo163@zte.com.cn>
msb2pilot/src/msb2pilot/util/yaml_test.go [new file with mode: 0644]

diff --git a/msb2pilot/src/msb2pilot/util/yaml_test.go b/msb2pilot/src/msb2pilot/util/yaml_test.go
new file mode 100644 (file)
index 0000000..6b5b938
--- /dev/null
@@ -0,0 +1,89 @@
+/**
+ * Copyright (c) 2018 ZTE Corporation.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * and the Apache License 2.0 which both accompany this distribution,
+ * and are available at http://www.eclipse.org/legal/epl-v10.html
+ * and http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Contributors:
+ *     ZTE - initial Project
+ */
+package util
+
+import (
+       "testing"
+)
+
+type T struct {
+       F int `yaml:"a,omitempty"`
+       B int
+}
+
+func TestMarshalYaml(t *testing.T) {
+
+       cases := []struct {
+               in   *T
+               want string
+       }{
+               {
+                       in: &T{
+                               1, 2,
+                       },
+                       want: "a: 1\nb: 2\n",
+               }, {
+                       in: &T{
+                               B: 2,
+                       },
+                       want: "b: 2\n",
+               }}
+
+       for _, cas := range cases {
+               got, err := MarshalYaml(cas.in)
+               if err != nil {
+                       t.Errorf(err.Error())
+               }
+
+               if got != cas.want {
+                       t.Errorf("MarshalYaml error: want %s, got %s", cas.want, got)
+               }
+
+       }
+
+}
+
+func TestUnmarshalYaml(t *testing.T) {
+       cases := []struct {
+               in   string
+               want T
+       }{
+               {
+                       in: "a: 1\nb: 2",
+                       want: T{
+                               1, 2,
+                       },
+               },
+               {
+                       in: "b: 2\n",
+                       want: T{
+                               B: 2,
+                       },
+               },
+               {
+                       in:   "c: 2\n",
+                       want: T{},
+               }}
+
+       for _, cas := range cases {
+               got := new(T)
+               err := UnmarshalYaml(cas.in, got)
+
+               if err != nil {
+                       t.Errorf("UnmarshalYaml error: want err, got %v", cas.want)
+               }
+
+               if got.F != cas.want.F || got.B != cas.want.B {
+                       t.Errorf("UnmarshalYaml error: want %v, got %v", cas.want, got)
+               }
+       }
+}