Module: Webgen::TestHelper

Defined in:
lib/webgen/test_helper.rb

Defined Under Namespace

Classes: RenderNode

Instance Method Summary collapse

Instance Method Details

#assert_error_on_line(error_class, line) ⇒ Object

Fails if the given block does not raise error_class and the error is not on the line.



44
45
46
47
48
49
50
51
52
# File 'lib/webgen/test_helper.rb', line 44

def assert_error_on_line(error_class, line)
  begin
    yield
  rescue error_class => e
    assert_equal(line, (e.respond_to?(:line) ? e.line : Webgen::Error.error_line(e)))
  else
    fail "No exception raised though #{error_class} expected"
  end
end

#assert_log_match(reg, io = @logio) ⇒ Object

Fails if the log string does not match the regular expression.

Uses the StringIO @logio if no other StringIO is given and resets the log string after the check.



65
66
67
68
# File 'lib/webgen/test_helper.rb', line 65

def assert_log_match(reg, io = @logio)
  assert_match(reg, io.string)
  @logio.string = ''
end

#assert_nothing_logged(io = @logio) ⇒ Object

Fails if the log string is not empty.

Uses the StringIO @logio if no other StringIO is given.



57
58
59
# File 'lib/webgen/test_helper.rb', line 57

def assert_nothing_logged(io = @logio)
  assert_equal('', io.string)
end

#setup_contextObject

Creates a Webgen::Context object that is returned and accessible via @context.

The needed website object is created using #setup_website and the :chain is set to a mock node that responds to :alcn with ‘/test’.



163
164
165
166
167
168
# File 'lib/webgen/test_helper.rb', line 163

def setup_context
  setup_website
  node = Object.new
  node.define_singleton_method(:alcn) { '/test' }
  @context = Webgen::Context.new(@website, :chain => [node], :doit => 'hallo')
end

#setup_default_nodes(tree, klass = Webgen::Node) ⇒ Object

Adds the following nodes (showing alcn=dest_path, title, other meta info) to the tree which has to be empty:

/
/file.en.html            'file en' sort_info=3
/file.en.html#frag       'frag'
/file.en.html#nested     'fragnested' routing_path="/routed.html"
/file.de.html            'file de' sort_info=5
/file.de.html#frag       'frag'
/other.html              'other'
/other.en.html           'other en'
/german.de.html          'german' dest_path='/german.other.html'
/dir/                    'dir'
/dir/subfile.html        'subfile'
/dir/subfile.html#frag   'frag'
/dir/dir/                'dir'
/dir/dir/file.html       'file'
/dir2/                   'dir2' proxy_path='index.html'
/dir2/index.en.html      'index en' routed_title='routed en' link_attrs={'class' => 'help'}
/dir2/index.de.html      'index de' routed_title='routed de'


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
# File 'lib/webgen/test_helper.rb', line 132

def setup_default_nodes(tree, klass = Webgen::Node)
  root = klass.new(tree.dummy_root, '/', '/')

  file_en = klass.new(root, 'file.html', '/file.en.html', {'lang' => 'en', 'title' => 'file en', 'sort_info' => 3})
  frag_en = klass.new(file_en, '#frag', '/file.en.html#frag', {'title' => 'frag'})
  klass.new(frag_en, '#nested', '/file.en.html#nested', {'title' => 'fragnested', 'routing_path' => '/routed.html'})
  file_de = klass.new(root, 'file.html', '/file.de.html', {'lang' => 'de', 'title' => 'file de', 'sort_info' => 5})
  klass.new(file_de, '#frag', '/file.de.html#frag', {'title' => 'frag'})

  klass.new(root, 'other.html', '/other.html', {'title' => 'other'})
  klass.new(root, 'other.html', '/other.en.html', {'lang' => 'en', 'title' => 'other en'})

  klass.new(root, 'german.html', '/german.other.html', {'title' => 'german', 'lang' => 'de'})

  dir = klass.new(root, 'dir/', '/dir/', {'title' => 'dir'})
  dir_file = klass.new(dir, 'subfile.html', '/dir/subfile.html', {'title' => 'subfile'})
  klass.new(dir_file, '#frag', '/dir/subfile.html#frag', {'title' => 'frag'})
  dir_dir = klass.new(dir, 'dir/' , '/dir/dir/', {'title' => 'dir'})
  klass.new(dir_dir, 'file.html', '/dir/dir/file.html', {'title' => 'file'})

  dir2 = klass.new(root, 'dir2/', '/dir2/', {'proxy_path' => 'index.html', 'title' => 'dir2'})
  klass.new(dir2, 'index.html', '/dir2/index.en.html',
                   {'lang' => 'en', 'routed_title' => 'routed', 'title' => 'index en', 'link_attrs' => {'class'=>'help'}})
  klass.new(dir2, 'index.html', '/dir2/index.de.html',
                   {'lang' => 'de', 'routed_title' => 'routed de', 'title' => 'index de'})
end

#setup_tag_template(root) ⇒ Object

Creates and returns a RenderNode /tag.template using the default tag template.



171
172
173
174
# File 'lib/webgen/test_helper.rb', line 171

def setup_tag_template(root)
  template_data = File.read(File.join(Webgen::Utils.data_dir, 'passive_sources', 'templates', 'tag.template'))
  RenderNode.new(template_data, root, 'tag.template', '/tag.template')
end

#setup_website(config = {}) ⇒ Object

Creates a basic stub website that is accessible via @website with the following methods:

@website.config

The given config object or {} if none specified

@website.directory

Set to a non-existent temporary directory

@website.tmpdir

Set to @website.directory/tmp

@website.ext

OpenStruct instance. The accessor item_tracker is set to an object that responds to add.

@website.blackboard

A Webgen::Blackboard instance

@website.logger

Webgen::Logger instance with a StringIO as target. The StringIO target is also available via @logio.

@website.tree

Webgen::Tree instance



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/webgen/test_helper.rb', line 93

def setup_website(config = {})
  @website = OpenStruct.new
  @website.config = config
  @website.directory = Dir::Tmpname.create("test-webgen-website") {|path| raise Errno::EEXIST if File.directory?(path)}
  @website.define_singleton_method(:tmpdir) do |path='', create=false|
    tmp = File.join(self.directory, 'tmp')
    FileUtils.mkdir_p(tmp) if create
    File.join(tmp, path)
  end
  @website.ext = OpenStruct.new
  @website.blackboard = Webgen::Blackboard.new
  @website.cache = Webgen::Cache.new
  @logio = StringIO.new
  @website.logger = Webgen::Logger.new(@logio)
  @website.tree = Webgen::Tree.new(@website)
  @website.ext.item_tracker = Object.new
  @website.ext.item_tracker.define_singleton_method(:add) {|*args|}
end