123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- /*
- * ECAL
- *
- * Copyright 2020 Matthias Ladkau. All rights reserved.
- *
- * This Source Code Form is subject to the terms of the MIT
- * License, If a copy of the MIT License was not distributed with this
- * file, You can obtain one at https://opensource.org/licenses/MIT.
- */
- package stdlib
- import (
- "fmt"
- "math"
- "reflect"
- "strconv"
- "testing"
- "devt.de/krotik/common/errorutil"
- "devt.de/krotik/ecal/scope"
- )
- func TestECALFunctionAdapter(t *testing.T) {
- res, err := runAdapterTest(
- reflect.ValueOf(strconv.Atoi),
- []interface{}{"1"},
- )
- if err != nil || res != float64(1) {
- t.Error("Unexpected result: ", res, err)
- return
- }
- res, err = runAdapterTest(
- reflect.ValueOf(strconv.ParseUint),
- []interface{}{"123", float64(0), float64(0)},
- )
- if err != nil || res != float64(123) {
- t.Error("Unexpected result: ", res, err)
- return
- }
- res, err = runAdapterTest(
- reflect.ValueOf(strconv.ParseFloat),
- []interface{}{"123.123", float64(0)},
- )
- if err != nil || res != float64(123.123) {
- t.Error("Unexpected result: ", res, err)
- return
- }
- res, err = runAdapterTest(
- reflect.ValueOf(fmt.Sprintf),
- []interface{}{"foo %v", "bar"},
- )
- if err != nil || res != "foo bar" {
- t.Error("Unexpected result: ", res, err)
- return
- }
- res, err = runAdapterTest(
- reflect.ValueOf(math.Float32bits),
- []interface{}{float64(11)},
- )
- if r := fmt.Sprintf("%X", uint32(res.(float64))); err != nil || r != fmt.Sprintf("%X", math.Float32bits(11)) {
- t.Error("Unexpected result: ", r, err)
- return
- }
- res, err = runAdapterTest(
- reflect.ValueOf(math.Float32frombits),
- []interface{}{float64(math.Float32bits(11))},
- )
- if r := fmt.Sprintf("%v", res.(float64)); err != nil || r != "11" {
- t.Error("Unexpected result: ", r, err)
- return
- }
- res, err = runAdapterTest(
- reflect.ValueOf(math.Float32frombits),
- []interface{}{math.Float32bits(11)}, // Giving the correct type also works
- )
- if r := fmt.Sprintf("%v", res.(float64)); err != nil || r != "11" {
- t.Error("Unexpected result: ", r, err)
- return
- }
- res, err = runAdapterTest(
- reflect.ValueOf(math.Float64bits),
- []interface{}{float64(11)},
- )
- if r := fmt.Sprintf("%X", uint64(res.(float64))); err != nil || r != fmt.Sprintf("%X", math.Float64bits(11)) {
- t.Error("Unexpected result: ", r, err)
- return
- }
- res, err = runAdapterTest(
- reflect.ValueOf(math.Float64frombits),
- []interface{}{float64(math.Float64bits(11))},
- )
- if r := fmt.Sprintf("%v", res.(float64)); err != nil || r != "11" {
- t.Error("Unexpected result: ", r, err)
- return
- }
- res, err = runAdapterTest(
- reflect.ValueOf(dummyUint),
- []interface{}{float64(1)},
- )
- if err != nil || res != "1" {
- t.Error("Unexpected result: ", res, err)
- return
- }
- res, err = runAdapterTest(
- reflect.ValueOf(dummyUint8),
- []interface{}{float64(1)},
- )
- if err != nil || res != "1" {
- t.Error("Unexpected result: ", res, err)
- return
- }
- res, err = runAdapterTest(
- reflect.ValueOf(dummyUint16),
- []interface{}{float64(1)},
- )
- if err != nil || res != "1" {
- t.Error("Unexpected result: ", res, err)
- return
- }
- res, err = runAdapterTest(
- reflect.ValueOf(dummyUintptr),
- []interface{}{float64(1)},
- )
- if err != nil || res != "1" {
- t.Error("Unexpected result: ", res, err)
- return
- }
- res, err = runAdapterTest(
- reflect.ValueOf(dummyInt8),
- []interface{}{float64(1)},
- )
- if err != nil || res != "1" {
- t.Error("Unexpected result: ", res, err)
- return
- }
- res, err = runAdapterTest(
- reflect.ValueOf(dummyInt16),
- []interface{}{float64(1)},
- )
- if err != nil || res != "1" {
- t.Error("Unexpected result: ", res, err)
- return
- }
- res, err = runAdapterTest(
- reflect.ValueOf(dummyInt32),
- []interface{}{float64(1)},
- )
- if err != nil || res != "1" {
- t.Error("Unexpected result: ", res, err)
- return
- }
- res, err = runAdapterTest(
- reflect.ValueOf(dummyInt64),
- []interface{}{float64(1)},
- )
- if err != nil || res != "1" {
- t.Error("Unexpected result: ", res, err)
- return
- }
- // Test Error cases
- res, err = runAdapterTest(
- reflect.ValueOf(strconv.ParseFloat),
- []interface{}{"123.123", 0, 0},
- )
- if err == nil || err.Error() != "Too many parameters - got 3 expected 2" {
- t.Error("Unexpected result: ", res, err)
- return
- }
- res, err = runAdapterTest(
- reflect.ValueOf(strconv.ParseFloat),
- []interface{}{"Hans", 0},
- )
- if err == nil || err.Error() != `strconv.ParseFloat: parsing "Hans": invalid syntax` {
- t.Error("Unexpected result: ", res, err)
- return
- }
- res, err = runAdapterTest(
- reflect.ValueOf(strconv.ParseFloat),
- []interface{}{123, 0},
- )
- if err == nil || err.Error() != `Parameter 1 should be of type string but is of type int` {
- t.Error("Unexpected result: ", res, err)
- return
- }
- // Make sure we are never panicing but just returning an error
- res, err = runAdapterTest(
- reflect.ValueOf(errorutil.AssertTrue),
- []interface{}{false, "Some Panic Description"},
- )
- if err == nil || err.Error() != `Error: Some Panic Description` {
- t.Error("Unexpected result: ", res, err)
- return
- }
- // Get documentation
- afuncEcal := &ECALFunctionAdapter{reflect.ValueOf(fmt.Sprint)}
- if s, err := afuncEcal.DocString(); s == "" || err != nil {
- t.Error("Docstring should return something")
- return
- }
- }
- func runAdapterTest(afunc reflect.Value, args []interface{}) (interface{}, error) {
- afuncEcal := &ECALFunctionAdapter{afunc}
- return afuncEcal.Run("test", scope.NewScope(""), make(map[string]interface{}), args)
- }
- func dummyUint(v uint) string {
- return fmt.Sprint(v)
- }
- func dummyUint8(v uint8) string {
- return fmt.Sprint(v)
- }
- func dummyUint16(v uint16) string {
- return fmt.Sprint(v)
- }
- func dummyUintptr(v uintptr) string {
- return fmt.Sprint(v)
- }
- func dummyInt8(v int8) string {
- return fmt.Sprint(v)
- }
- func dummyInt16(v int16) string {
- return fmt.Sprint(v)
- }
- func dummyInt32(v int32) string {
- return fmt.Sprint(v)
- }
- func dummyInt64(v int64) string {
- return fmt.Sprint(v)
- }
|