generate_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 main
  11. import (
  12. "bytes"
  13. "fmt"
  14. "io/ioutil"
  15. "os"
  16. "testing"
  17. )
  18. const InvalidFileName = "**" + string(0x0)
  19. func TestGenerate(t *testing.T) {
  20. filename = InvalidFileName
  21. generateDoc = false
  22. var buf bytes.Buffer
  23. stderrPrint = func(v ...interface{}) (int, error) {
  24. buf.WriteString(fmt.Sprint(v...))
  25. return 0, nil
  26. }
  27. stdoutPrint = func(v ...interface{}) (int, error) {
  28. return 0, nil
  29. }
  30. main()
  31. if buf.String() != "Error:open **"+string(0)+": invalid argument" {
  32. t.Error("Unexpected output:", buf.String())
  33. return
  34. }
  35. filename = "test_out.txt"
  36. pkgNames = map[string][]string{
  37. "math": {"Pi"},
  38. "fmt": {"Println"},
  39. }
  40. defer func() {
  41. os.Remove(filename)
  42. }()
  43. main()
  44. out, err := ioutil.ReadFile(filename)
  45. if err != nil {
  46. t.Error("Could not read file:", filename, " ", err)
  47. return
  48. }
  49. if string(out) != `
  50. // Code generated by ecal/stdlib/generate; DO NOT EDIT.
  51. package stdlib
  52. import (
  53. "fmt"
  54. "math"
  55. "reflect"
  56. )
  57. /*
  58. genStdlib contains all generated stdlib constructs.
  59. */
  60. var genStdlib = map[interface{}]interface{}{
  61. "fmt-const" : fmtConstMap,
  62. "fmt-func" : fmtFuncMap,
  63. "fmt-func-doc" : fmtFuncDocMap,
  64. "math-const" : mathConstMap,
  65. "math-func" : mathFuncMap,
  66. "math-func-doc" : mathFuncDocMap,
  67. }
  68. /*
  69. fmtConstMap contains the mapping of stdlib fmt constants.
  70. */
  71. var fmtConstMap = map[interface{}]interface{}{
  72. }
  73. /*
  74. fmtFuncMap contains the mapping of stdlib fmt functions.
  75. */
  76. var fmtFuncMap = map[interface{}]interface{}{
  77. "Println": &ECALFunctionAdapter{reflect.ValueOf(fmt.Println)},
  78. }
  79. /*
  80. fmtFuncDocMap contains the documentation of stdlib fmt functions.
  81. */
  82. var fmtFuncDocMap = map[interface{}]interface{}{
  83. }
  84. /*
  85. mathConstMap contains the mapping of stdlib math constants.
  86. */
  87. var mathConstMap = map[interface{}]interface{}{
  88. "Pi": float64(math.Pi),
  89. }
  90. /*
  91. mathFuncMap contains the mapping of stdlib math functions.
  92. */
  93. var mathFuncMap = map[interface{}]interface{}{
  94. }
  95. /*
  96. mathFuncDocMap contains the documentation of stdlib math functions.
  97. */
  98. var mathFuncDocMap = map[interface{}]interface{}{
  99. }
  100. ` {
  101. t.Errorf("Unexpected result: Go string: %#v\nNormal output: %v", string(out), string(out))
  102. return
  103. }
  104. }