|
| 1 | +// Copyright 2021 The Serverless Workflow Specification Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package floatstr |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/json" |
| 19 | + "k8s.io/apimachinery/pkg/util/yaml" |
| 20 | + "reflect" |
| 21 | + "testing" |
| 22 | +) |
| 23 | + |
| 24 | +func TestFromFloat(t *testing.T) { |
| 25 | + i := FromFloat(93.93) |
| 26 | + if i.Type != Float || i.FloatVal != 93.93 { |
| 27 | + t.Errorf("Expected FloatVal=93.93, got %+v", i) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func TestFromString(t *testing.T) { |
| 32 | + i := FromString("76.76") |
| 33 | + if i.Type != String || i.StrVal != "76.76" { |
| 34 | + t.Errorf("Expected StrVal=\"76.76\", got %+v", i) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +type FloatOrStringHolder struct { |
| 39 | + FOrS Float32OrString `json:"val"` |
| 40 | +} |
| 41 | + |
| 42 | +func TestIntOrStringUnmarshalJSON(t *testing.T) { |
| 43 | + cases := []struct { |
| 44 | + input string |
| 45 | + result Float32OrString |
| 46 | + }{ |
| 47 | + {"{\"val\": 123.123}", FromFloat(123.123)}, |
| 48 | + {"{\"val\": \"123.123\"}", FromString("123.123")}, |
| 49 | + } |
| 50 | + |
| 51 | + for _, c := range cases { |
| 52 | + var result FloatOrStringHolder |
| 53 | + if err := json.Unmarshal([]byte(c.input), &result); err != nil { |
| 54 | + t.Errorf("Failed to unmarshal input '%v': %v", c.input, err) |
| 55 | + } |
| 56 | + if result.FOrS != c.result { |
| 57 | + t.Errorf("Failed to unmarshal input '%v': expected %+v, got %+v", c.input, c.result, result) |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func TestIntOrStringMarshalJSON(t *testing.T) { |
| 63 | + cases := []struct { |
| 64 | + input Float32OrString |
| 65 | + result string |
| 66 | + }{ |
| 67 | + {FromFloat(123.123), "{\"val\":123.123}"}, |
| 68 | + {FromString("123.123"), "{\"val\":\"123.123\"}"}, |
| 69 | + } |
| 70 | + |
| 71 | + for _, c := range cases { |
| 72 | + input := FloatOrStringHolder{c.input} |
| 73 | + result, err := json.Marshal(&input) |
| 74 | + if err != nil { |
| 75 | + t.Errorf("Failed to marshal input '%v': %v", input, err) |
| 76 | + } |
| 77 | + if string(result) != c.result { |
| 78 | + t.Errorf("Failed to marshal input '%v': expected: %+v, got %q", input, c.result, string(result)) |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func TestIntOrStringMarshalJSONUnmarshalYAML(t *testing.T) { |
| 84 | + cases := []struct { |
| 85 | + input Float32OrString |
| 86 | + }{ |
| 87 | + {FromFloat(123.123)}, |
| 88 | + {FromString("123.123")}, |
| 89 | + } |
| 90 | + |
| 91 | + for _, c := range cases { |
| 92 | + input := FloatOrStringHolder{c.input} |
| 93 | + jsonMarshalled, err := json.Marshal(&input) |
| 94 | + if err != nil { |
| 95 | + t.Errorf("1: Failed to marshal input: '%v': %v", input, err) |
| 96 | + } |
| 97 | + |
| 98 | + var result FloatOrStringHolder |
| 99 | + err = yaml.Unmarshal(jsonMarshalled, &result) |
| 100 | + if err != nil { |
| 101 | + t.Errorf("2: Failed to unmarshal '%+v': %v", string(jsonMarshalled), err) |
| 102 | + } |
| 103 | + |
| 104 | + if !reflect.DeepEqual(input, result) { |
| 105 | + t.Errorf("3: Failed to marshal input '%+v': got %+v", input, result) |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments