浏览代码

Adding Tests and move source to src/

Michael Mächtel 8 年前
父节点
当前提交
72a9b7e93e
共有 4 个文件被更改,包括 112 次插入0 次删除
  1. 0
    0
      hw7/task1/src/hasher_sha256.rs
  2. 0
    0
      hw7/task1/src/unit_tests.rs
  3. 59
    0
      hw7/task1/tests/output.bats
  4. 53
    0
      hw7/task1/tests/test_helper.bash

hw7/task1/hasher_sha256.rs → hw7/task1/src/hasher_sha256.rs 查看文件


hw7/task1/unit_tests.rs → hw7/task1/src/unit_tests.rs 查看文件


+ 59
- 0
hw7/task1/tests/output.bats 查看文件

@@ -0,0 +1,59 @@
1
+#!/usr/bin/env bats
2
+
3
+load test_helper
4
+
5
+
6
+@test "task1: Check that we have a debug output" {
7
+    run stat "$BATS_TEST_DIRNAME/../target/debug/task1"
8
+    [ "$status" -eq 0 ]
9
+}
10
+
11
+# Check lines of output
12
+
13
+# wc output with white spaces is trimmed by xargs
14
+@test "task1: Error Output must at least 8 Lines long" {
15
+    run bash -c "'$BATS_TEST_DIRNAME/../target/debug/task1' 2>&1 | wc -l | xargs"
16
+    assert_range 8 6 10   
17
+
18
+}
19
+
20
+# wc output with white correct input is trimmed by xargs
21
+@test "task1: Normal Output must be at least 4 line long" {
22
+    run bash -c "'$BATS_TEST_DIRNAME/../target/debug/task1' 42 123 | wc -l | xargs"
23
+    assert_range 4 4 6
24
+}
25
+
26
+# wc output with white correct input is trimmed by xargs
27
+@test "task1: Help Output must be at least 18 line long" {
28
+    run bash -c "'$BATS_TEST_DIRNAME/../target/debug/task1' -- --help | wc -l | xargs"
29
+    assert_range 18 18 20
30
+}
31
+
32
+# timeout loop
33
+@test "task1: Output with wrong pattern" {
34
+    run timeout 2 bash -c "'$BATS_TEST_DIRNAME/../target/debug/task1' 42 j "
35
+    no_timeout_fail
36
+
37
+}
38
+
39
+
40
+# Status checks
41
+@test "task1: Output with wrong input paras does not crash" {
42
+    run bash -c "'$BATS_TEST_DIRNAME/../target/debug/task1' x y z "
43
+    assert_fail
44
+}
45
+
46
+@test "task1: Output with wrong PARAM does not crash" {
47
+    run bash -c "'$BATS_TEST_DIRNAME/../target/debug/task1' a a"
48
+    assert_fail
49
+}
50
+
51
+#@test "task1: Output with wrong pattern does not crash" {
52
+#    run bash -c "'$BATS_TEST_DIRNAME/../target/debug/task1' 2 a "
53
+#    assert_fail
54
+#}
55
+
56
+
57
+
58
+
59
+

+ 53
- 0
hw7/task1/tests/test_helper.bash 查看文件

@@ -0,0 +1,53 @@
1
+#!/usr/bin/env bash
2
+
3
+assert_equal() {
4
+	if [ "$1" != "$2" ]; then
5
+		echo "expected: $1"
6
+		echo "actual:   $2"
7
+		return 1
8
+	fi
9
+}
10
+
11
+assert_range() {
12
+	if [ $1 -lt $2 ]; then
13
+		echo "expected: $1"
14
+		echo "greater than: $2"
15
+		return 1
16
+	fi
17
+	if [ $1 -gt $3 ]; then
18
+		echo "expected: $1"
19
+		echo "less than: $3"
20
+		return 1
21
+	fi
22
+}
23
+
24
+assert_output() {
25
+	assert_equal "$1" "$output"
26
+}
27
+
28
+assert_success() {
29
+	if [ "$status" -ne 0 ]; then
30
+		echo "command failed with exit status $status"
31
+		return 1
32
+	elif [ "$#" -gt 0 ]; then
33
+		assert_output "$1"
34
+	fi
35
+}
36
+
37
+assert_fail() {
38
+	if [ "$status" -eq 0 ]; then
39
+		echo "command successed, but should fail"
40
+		return 1
41
+	elif [ "$#" -gt 0 ]; then
42
+		assert_output "$1"
43
+	fi
44
+}
45
+
46
+no_timeout_fail() {
47
+	if [ "$status" -eq 124 ]; then
48
+		echo "timeout with command"
49
+		return 1
50
+	elif [ "$#" -gt 0 ]; then
51
+		assert_output "$1"
52
+	fi
53
+}

正在加载...
取消
保存