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