summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOpportunityLiu <[email protected]>2019-07-24 18:36:49 +0800
committerOpportunityLiu <[email protected]>2019-07-25 15:25:40 +0800
commit9774a017a3baadd3caf3b442f4a22a0bf151d4c9 (patch)
tree3df07f70ec715cf98fb3f43e1e8cb89a878a72f8
parent5727deff9988a851f528308c1c6a941f3cab56f3 (diff)
Improve string.serialize; add unit test
-rw-r--r--tests/modules/string/serialize/test.lua36
-rw-r--r--tests/test_utils/check.lua3
-rw-r--r--xmake/core/base/deprecated.lua25
-rw-r--r--xmake/core/base/dump.lua23
-rw-r--r--xmake/core/base/math.lua58
-rw-r--r--xmake/core/base/string.lua63
-rw-r--r--xmake/core/sandbox/modules/math.lua2
7 files changed, 160 insertions, 50 deletions
diff --git a/tests/modules/string/serialize/test.lua b/tests/modules/string/serialize/test.lua
new file mode 100644
index 000000000..8d13f6fee
--- /dev/null
+++ b/tests/modules/string/serialize/test.lua
@@ -0,0 +1,36 @@
+
+function roundtrip(v)
+ return string.serialize(v):deserialize()
+end
+
+function test_number(t)
+ t:are_equal(roundtrip(12), 12)
+ t:are_equal(roundtrip(0), 0)
+ t:are_equal(roundtrip(-1), -1)
+ t:are_equal(roundtrip(7.25), 7.25)
+ t:are_equal(roundtrip(math.huge), math.huge)
+ t:are_equal(roundtrip(-math.huge), -math.huge)
+ t:are_equal(roundtrip(math.nan), math.nan)
+end
+
+function test_boolean(t)
+ t:are_equal(roundtrip(true), true)
+ t:are_equal(roundtrip(false), false)
+end
+
+function test_nil(t)
+ t:are_equal(roundtrip(nil), nil)
+end
+
+function test_table(t)
+ t:are_equal(roundtrip({}), {})
+ t:are_equal(roundtrip({1, 2, 3}), {1, 2, 3})
+ t:are_equal(roundtrip({1, "", 3}), {1, "", 3})
+ --t:are_equal(roundtrip({{1, 2, 3, nil, 4}}), {{1, 2, 3, nil, 4}})
+end
+
+function test_function(t)
+ t:are_equal(roundtrip(function() return {} end)(), {})
+ t:are_equal(roundtrip(function() return {1, 2, 3} end)(), {1, 2, 3})
+ t:are_equal(roundtrip(function() return {{1, 2, 3, nil, 4}} end)(), {{1, 2, 3, nil, 4}})
+end
diff --git a/tests/test_utils/check.lua b/tests/test_utils/check.lua
index bc9ddb6b6..86fa5d6b5 100644
--- a/tests/test_utils/check.lua
+++ b/tests/test_utils/check.lua
@@ -13,6 +13,9 @@ function _get_rep(value)
end
function same(actual, expacted)
+ if actual ~= actual and expacted ~= expacted then
+ return true, _get_rep(actual), _get_rep(expacted)
+ end
return actual == expacted, _get_rep(actual), _get_rep(expacted)
end
diff --git a/xmake/core/base/deprecated.lua b/xmake/core/base/deprecated.lua
index b37851f20..467bc2964 100644
--- a/xmake/core/base/deprecated.lua
+++ b/xmake/core/base/deprecated.lua
@@ -21,12 +21,6 @@
-- define module
local deprecated = deprecated or {}
--- load modules
-local utils = require("base/utils")
-local table = require("base/table")
-local string = require("base/string")
-local option = require("base/option")
-
-- add deprecated entry
function deprecated.add(newformat, oldformat, ...)
@@ -44,6 +38,10 @@ end
-- dump all deprecated entries
function deprecated.dump()
+ -- lazy load modules to avoid loop
+ local utils = require("base/utils")
+ local option = require("base/option")
+
-- dump one or more ..
local index = 0
deprecated._ENTRIES = deprecated._ENTRIES or {}
@@ -53,17 +51,18 @@ function deprecated.dump()
if index == 0 then
print("")
end
- if new then
- utils.cprint("${bright color.warning}deprecated: ${clear}please uses %s instead of %s", new, old)
- else
- utils.cprint("${bright color.warning}deprecated: ${clear}please remove %s", old)
- end
-- show more?
- if not option.get("verbose") then
- utils.cprint("${bright color.warning}deprecated: ${clear}add -v for getting more ..")
+ if not option.get("verbose") and index > 0 then
+ utils.cprint("${bright color.warning}deprecated:${clear} add -v for getting more ..")
break
end
+
+ if new then
+ utils.cprint("${bright color.warning}deprecated:${clear} please uses %s instead of %s", new, old)
+ else
+ utils.cprint("${bright color.warning}deprecated:${clear} please remove %s", old)
+ end
index = index + 1
end
end
diff --git a/xmake/core/base/dump.lua b/xmake/core/base/dump.lua
index 199dc1e19..ce3b87b91 100644
--- a/xmake/core/base/dump.lua
+++ b/xmake/core/base/dump.lua
@@ -72,16 +72,19 @@ end
-- print function
function dump._print_function(func, as_key)
+ io.write(dump._translate("${reset}${color.dump.function}"))
if as_key then
- return dump._print_default(func)
- end
- local funcinfo = debug.getinfo(func)
- local srcinfo = funcinfo.short_src
- if funcinfo.linedefined >= 0 then
- srcinfo = srcinfo .. ":" .. funcinfo.linedefined
+ io.write(dump._format("text.dump.default_format", "%s", func))
+ else
+ local funcinfo = debug.getinfo(func)
+ local srcinfo = funcinfo.short_src
+ if funcinfo.linedefined >= 0 then
+ srcinfo = srcinfo .. ":" .. funcinfo.linedefined
+ end
+ local funcname = funcinfo.name and (funcinfo.name .. " ") or ""
+ io.write(dump._translate("function ${bright}"), funcname, dump._translate("${reset}${dim}"), srcinfo)
end
- local funcname = funcinfo.name and (funcinfo.name .. " ") or ""
- io.write(dump._translate("${reset}${color.dump.function}function ${bright}"), funcname, dump._translate("${reset}${dim}"), srcinfo)
+ io.write(dump._translate("${reset}"))
end
-- print value with default format
@@ -101,9 +104,7 @@ end
-- print scalar value
function dump._print_scalar(value, as_key)
- if type(value) == "nil" then
- dump._print_keyword("nil")
- elseif type(value) == "boolean" then
+ if type(value) == "nil" or type(value) == "boolean" then
dump._print_keyword(value)
elseif type(value) == "number" then
dump._print_number(value)
diff --git a/xmake/core/base/math.lua b/xmake/core/base/math.lua
new file mode 100644
index 000000000..a2dad7a6e
--- /dev/null
+++ b/xmake/core/base/math.lua
@@ -0,0 +1,58 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015 - 2019, TBOOX Open Source Group.
+--
+-- @author OpportunityLiu
+-- @file math.lua
+--
+
+-- define module
+local math = math or {}
+
+-- init constants
+math.nan = math.log(-1)
+math.e = math.exp(1)
+
+-- check a number is inf or -inf
+--
+-- @returns 1 for inf, -1 for -inf, otherwise false
+function math:isinf()
+
+ -- check
+ assert(type(self) == "number", "number expacted")
+
+ if self == math.huge then
+ return 1
+ elseif self == -math.huge then
+ return -1
+ else
+ return false
+ end
+end
+
+-- check a number is nan
+--
+-- @returns true for nan, otherwise false
+function math:isnan()
+
+ -- check
+ assert(type(self) == "number", "number expacted")
+
+ return self ~= self
+end
+
+
+-- return module
+return math
diff --git a/xmake/core/base/string.lua b/xmake/core/base/string.lua
index b59706a6d..d3a584f3e 100644
--- a/xmake/core/base/string.lua
+++ b/xmake/core/base/string.lua
@@ -21,6 +21,9 @@
-- define module: string
local string = string or {}
+-- load modules
+local deprecated = require("base/deprecated")
+
-- save original interfaces
string._dump = string._dump or string.dump
string._trim = string._trim or string.trim
@@ -29,12 +32,30 @@ string._trim = string._trim or string.trim
function string._makestr(object, deflate, serialize, level)
if type(object) == "string" then
return serialize and string.format("%q", object) or object
- elseif type(object) == "boolean" or type(object) == "number" then
+ elseif type(object) == "boolean" or type(object) == "nil" then
+ return tostring(object)
+ elseif type(object) == "number" then
+ if serialize then
+ if math.isnan(object) then
+ return "math.nan"
+ end
+ local inf = math.isinf(object)
+ if inf == 1 then
+ return "math.huge"
+ elseif inf == -1 then
+ return "-math.huge"
+ end
+ end
return tostring(object)
elseif not serialize and type(object) == "table" and (getmetatable(object) or {}).__tostring then
return tostring(object)
elseif type(object) == "table" then
+ local indent = ""
+ if not deflate then
+ indent = string.rep(" ", level)
+ end
+
-- make head
local s = ""
if deflate then
@@ -43,41 +64,36 @@ function string._makestr(object, deflate, serialize, level)
if level > 0 then
s = s .. "\n"
end
- for l = 1, level do
- s = s .. " "
- end
- s = s .. "{\n"
+ s = s .. indent .. "{\n"
end
-- make body
local i = 0
- for k, v in pairs(object) do
+ for k, v in pairs(object) do
if deflate then
s = s .. (i ~= 0 and "," or "")
else
- for l = 1, level do
- s = s .. " "
- end
+ s = s .. indent
if i == 0 then
s = s .. " "
else
s = s .. ", "
end
end
-
+
-- make key = value
if type(k) == "string" then
- if serialize and not k:match("^%a[%w_]+$") then
+ if serialize and not k:match("^[%a_][%w_]+$") then
k = string.format("[%q]", k)
end
if deflate then
- s = s .. k .. "="
+ s = s .. k .. "="
else
- s = s .. k .. " = "
+ s = s .. k .. " = "
end
end
- local substr, errors = string._makestr(v, deflate, serialize, level + 1)
+ local substr, errors = string._makestr(v, deflate, serialize, level + 1)
if substr == nil then
return nil, errors
end
@@ -87,17 +103,12 @@ function string._makestr(object, deflate, serialize, level)
s = s .. "\n"
end
i = i + 1
- end
+ end
-- make tail
- if not deflate then
- for l = 1, level do
- s = s .. " "
- end
- end
- s = s .. "}"
+ s = s .. indent .. "}"
return s
- elseif serialize and type(object) == "function" then
+ elseif serialize and type(object) == "function" then
return string.format("%q", string._dump(object))
elseif serialize then
return nil, "cannot serialize object: " .. type(object)
@@ -113,7 +124,7 @@ function string._loadstr(object)
-- only load luajit function data: e.g. "\27LJ\2\0\6=stdin"
if type(object) == "string" and object:startswith("\27LJ") then
return loadstring(object)
- elseif type(object) == "table" then
+ elseif type(object) == "table" then
for k, v in pairs(object) do
local value, errors = string._loadstr(v)
if value ~= nil then
@@ -286,6 +297,7 @@ function string.ipattern(pattern, brackets)
return table.concat(tmp)
end
+-- @deprecated
-- dump to string from the given object (more readable)
--
-- @param deflate deflate empty characters
@@ -293,6 +305,7 @@ end
-- @return string, errors
--
function string.dump(object, deflate)
+ deprecated.add("utils.dump() or string.serialize()", "string.dump()")
return string._makestr(object, deflate, false, 0)
end
@@ -318,10 +331,10 @@ function string:deserialize()
local result = nil
local script, errors = loadstring("return " .. self)
if script then
-
+
-- load object
local ok, object = pcall(script)
- if ok and object then
+ if ok then
result = object
elseif object then
-- error
diff --git a/xmake/core/sandbox/modules/math.lua b/xmake/core/sandbox/modules/math.lua
index ebaf989b0..a52b13310 100644
--- a/xmake/core/sandbox/modules/math.lua
+++ b/xmake/core/sandbox/modules/math.lua
@@ -19,5 +19,5 @@
--
-- load module
-return math
+return require("base/math")