File: util.rb

package info (click to toggle)
gonzui 1.2-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,824 kB
  • ctags: 1,448
  • sloc: ruby: 9,570; sh: 5,684; ansic: 1,334; lex: 1,140; makefile: 466; perl: 205; ml: 131
file content (112 lines) | stat: -rwxr-xr-x 2,936 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#! /usr/bin/env ruby
$LOAD_PATH.unshift("..")
require 'test/unit'
require 'ftools'
require 'fileutils'
require 'gonzui'
require 'test-util'
include FileUtils
include Gonzui
include Gonzui::Util

class UtilTest < Test::Unit::TestCase
  def test_string_methods
    assert_equal("oo", "foo".prechop)
    assert_equal("", "".prechop)
    assert_equal(0...3, "foo\nbar\nbaz\n".line_range(0))
    assert_equal(0...3, "foo\nbar\nbaz\n".line_range(2))
    assert_equal(4...7, "foo\nbar\nbaz\n".line_range(4))

    "foo\nbar\nbaz\n".each_line_range(4, 1) {|lineno_offset, range|
      case lineno_offset
      when -1
        assert_equal(0...3, range)
      when 0
        assert_equal(4...7, range)
      when 1
        assert_equal(8...11, range)
      else
        assert(false)
      end
    }
  end

  def test_dir_methods
    rm_rf("t")
    File.mkpath("t")
    assert_equal([], Dir.entries_without_dots("t"))

    touch(["t/a", "t/b"])
    assert_equal(["a", "b"], Dir.entries_without_dots("t"))

    File.mkpath("t/t")
    assert_equal(["a", "b", "t"], Dir.entries_without_dots("t"))

    touch(["t/t/c"])
    assert_equal(["t/a", "t/b", "t/t/c", ], Dir.all_files("t"))
    rm_rf("t")
  end

  def test_file_methods
    assert_equal("bar", File.relative_path("/foo/bar", "/foo"))
    assert_equal("foo", File.relative_path("/foo", "/"))
    assert_equal("", File.relative_path("/foo", "/foo"))
    assert(File.any_exist?("/"))
  end

  def test_command_operations
    unless windows?
      assert(command_exist?("sh")) 
      assert(require_command("sh"))
    end
  end

  def test_shell_escape
    assert_equal('"foo"', shell_escape('foo'))
    assert_equal('"\\$foo"', shell_escape('$foo'))
    assert_equal('"\\`foo\\`"', shell_escape('`foo`'))
    assert_equal('"foo\\\\bar"', shell_escape('foo\\bar'))
  end

  def test_commify
    assert_equal("1",         commify(1))
    assert_equal("12",        commify(12))
    assert_equal("123",       commify(123))
    assert_equal("1,234",     commify(1234))
    assert_equal("12,345",    commify(12345))
    assert_equal("123,456",   commify(123456))
    assert_equal("1,234,567", commify(1234567))
  end

  def test_temporary_directory_util
    foo = Object.new
    foo.extend(TemporaryDirectoryUtil)
    foo.set_temporary_directory(".")
    foo.prepare_temporary_directory
    begin
      foo.prepare_temporary_directory
      assert(false)
    rescue
      assert(true)
    end
    assert(File.directory?(foo.temporary_directory))
    foo.clean_temporary_directory
    assert_equal(false, File.exist?(foo.temporary_directory))
  end

  def test_utf8
    str, name = UTF8.to_utf8("foo")
    assert_equal("foo", str)
    assert_equal("us-ascii", name)


    utf8 = "日本語です"
    ["shift_jis", "euc-jp", "iso-2022-jp"].each {|name|
      str = Iconv.conv(name, "utf8", utf8)
      s, n = UTF8.to_utf8(str)
      assert_equal(name, n)
      assert_equal(utf8, s)
    }
  end
end