about summary refs log tree commit homepage
path: root/test/test_wordexp.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_wordexp.rb')
-rw-r--r--test/test_wordexp.rb140
1 files changed, 140 insertions, 0 deletions
diff --git a/test/test_wordexp.rb b/test/test_wordexp.rb
new file mode 100644
index 0000000..29f131f
--- /dev/null
+++ b/test/test_wordexp.rb
@@ -0,0 +1,140 @@
+require 'tempfile'
+require 'minitest/autorun'
+require 'wordexp'
+
+class TestWordexp < Minitest::Unit::TestCase
+  include Wordexp
+
+  def test_wordexp
+    assert_equal(%w(HELLO WORLD), wordexp("HELLO WORLD"))
+    assert_equal(%w(HELLO WORLD), wordexp("HELLO WORLD", 0))
+  end
+
+  def test_constants
+    consts = %w(WRDE_NOCMD WRDE_SHOWERR WRDE_UNDEF)
+    consts.each do |c|
+      val = self.class.const_get(c)
+      assert_kind_of Integer, val
+    end
+  end
+
+  def test_showerr
+    olderr = $stderr.dup
+    err = Tempfile.new(%w(test_wordexp .err))
+    $stderr.reopen(err)
+    res = wordexp("$(sh -c 'echo >&2 hihi')", WRDE_SHOWERR)
+    err.rewind
+    assert_equal "hihi\n", err.read
+    assert_equal [], res
+  ensure
+    $stderr.reopen(olderr)
+    err.close!
+  end
+
+  def test_no_showerr
+    olderr = $stderr.dup
+    err = Tempfile.new(%w(test_wordexp .err))
+    $stderr.reopen(err)
+    res = wordexp("$(sh -c 'echo >&2 hihi')")
+    err.rewind
+    assert_equal "", err.read
+    assert_equal [], res
+  ensure
+    $stderr.reopen(olderr)
+    err.close!
+  end
+
+  def test_envsub
+    key = "TEST_WORDEXP_ENVSUB_#$$"
+    ENV[key] = "echo"
+    res = wordexp("$#{key}")
+    assert_equal %w(echo), res
+  end
+
+  def test_syntax
+    assert_raises(WordexpError::Syntax) do
+      wordexp("$(sh -c '")
+    end
+  end
+
+  def test_badval
+    key = "TEST_WORDEXP_BADVAL_#$$"
+    ENV.delete(key) # just in case
+    s = "a$#{key}"
+    res = wordexp(s)
+    assert_equal ['a'], res
+
+    assert_raises(WordexpError::Badval) do
+      wordexp(s, WRDE_UNDEF)
+    end
+  end
+
+  def test_badchar
+    assert_raises(WordexpError::Badchar) do
+      wordexp("true|true")
+    end
+    assert_equal %w(true|true), wordexp('"true|true"')
+  end
+
+  def test_cmdsub_err
+    assert_raises(WordexpError::Cmdsub) do
+      wordexp("$(echo hi)", WRDE_NOCMD)
+    end
+  end
+
+  def test_cmdsub_nogvl
+    return if ENV["NO_SLEEP"]
+    times = []
+    start = Time.now
+    stop = nil
+    thr = Thread.new do
+      until stop do
+        times << Time.now.to_f
+        sleep 0.05
+      end
+    end
+    assert_equal %w(now), wordexp("$(sleep 1 >/dev/null 2>&1) now")
+    stop = Time.now
+    assert_in_delta(stop - start, 1, 0.1)
+    assert_operator times.size, :>, 15
+  end
+
+  def test_arithmetic
+    assert_equal %w(666), wordexp("$((333 + 333))")
+    assert_equal %w(8), wordexp("$((010))")
+    assert_equal %w(1638), wordexp("$((0x666))")
+  end
+
+  def test_shell_funcs
+    key = "TEST_WORDEXP_SHFUNCS_#$$"
+    ENV.delete(key) # just in case
+    assert_equal %w(foo), wordexp("${#{key}-foo}")
+    ENV[key] = "moo"
+    assert_equal %w(moo), wordexp("${#{key}-foo}")
+    assert_equal %w(3), wordexp("${\##{key}}")
+    assert_equal %w(oo), wordexp("${#{key}#m}")
+    assert_equal %w(m), wordexp("${#{key}%oo}")
+  ensure
+    ENV.delete(key)
+  end
+
+  def test_ifs
+    key = "TEST_WORDEXP_IFS_#$$"
+    orig = ENV["IFS"]
+
+    ENV["IFS"] = ""
+    ENV[key] = "a b c"
+    assert_equal ["a b c"], wordexp("$#{key}")
+
+    ENV[key] = "a\nb\tc"
+    ENV.delete("IFS")
+    assert_equal %w(a b c), wordexp("$#{key}")
+
+    ENV[key] = "a,b,c"
+    ENV["IFS"] = ","
+    assert_equal %w(a b c), wordexp("$#{key}")
+  ensure
+    ENV["IFS"] = orig
+    ENV.delete(key)
+  end
+end