generate_test.go 2.0 KB

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