blob: 13b1af7882899b426741e5428584fa4358fef227 [file] [log] [blame]
Ashwini Sharma577377c2013-08-01 01:52:32 -05001#!/bin/bash
2
3[ -f testing.sh ] && . testing.sh
4
5# Copyright 2013 by Kyungsu Kim <kaspyx@gmail.com>
6# Copyright 2013 by Kyungwan Han <asura321@gmail.com>
7
Ashwini Sharma577377c2013-08-01 01:52:32 -05008#testing "name" "command" "result" "infile" "stdin"
9
Rob Landley3595eff2013-08-01 15:22:52 -050010testing "grep -c" "grep -c 123 input" "3\n" "123\ncount 123\n123\nfasdfasdf" ""
Ashwini Sharma577377c2013-08-01 01:52:32 -050011
Ashwini Sharma577377c2013-08-01 01:52:32 -050012echo -e "this is test" > foo
13echo -e "this is test2" > foo2
14echo -e "this is foo3" > foo3
Rob Landley3595eff2013-08-01 15:22:52 -050015testing "grep -l" "grep -l test foo foo2 foo3" "foo\nfoo2\n" "" ""
Ashwini Sharma577377c2013-08-01 01:52:32 -050016rm foo foo2 foo3
17
Rob Landley3595eff2013-08-01 15:22:52 -050018testing "grep -q" "grep -q test input && echo yes" "yes\n" "this is a test\n" ""
Rob Landley9f088a12013-08-12 04:28:03 -050019testing "grep -E" "grep -E '[0-9]' input" "1234123asdfas123123\n1\n" \
Rob Landley3595eff2013-08-01 15:22:52 -050020 "1234123asdfas123123\nabc\n1\nabcde" ""
Rob Landley9f088a12013-08-12 04:28:03 -050021testing "grep -e" "grep -e '[0-9]' input" "1234123asdfas123123\n1\n" \
Rob Landley3595eff2013-08-01 15:22:52 -050022 "1234123asdfas123123\nabc\n1\nabcde" ""
Rob Landley9f088a12013-08-12 04:28:03 -050023testing "grep -e -e" "grep -e one -e two -e three input" \
24 "two\ntwo\nthree\none\n" "two\ntwo\nthree\nand\none\n" ""
Rob Landley3595eff2013-08-01 15:22:52 -050025testing "grep -F" "grep -F is input" "this is test\nthis is test2\n" \
26 "this is test\nthis is test2\ntest case" ""
Ashwini Sharma577377c2013-08-01 01:52:32 -050027
28echo -e "this is test\nthis is test2\ntest case" > foo
29echo -e "hello this is test" > foo2
30echo -e "hi hello" > foo3
Rob Landley3595eff2013-08-01 15:22:52 -050031testing "grep -H" "grep -H is foo foo2 foo3" "foo:this is test\nfoo:this is test2\nfoo2:hello this is test\n" "" ""
32rm foo foo2 foo3
Ashwini Sharma577377c2013-08-01 01:52:32 -050033
Rob Landley3595eff2013-08-01 15:22:52 -050034testing "grep -b" "grep -b is input" "0:this is test\n13:this is test2\n" \
35 "this is test\nthis is test2\ntest case" ""
36testing "grep -i" "grep -i is input" "thisIs test\nthis is test2\n" \
37 "thisIs test\nthis is test2\ntest case" ""
38testing "grep -n" "grep -n is input" "1:this is test\n2:this is test2\n" \
39 "this is test\nthis is test2\ntest case" ""
40testing "grep -o" "grep -o is input" "is\nis\nis\nis\n" \
41 "this is test\nthis is test2\ntest case" ""
Rob Landley7b72ab42013-08-04 15:04:08 -050042testing "grep -s" "grep -hs hello asdf input 2>&1" "hello\n" "hello\n" ""
Rob Landley3595eff2013-08-01 15:22:52 -050043testing "grep -v" "grep -v abc input" "1234123asdfas123123\n1ABa\n" \
44 "1234123asdfas123123\n1ABabc\nabc\n1ABa\nabcde" ""
45testing "grep -w" "grep -w abc input" "abc\n" \
46 "1234123asdfas123123\n1ABabc\nabc\n1ABa\nabcde" ""
Rob Landley7b72ab42013-08-04 15:04:08 -050047testing "grep -x" "grep -x abc input" "abc\n" \
48 "aabcc\nabc\n" ""
49
50testing "grep -H (standard input)" "grep -H abc" "(standard input):abc\n" \
51 "" "abc\n"
52testing "grep -l (standard input)" "grep -l abc" "(standard input)\n" \
53 "" "abc\n"
54testing "grep -n two inputs" "grep -hn def - input" "2:def\n2:def\n" \
55 "abc\ndef\n" "abc\ndef\n"
56
57testing "grep pattern with newline" "grep 'abc
58def' input" "aabcc\nddeff\n" \
59 "aaaa\naabcc\n\dddd\nddeff\nffff\n" ""
60
61testing "grep -lH" "grep -lH abc input" "input\n" "abc\n" ""
62testing "grep -cn" "grep -cn abc input" "1\n" "abc\n" ""
63testing "grep -qs" "grep -qs abc none input && echo yes" "yes\n" "abc\n" ""
64testing "grep -hl" "grep -hl abc input" "input\n" "abc\n" ""
65testing "grep -b stdin" "grep -b one" "0:one\n4:one\n8:one\n" "" "one\none\none\n"
66testing "grep -o overlap" "grep -bo aaa" "1:aaa\n" "" "baaaa\n"
Rob Landley9f088a12013-08-12 04:28:03 -050067# nonobvious: -co counts lines, not matches
68testing "grep -co" "grep -co one input" "1\n" "one one one\n" ""
Rob Landley7b72ab42013-08-04 15:04:08 -050069testing "grep -nom" "grep -nom 2 one" "1:one\n1:one\n1:one\n2:one\n2:one\n" \
70 "" "one one one\none one\none"
Rob Landleyfce85e92013-08-19 03:17:51 -050071testing "grep -vo" "grep -vo one input" "two\nthree\n" "onetwoonethreeone\n" ""
Rob Landley7b72ab42013-08-04 15:04:08 -050072testing "grep no newline" "grep -h one input -" \
73 "hello one\nthere one\n" "hello one" "there one"
Rob Landley133cc5b2013-08-11 01:03:26 -050074
75testing "grep -e multi" "grep -e one -ethree input" \
76 "three\none\n" "three\ntwo\none\n" ""
77# Suppress filenames for recursive test because dunno order they'd occur in
78mkdir sub
79echo -e "one\ntwo\nthree" > sub/one
80echo -e "three\ntwo\none" > sub/two
Rob Landleyfce85e92013-08-19 03:17:51 -050081testing "grep -hr" "grep -hr one sub" "one\none\n" "" ""
82testing "grep -r file" "grep -r three sub/two" "three\n" "" ""
83testing "grep -r dir" "grep -r one sub | sort" "sub/one:one\nsub/two:one\n" \
84 "" ""
Rob Landley133cc5b2013-08-11 01:03:26 -050085rm -rf sub
86
Rob Landleyfce85e92013-08-19 03:17:51 -050087# Not sure if -Fx '' should do? Posix is unclear, '' match every line but does
88# it match every _byte_ in that line?
89testing "grep -Fx ''" "grep -Fx '' input" "one one one\n" "one one one\n" ""
Rob Landley133cc5b2013-08-11 01:03:26 -050090testing "grep -F ''" "grep -F '' input" "one one one\n" "one one one\n" ""
Rob Landleyfce85e92013-08-19 03:17:51 -050091testing "grep -F -e blah -e ''" "grep -F -e blah -e '' input" "one one one\n" \
92 "one one one\n" ""
93testing "grep -e blah -e ''" "grep -e blah -e '' input" "one one one\n" \
94 "one one one\n" ""
Rob Landley133cc5b2013-08-11 01:03:26 -050095testing "grep -w ''" "grep -w '' input" "" "one one one\n" ""
96testing "grep -o ''" "grep -o '' input" "" "one one one\n" ""
Rob Landleyfce85e92013-08-19 03:17:51 -050097testing "grep backref" 'grep -e "a\(b\)" -e "b\(c\)\1"' "bcc\nab\n" \
98 "" "bcc\nbcb\nab\n"