Top Level Namespace

Defined Under Namespace

Classes: Document, DocumentList

Instance Method Summary collapse

Instance Method Details

#_load_document(path) ⇒ Object

Internal



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/document.rb', line 138

def _load_document(path)

  # Remote document
  if path.start_with?('http')
    return Net::HTTP.get(URI(path))

  # Local document
  else
    return File.read(path)
  end

end

#_parse_document(contents) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
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
# File 'lib/document.rb', line 152

def _parse_document(contents)
  elements = []
  codeblock = ''
  capture = false

  # Parse file lines
  for line in contents.strip().split("\n")

    # Heading
    if line.start_with?('#')
      heading = line.strip().tr('#', '')
      level = line.length - line.tr('#', '').length
      if (!elements.empty? &&
          elements[-1]['type'] == 'heading' &&
          elements[-1]['level'] == level)
        next
      end
      elements.push({
        'type' => 'heading',
        'value' => heading,
        'level' => level,
      })
    end

    # Codeblock
    if line.start_with?('```ruby')
      if line.include?('goodread')
        capture = true
      end
      codeblock = ''
      next
    end
    if line.start_with?('```')
      if capture
        elements.push({
          'type' => 'codeblock',
          'value' => codeblock,
        })
      end
      capture = false
    end
    if capture && !line.empty?
      codeblock += line + "\n"
      next
    end

  end

  return elements
end

#_validate_document(elements, exit_first: false) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/document.rb', line 204

def _validate_document(elements, exit_first:false)
  scope = binding()
  passed = 0
  failed = 0
  skipped = 0
  title = nil
  exception = nil

  # Test elements
  for element in elements

    # Heading
    if element['type'] == 'heading'
      print_message(element['value'], 'heading', level:element['level'])
      if title == nil
        title = element['value']
        print_message(nil, 'separator')
      end

    # Codeblock
    elsif element['type'] == 'codeblock'
      exception, exception_line = run_codeblock(element['value'], scope)
      lines = element['value'].strip().split("\n")
      for line, index in lines.each_with_index
        line_number = index + 1
        if line_number < exception_line
          print_message(line, 'success')
          passed += 1
        elsif line_number == exception_line
          print_message(line, 'failure', exception:exception)
          if exit_first
            print_message(scope, 'scope')
            raise exception
          end
          failed += 1
        elsif line_number > exception_line
          print_message(line, 'skipped')
          skipped += 1
        end
      end
    end

  end

  # Print summary
  if title != nil
    print_message(title, 'summary', passed:passed, failed:failed, skipped:skipped)
  end

  return {
    'valid' => exception == nil,
    'passed' => passed,
    'failed' => failed,
    'skipped' => skipped,
  }
end


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

def print_message(message, type, level: nil, exception: nil, passed: nil, failed: nil, skipped: nil)
  text = ''
  if type == 'blank'
    return puts('')
  elsif type == 'separator'
    text = Emoji.find_by_alias('heavy_minus_sign').raw * 3
  elsif type == 'heading'
    text = " #{'#' * (level || 1)}" + message.bold
  elsif type == 'success'
    text = " #{Emoji.find_by_alias('heavy_check_mark').raw}  ".green + message
  elsif type == 'failure'
    text = " #{Emoji.find_by_alias('x').raw}  ".red + message + "\n"
    text += "Exception: #{exception}".red.bold
  elsif type == 'scope'
    text += "---\n\n"
    text += "Scope (current execution scope):\n"
    text += "#{message}\n"
    text += "\n---\n"
  elsif type == 'skipped'
    text = " #{Emoji.find_by_alias('heavy_minus_sign').raw}  ".yellow + message
  elsif type == 'summary'
    color = :green
    text = (' ' + Emoji.find_by_alias('heavy_check_mark').raw + ' ').green.bold
    if (failed + skipped) > 0
      color = :red
      text = ("\n " + Emoji.find_by_alias('x').raw + ' ').red.bold
    end
    text += "#{message}: #{passed}/#{passed + failed + skipped}".colorize(color).bold
  end
  if ['success', 'failure', 'skipped'].include?(type)
    type = 'test'
  end
  if text
    if $state['last_message_type'] != type
      text = "\n" + text
    end
    puts(text)
    $state['last_message_type'] = type
  end
end

#read_configObject

Module API



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/helpers.rb', line 9

def read_config()
  config = {'documents' => ['README.md']}
  if File.file?('goodread.yml')
    config = YAML.load(File.read('goodread.yml'))
    for document, index in config['documents'].each_with_index
      if document.is_a?(Hash)
        if !document.include?('main')
          raise Exception.new('Document requires "main" property')
        end
      end
      if document.is_a?(String)
        config['documents'][index] = {'main' => document}
      end
    end
  end
  return config
end

#run_codeblock(codeblock, scope) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/helpers.rb', line 28

def run_codeblock(codeblock, scope)
  lines = []
  for line in codeblock.strip().split("\n")
    if line.include?(' # ')
      left, right = line.split(' # ')
      left = left.strip()
      right = right.strip()
      if left && right
        message = "#{left} != #{right}"
        line = "raise '#{message}' unless #{left} == #{right}"
      end
    end
    lines.push(line)
  end
  exception_line = 1000 # infiinity
  exception = nil
  begin
    eval(lines.join("\n"), scope)
  rescue Exception => exc
    exception = exc
    exception_line = 1
  end
  return [exception, exception_line]
end