Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * 'License'); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | package tests |
| 21 | |
| 22 | import ( |
| 23 | "reflect" |
| 24 | "testing" |
Yuxuan 'fishy' Wang | b71f11e | 2021-03-22 15:01:00 -0700 | [diff] [blame] | 25 | |
| 26 | "github.com/apache/thrift/lib/go/test/gopath/src/thrifttest" |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | type ThriftTestDriver struct { |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 30 | client thrifttest.ThriftTest |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 31 | t *testing.T |
| 32 | } |
| 33 | |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 34 | func NewThriftTestDriver(t *testing.T, client thrifttest.ThriftTest) *ThriftTestDriver { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 35 | return &ThriftTestDriver{client, t} |
| 36 | } |
| 37 | |
| 38 | func (p *ThriftTestDriver) Start() { |
| 39 | client := p.client |
| 40 | t := p.t |
| 41 | |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 42 | if client.TestVoid(defaultCtx) != nil { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 43 | t.Fatal("TestVoid failed") |
| 44 | } |
| 45 | |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 46 | if r, err := client.TestString(defaultCtx, "Test"); r != "Test" || err != nil { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 47 | t.Fatal("TestString with simple text failed") |
| 48 | } |
| 49 | |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 50 | if r, err := client.TestString(defaultCtx, ""); r != "" || err != nil { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 51 | t.Fatal("TestString with empty text failed") |
| 52 | } |
| 53 | |
| 54 | stringTest := "Afrikaans, Alemannisch, Aragonés, العربية, مصرى, " + |
| 55 | "Asturianu, Aymar aru, Azərbaycan, Башҡорт, Boarisch, Žemaitėška, " + |
| 56 | "Беларуская, Беларуская (тарашкевіца), Български, Bamanankan, " + |
| 57 | "বাংলা, Brezhoneg, Bosanski, Català, Mìng-dĕ̤ng-ngṳ̄, Нохчийн, " + |
| 58 | "Cebuano, ᏣᎳᎩ, Česky, Словѣ́ньскъ / ⰔⰎⰑⰂⰡⰐⰠⰔⰍⰟ, Чӑвашла, Cymraeg, " + |
| 59 | "Dansk, Zazaki, ދިވެހިބަސް, Ελληνικά, Emiliàn e rumagnòl, English, " + |
| 60 | "Esperanto, Español, Eesti, Euskara, فارسی, Suomi, Võro, Føroyskt, " + |
| 61 | "Français, Arpetan, Furlan, Frysk, Gaeilge, 贛語, Gàidhlig, Galego, " + |
| 62 | "Avañe'ẽ, ગુજરાતી, Gaelg, עברית, हिन्दी, Fiji Hindi, Hrvatski, " + |
| 63 | "Kreyòl ayisyen, Magyar, Հայերեն, Interlingua, Bahasa Indonesia, " + |
| 64 | "Ilokano, Ido, Íslenska, Italiano, 日本語, Lojban, Basa Jawa, " + |
| 65 | "ქართული, Kongo, Kalaallisut, ಕನ್ನಡ, 한국어, Къарачай-Малкъар, " + |
| 66 | "Ripoarisch, Kurdî, Коми, Kernewek, Кыргызча, Latina, Ladino, " + |
| 67 | "Lëtzebuergesch, Limburgs, Lingála, ລາວ, Lietuvių, Latviešu, Basa " + |
| 68 | "Banyumasan, Malagasy, Македонски, മലയാളം, मराठी, مازِرونی, Bahasa " + |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame^] | 69 | //lint:ignore ST1018 intentionally use unicode characters here |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 70 | "Melayu, Nnapulitano, Nedersaksisch, नेपाल भाषा, Nederlands, " + |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame^] | 71 | //lint:ignore ST1018 intentionally use unicode characters here |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 72 | "Norsk (nynorsk), Norsk (bokmål), Nouormand, Diné bizaad, " + |
| 73 | "Occitan, Иронау, Papiamentu, Deitsch, Polski, پنجابی, پښتو, " + |
| 74 | "Norfuk / Pitkern, Português, Runa Simi, Rumantsch, Romani, Română, " + |
| 75 | "Русский, Саха тыла, Sardu, Sicilianu, Scots, Sámegiella, Simple " + |
| 76 | "English, Slovenčina, Slovenščina, Српски / Srpski, Seeltersk, " + |
| 77 | "Svenska, Kiswahili, தமிழ், తెలుగు, Тоҷикӣ, ไทย, Türkmençe, Tagalog, " + |
| 78 | "Türkçe, Татарча/Tatarça, Українська, اردو, Tiếng Việt, Volapük, " + |
| 79 | "Walon, Winaray, 吴语, isiXhosa, ייִדיש, Yorùbá, Zeêuws, 中文, " + |
| 80 | "Bân-lâm-gú, 粵語" |
| 81 | |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 82 | if r, err := client.TestString(defaultCtx, stringTest); r != stringTest || err != nil { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 83 | t.Fatal("TestString with all languages failed") |
| 84 | } |
| 85 | |
| 86 | specialCharacters := "quote: \" backslash:" + |
| 87 | " backspace: \b formfeed: \f newline: \n return: \r tab: " + |
| 88 | " now-all-of-them-together: '\\\b\n\r\t'" + |
| 89 | " now-a-bunch-of-junk: !@#$%&()(&%$#{}{}<><><" + |
| 90 | " char-to-test-json-parsing: ]] \"]] \\\" }}}{ [[[ " |
| 91 | |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 92 | if r, err := client.TestString(defaultCtx, specialCharacters); r != specialCharacters || err != nil { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 93 | t.Fatal("TestString with specialCharacters failed") |
| 94 | } |
| 95 | |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 96 | if r, err := client.TestByte(defaultCtx, 1); r != 1 || err != nil { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 97 | t.Fatal("TestByte(1) failed") |
| 98 | } |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 99 | if r, err := client.TestByte(defaultCtx, 0); r != 0 || err != nil { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 100 | t.Fatal("TestByte(0) failed") |
| 101 | } |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 102 | if r, err := client.TestByte(defaultCtx, -1); r != -1 || err != nil { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 103 | t.Fatal("TestByte(-1) failed") |
| 104 | } |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 105 | if r, err := client.TestByte(defaultCtx, -127); r != -127 || err != nil { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 106 | t.Fatal("TestByte(-127) failed") |
| 107 | } |
| 108 | |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 109 | if r, err := client.TestI32(defaultCtx, -1); r != -1 || err != nil { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 110 | t.Fatal("TestI32(-1) failed") |
| 111 | } |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 112 | if r, err := client.TestI32(defaultCtx, 1); r != 1 || err != nil { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 113 | t.Fatal("TestI32(1) failed") |
| 114 | } |
| 115 | |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 116 | if r, err := client.TestI64(defaultCtx, -5); r != -5 || err != nil { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 117 | t.Fatal("TestI64(-5) failed") |
| 118 | } |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 119 | if r, err := client.TestI64(defaultCtx, 5); r != 5 || err != nil { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 120 | t.Fatal("TestI64(5) failed") |
| 121 | } |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 122 | if r, err := client.TestI64(defaultCtx, -34359738368); r != -34359738368 || err != nil { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 123 | t.Fatal("TestI64(-34359738368) failed") |
| 124 | } |
| 125 | |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 126 | if r, err := client.TestDouble(defaultCtx, -5.2098523); r != -5.2098523 || err != nil { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 127 | t.Fatal("TestDouble(-5.2098523) failed") |
| 128 | } |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 129 | if r, err := client.TestDouble(defaultCtx, -7.012052175215044); r != -7.012052175215044 || err != nil { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 130 | t.Fatal("TestDouble(-7.012052175215044) failed") |
| 131 | } |
| 132 | |
Jens Geyer | 8bcfdd9 | 2014-12-14 03:14:26 +0100 | [diff] [blame] | 133 | // TODO: add testBinary() call |
| 134 | |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 135 | out := thrifttest.NewXtruct() |
| 136 | out.StringThing = "Zero" |
| 137 | out.ByteThing = 1 |
| 138 | out.I32Thing = -3 |
| 139 | out.I64Thing = 1000000 |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 140 | if r, err := client.TestStruct(defaultCtx, out); !reflect.DeepEqual(r, out) || err != nil { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 141 | t.Fatal("TestStruct failed") |
| 142 | } |
| 143 | |
| 144 | out2 := thrifttest.NewXtruct2() |
| 145 | out2.ByteThing = 1 |
| 146 | out2.StructThing = out |
| 147 | out2.I32Thing = 5 |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 148 | if r, err := client.TestNest(defaultCtx, out2); !reflect.DeepEqual(r, out2) || err != nil { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 149 | t.Fatal("TestNest failed") |
| 150 | } |
| 151 | |
| 152 | mapout := make(map[int32]int32) |
| 153 | for i := int32(0); i < 5; i++ { |
| 154 | mapout[i] = i - 10 |
| 155 | } |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 156 | if r, err := client.TestMap(defaultCtx, mapout); !reflect.DeepEqual(r, mapout) || err != nil { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 157 | t.Fatal("TestMap failed") |
| 158 | } |
| 159 | |
| 160 | mapTestInput := map[string]string{ |
| 161 | "a": "123", "a b": "with spaces ", "same": "same", "0": "numeric key", |
| 162 | "longValue": stringTest, stringTest: "long key", |
| 163 | } |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 164 | if r, err := client.TestStringMap(defaultCtx, mapTestInput); !reflect.DeepEqual(r, mapTestInput) || err != nil { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 165 | t.Fatal("TestStringMap failed") |
| 166 | } |
| 167 | |
D. Can Celasun | 43fb34d | 2017-01-15 10:53:19 +0100 | [diff] [blame] | 168 | setTestInput := []int32{1, 2, 3} |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 169 | if r, err := client.TestSet(defaultCtx, setTestInput); !reflect.DeepEqual(r, setTestInput) || err != nil { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 170 | t.Fatal("TestSet failed") |
| 171 | } |
| 172 | |
| 173 | listTest := []int32{1, 2, 3} |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 174 | if r, err := client.TestList(defaultCtx, listTest); !reflect.DeepEqual(r, listTest) || err != nil { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 175 | t.Fatal("TestList failed") |
| 176 | } |
| 177 | |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 178 | if r, err := client.TestEnum(defaultCtx, thrifttest.Numberz_ONE); r != thrifttest.Numberz_ONE || err != nil { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 179 | t.Fatal("TestEnum failed") |
| 180 | } |
| 181 | |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 182 | if r, err := client.TestTypedef(defaultCtx, 69); r != 69 || err != nil { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 183 | t.Fatal("TestTypedef failed") |
| 184 | } |
| 185 | |
| 186 | mapMapTest := map[int32]map[int32]int32{ |
| 187 | 4: {1: 1, 2: 2, 3: 3, 4: 4}, |
| 188 | -4: {-4: -4, -3: -3, -2: -2, -1: -1}, |
| 189 | } |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 190 | if r, err := client.TestMapMap(defaultCtx, 1); !reflect.DeepEqual(r, mapMapTest) || err != nil { |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 191 | t.Fatal("TestMapMap failed") |
| 192 | } |
| 193 | |
| 194 | crazyX1 := thrifttest.NewXtruct() |
| 195 | crazyX1.StringThing = "Goodbye4" |
| 196 | crazyX1.ByteThing = 4 |
| 197 | crazyX1.I32Thing = 4 |
| 198 | crazyX1.I64Thing = 4 |
| 199 | |
| 200 | crazyX2 := thrifttest.NewXtruct() |
| 201 | crazyX2.StringThing = "Hello2" |
| 202 | crazyX2.ByteThing = 2 |
| 203 | crazyX2.I32Thing = 2 |
| 204 | crazyX2.I64Thing = 2 |
| 205 | |
| 206 | crazy := thrifttest.NewInsanity() |
| 207 | crazy.UserMap = map[thrifttest.Numberz]thrifttest.UserId{5: 5, 8: 8} |
| 208 | crazy.Xtructs = []*thrifttest.Xtruct{crazyX1, crazyX2} |
| 209 | |
| 210 | crazyEmpty := thrifttest.NewInsanity() |
| 211 | crazyEmpty.UserMap = map[thrifttest.Numberz]thrifttest.UserId{} |
| 212 | crazyEmpty.Xtructs = []*thrifttest.Xtruct{} |
| 213 | |
| 214 | insanity := map[thrifttest.UserId]map[thrifttest.Numberz]*thrifttest.Insanity{ |
| 215 | 1: {thrifttest.Numberz_TWO: crazy, thrifttest.Numberz_THREE: crazy}, |
| 216 | 2: {thrifttest.Numberz_SIX: crazyEmpty}, |
| 217 | } |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 218 | if r, err := client.TestInsanity(defaultCtx, crazy); !reflect.DeepEqual(r, insanity) || err != nil { |
Yuxuan 'fishy' Wang | 4653009 | 2019-11-05 13:31:13 -0800 | [diff] [blame] | 219 | t.Fatal("TestInsanity failed:", err) |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 220 | } |
| 221 | |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 222 | if err := client.TestException(defaultCtx, "TException"); err == nil { |
Yuxuan 'fishy' Wang | 4653009 | 2019-11-05 13:31:13 -0800 | [diff] [blame] | 223 | t.Fatal("TestException TException failed:", err) |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 224 | } |
| 225 | |
Yuxuan 'fishy' Wang | 4653009 | 2019-11-05 13:31:13 -0800 | [diff] [blame] | 226 | err := client.TestException(defaultCtx, "Xception") |
Yuxuan 'fishy' Wang | 17373a3 | 2021-08-26 11:04:27 -0700 | [diff] [blame^] | 227 | if e, ok := err.(*thrifttest.Xception); !ok || e == nil { |
Yuxuan 'fishy' Wang | 4653009 | 2019-11-05 13:31:13 -0800 | [diff] [blame] | 228 | t.Fatal("TestException Xception failed:", err) |
| 229 | } else if e.ErrorCode != 1001 || e.Message != "Xception" { |
| 230 | t.Fatal("TestException Xception failed:", e) |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 231 | } |
| 232 | |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 233 | if err := client.TestException(defaultCtx, "no Exception"); err != nil { |
Yuxuan 'fishy' Wang | 4653009 | 2019-11-05 13:31:13 -0800 | [diff] [blame] | 234 | t.Fatal("TestException no Exception failed:", err) |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 235 | } |
| 236 | |
taozle | 5c302e0 | 2017-07-23 15:21:44 +0200 | [diff] [blame] | 237 | if err := client.TestOneway(defaultCtx, 0); err != nil { |
Yuxuan 'fishy' Wang | 4653009 | 2019-11-05 13:31:13 -0800 | [diff] [blame] | 238 | t.Fatal("TestOneway failed:", err) |
Jens Geyer | 5f9bdff | 2014-11-18 21:57:03 +0100 | [diff] [blame] | 239 | } |
| 240 | } |