From 8cce6f20d71c009380d9eff4097cee98aef0da4e Mon Sep 17 00:00:00 2001 From: Lvbo163 Date: Tue, 31 Jul 2018 15:28:24 +0800 Subject: add ut for yaml convertor Issue-ID: MSB-236 Change-Id: Ib6899928d7c41b05cdfaae617dd3ab1c41352df2 Signed-off-by: Lvbo163 --- msb2pilot/src/msb2pilot/util/yaml_test.go | 89 +++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 msb2pilot/src/msb2pilot/util/yaml_test.go (limited to 'msb2pilot') diff --git a/msb2pilot/src/msb2pilot/util/yaml_test.go b/msb2pilot/src/msb2pilot/util/yaml_test.go new file mode 100644 index 0000000..6b5b938 --- /dev/null +++ b/msb2pilot/src/msb2pilot/util/yaml_test.go @@ -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) + } + } +} -- cgit 1.2.3-korg