adapter_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package stdlib
  11. import (
  12. "fmt"
  13. "math"
  14. "reflect"
  15. "strconv"
  16. "testing"
  17. "devt.de/krotik/common/errorutil"
  18. "devt.de/krotik/ecal/scope"
  19. )
  20. func TestECALFunctionAdapter(t *testing.T) {
  21. res, err := runAdapterTest(
  22. reflect.ValueOf(strconv.Atoi),
  23. []interface{}{"1"},
  24. )
  25. if err != nil || res != float64(1) {
  26. t.Error("Unexpected result: ", res, err)
  27. return
  28. }
  29. res, err = runAdapterTest(
  30. reflect.ValueOf(strconv.ParseUint),
  31. []interface{}{"123", float64(0), float64(0)},
  32. )
  33. if err != nil || res != float64(123) {
  34. t.Error("Unexpected result: ", res, err)
  35. return
  36. }
  37. res, err = runAdapterTest(
  38. reflect.ValueOf(strconv.ParseFloat),
  39. []interface{}{"123.123", float64(0)},
  40. )
  41. if err != nil || res != float64(123.123) {
  42. t.Error("Unexpected result: ", res, err)
  43. return
  44. }
  45. res, err = runAdapterTest(
  46. reflect.ValueOf(fmt.Sprintf),
  47. []interface{}{"foo %v", "bar"},
  48. )
  49. if err != nil || res != "foo bar" {
  50. t.Error("Unexpected result: ", res, err)
  51. return
  52. }
  53. res, err = runAdapterTest(
  54. reflect.ValueOf(math.Float32bits),
  55. []interface{}{float64(11)},
  56. )
  57. if r := fmt.Sprintf("%X", uint32(res.(float64))); err != nil || r != fmt.Sprintf("%X", math.Float32bits(11)) {
  58. t.Error("Unexpected result: ", r, err)
  59. return
  60. }
  61. res, err = runAdapterTest(
  62. reflect.ValueOf(math.Float32frombits),
  63. []interface{}{float64(math.Float32bits(11))},
  64. )
  65. if r := fmt.Sprintf("%v", res.(float64)); err != nil || r != "11" {
  66. t.Error("Unexpected result: ", r, err)
  67. return
  68. }
  69. res, err = runAdapterTest(
  70. reflect.ValueOf(math.Float32frombits),
  71. []interface{}{math.Float32bits(11)}, // Giving the correct type also works
  72. )
  73. if r := fmt.Sprintf("%v", res.(float64)); err != nil || r != "11" {
  74. t.Error("Unexpected result: ", r, err)
  75. return
  76. }
  77. res, err = runAdapterTest(
  78. reflect.ValueOf(math.Float64bits),
  79. []interface{}{float64(11)},
  80. )
  81. if r := fmt.Sprintf("%X", uint64(res.(float64))); err != nil || r != fmt.Sprintf("%X", math.Float64bits(11)) {
  82. t.Error("Unexpected result: ", r, err)
  83. return
  84. }
  85. res, err = runAdapterTest(
  86. reflect.ValueOf(math.Float64frombits),
  87. []interface{}{float64(math.Float64bits(11))},
  88. )
  89. if r := fmt.Sprintf("%v", res.(float64)); err != nil || r != "11" {
  90. t.Error("Unexpected result: ", r, err)
  91. return
  92. }
  93. res, err = runAdapterTest(
  94. reflect.ValueOf(dummyUint),
  95. []interface{}{float64(1)},
  96. )
  97. if err != nil || res != "1" {
  98. t.Error("Unexpected result: ", res, err)
  99. return
  100. }
  101. res, err = runAdapterTest(
  102. reflect.ValueOf(dummyUint8),
  103. []interface{}{float64(1)},
  104. )
  105. if err != nil || res != "1" {
  106. t.Error("Unexpected result: ", res, err)
  107. return
  108. }
  109. res, err = runAdapterTest(
  110. reflect.ValueOf(dummyUint16),
  111. []interface{}{float64(1)},
  112. )
  113. if err != nil || res != "1" {
  114. t.Error("Unexpected result: ", res, err)
  115. return
  116. }
  117. res, err = runAdapterTest(
  118. reflect.ValueOf(dummyUintptr),
  119. []interface{}{float64(1)},
  120. )
  121. if err != nil || res != "1" {
  122. t.Error("Unexpected result: ", res, err)
  123. return
  124. }
  125. res, err = runAdapterTest(
  126. reflect.ValueOf(dummyInt8),
  127. []interface{}{float64(1)},
  128. )
  129. if err != nil || res != "1" {
  130. t.Error("Unexpected result: ", res, err)
  131. return
  132. }
  133. res, err = runAdapterTest(
  134. reflect.ValueOf(dummyInt16),
  135. []interface{}{float64(1)},
  136. )
  137. if err != nil || res != "1" {
  138. t.Error("Unexpected result: ", res, err)
  139. return
  140. }
  141. res, err = runAdapterTest(
  142. reflect.ValueOf(dummyInt32),
  143. []interface{}{float64(1)},
  144. )
  145. if err != nil || res != "1" {
  146. t.Error("Unexpected result: ", res, err)
  147. return
  148. }
  149. res, err = runAdapterTest(
  150. reflect.ValueOf(dummyInt64),
  151. []interface{}{float64(1)},
  152. )
  153. if err != nil || res != "1" {
  154. t.Error("Unexpected result: ", res, err)
  155. return
  156. }
  157. // Test Error cases
  158. res, err = runAdapterTest(
  159. reflect.ValueOf(strconv.ParseFloat),
  160. []interface{}{"123.123", 0, 0},
  161. )
  162. if err == nil || err.Error() != "Too many parameters - got 3 expected 2" {
  163. t.Error("Unexpected result: ", res, err)
  164. return
  165. }
  166. res, err = runAdapterTest(
  167. reflect.ValueOf(strconv.ParseFloat),
  168. []interface{}{"Hans", 0},
  169. )
  170. if err == nil || err.Error() != `strconv.ParseFloat: parsing "Hans": invalid syntax` {
  171. t.Error("Unexpected result: ", res, err)
  172. return
  173. }
  174. res, err = runAdapterTest(
  175. reflect.ValueOf(strconv.ParseFloat),
  176. []interface{}{123, 0},
  177. )
  178. if err == nil || err.Error() != `Parameter 1 should be of type string but is of type int` {
  179. t.Error("Unexpected result: ", res, err)
  180. return
  181. }
  182. // Make sure we are never panicing but just returning an error
  183. res, err = runAdapterTest(
  184. reflect.ValueOf(errorutil.AssertTrue),
  185. []interface{}{false, "Some Panic Description"},
  186. )
  187. if err == nil || err.Error() != `Error: Some Panic Description` {
  188. t.Error("Unexpected result: ", res, err)
  189. return
  190. }
  191. // Get documentation
  192. afuncEcal := &ECALFunctionAdapter{reflect.ValueOf(fmt.Sprint)}
  193. if s, err := afuncEcal.DocString(); s == "" || err != nil {
  194. t.Error("Docstring should return something")
  195. return
  196. }
  197. }
  198. func runAdapterTest(afunc reflect.Value, args []interface{}) (interface{}, error) {
  199. afuncEcal := &ECALFunctionAdapter{afunc}
  200. return afuncEcal.Run("test", scope.NewScope(""), make(map[string]interface{}), args)
  201. }
  202. func dummyUint(v uint) string {
  203. return fmt.Sprint(v)
  204. }
  205. func dummyUint8(v uint8) string {
  206. return fmt.Sprint(v)
  207. }
  208. func dummyUint16(v uint16) string {
  209. return fmt.Sprint(v)
  210. }
  211. func dummyUintptr(v uintptr) string {
  212. return fmt.Sprint(v)
  213. }
  214. func dummyInt8(v int8) string {
  215. return fmt.Sprint(v)
  216. }
  217. func dummyInt16(v int16) string {
  218. return fmt.Sprint(v)
  219. }
  220. func dummyInt32(v int32) string {
  221. return fmt.Sprint(v)
  222. }
  223. func dummyInt64(v int64) string {
  224. return fmt.Sprint(v)
  225. }