Class: TestDoc::TestDoc

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

Constant Summary collapse

GENERATORS =

Not in use

{}
DOT_DOC_FILENAME =

Name of the dotfile that contains the description of files to be processed in the current directory

".document"

Instance Method Summary collapse

Instance Method Details

#document(argv) ⇒ Object

Main method



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
# File 'lib/testdoc_module.rb', line 22

def document(argv)

  options = Options.instance
  options.parse(argv, GENERATORS)
  @generated_content = ""

  puts "Title: " + options.title
  @title =  options.title
  header = File.dirname(__FILE__) + "/testdoc/header.html.erb"
  @generated_content = process_template(header, nil) unless options.text
  @progress = $stderr unless options.quiet
  @num_files = 0
  @options = options
  files = options.files
  files = ["."] if files.empty?

  file_list = normalized_file_list(options, files, true)

  template_file = File.dirname(__FILE__) + "/testdoc/template.html.erb"
  if(options.text) then
    template_file = File.dirname(__FILE__) + "/testdoc/template.debug.erb"
  end

  file_list.each do |fn|
    $stderr.printf("\n%35s: ", File.basename(fn)) unless options.quiet
    content = File.open(fn, "r") {|f| f.read}
    testdoc_directives = []
    testdoc_directives = parse_file(content)
    parser = TestDocParser.new(testdoc_directives,File.basename(fn))
    parser.options = options
    testplans = []
    testplans = parser.process_testplan
    if(testplans) then
      @generated_content += process_template(template_file,testplans)
      @num_files += 1
    end

    # basename = File.basename(fn).split("\.")[0]
    # puts "DEBUG" + basename + ".html"
  end

  if(not options.quiet) then
    puts ''
    puts ''
    puts 'Generating TestDoc HTML: testplan.html...'
    # puts "Files:   #@num_files"
    puts "Parsed files: #@num_files"
  end

  if( not options.text) then
    footer = File.dirname(__FILE__) + "/testdoc/footer.html.erb"
    @generated_content += process_template(footer, nil)
    output_file = File.new("testplan.html", "w")
    output_file.puts @generated_content
  else
    puts @generated_content
  end

end

#list_files_in_directory(dir, options) ⇒ Object

Return a list of the files to be processed in a directory. We know that this directory doesn’t have a .document file, so we’re looking for real files. However we may well contain subdirectories which must be tested for .document files



171
172
173
# File 'lib/testdoc_module.rb', line 171

def list_files_in_directory(dir, options)
  normalized_file_list(options, Dir.glob(File.join(dir, "*")), false, options.exclude)
end

#normalized_file_list(options, relative_files, force_doc = false, exclude_pattern = nil) ⇒ Object

Given a list of files and directories, create a list of all the Ruby files they contain.



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/testdoc_module.rb', line 140

def normalized_file_list(options, relative_files, force_doc = false, exclude_pattern=nil)
  file_list = []

  relative_files.each do |rel_file_name|
    next if exclude_pattern && exclude_pattern =~ rel_file_name
    stat = File.stat(rel_file_name)
    case type = stat.ftype
    when "file"
      next if @last_created and stat.mtime < @last_created
      file_list << rel_file_name.sub(/^\.\//, '') ##  if force_doc || ParserFactory.can_parse(rel_file_name)
    when "directory"
      next if rel_file_name == "CVS" || rel_file_name == ".svn"
      # puts "DEBUG: rel_file_name: " + rel_file_name
      dot_doc = File.join(rel_file_name, DOT_DOC_FILENAME)
      if File.file?(dot_doc)
        file_list.concat(parse_dot_doc_file(rel_file_name, dot_doc, options))
      else
        file_list.concat(list_files_in_directory(rel_file_name, options))
      end
    else
      raise TestDocError.new("I can't deal with a #{type} #{rel_file_name}")
    end
  end
  file_list
end

#parse_file(content) ⇒ Object

Exctract comments from file and return it in an array



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
131
132
133
134
135
# File 'lib/testdoc_module.rb', line 89

def parse_file(content)

  if /\t/ =~ content
    tab_width = Options.instance.tab_width
    content = content.split(/\n/).map do |line|
      1 while line.gsub!(/\t+/) { ' ' * (tab_width*$&.length - $`.length % tab_width)}  && $~ #`
      line
    end .join("\n")
  end
  @content   = content
  @content << "\n" unless @content[-1,1] == "\n"

  testdoc_array = []
  counter = 0

  symbol = nil
  line_number = 0
  content.split(/\n/).map do |line|
    line_number = line_number + 1

    if line =~ /.*#.*test:(.*)/i then
      symbol = :test
      testdoc_array[counter] = [:test,$1.gsub(/^\s+/, ""),line_number]
      counter = counter + 1
    elsif line =~ /.*#.*task:(.*)/i then
      testdoc_array[counter] = [:task,$1.gsub(/^\s+/, ""),line_number]
      counter = counter + 1
      symbol = :task
    elsif line =~ /.*#.*check:(.*)/i then
      testdoc_array[counter] = [:check,$1.gsub(/^\s+/, ""),line_number]
      counter = counter + 1
      symbol = :check
    elsif line =~ /.*#.*testplan:(.*)/i then
      testdoc_array[counter] = [:testplan,$1.gsub(/^\s+/, "") + "#",line_number]
      counter = counter + 1
      symbol = :testplan
    elsif line =~ /.*#(.*)/ && symbol != nil then
      testdoc_array[counter - 1][1] = testdoc_array[counter - 1][1] + " " + $1.gsub(/^ */, ' ')

    else
      symbol = nil
    end

  end

  return testdoc_array
end

#process_template(template_file, testplans) ⇒ Object



82
83
84
85
86
# File 'lib/testdoc_module.rb', line 82

def process_template(template_file,testplans)
  template = File.open(template_file, "r") {|f| f.read}
  rhtml = ERB.new(template, safe_level = 0, trim_mode = 1 )
  generated_html = rhtml.result(b=binding() ).gsub(/\n\n/,"\n")
end