stdlib_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. AddStdlibPkg("foo", "foo doc")
  19. mathFuncMap["Println"] = &ECALFunctionAdapter{reflect.ValueOf(fmt.Println), "foo"}
  20. f, _ := GetStdlibFunc("math.Println")
  21. if s, _ := f.DocString(); s != "foo" {
  22. t.Error("Unexpected result:", s)
  23. return
  24. }
  25. doc, _ := GetPkgDocString("math")
  26. if doc == "" {
  27. t.Error("Unexpected result:", doc)
  28. return
  29. }
  30. doc, _ = GetPkgDocString("foo")
  31. if doc != "foo doc" {
  32. t.Error("Unexpected result:", doc)
  33. return
  34. }
  35. if err := AddStdlibPkg("foo", "foo doc"); err == nil || err.Error() != "Package foo already exists" {
  36. t.Error("Unexpected error:", err)
  37. return
  38. }
  39. }
  40. func TestSymbols(t *testing.T) {
  41. AddStdlibPkg("foo", "foo doc")
  42. AddStdlibFunc("foo", "bar", nil)
  43. p, c, f := GetStdlibSymbols()
  44. if len(p) == 0 || len(c) == 0 || len(f) == 0 {
  45. t.Error("Should have some entries in symbol lists:", p, c, f)
  46. return
  47. }
  48. }
  49. func TestSplitModuleAndName(t *testing.T) {
  50. if m, n := splitModuleAndName("fmt.Println"); m != "fmt" || n != "Println" {
  51. t.Error("Unexpected result:", m, n)
  52. return
  53. }
  54. if m, n := splitModuleAndName(""); m != "" || n != "" {
  55. t.Error("Unexpected result:", m, n)
  56. return
  57. }
  58. if m, n := splitModuleAndName("a"); m != "a" || n != "" {
  59. t.Error("Unexpected result:", m, n)
  60. return
  61. }
  62. if m, n := splitModuleAndName("my.FuncCall"); m != "my" || n != "FuncCall" {
  63. t.Error("Unexpected result:", m, n)
  64. return
  65. }
  66. }
  67. func TestGetStdLibItems(t *testing.T) {
  68. dummyFunc := &ECALFunctionAdapter{}
  69. AddStdlibFunc("foo", "bar", dummyFunc)
  70. mathFuncMap["Println"] = &ECALFunctionAdapter{reflect.ValueOf(fmt.Println), "foo"}
  71. if f, _ := GetStdlibFunc("math.Println"); f != mathFuncMap["Println"] {
  72. t.Error("Unexpected resutl: functions should lookup correctly")
  73. return
  74. }
  75. if f, _ := GetStdlibFunc("foo.bar"); f != dummyFunc {
  76. t.Error("Unexpected resutl: functions should lookup correctly")
  77. return
  78. }
  79. if c, ok := GetStdlibFunc("foo"); c != nil || ok {
  80. t.Error("Unexpected resutl: func should lookup correctly")
  81. return
  82. }
  83. if c, _ := GetStdlibConst("math.Pi"); c != math.Pi {
  84. t.Error("Unexpected resutl: constants should lookup correctly")
  85. return
  86. }
  87. if c, ok := GetStdlibConst("foo"); c != nil || ok {
  88. t.Error("Unexpected resutl: constants should lookup correctly")
  89. return
  90. }
  91. }
  92. func TestAddStdLibFunc(t *testing.T) {
  93. dummyFunc := &ECALFunctionAdapter{}
  94. AddStdlibFunc("foo", "bar", dummyFunc)
  95. if f, _ := GetStdlibFunc("foo.bar"); f != dummyFunc {
  96. t.Error("Unexpected resutl: functions should lookup correctly")
  97. return
  98. }
  99. if err := AddStdlibFunc("foo2", "bar", dummyFunc); err == nil || err.Error() != "Package foo2 does not exist" {
  100. t.Error("Unexpected error:", err)
  101. return
  102. }
  103. }