|
1 | | -/* |
2 | | - * |
3 | | - * k6 - a next-generation load testing tool |
4 | | - * Copyright (C) 2016 Load Impact |
5 | | - * |
6 | | - * This program is free software: you can redistribute it and/or modify |
7 | | - * it under the terms of the GNU Affero General Public License as |
8 | | - * published by the Free Software Foundation, either version 3 of the |
9 | | - * License, or (at your option) any later version. |
10 | | - * |
11 | | - * This program is distributed in the hope that it will be useful, |
12 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | - * GNU Affero General Public License for more details. |
15 | | - * |
16 | | - * You should have received a copy of the GNU Affero General Public License |
17 | | - * along with this program. If not, see <http://www.gnu.org/licenses/>. |
18 | | - * |
19 | | - */ |
20 | | - |
21 | 1 | package js |
22 | 2 |
|
23 | 3 | import ( |
@@ -107,6 +87,87 @@ func extractLogger(fl logrus.FieldLogger) *logrus.Logger { |
107 | 87 | return nil |
108 | 88 | } |
109 | 89 |
|
| 90 | +func TestConsoleLogWithGojaNativeObject(t *testing.T) { |
| 91 | + t.Parallel() |
| 92 | + |
| 93 | + rt := goja.New() |
| 94 | + rt.SetFieldNameMapper(common.FieldNameMapper{}) |
| 95 | + |
| 96 | + obj := rt.NewObject() |
| 97 | + err := obj.Set("text", "nativeObject") |
| 98 | + require.NoError(t, err) |
| 99 | + |
| 100 | + logger := testutils.NewLogger(t) |
| 101 | + hook := logtest.NewLocal(logger) |
| 102 | + |
| 103 | + c := newConsole(logger) |
| 104 | + c.Log(obj) |
| 105 | + |
| 106 | + entry := hook.LastEntry() |
| 107 | + require.NotNil(t, entry, "nothing logged") |
| 108 | + assert.JSONEq(t, `{"text":"nativeObject"}`, entry.Message) |
| 109 | +} |
| 110 | + |
| 111 | +func TestConsoleLogObjectsWithGoTypes(t *testing.T) { |
| 112 | + t.Parallel() |
| 113 | + |
| 114 | + type value struct { |
| 115 | + Text string |
| 116 | + } |
| 117 | + |
| 118 | + tests := []struct { |
| 119 | + name string |
| 120 | + in interface{} |
| 121 | + exp string |
| 122 | + }{ |
| 123 | + { |
| 124 | + name: "StructLiteral", |
| 125 | + in: value{ |
| 126 | + Text: "test1", |
| 127 | + }, |
| 128 | + exp: `{"text":"test1"}`, |
| 129 | + }, |
| 130 | + { |
| 131 | + name: "StructPointer", |
| 132 | + in: &value{ |
| 133 | + Text: "test2", |
| 134 | + }, |
| 135 | + exp: `{"text":"test2"}`, |
| 136 | + }, |
| 137 | + { |
| 138 | + name: "Map", |
| 139 | + in: map[string]interface{}{ |
| 140 | + "text": "test3", |
| 141 | + }, |
| 142 | + exp: `{"text":"test3"}`, |
| 143 | + }, |
| 144 | + } |
| 145 | + |
| 146 | + expFields := logrus.Fields{"source": "console"} |
| 147 | + for _, tt := range tests { |
| 148 | + tt := tt |
| 149 | + |
| 150 | + t.Run(tt.name, func(t *testing.T) { |
| 151 | + t.Parallel() |
| 152 | + |
| 153 | + rt := goja.New() |
| 154 | + rt.SetFieldNameMapper(common.FieldNameMapper{}) |
| 155 | + obj := rt.ToValue(tt.in) |
| 156 | + |
| 157 | + logger := testutils.NewLogger(t) |
| 158 | + hook := logtest.NewLocal(logger) |
| 159 | + |
| 160 | + c := newConsole(logger) |
| 161 | + c.Log(obj) |
| 162 | + |
| 163 | + entry := hook.LastEntry() |
| 164 | + require.NotNil(t, entry, "nothing logged") |
| 165 | + assert.JSONEq(t, tt.exp, entry.Message) |
| 166 | + assert.Equal(t, expFields, entry.Data) |
| 167 | + }) |
| 168 | + } |
| 169 | +} |
| 170 | + |
110 | 171 | func TestConsoleLog(t *testing.T) { |
111 | 172 | t.Parallel() |
112 | 173 |
|
|
0 commit comments