aboutsummaryrefslogtreecommitdiff
path: root/tests/grep.test
blob: 13b1af7882899b426741e5428584fa4358fef227 (plain)
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
#!/bin/bash

[ -f testing.sh ] && . testing.sh

# Copyright 2013 by Kyungsu Kim <kaspyx@gmail.com>
# Copyright 2013 by Kyungwan Han <asura321@gmail.com>

#testing "name" "command" "result" "infile" "stdin"

testing "grep -c" "grep -c 123 input" "3\n" "123\ncount 123\n123\nfasdfasdf" ""

echo -e "this is test" > foo
echo -e "this is test2" > foo2
echo -e "this is foo3" > foo3
testing "grep -l" "grep -l test foo foo2 foo3" "foo\nfoo2\n" "" ""
rm foo foo2 foo3

testing "grep -q" "grep -q test input && echo yes" "yes\n" "this is a test\n" ""
testing "grep -E" "grep -E '[0-9]' input" "1234123asdfas123123\n1\n" \
  "1234123asdfas123123\nabc\n1\nabcde" ""
testing "grep -e" "grep -e '[0-9]' input" "1234123asdfas123123\n1\n" \
  "1234123asdfas123123\nabc\n1\nabcde" ""
testing "grep -e -e" "grep -e one -e two -e three input" \
  "two\ntwo\nthree\none\n" "two\ntwo\nthree\nand\none\n" ""
testing "grep -F" "grep -F is input" "this is test\nthis is test2\n" \
  "this is test\nthis is test2\ntest case" ""

echo -e "this is test\nthis is test2\ntest case" > foo
echo -e "hello this is test" > foo2
echo -e "hi hello" > foo3
testing "grep -H" "grep -H is foo foo2 foo3" "foo:this is test\nfoo:this is test2\nfoo2:hello this is test\n" "" ""
rm foo foo2 foo3

testing "grep -b" "grep -b is input" "0:this is test\n13:this is test2\n" \
  "this is test\nthis is test2\ntest case" ""
testing "grep -i" "grep -i is input" "thisIs test\nthis is test2\n" \
  "thisIs test\nthis is test2\ntest case" ""
testing "grep -n" "grep -n is input" "1:this is test\n2:this is test2\n" \
  "this is test\nthis is test2\ntest case" ""
testing "grep -o" "grep -o is input" "is\nis\nis\nis\n" \
  "this is test\nthis is test2\ntest case" ""
testing "grep -s" "grep -hs hello asdf input 2>&1" "hello\n" "hello\n" ""
testing "grep -v" "grep -v abc input" "1234123asdfas123123\n1ABa\n" \
  "1234123asdfas123123\n1ABabc\nabc\n1ABa\nabcde" ""
testing "grep -w" "grep -w abc input" "abc\n" \
  "1234123asdfas123123\n1ABabc\nabc\n1ABa\nabcde" ""
testing "grep -x" "grep -x abc input" "abc\n" \
  "aabcc\nabc\n" ""

testing "grep -H (standard input)" "grep -H abc" "(standard input):abc\n" \
  "" "abc\n"
testing "grep -l (standard input)" "grep -l abc" "(standard input)\n" \
  "" "abc\n"
testing "grep -n two inputs" "grep -hn def - input" "2:def\n2:def\n" \
  "abc\ndef\n" "abc\ndef\n"

testing "grep pattern with newline" "grep 'abc
def' input" "aabcc\nddeff\n" \
  "aaaa\naabcc\n\dddd\nddeff\nffff\n" ""

testing "grep -lH" "grep -lH abc input" "input\n" "abc\n" ""
testing "grep -cn" "grep -cn abc input" "1\n" "abc\n" ""
testing "grep -qs" "grep -qs abc none input && echo yes" "yes\n" "abc\n" ""
testing "grep -hl" "grep -hl abc input" "input\n" "abc\n" ""
testing "grep -b stdin" "grep -b one" "0:one\n4:one\n8:one\n" "" "one\none\none\n"
testing "grep -o overlap" "grep -bo aaa" "1:aaa\n" "" "baaaa\n"
# nonobvious: -co counts lines, not matches
testing "grep -co" "grep -co one input" "1\n" "one one one\n" ""
testing "grep -nom" "grep -nom 2 one" "1:one\n1:one\n1:one\n2:one\n2:one\n" \
  "" "one one one\none one\none"
testing "grep -vo" "grep -vo one input" "two\nthree\n" "onetwoonethreeone\n" ""
testing "grep no newline" "grep -h one input -" \
  "hello one\nthere one\n" "hello one" "there one"

testing "grep -e multi" "grep -e one -ethree input" \
  "three\none\n" "three\ntwo\none\n" ""
# Suppress filenames for recursive test because dunno order they'd occur in
mkdir sub
echo -e "one\ntwo\nthree" > sub/one
echo -e "three\ntwo\none" > sub/two
testing "grep -hr" "grep -hr one sub" "one\none\n" "" ""
testing "grep -r file" "grep -r three sub/two" "three\n" "" ""
testing "grep -r dir" "grep -r one sub | sort" "sub/one:one\nsub/two:one\n" \
  "" ""
rm -rf sub

# Not sure if -Fx '' should do? Posix is unclear, '' match every line but does
# it match every _byte_ in that line?
testing "grep -Fx ''" "grep -Fx '' input" "one one one\n" "one one one\n" ""
testing "grep -F ''" "grep -F '' input" "one one one\n" "one one one\n" ""
testing "grep -F -e blah -e ''" "grep -F -e blah -e '' input" "one one one\n" \
  "one one one\n" ""
testing "grep -e blah -e ''" "grep -e blah -e '' input" "one one one\n" \
  "one one one\n" ""
testing "grep -w ''" "grep -w '' input" "" "one one one\n" ""
testing "grep -o ''" "grep -o '' input" "" "one one one\n" ""
testing "grep backref" 'grep -e "a\(b\)" -e "b\(c\)\1"' "bcc\nab\n" \
  "" "bcc\nbcb\nab\n"