Class: RsFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/rsformat.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(y) ⇒ RsFormat

Returns a new instance of RsFormat.



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
# File 'lib/rsformat.rb', line 8

def initialize y

  @summary = {}
  @failed = []
  @groups = []
  @count = 0
  @groupCount = 0

  @projectName = 'SB'
  @footerMessage = ''
  @outPath = ''
  @documentation = false

  if File.exist?('.sbfrc')
    begin
      x = JSON.parse(IO.read('.sbfrc'))
      if(x.has_key? 'project')
        @projectName = x['project']
      end

      if(x.has_key? 'footer')
        @footerMessage = x['footer']
      end

      if(x.has_key? 'outPath')
        @outPath = x['outPath']
      end

      if(x.has_key? 'document')
        @documentation = x['document'].eql?('true') ? true : false
      end
    rescue
      puts 'rescue block'
    end
  else
    puts "No user specification (.sbfrc) found. Using default configuration."
  end
end

Instance Attribute Details

#summaryObject

Returns the value of attribute summary.



5
6
7
# File 'lib/rsformat.rb', line 5

def summary
  @summary
end

Instance Method Details

#addOrAppendGroup(test, notification) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/rsformat.rb', line 164

def addOrAppendGroup test, notification
  grps = @groups.select{|y| y[:grpName] == notification.example.example_group.to_s.split('::')[2]}
  if(!grps.any?) # No entry for this group Yet.
    grp = {
      grpId: @groupCount + 1,
      grpName: notification.example.example_group.to_s.split('::')[2],
      status: notification.example.execution_result.status.to_s == "passed" ? "P" :
        (notification.example.execution_result.status.to_s == "failed" ? "F" :
        (notification.example.execution_result.status.to_s == "pending" ? "W" : "X")),
      location: notification.example.location.split(":")[0],
      totalTimeForSuit: notification.example.execution_result.run_time.round(3),
      testCount: 1,
      passedCount: notification.example.execution_result.status.to_s == "passed" ? 1 : 0,
      failedCount: notification.example.execution_result.status.to_s == "failed" ? 1 : 0,
      pendingCount: notification.example.execution_result.status.to_s == "pending" ? 1 : 0,
      grpTests: [test],
    }

    @groups.push(grp)
    @groupCount = @groupCount + 1
  else
    # grp status can change
    if(grps[0][:status] == "P")
      grps[0][:status] = notification.example.execution_result.status.to_s == "failed" ? "F" :
        (notification.example.execution_result.status.to_s == "pending" ? "W" : "P")
    elsif (grps[0][:status] == "F")
      grps[0][:status] = notification.example.execution_result.status.to_s == "pending" ? "FW" : "F"
    elsif (grps[0][:status] == "W")
      grps[0][:status] = notification.example.execution_result.status.to_s == "failed" ? "FW" : "W"
    end

    # grp total time for suit
    grps[0][:totalTimeForSuit] = (grps[0][:totalTimeForSuit] + notification.example.execution_result.run_time).round(3)

    # grp test count
    grps[0][:testCount] = grps[0][:testCount] + 1

    # grp passed / failed / pending count
    if(notification.example.execution_result.status.to_s == "passed")
      grps[0][:passedCount] = grps[0][:passedCount] + 1
    elsif(notification.example.execution_result.status.to_s == "failed")
      grps[0][:failedCount] = grps[0][:failedCount] + 1
    elsif(notification.example.execution_result.status.to_s == "pending")
      grps[0][:pendingCount] = grps[0][:pendingCount] + 1
    end

    # grp Tests should be added.
    grps[0][:grpTests].push(test)
  end
end

#close(notification) ⇒ Object

NullNotification



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/rsformat.rb', line 132

def close notification # NullNotification
  puts "\n"
  puts "---------------------------------------------------------"
  puts "Total Suits      : #{@summary[:groupCount]}"
  puts "Total Tests      : #{@summary[:testCount]}"
  puts "Total Passed     : #{@summary[:passCount]}"
  puts "Total Failed     : #{@summary[:failureCount]}"
  puts "Total Pending    : #{@summary[:pendingCount]}"
  puts "Total Time Taken : #{@summary[:duration]}"
  puts "---------------------------------------------------------"

  if(@documentation)
    if(@failed.length == 0)
      puts "******************* NO Exceptions found **************************"
    else
      puts "=====================Exceptions=========================="
      cnt = 0

      @failed.each do |failedTest|
        exMessage = failedTest[:exception][:message].gsub("\n","")
        exType = failedTest[:exception][:type]
        puts "\n#{cnt + 1}. #{failedTest[:fullName]}"
        puts "Exception : #{exType}"
        puts "Message   : #{exMessage}"
        cnt = cnt + 1
        puts "\n"
      end
      puts "========================================================="
    end
  end
end

#dump_summary(notification) ⇒ Object

SummaryNotification



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/rsformat.rb', line 106

def dump_summary notification # SummaryNotification
  # puts "\nTesting ends ...  Generating Report"
  # puts "Total Tests: #{@count}"
  # puts @groups.length

  @summary = {
    duration: RSpec::Core::Formatters::Helpers.format_duration(notification.duration.round(3)),
    groupCount: @groups.length,
    testCount: notification.example_count,
    pendingCount: notification.pending_count,
    failureCount: notification.failure_count,
    passCount: notification.example_count - ( notification.pending_count + notification.failure_count),
    projectName: @projectName
  }

  userData = {
    projectName: @projectName,
    footerMessage: @footerMessage,
    outPath: @outPath,
    documentation: @documentation
  }

  html = Htmler.new(JSON.generate(@summary), JSON.generate(@groups), JSON.generate(userData))
  html.inits
end

#example_failed(notification) ⇒ Object

FailedExampleNotification



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rsformat.rb', line 64

def example_failed notification # FailedExampleNotification
  test = {
    group: notification.example.example_group,
    name: notification.example.description,
    fullName: notification.example.full_description,
    status: notification.example.execution_result.status.to_s,
    location: notification.example.location.split(":")[0],
    lineNo: notification.example.location.split(":")[1],
    runtime: notification.example.execution_result.run_time,
    id: @count + 1,
    exception: {
      type: notification.example.exception.class,
      message: notification.example.execution_result.exception.message,
      backtrace: notification.example.exception.backtrace
    },
    pendingMessage: notification.example.execution_result.pending_message
  }

  addOrAppendGroup test, notification
  @count = @count + 1
  @failed.push(test)
  print "F"
end

#example_passed(notification) ⇒ Object

ExampleNotification



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rsformat.rb', line 47

def example_passed notification # ExampleNotification
  test = {
    name: notification.example.description,
    fullName: notification.example.full_description,
    status: notification.example.execution_result.status.to_s,
    location: notification.example.location.split(":")[0],
    lineNo: notification.example.location.split(":")[1],
    runtime: notification.example.execution_result.run_time,
    pendingMessage: notification.example.execution_result.pending_message,
    id: @count + 1
  }

  addOrAppendGroup test, notification
  @count = @count + 1
  print "."
end

#example_pending(notification) ⇒ Object

ExampleNotification



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rsformat.rb', line 88

def example_pending notification # ExampleNotification
  test = {
    group: notification.example.example_group,
    name: notification.example.description,
    fullName: notification.example.full_description,
    status: notification.example.execution_result.status.to_s,
    location: notification.example.location.split(":")[0],
    lineNo: notification.example.location.split(":")[1],
    runtime: notification.example.execution_result.run_time,
    pendingMessage: notification.example.execution_result.pending_message,
    id: @count + 1
  }

  addOrAppendGroup test, notification
  @count = @count + 1
  print "*"
end