Kouhei Sutou
null+****@clear*****
Sun Jan 5 23:16:20 JST 2014
Kouhei Sutou 2014-01-05 23:16:20 +0900 (Sun, 05 Jan 2014) New Revision: 20883a295d6b6110db1e15f9a4260c8577a7d4c7 https://github.com/ranguba/chupa-text/commit/20883a295d6b6110db1e15f9a4260c8577a7d4c7 Message: Add SizeParser Added files: lib/chupa-text/size-parser.rb test/test-size-parser.rb Copied files: lib/chupa-text/error.rb (from lib/chupa-text.rb) Modified files: lib/chupa-text.rb Modified: lib/chupa-text.rb (+3 -0) =================================================================== --- lib/chupa-text.rb 2014-01-05 16:04:35 +0900 (f5d8d81) +++ lib/chupa-text.rb 2014-01-05 23:16:20 +0900 (dbd15d0) @@ -16,6 +16,9 @@ require "chupa-text/version" +require "chupa-text/error" + +require "chupa-text/size-parser" require "chupa-text/configuration" require "chupa-text/configuration-loader" require "chupa-text/decomposer" Copied: lib/chupa-text/error.rb (+4 -21) 57% =================================================================== --- lib/chupa-text.rb 2014-01-05 16:04:35 +0900 (f5d8d81) +++ lib/chupa-text/error.rb 2014-01-05 23:16:20 +0900 (34e02d8) @@ -14,24 +14,7 @@ # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -require "chupa-text/version" - -require "chupa-text/configuration" -require "chupa-text/configuration-loader" -require "chupa-text/decomposer" -require "chupa-text/decomposer-registry" -require "chupa-text/decomposers" -require "chupa-text/extractor" -require "chupa-text/formatters" -require "chupa-text/mime-type" -require "chupa-text/mime-type-registry" - -require "chupa-text/file-content" -require "chupa-text/virtual-content" - -require "chupa-text/data" -require "chupa-text/input-data" -require "chupa-text/virtual-file-data" -require "chupa-text/text-data" - -require "chupa-text/command" +module ChupaText + class Error < StandardError + end +end Added: lib/chupa-text/size-parser.rb (+65 -0) 100644 =================================================================== --- /dev/null +++ lib/chupa-text/size-parser.rb 2014-01-05 23:16:20 +0900 (ee023a7) @@ -0,0 +1,65 @@ +# Copyright (C) 2013 Kouhei Sutou <kou �� clear-code.com> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +require "English" + +module ChupaText + class SizeParser + class InvalidSizeError < Error + attr_reader :size + def initialize(size) + @size = size + super("invalid size: <#{@size.inspect}>") + end + end + + class << self + def parse(size) + new.parse(size) + end + end + + def parse(size) + case size + when /TB?\z/i + scale = 1024 ** 4 + number_part = $PREMATCH + when /GB?\z/i + scale = 1024 ** 3 + number_part = $PREMATCH + when /MB?\z/i + scale = 1024 ** 2 + number_part = $PREMATCH + when /KB?\z/i + scale = 1024 ** 1 + number_part = $PREMATCH + when /B?\z/i + scale = 1 + number_part = $PREMATCH + else + scale = 1 + number_part = size + end + + begin + number = Float(number_part) + rescue ArgumentError + raise InvalidSizeError.new(size) + end + (number * scale).round + end + end +end Added: test/test-size-parser.rb (+79 -0) 100644 =================================================================== --- /dev/null +++ test/test-size-parser.rb 2014-01-05 23:16:20 +0900 (7bf2049) @@ -0,0 +1,79 @@ +# Copyright (C) 2013 Kouhei Sutou <kou �� clear-code.com> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +class TestSizeParser < Test::Unit::TestCase + private + def parse(value) + ChupaText::SizeParser.parse(value) + end + + sub_test_case("unit") do + def test_terabyte + assert_equal(1024 ** 4, parse("1TB")) + end + + def test_tera + assert_equal(1024 ** 4, parse("1T")) + end + + def test_gigabyte + assert_equal(1024 ** 3, parse("1GB")) + end + + def test_giga + assert_equal(1024 ** 3, parse("1G")) + end + + def test_megabyte + assert_equal(1024 ** 2, parse("1MB")) + end + + def test_mega + assert_equal(1024 ** 2, parse("1M")) + end + + def test_kilobyte + assert_equal(1024 ** 1, parse("1KB")) + end + + def test_kilo + assert_equal(1024 ** 1, parse("1K")) + end + + def test_byte + assert_equal(1, parse("1B")) + end + end + + sub_test_case("float") do + def test_with_unit + assert_equal(1024 + 512, parse("1.5KB")) + end + + def test_without_unit + assert_equal(2, parse("1.5")) + end + end + + sub_test_case("invalid") do + def test_unknwon_unit + size = "1.5PB" + assert_raise(ChupaText::SizeParser::InvalidSizeError.new(size)) do + parse(size) + end + end + end +end -------------- next part -------------- HTML����������������������������... ダウンロード