generate_test.go 2.9 KB

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