aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/LuaSL/testLua/yueliang-0.4.1/nat-5.0.3/test/test_lparser_mk3b_2.lua
blob: 4d4e8c559c36bdc14a5ac9e637eb64edf0c9d22b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
--[[--------------------------------------------------------------------

  test_lparser_mk3b_2.lua
  Test for lparser_mk3b.lua, using the test case file
  This file is part of Yueliang.

  Copyright (c) 2006-2008 Kein-Hong Man <khman@users.sf.net>
  The COPYRIGHT file describes the conditions
  under which this software may be distributed.

  See the ChangeLog for more information.

----------------------------------------------------------------------]]

--[[--------------------------------------------------------------------
-- Notes:
-- * unlike the equivalent in the orig-5.0.3/ directory, this version
--   tests only parsing, lparser_mk3 cannot generate binary chunks
-- * the test cases are in the test_lua directory (test_parser-5.0.lua)
----------------------------------------------------------------------]]

-- * true if you want an output of all failure cases in native Lua,
--   for checking whether test cases fail where you intend them to
local DEBUG_FAILS = false

------------------------------------------------------------------------
-- test the whole kaboodle
------------------------------------------------------------------------

local lex_init = require("../llex_mk3")
local parser_init = require("../lparser_mk3b")

------------------------------------------------------------------------
-- load test cases
------------------------------------------------------------------------

require("../../test_lua/test_parser-5.0")

local test, expect, heading = {}, {}, {}
local total, total_pass, total_fail = 0, 0, 0

for ln in string.gfind(tests_source, "([^\n]*)\n") do
  if string.find(ln, "^%s*%-%-") then
    -- comment, ignore
  else
    local m, _, head = string.find(ln, "^%s*(TESTS:%s*.*)$")
    if m then
      heading[total + 1] = head         -- informational heading
    else
      total = total + 1
      local n, _, flag = string.find(ln, "%s*%-%-%s*FAIL%s*$")
      if n then                         -- FAIL test case
        ln = string.sub(ln, 1, n - 1)   -- remove comment
        expect[total] = "FAIL"
        total_fail = total_fail + 1
      else                              -- PASS test case
        expect[total] = "PASS"
        total_pass = total_pass + 1
      end--n
      test[total] = ln
    end--m
  end--ln
end--for

print("Tests loaded: "..total.." (total), "
                      ..total_pass.." (passes), "
                      ..total_fail.." (fails)")

------------------------------------------------------------------------
-- verify test cases using native Lua
------------------------------------------------------------------------

local last_head = "TESTS: no heading yet"
for i = 1, total do
  local test_case, expected, head = test[i], expect[i], heading[i]
  -- show progress
  if head then
    last_head = head
    if DEBUG_FAILS then print("\n"..head.."\n") end
  end
  ------------------------------------------------------------------
  -- perform test
  local f, err = loadstring(test_case)
  -- look at outcome
  ------------------------------------------------------------------
  if f then-- actual PASS
    if expected == "FAIL" then
      print("\nVerified as PASS but expected to FAIL"..
            "\n-------------------------------------")
      print("Lastest heading: "..last_head)
      print("TEST: "..test_case)
      os.exit()
    end
  ------------------------------------------------------------------
  else-- actual FAIL
    if expected == "PASS" then
      print("\nVerified as FAIL but expected to PASS"..
            "\n-------------------------------------")
      print("Lastest heading: "..last_head)
      print("TEST: "..test_case)
      print("ERROR: "..err)
      os.exit()
    end
    if DEBUG_FAILS then
      print("TEST: "..test_case)
      print("ERROR: "..err.."\n")
    end
  ------------------------------------------------------------------
  end--f
end--for

print("Test cases verified using native Lua, no anomalies.")

------------------------------------------------------------------------
-- test using Yueliang front end
------------------------------------------------------------------------

local last_head = "TESTS: no heading yet"
for i = 1, total do
  local test_case, expected, head = test[i], expect[i], heading[i]
  -- show progress
  if head then last_head = head end
  ------------------------------------------------------------------
  -- perform test
  luaX = lex_init(test_case, "=test_sample")
  luaY = parser_init(luaX)

  local status, func = pcall(luaY.parser, luaY)
  -- look at outcome
  ------------------------------------------------------------------
  if status then-- actual PASS
    if expected == "FAIL" then
      print("\nTested as PASS but expected to FAIL"..
            "\n-----------------------------------")
      print("Lastest heading: "..last_head)
      print("TEST: "..test_case)
      os.exit()
    end
  ------------------------------------------------------------------
  else-- actual FAIL
    if expected == "PASS" then
      print("\nTested as FAIL but expected to PASS"..
            "\n-----------------------------------")
      print("Lastest heading: "..last_head)
      print("TEST: "..test_case)
      os.exit()
    else
      io.stdout:write("-")
    end
  ------------------------------------------------------------------
  end--status
  io.stdout:write("\rTesting ["..i.."]...")
end--for
print(" done.")

print("Test cases run on Yueliang, no anomalies.")

-- end