소스 검색

Status helper

new-scoring
aeris 2 년 전
부모
커밋
d0160f1067
3개의 변경된 파일150개의 추가작업 그리고 0개의 파일을 삭제
  1. 1
    0
      lib/cryptcheck.rb
  2. 28
    0
      lib/cryptcheck/status.rb
  3. 121
    0
      spec/cryptcheck/status_spec.rb

+ 1
- 0
lib/cryptcheck.rb 파일 보기

@@ -31,6 +31,7 @@ module CryptCheck
31 31
 		end
32 32
 	end
33 33
 
34
+	autoload :Status, 'cryptcheck/status'
34 35
 	autoload :Logger, 'cryptcheck/logger'
35 36
 	autoload :Tls, 'cryptcheck/tls'
36 37
 	module Tls

+ 28
- 0
lib/cryptcheck/status.rb 파일 보기

@@ -0,0 +1,28 @@
1
+module CryptCheck
2
+	module Status
3
+		LEVELS = %i(critical error warning good perfect best).freeze
4
+		PROBLEMS = %i(critical error warning).freeze
5
+
6
+		def self.status(statuses)
7
+			statuses = self.collect statuses
8
+			self.select LEVELS, statuses
9
+		end
10
+
11
+		def self.problem(statuses)
12
+			statuses = self.collect statuses
13
+			self.select PROBLEMS, statuses
14
+		end
15
+
16
+		private
17
+		def self.collect(statuses)
18
+			first = statuses.first
19
+			statuses = statuses.collect &:status if first.respond_to? :status
20
+			statuses
21
+		end
22
+
23
+		def self.select(levels, statuses)
24
+			return nil if statuses.empty?
25
+			(levels & statuses).first
26
+		end
27
+	end
28
+end

+ 121
- 0
spec/cryptcheck/status_spec.rb 파일 보기

@@ -0,0 +1,121 @@
1
+require 'ostruct'
2
+
3
+describe CryptCheck::Status do
4
+	describe '::status' do
5
+		it 'must handle empty list' do
6
+			expect(CryptCheck::Status.status []).to be_nil
7
+		end
8
+
9
+		it 'must answer correctly' do
10
+			{
11
+					[:critical, :critical] => :critical,
12
+					[:critical, :error]    => :critical,
13
+					[:critical, :warning]  => :critical,
14
+					[:critical, :good]     => :critical,
15
+					[:critical, :perfect]  => :critical,
16
+					[:critical, :best]     => :critical,
17
+
18
+					[:error, :critical]    => :critical,
19
+					[:error, :error]       => :error,
20
+					[:error, :warning]     => :error,
21
+					[:error, :good]        => :error,
22
+					[:error, :perfect]     => :error,
23
+					[:error, :best]        => :error,
24
+
25
+					[:warning, :critical]  => :critical,
26
+					[:warning, :error]     => :error,
27
+					[:warning, :warning]   => :warning,
28
+					[:warning, :good]      => :warning,
29
+					[:warning, :perfect]   => :warning,
30
+					[:warning, :best]      => :warning,
31
+
32
+					[:good, :critical]     => :critical,
33
+					[:good, :error]        => :error,
34
+					[:good, :warning]      => :warning,
35
+					[:good, :good]         => :good,
36
+					[:good, :perfect]      => :good,
37
+					[:good, :best]         => :good,
38
+
39
+					[:perfect, :critical]  => :critical,
40
+					[:perfect, :error]     => :error,
41
+					[:perfect, :warning]   => :warning,
42
+					[:perfect, :good]      => :good,
43
+					[:perfect, :perfect]   => :perfect,
44
+					[:perfect, :best]      => :perfect,
45
+
46
+					[:best, :critical]     => :critical,
47
+					[:best, :error]        => :error,
48
+					[:best, :warning]      => :warning,
49
+					[:best, :good]         => :good,
50
+					[:best, :perfect]      => :perfect,
51
+					[:best, :best]         => :best
52
+			}.each do |levels, result|
53
+				got = CryptCheck::Status.status levels
54
+				expect(got).to be(result), "#{levels} : expected #{result.inspect}, got #{got.inspect}"
55
+			end
56
+		end
57
+
58
+		it 'must handle object list' do
59
+			critical = OpenStruct.new status: :critical
60
+			warning  = OpenStruct.new status: :warning
61
+			expect(CryptCheck::Status.status [critical, warning]).to be :critical
62
+		end
63
+	end
64
+
65
+	describe '::problem' do
66
+		it 'must answer correctly' do
67
+			{
68
+					[:critical, :critical] => :critical,
69
+					[:critical, :error]    => :critical,
70
+					[:critical, :warning]  => :critical,
71
+					[:critical, :good]     => :critical,
72
+					[:critical, :perfect]  => :critical,
73
+					[:critical, :best]     => :critical,
74
+
75
+					[:error, :critical]    => :critical,
76
+					[:error, :error]       => :error,
77
+					[:error, :warning]     => :error,
78
+					[:error, :good]        => :error,
79
+					[:error, :perfect]     => :error,
80
+					[:error, :best]        => :error,
81
+
82
+					[:warning, :critical]  => :critical,
83
+					[:warning, :error]     => :error,
84
+					[:warning, :warning]   => :warning,
85
+					[:warning, :good]      => :warning,
86
+					[:warning, :perfect]   => :warning,
87
+					[:warning, :best]      => :warning,
88
+
89
+					[:good, :critical]     => :critical,
90
+					[:good, :error]        => :error,
91
+					[:good, :warning]      => :warning,
92
+					[:good, :good]         => nil,
93
+					[:good, :perfect]      => nil,
94
+					[:good, :best]         => nil,
95
+
96
+					[:perfect, :critical]  => :critical,
97
+					[:perfect, :error]     => :error,
98
+					[:perfect, :warning]   => :warning,
99
+					[:perfect, :good]      => nil,
100
+					[:perfect, :perfect]   => nil,
101
+					[:perfect, :best]      => nil,
102
+
103
+					[:best, :critical]     => :critical,
104
+					[:best, :error]        => :error,
105
+					[:best, :warning]      => :warning,
106
+					[:best, :good]         => nil,
107
+					[:best, :perfect]      => nil,
108
+					[:best, :best]         => nil
109
+			}.each do |levels, result|
110
+				got = CryptCheck::Status.problem levels
111
+				expect(got).to be(result), "#{levels} : expected #{result.inspect}, got #{got.inspect}"
112
+			end
113
+		end
114
+
115
+		it 'must handle object list' do
116
+			critical = OpenStruct.new status: :critical
117
+			warning  = OpenStruct.new status: :warning
118
+			expect(CryptCheck::Status.problem [critical, warning]).to be :critical
119
+		end
120
+	end
121
+end

Loading…
취소
저장