1-- Licensed to the Apache Software Foundation (ASF) under one or more 2-- contributor license agreements. See the NOTICE file distributed with 3-- this work for additional information regarding copyright ownership. 4-- The ASF licenses this file to You under the Apache License, Version 2.0 5-- (the "License"); you may not use this file except in compliance with 6-- the License. You may obtain a copy of the License at 7-- 8-- http://www.apache.org/licenses/LICENSE-2.0 9-- 10-- Unless required by applicable law or agreed to in writing, software 11-- distributed under the License is distributed on an "AS IS" BASIS, 12-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13-- See the License for the specific language governing permissions and 14-- limitations under the License. 15 16module("moonunit", package.seeall) 17 18TestCase = {} 19 20function TestCase:new(it) 21 it = it or {} 22 setmetatable(it, self) 23 self.__index = self 24 return it 25end 26 27function TestCase:run(args) 28 args = args or arg 29 local function run_test(t, name) 30 local status, err = pcall(t, self) 31 if status then 32 print(("%-39s \27[32mpass\27[39m"):format("[" .. name .. "]")) 33 else 34 print(("%-39s \27[31mFAIL\27[39m %s"):format("[" .. name .. "]", err)) 35 end 36 end 37 38 if (args and #args > 0) then 39 for _, v in ipairs(args) do 40 if type(self[v]) == "function" then 41 run_test(self[v], v) 42 else 43 print(("%-39s FAIL %s"):format("[" .. v .. "]", 44 "'" .. v .. "' doesn't appear to be a test function")) 45 end 46 end 47 else 48 for k, v in pairs(self) do 49 run_test(v, k) 50 end 51 end 52end 53