|
| 1 | +// Copyright 2022 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 model |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/json" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | +) |
| 23 | + |
| 24 | +func TestOperationStateUnmarshalJSON(t *testing.T) { |
| 25 | + type testCase struct { |
| 26 | + desp string |
| 27 | + data string |
| 28 | + expect OperationState |
| 29 | + err string |
| 30 | + } |
| 31 | + testCases := []testCase{ |
| 32 | + { |
| 33 | + desp: "all fields set", |
| 34 | + data: `{"actionMode": "parallel"}`, |
| 35 | + expect: OperationState{ |
| 36 | + ActionMode: ActionModeParallel, |
| 37 | + }, |
| 38 | + err: ``, |
| 39 | + }, |
| 40 | + { |
| 41 | + desp: "actionMode unset", |
| 42 | + data: `{}`, |
| 43 | + expect: OperationState{ |
| 44 | + ActionMode: ActionModeSequential, |
| 45 | + }, |
| 46 | + err: ``, |
| 47 | + }, |
| 48 | + { |
| 49 | + desp: "invalid object format", |
| 50 | + data: `{"actionMode": parallel}`, |
| 51 | + expect: OperationState{ |
| 52 | + ActionMode: ActionModeParallel, |
| 53 | + }, |
| 54 | + err: `invalid character 'p' looking for beginning of value`, |
| 55 | + }, |
| 56 | + } |
| 57 | + for _, tc := range testCases { |
| 58 | + t.Run(tc.desp, func(t *testing.T) { |
| 59 | + v := OperationState{} |
| 60 | + err := json.Unmarshal([]byte(tc.data), &v) |
| 61 | + |
| 62 | + if tc.err != "" { |
| 63 | + assert.Error(t, err) |
| 64 | + assert.Regexp(t, tc.err, err) |
| 65 | + return |
| 66 | + } |
| 67 | + |
| 68 | + assert.NoError(t, err) |
| 69 | + assert.Equal(t, tc.expect, v) |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
0 commit comments