test.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/env node
  2. let KumaChan = require(`${__dirname}/build/dev/runtime.js`)
  3. let ChildProcess = require('child_process')
  4. let FS = require('fs')
  5. let Compiler = mode => `${__dirname}/build/dev/compiler ${mode}`
  6. let e = String.fromCodePoint(27)
  7. let Bold = `${e}[1m`
  8. let Reset = `${e}[0m`
  9. let Red = `${e}[31m`
  10. let Green = `${e}[32m`
  11. function assert (expr) {
  12. if (!expr) {
  13. throw Error('Assertion Failed')
  14. }
  15. return expr
  16. }
  17. let previous_length = 0
  18. function display (info) {
  19. assert(typeof info == 'string')
  20. process.stdout.write('\r')
  21. process.stdout.write(' '.repeat(previous_length))
  22. process.stdout.write('\r')
  23. process.stdout.write(info)
  24. previous_length = info.length
  25. }
  26. function print (info) {
  27. assert(typeof info == 'string')
  28. process.stdout.write(info)
  29. process.stdout.write('\n')
  30. previous_length = 0
  31. }
  32. function eval_test (code) {
  33. let resolve = null
  34. let promise = new Promise((res, rej) => { resolve = res })
  35. let p = ChildProcess.exec(Compiler('eval'), (error, stdout) => {
  36. if (error != null) {
  37. print(`${Bold}${Red} Error occured during compiling:${Reset}`)
  38. console.log(error)
  39. process.exit(-1)
  40. }
  41. try {
  42. let result = eval(stdout)
  43. if (result instanceof Promise) {
  44. let settled = false
  45. result
  46. .then(_ => {
  47. settled = true
  48. resolve({ ok: true })
  49. })
  50. .catch(error => {
  51. setteld = true
  52. resolve({ ok: false, error })
  53. })
  54. setTimeout(() => {
  55. if (!settled) {
  56. let error = new Error('Test Case Timed Out')
  57. resolve({ ok: false, error })
  58. }
  59. }, 500)
  60. } else {
  61. resolve({ ok: true })
  62. }
  63. } catch (error) {
  64. resolve({ ok: false, error })
  65. }
  66. })
  67. p.stdin.write(code)
  68. p.stdin.end()
  69. return promise
  70. }
  71. async function main () {
  72. let index_text = FS.readFileSync(`${__dirname}/test/index.json`)
  73. let index = JSON.parse(index_text)
  74. assert(index.sequence instanceof Array)
  75. let total = 0
  76. let passed = 0
  77. for (let group of index.sequence) {
  78. assert(typeof group == 'string')
  79. let units = FS.readdirSync(`${__dirname}/test/${group}`)
  80. for (let unit of units) {
  81. let unit_name = unit.replace(/\.k$/, '')
  82. let info_base = (
  83. `${Bold}Running Unit Test:${Reset} ${group}/${unit_name}`
  84. )
  85. display(`${info_base} ...`)
  86. let code = FS.readFileSync(`${__dirname}/test/${group}/${unit}`)
  87. let result = await eval_test(code)
  88. if (result.ok) {
  89. display(`${info_base} [ ${Bold}${Green}OK${Reset} ]`)
  90. passed += 1
  91. } else {
  92. display(`${info_base} [ ${Bold}${Red}FAILED${Reset} ]`)
  93. print('')
  94. let error = result.error
  95. if (
  96. error instanceof KumaChan.CustomError
  97. || error instanceof KumaChan.RuntimeError
  98. ) {
  99. print(error.message)
  100. } else {
  101. console.log(error)
  102. }
  103. }
  104. total += 1
  105. }
  106. }
  107. if (passed == total) {
  108. display(`${Bold}Test Finished, ${Green}All Passed.${Reset}`)
  109. print('')
  110. } else {
  111. print('')
  112. print(`${Bold}Test Finished, ${Red}${passed}/${total} Passed.${Reset}`)
  113. process.exit(1)
  114. }
  115. }
  116. main()