~nchohan/+junk/mytools

« back to all changes in this revision

Viewing changes to test/test_parse_args.rb

  • Committer: root
  • Date: 2010-11-03 07:43:57 UTC
  • Revision ID: root@appscale-image0-20101103074357-xea7ja3sor3x93oc
init

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/ruby -w
 
2
# Programmer: Chris Bunch
 
3
# Test code for lib/parse_args.rb
 
4
# Right now, just testing argument parsing, not actual content of args
 
5
 
 
6
require 'rubygems'
 
7
require 'flexmock/test_unit'
 
8
require 'redgreen'
 
9
require 'shoulda'
 
10
 
 
11
USAGE = "baz"
 
12
ALL_FLAGS = ["file", "foo", "ips"]
 
13
TARFILE = "goo.tar.gz"
 
14
YAMLFILE = "ips.yaml"
 
15
 
 
16
class TestParseArgs < Test::Unit::TestCase
 
17
  context "test bad input" do
 
18
    setup do
 
19
      # blank out STDERR.write so that abort(msg) doesn't write msg
 
20
      # to stderr during test runs and clog up the rake results
 
21
      stderr = flexmock(STDERR)
 
22
      stderr.should_receive(:write).and_return("")
 
23
 
 
24
      $:.unshift File.join(File.dirname(__FILE__), "..", "lib")
 
25
      require 'parse_args'
 
26
    end
 
27
 
 
28
    should "throw exceptions on bad input types" do
 
29
      assert_raise(RuntimeError) { parse_args(nil) }
 
30
      assert_raise(RuntimeError) { parse_args(1) }
 
31
      assert_raise(RuntimeError) { parse_args("") }
 
32
    end
 
33
    
 
34
    should "return an empty hash on no args" do
 
35
      assert_equal({}, parse_args([]))
 
36
    end
 
37
    
 
38
    should "exit if params are present but not flags" do
 
39
      assert_raise(SystemExit) { parse_args(["foo", "boo", "goo"]) }
 
40
    end
 
41
 
 
42
    should "exit on invalid flags" do
 
43
      args = ["--file", TARFILE, "--baz"]
 
44
      assert_raise(SystemExit) { parse_args(args) }
 
45
    end
 
46
 
 
47
    should "exit if a param is present without a flag" do
 
48
      args = ["baz", "-file", TARFILE]
 
49
      assert_raise(SystemExit) { parse_args(args) }
 
50
 
 
51
      args = ["-file", TARFILE, "baz"]
 
52
      assert_raise(SystemExit) { parse_args(args) }
 
53
    end
 
54
  end
 
55
  
 
56
  context "test good input" do
 
57
    should "test out one flag with one param" do
 
58
      args = ["-file", TARFILE]
 
59
      assert_equal({"file" => TARFILE}, parse_args(args))
 
60
    end
 
61
  
 
62
    should "test out one flag with no param" do
 
63
      args = ["-foo"]
 
64
      assert_equal({"foo" => "NO ARG"}, parse_args(args))
 
65
    end
 
66
  
 
67
    should "test out two flags, one with a param" do
 
68
      args = ["-foo", "-file", TARFILE]
 
69
      assert_equal({"foo" => "NO ARG", "file" => TARFILE}, parse_args(args))
 
70
    end
 
71
  
 
72
    should "same as before but in a different order" do
 
73
      args = ["-file", TARFILE, "-foo"]
 
74
      assert_equal({"foo" => "NO ARG", "file" => TARFILE}, parse_args(args))
 
75
    end
 
76
    
 
77
    should "match common virtualized deployment flags" do
 
78
      args = ["-file", TARFILE, "-ips", YAMLFILE]
 
79
      assert_equal({"file" => TARFILE, "ips" => YAMLFILE}, parse_args(args))
 
80
    end  
 
81
  
 
82
    should "allow users to type in flags with two dashes as well" do
 
83
      args = ["--file", TARFILE]
 
84
      assert_equal({"file" => TARFILE}, parse_args(args))
 
85
    end
 
86
  
 
87
    should "test two dashes, no param" do
 
88
      args = ["--foo"]
 
89
      assert_equal({"foo" => "NO ARG"}, parse_args(args))
 
90
    end
 
91
  
 
92
    should "test two flags, one with a param, both with two dashes" do
 
93
      args = ["--foo", "--file", TARFILE]
 
94
      assert_equal({"foo" => "NO ARG", "file" => TARFILE}, parse_args(args))
 
95
    end
 
96
  
 
97
    should "test two flags with params, both with dashes" do
 
98
      args = ["--file", TARFILE, "--foo"]
 
99
      assert_equal({"foo" => "NO ARG", "file" => TARFILE}, parse_args(args))
 
100
    end
 
101
    
 
102
    should "match common virtualized deployment flags with two dashes" do
 
103
      args = ["--file", TARFILE, "--ips", YAMLFILE]
 
104
      assert_equal({"file" => TARFILE, "ips" => YAMLFILE}, parse_args(args))
 
105
    end 
 
106
  end 
 
107
end
 
 
b'\\ No newline at end of file'