stdlib_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. "testing"
  16. )
  17. func TestGetPkgDocString(t *testing.T) {
  18. mathFuncMap["Println"] = &ECALFunctionAdapter{reflect.ValueOf(fmt.Println), "foo"}
  19. f, _ := GetStdlibFunc("math.Println")
  20. if s, _ := f.DocString(); s != "foo" {
  21. t.Error("Unexpected result:", s)
  22. return
  23. }
  24. doc, _ := GetPkgDocString("math")
  25. if doc == "" {
  26. t.Error("Unexpected result:", doc)
  27. return
  28. }
  29. }
  30. func TestSymbols(t *testing.T) {
  31. p, c, f := GetStdlibSymbols()
  32. if len(p) == 0 || len(c) == 0 || len(f) == 0 {
  33. t.Error("Should have some entries in symbol lists:", p, c, f)
  34. return
  35. }
  36. }
  37. func TestSplitModuleAndName(t *testing.T) {
  38. if m, n := splitModuleAndName("fmt.Println"); m != "fmt" || n != "Println" {
  39. t.Error("Unexpected result:", m, n)
  40. return
  41. }
  42. if m, n := splitModuleAndName(""); m != "" || n != "" {
  43. t.Error("Unexpected result:", m, n)
  44. return
  45. }
  46. if m, n := splitModuleAndName("a"); m != "a" || n != "" {
  47. t.Error("Unexpected result:", m, n)
  48. return
  49. }
  50. if m, n := splitModuleAndName("my.FuncCall"); m != "my" || n != "FuncCall" {
  51. t.Error("Unexpected result:", m, n)
  52. return
  53. }
  54. }
  55. func TestGetStdLibItems(t *testing.T) {
  56. mathFuncMap["Println"] = &ECALFunctionAdapter{reflect.ValueOf(fmt.Println), "foo"}
  57. if f, _ := GetStdlibFunc("math.Println"); f != mathFuncMap["Println"] {
  58. t.Error("Unexpected resutl: functions should lookup correctly")
  59. return
  60. }
  61. if c, ok := GetStdlibFunc("foo"); c != nil || ok {
  62. t.Error("Unexpected resutl: constants should lookup correctly")
  63. return
  64. }
  65. if c, _ := GetStdlibConst("math.Pi"); c != math.Pi {
  66. t.Error("Unexpected resutl: constants should lookup correctly")
  67. return
  68. }
  69. if c, ok := GetStdlibConst("foo"); c != nil || ok {
  70. t.Error("Unexpected resutl: constants should lookup correctly")
  71. return
  72. }
  73. }