diff --git a/spec/pretty_spec.lua b/spec/pretty_spec.lua new file mode 100644 index 00000000..85e37706 --- /dev/null +++ b/spec/pretty_spec.lua @@ -0,0 +1,40 @@ +local pretty = require("pl.pretty") + +describe("pl.pretty.number", function () + + it("should format memory", function () + local function assert_memory (expected, input) + assert.is.equal(expected, pretty.number(input, "M")) + end + assert_memory("123B", 123) + assert_memory("1.2KiB", 1234) + assert_memory("10.0KiB", 10*1024) + assert_memory("1.0MiB", 1024*1024) + assert_memory("1.0GiB", 1024*1024*1024) + end) + + it("should format postfixes", function () + local function assert_postfix(expected, input) + assert.is.equal(expected, pretty.number(input, "N", 2)) + end + assert_postfix("123", 123) + assert_postfix("1.23K", 1234) + assert_postfix("10.24K", 10*1024) + assert_postfix("1.05M", 1024*1024) + assert_postfix("1.07B", 1024*1024*1024) + end) + + it("should format postfixes", function () + local function assert_separator(expected, input) + assert.is.equal(expected, pretty.number(input, "T")) + end + assert_separator('123', 123) + assert_separator('1,234', 1234) + assert_separator('12,345', 12345) + assert_separator('123,456', 123456) + assert_separator('1,234,567', 1234567) + assert_separator('12,345,678', 12345678) + end) + + +end) diff --git a/tests/test-pretty-number.lua b/tests/test-pretty-number.lua deleted file mode 100644 index 347a5cf5..00000000 --- a/tests/test-pretty-number.lua +++ /dev/null @@ -1,33 +0,0 @@ -local test = require 'pl.test' -local pretty = require 'pl.pretty' - -function testm(x,s) - test.asserteq(pretty.number(x,'M'),s) -end - -testm(123,'123B') -testm(1234,'1.2KiB') -testm(10*1024,'10.0KiB') -testm(1024*1024,'1.0MiB') - -function testn(x,s) - test.asserteq(pretty.number(x,'N',2),s) -end - -testn(123,'123') -testn(1234,'1.23K') -testn(10*1024,'10.24K') -testn(1024*1024,'1.05M') -testn(1024*1024*1024,'1.07B') - -function testc(x,s) - test.asserteq(pretty.number(x,'T'),s) -end - -testc(123,'123') -testc(1234,'1,234') -testc(12345,'12,345') -testc(123456,'123,456') -testc(1234567,'1,234,567') -testc(12345678,'12,345,678') -