Files

MiniTest.src
  • __scope = function()
  • MiniTest = {}
  • MiniTest._tests = []
  • MiniTest._testSetup = null
  • MiniTest._testTeardown = null
  • MiniTest._hdr_printed = false
  • isGH = globals.hasIndex("isGH") // use colours if set.
  • ifGH = function(s)
  • if isGH then return s
  • return ""
  • end function
  • OK = ifGH("<color=#4fd2e0>")
  • OK_E = ifGH("</color>")
  • ERR = ifGH("<color=""red"">")
  • ERR_E = ifGH("</color>")
  • DATA = ifGH("<color=#bfb360>")
  • DATA_E = ifGH("</color>")
  • BDATA = ifGH("<color=#ab591f>")
  • BDATA_E= ifGH("</color>")
  • NOTE = ifGH("<i><color=#4375d1>")
  • NOTE_E = ifGH("</color></i>")
  • NAME = ifGH("<b>")
  • NAME_E = ifGH("</b>")
  • globals.TEST = function(str, func)
  • MiniTest._tests.push([str, @func, @MiniTest._testSetup, @MiniTest._testTeardown])
  • return MiniTest
  • end function
  • globals.TEST_SETUP = function(func)
  • MiniTest._testSetup = @func
  • end function
  • globals.TEST_TEARDOWN = function(func)
  • MiniTest._testTeardown = @func
  • end function
  • _print_error = function(s = null, terminate = false)
  • if not MiniTest._hdr_printed then print NAME + MiniTest._current + NAME_E + " - " + ERR + "FAILED" + ERR_E
  • MiniTest._hdr_printed = true
  • if s then print s + "\n"
  • if terminate then exit
  • end function
  • globals.minitest_print_error = @_print_error
  • globals.RUN_ALL_TESTS = function(verbose = true, retainTests = false)
  • globals.MINITEST_RUNNING = true
  • for test in MiniTest._tests
  • MiniTest._current = test[0]
  • if @test[2] then test[2]()
  • test[1]()
  • if @test[3] then test[3]()
  • if verbose and not MiniTest._hdr_printed then print(NAME + test[0] + NAME_E + " - " + OK + "PASSED" + OK_E)
  • MiniTest._hdr_printed = false
  • end for
  • globals.MINITEST_RUNNING = false
  • MiniTest._current = null
  • if not retainTests then
  • MiniTest._tests = []
  • MiniTest._testSetup = null
  • MiniTest._testTeardown = null
  • end if
  • end function
  • _checkMatch = function(actual, expected, note = "", isEq = true, isExpect = false, isIsa = false)
  • if isEq and ((isIsa and actual isa expected) or (not isIsa and actual == expected)) then return
  • if not isEq and ((isIsa and not actual isa expected) or (not isIsa and actual != expected)) then return
  • s = "ed: " + DATA + expected + DATA_E + "\n actual: " + BDATA + actual + BDATA_E
  • if isEq then s = "expect" + s
  • if not isEq then s = "unwant" + s
  • if note then s = s + "\n note: " + NOTE + note + NOTE_E
  • _print_error s, not isExpect
  • end function
  • globals.assertEqual = function(actual, expected, note = "")
  • return _checkMatch(actual, expected, note)
  • end function
  • globals.expectEqual = function(actual, expected, note = "")
  • return _checkMatch(actual, expected, note, true, true)
  • end function
  • globals.assertNotEq = function(actual, expected, note = "")
  • return _checkMatch(actual, expected, note, false)
  • end function
  • globals.expectNotEq = function(actual, expected, note = "")
  • return _checkMatch(actual, expected, note, false, true)
  • end function
  • globals.assertIsa = function(actual, expected, note = "")
  • return _checkMatch(actual, expected, note, true, false, true)
  • end function
  • globals.expectIsa = function(actual, expected, note = "")
  • return _checkMatch(actual, expected, note, true, true, true)
  • end function
  • globals.assertIsnt = function(actual, expected, note = "")
  • return _checkMatch(actual, expected, note, false, false, true)
  • end function
  • globals.expectIsnt = function(actual, expected, note = "")
  • return _checkMatch(actual, expected, note, false, true, true)
  • end function
  • globals.assertTrue = function(actual, note = "")
  • return assertEqual(actual, true, note)
  • end function
  • globals.expectTrue = function(actual, note = "")
  • return expectEqual(actual, true, note)
  • end function
  • globals.assertTruthy = function(actual, note = "")
  • return assertEqual(actual != false and actual != null, true, note)
  • end function
  • globals.expectTruthy = function(actual, note = "")
  • return expectEqual(actual != false and actual != null, true, note)
  • end function
  • globals.assertFalse = function(actual, note = "")
  • return assertEqual(actual, false, note)
  • end function
  • globals.expectFalse = function(actual, note = "")
  • return expectEqual(actual, false, note)
  • end function
  • globals.assertNull = function(actual, note = "")
  • return assertEqual(actual, null, note)
  • end function
  • globals.expectNull = function(actual, note = "")
  • return expectEqual(actual, null, note)
  • end function
  • globals.MINITEST_DEFINED = true
  • end function
  • if not globals.hasIndex("MINITEST_DEFINED") then __scope()
  • __scope = null
MiniMock.src
  • Mock = {}
  • Mock.Any = {}
  • __scope = function()
  • isGH = globals.hasIndex("isGH") // use colours if set.
  • ifGH = function(s)
  • if isGH then return s
  • return ""
  • end function
  • ERR = ifGH("<color=""red"">")
  • ERR_E = ifGH("</color>")
  • DATA = ifGH("<color=#bfb360>")
  • DATA_E = ifGH("</color>")
  • BARG = ifGH("<color=#ff4203>")
  • BARG_E = ifGH("</color>")
  • FUNC = ifGH("<b><color=#3644a3>")
  • FUNC_E = ifGH("</color></b>")
  • Mock.__mockCall = function(actualParams, funcName)
  • if self._expect.len == 0 or funcName != self._expect[0].target then
  • s = ERR + "Fatal error, unexpected call to " + FUNC + funcName + FUNC_E + ":" + ERR_E +
  • "\n params: " + DATA + actualParams + DATA_E
  • if MINITEST_RUNNING then minitest_print_error(s, true)
  • print s
  • exit()
  • end if
  • expected = self._expect.pull
  • expectedArgCnt = self._argCnt[expected.target]
  • if expectedArgCnt != actualParams.len then
  • s = ERR + "Fatal error, wrong argument count:" + ERR_E
  • if actualParams.len > 0 then s = s +
  • "\n params: " + DATA + actualParams + DATA_E
  • s = "\n expected: " + DATA + expectedArgCnt + DATA_E + " args" +
  • "\n actual: " + DATA + actualParams.len + DATA_E + "\n args" +
  • "\nexpecting function: " + FUNC + expected.target + FUNC_E
  • if MINITEST_RUNNING then minitest_print_error(s, true)
  • print s
  • exit()
  • end if
  • for i in expected.params.indexes
  • if not expected.params[i] == Mock.Any and expected.params[i] != actualParams[i] then
  • s = "Error In expected call to " + FUNC + expected.target + FUNC_E + "\n"
  • s = s + " expected args: " + DATA + expected.params + DATA_E + "\n"
  • s = s + " actual args: " + BARG + actualParams + BARG_E
  • //s = s + "NOTE: ensure the function name that was actually called matches, too."
  • p = @print
  • if MINITEST_RUNNING then p = @minitest_print_error
  • p s
  • break
  • end if
  • end for
  • maybeRet = null
  • if @expected.clbk then maybeRet = expected.clbk(actualParams)
  • if @expected.retVal == null then return maybeRet
  • return @expected.retVal
  • end function
  • Mock.genFunc = function(paramCount, funcName)
  • locals.self = self
  • locals.funcName = @funcName
  • funcs = []
  • f = function()
  • return outer.self.__mockCall([], outer.funcName)
  • end function
  • funcs.push @f
  • f = function(a)
  • return outer.self.__mockCall([@a], outer.funcName)
  • end function
  • funcs.push @f
  • f = function(a, b)
  • return outer.self.__mockCall([@a, @b], outer.funcName)
  • end function
  • funcs.push @f
  • f = function(a, b, c)
  • return outer.self.__mockCall([@a, @b, @c], outer.funcName)
  • end function
  • funcs.push @f
  • f = function(a, b, c, d)
  • return outer.self.__mockCall([@a, @b, @c, @d], outer.funcName)
  • end function
  • funcs.push @f
  • f = function(a, b, c, d, e)
  • return outer.self.__mockCall([@a, @b, @c, @d, @e], outer.funcName)
  • end function
  • funcs.push @f
  • f = function(a, b, c, d, e, f)
  • return outer.self.__mockCall([@a, @b, @c, @d, @e, @f], outer.funcName)
  • end function
  • funcs.push @f
  • // Add more as needed.
  • return @funcs[paramCount]
  • end function
  • Mock._addMockFunc = function(kv)
  • paramCount = ("" + @kv.value).split(",").len - 1
  • self[kv["key"]] = @self.genFunc(paramCount, @kv.key)
  • self._argCnt[kv["key"]] = paramCount
  • end function
  • Mock._addMockData = function(kv)
  • self[kv["key"]] = @self.genFunc(0)
  • end function
  • Mock._build = function(mapToMock, ret)
  • for kv in mapToMock
  • if @kv.key == "__isa" then
  • self._build(kv.value, ret)
  • else if @kv.value isa funcRef then
  • ret._addMockFunc(kv)
  • else
  • ret._addMockData(kv)
  • end if
  • end for
  • return ret
  • end function
  • Mock.define = function(key, paramCount)
  • func = @self.genFunc(paramCount, @key)
  • self._argCnt[key] = paramCount
  • self[key] = @func
  • return @func
  • end function
  • Mock.build = function(mapToMock)
  • map = {"__isa": mapToMock, "_expect": [], "_argCnt": {}}
  • for kv in self
  • map[@kv.key] = @kv.value
  • end for
  • return self._build(mapToMock, map)
  • end function
  • Expect = {}
  • Expect.build = function(funcName)
  • ret = new Expect
  • ret.target = funcName
  • ret.params = []
  • ret.clbk = null
  • ret.retVal = null
  • return ret
  • end function
  • // params should be enclosed in a list[]
  • // However, a single argument will be converted for you.
  • Expect.withParams = function(params)
  • if not params isa list then params = [params]
  • self.params = params
  • return self
  • end function
  • Expect.thenInvoke = function(func)
  • self.clbk = @func
  • return self
  • end function
  • Expect.andReturn = function(retVal)
  • self.retVal = @retVal
  • return self
  • end function
  • Mock.expectCall = function(funcName)
  • ret = Expect.build(funcName)
  • self._expect.push(ret)
  • return ret
  • end function
  • Mock.expectData = @Mock.expectCall
  • Mock.getResult = function()
  • if self._expect.len == 0 then return true
  • ret = "Error: Unfulfilled call(s):"
  • for call in self._expect
  • ret = ret + "\n callee: " + FUNC + call.target + FUNC_E
  • if call.params.len > 0 then ret = ret + "\n params: " + DATA + call.params + DATA_E
  • end for
  • return ret
  • end function
  • end function //end __scope
  • if not globals.hasIndex("MINIMOCK_DEFINED") then __scope()
  • __scope = null
  • MINIMOCK_DEFINED = true
Example.src
  • mockRouter = null
  • mockGlobals = null
  • toMock = ["is_valid_ip", "get_router", "get_switch"]
  • TEST_SETUP function()
  • globals.mockGlobals = Mock.build({})
  • toMock.forEach function(key) // forEach is from an extension I added to list. Published separately.
  • globals[key] = mockGlobals.define(key, 1)
  • end function
  • globals.mockRouter = Mock.build({})
  • mockRouter.define("firewall_rules", 0)
  • mockRouter.define("local_ip", 0)
  • mockRouter.define("devices_lan_ip", 0)
  • end function
  • TEST_TEARDOWN function()
  • assertTrue mockRouter.getResult
  • assertTrue mockGlobals.getResult
  • toMock.forEach(@globals.remove, globals)
  • end function
  • TEST "NetDev initialises correctly and returns valid data", function()
  • fwRules = ["ALLOW 1234 Any 10.0.0.1", "DENY 4321 Any Any"]
  • devices = ["192.168.1.1", "10.0.0.1", "172.16.0.1"]
  • firstArg = "192.168.1.1"
  • mockGlobals.expectCall("is_valid_ip").withParams(firstArg).andReturn(true)
  • mockGlobals.expectCall("get_router").withParams(firstArg).andReturn(mockRouter)
  • mockRouter.expectCall("firewall_rules").andReturn(fwRules)
  • mockRouter.expectCall("local_ip").andReturn(firstArg)
  • mockRouter.expectCall("devices_lan_ip").andReturn(devices)
  • mockGlobals.expectCall("is_valid_ip").withParams(devices[1]).andReturn(true)
  • mockGlobals.expectCall("get_router").withParams(devices[1]).andReturn(null)
  • mockGlobals.expectCall("get_switch").withParams(devices[1]).andReturn(mockRouter)
  • mockRouter.expectCall("firewall_rules").andReturn([])
  • mockRouter.expectCall("local_ip").andReturn(devices[1])
  • mockRouter.expectCall("devices_lan_ip").andReturn(devices)
  • mockGlobals.expectCall("is_valid_ip").withParams(devices[2]).andReturn(true)
  • mockGlobals.expectCall("get_router").withParams(devices[2]).andReturn(null)
  • mockGlobals.expectCall("get_switch").withParams(devices[2]).andReturn(null)
  • netDev = NetDev.GetDevInfo(firstArg)
  • expectIsa netDev, NetDev.Router
  • expectEqual netDev.dev, mockRouter
  • expectEqual netDev.localIp, devices[0]
  • expectEqual netDev.connections.len, devices.len - 1
  • assertEqual netDev.fwRules.len, fwRules.len
  • expectIsa netDev.fwRules[0], FwRule.ALLOW
  • expectEqual netDev.fwRules[0].port, 1234
  • expectIsa netDev.fwRules[0].src, FwRule.Any
  • expectEqual netDev.fwRules[0].dst, "10.0.0.1"
  • expectIsa netDev.fwRules[1], FwRule.DENY
  • expectEqual netDev.fwRules[1].port, 4321
  • expectIsa netDev.fwRules[1].src, FwRule.Any
  • expectIsa netDev.fwRules[1].dst, FwRule.Any
  • switch = netDev.connections[0]
  • expectIsa switch, NetDev.Switch
  • expectEqual switch.dev, mockRouter
  • expectEqual switch.fwRules, []
  • expectEqual switch.localIp, "10.0.0.1"
  • assertEqual switch.connections.len, devices.len - 1
  • host = netDev.connections[1]
  • expectIsa host, LANDevice
  • expectEqual host.localIp, "172.16.0.1"
  • end function
  • RUN_ALL_TESTS // CRUCIAL!