File: utils_spec.cr

package info (click to toggle)
crystal 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,384 kB
  • sloc: javascript: 6,400; sh: 695; makefile: 269; ansic: 121; python: 105; cpp: 77; xml: 32
file content (120 lines) | stat: -rw-r--r-- 6,415 bytes parent folder | download
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
113
114
115
116
117
118
119
120
require "spec"

describe Process do
  describe ".executable_path" do
    it "searches executable" do
      Process.executable_path.should be_a(String | Nil)
    end
  end

  describe ".quote_posix" do
    it { Process.quote_posix("").should eq "''" }
    it { Process.quote_posix(" ").should eq "' '" }
    it { Process.quote_posix("$hi").should eq "'$hi'" }
    it { Process.quote_posix(orig = "aZ5+,-./:=@_").should eq orig }
    it { Process.quote_posix(orig = "cafe").should eq orig }
    it { Process.quote_posix("café").should eq "'café'" }
    it { Process.quote_posix("I'll").should eq %('I'"'"'ll') }
    it { Process.quote_posix("'").should eq %(''"'"'') }
    it { Process.quote_posix("\\").should eq "'\\'" }

    context "join" do
      it { Process.quote_posix([] of String).should eq "" }
      it { Process.quote_posix(["my file.txt", "another.txt"]).should eq "'my file.txt' another.txt" }
      it { Process.quote_posix(["foo ", "", " ", " bar"]).should eq "'foo ' '' ' ' ' bar'" }
      it { Process.quote_posix(["foo'", "\"bar"]).should eq %('foo'"'"'' '"bar') }
    end
  end

  describe ".quote_windows" do
    it { Process.quote_windows("").should eq %("") }
    it { Process.quote_windows(" ").should eq %(" ") }
    it { Process.quote_windows(orig = "%hi%").should eq orig }
    it { Process.quote_windows(%q(C:\"foo" project.txt)).should eq %q("C:\\\"foo\" project.txt") }
    it { Process.quote_windows(%q(C:\"foo"_project.txt)).should eq %q(C:\\\"foo\"_project.txt) }
    it { Process.quote_windows(%q(C:\Program Files\Foo Bar\foobar.exe)).should eq %q("C:\Program Files\Foo Bar\foobar.exe") }
    it { Process.quote_windows(orig = "café").should eq orig }
    it { Process.quote_windows(%(")).should eq %q(\") }
    it { Process.quote_windows(%q(a\\b\ c\)).should eq %q("a\\b\ c\\") }
    it { Process.quote_windows(orig = %q(a\\b\c\)).should eq orig }

    context "join" do
      it { Process.quote_windows([] of String).should eq "" }
      it { Process.quote_windows(["my file.txt", "another.txt"]).should eq %("my file.txt" another.txt) }
      it { Process.quote_windows(["foo ", "", " ", " bar"]).should eq %("foo " "" " " " bar") }
    end
  end

  {% if flag?(:unix) %}
    describe ".parse_arguments" do
      it "uses the native platform rules" do
        Process.parse_arguments(%q[a\ b'c']).should eq [%q[a bc]]
      end
    end
  {% elsif flag?(:win32) %}
    describe ".parse_arguments" do
      it "uses the native platform rules" do
        Process.parse_arguments(%q[a\ b'c']).should eq [%q[a\], %q[b'c']]
      end
    end
  {% else %}
    pending ".parse_arguments"
  {% end %}

  describe ".parse_arguments_posix" do
    it { Process.parse_arguments_posix(%q[]).should eq([] of String) }
    it { Process.parse_arguments_posix(%q[ ]).should eq([] of String) }
    it { Process.parse_arguments_posix(%q[foo]).should eq [%q[foo]] }
    it { Process.parse_arguments_posix(%q[foo bar]).should eq [%q[foo], %q[bar]] }
    it { Process.parse_arguments_posix(%q["foo bar" 'foo bar' baz]).should eq [%q[foo bar], %q[foo bar], %q[baz]] }
    it { Process.parse_arguments_posix(%q["foo bar"'foo bar'baz]).should eq [%q[foo barfoo barbaz]] }
    it { Process.parse_arguments_posix(%q[foo\ bar]).should eq [%q[foo bar]] }
    it { Process.parse_arguments_posix(%q["foo\ bar"]).should eq [%q[foo\ bar]] }
    it { Process.parse_arguments_posix(%q['foo\ bar']).should eq [%q[foo\ bar]] }
    it { Process.parse_arguments_posix(%q[\]).should eq [%q[\]] }
    it { Process.parse_arguments_posix(%q["foo bar" '\hello/' Fizz\ Buzz]).should eq [%q[foo bar], %q[\hello/], %q[Fizz Buzz]] }
    it { Process.parse_arguments_posix(%q[foo"bar"baz]).should eq [%q[foobarbaz]] }
    it { Process.parse_arguments_posix(%q[foo'bar'baz]).should eq [%q[foobarbaz]] }
    it { Process.parse_arguments_posix(%q[this 'is a "'very wei"rd co"m"mand please" don't do t'h'a't p"leas"e]).should eq [%q[this], %q[is a "very], %q[weird command please], %q[dont do that], %q[please]] }

    it "raises an error when double quote is unclosed" do
      expect_raises ArgumentError, "Unmatched quote" do
        Process.parse_arguments_posix(%q["foo])
      end
    end

    it "raises an error if single quote is unclosed" do
      expect_raises ArgumentError, "Unmatched quote" do
        Process.parse_arguments_posix(%q['foo])
      end
    end
  end

  describe ".parse_arguments_windows" do
    it { Process.parse_arguments_windows(%q[]).should eq([] of String) }
    it { Process.parse_arguments_windows(%q[ ]).should eq([] of String) }
    it { Process.parse_arguments_windows(%q[foo]).should eq [%q[foo]] }
    it { Process.parse_arguments_windows(%q[foo bar]).should eq [%q[foo], %q[bar]] }
    it { Process.parse_arguments_windows(%q["foo bar" 'foo bar' baz]).should eq [%q[foo bar], %q['foo], %q[bar'], %q[baz]] }
    it { Process.parse_arguments_windows(%q["foo bar"baz]).should eq [%q[foo barbaz]] }
    it { Process.parse_arguments_windows(%q[foo"bar baz"]).should eq [%q[foobar baz]] }
    it { Process.parse_arguments_windows(%q[foo\bar]).should eq [%q[foo\bar]] }
    it { Process.parse_arguments_windows(%q[foo\ bar]).should eq [%q[foo\], %q[bar]] }
    it { Process.parse_arguments_windows(%q[foo\\bar]).should eq [%q[foo\\bar]] }
    it { Process.parse_arguments_windows(%q[foo\\\bar]).should eq [%q[foo\\\bar]] }
    it { Process.parse_arguments_windows(%q[ /LIBPATH:C:\crystal\lib ]).should eq [%q[/LIBPATH:C:\crystal\lib]] }
    it { Process.parse_arguments_windows(%q[a\\\b d"e f"g h]).should eq [%q[a\\\b], %q[de fg], %q[h]] }
    it { Process.parse_arguments_windows(%q[a\\\"b c d]).should eq [%q[a\"b], %q[c], %q[d]] }
    it { Process.parse_arguments_windows(%q[a\\\\"b c" d e]).should eq [%q[a\\b c], %q[d], %q[e]] }
    it { Process.parse_arguments_windows(%q["foo bar" '\hello/' Fizz\ Buzz]).should eq [%q[foo bar], %q['\hello/'], %q[Fizz\], %q[Buzz]] }
    it { Process.parse_arguments_windows(%q[this 'is a "'very wei"rd co"m"mand please" don't do t'h'a't p"leas"e"]).should eq [%q[this], %q['is], %q[a], %q['very weird], %q[command], %q[please don't do t'h'a't please]] }

    it "raises an error if double quote is unclosed" do
      expect_raises ArgumentError, "Unmatched quote" do
        Process.parse_arguments_windows(%q["foo])
        Process.parse_arguments_windows(%q[\"foo])
        Process.parse_arguments_windows(%q["f\"oo\\\"])
      end
    end
  end
end