test/lib/docparser/document_test.rb in docparser-0.2.3 vs test/lib/docparser/document_test.rb in docparser-0.3.0
- old
+ new
@@ -1,9 +1,11 @@
+# frozen_string_literal: true
+
require_relative '../../test_helper'
+
describe DocParser::Document do
before do
- Log4r::Logger['docparser'].level = Log4r::INFO
$output = DocParser::NilOutput.new
@parser = Class.new do
define_method(:outputs) { [$output] }
end.new
@test_doc_path = File.join($SUPPORT_DIR, 'test_html.html')
@@ -52,19 +54,19 @@
end
it 'should be possible to use css queries' do
css = 'article > h1 + p'
css_content = @test_doc.css_content(css)
- css_element = @test_doc.css(css)
+ css_element = @test_doc.elements(css)
css_content.must_equal('Great article it is')
css_content.must_equal(css_element.first.content)
end
it 'should be possible to use xpath queries' do
xpath = '//li/ancestor::article/h1'
- xpath_content = @test_doc.xpath_content(xpath)
- xpath_element = @test_doc.xpath(xpath)
+ xpath_content = @test_doc.element_content(xpath)
+ xpath_element = @test_doc.elements(xpath)
xpath_content.must_equal('This is an article')
xpath_content.must_equal(xpath_element.first.content)
end
it 'should be possible to use regular expressions' do
@@ -81,32 +83,36 @@
array2 = []
@test_doc.xpath('//p') do |element|
array2 << element.content
end
array2.must_equal(array)
+ array2 = []
+ @test_doc.each_element('//p') do |element|
+ array2 << element.content
+ end
+ array2.must_equal(array)
end
it 'should warn when providing an empty file' do
file = Tempfile.new('empty')
file.write('')
file.close
open(file.path).read.empty?.must_equal true
- _, err = capture_io do
- # Switch to hijacked IO
- Log4r::Outputter['docparser'].instance_variable_set(:@out, $stderr)
- DocParser::Document.new(filename: file.path, parser: @parser)
- end
- # Restore IO
- Log4r::Outputter['docparser'].instance_variable_set(:@out, $stderr)
- err.must_include "#{file.path} is empty"
+ err = StringIO.new
+
+ DocParser::Document.new(filename: file.path,
+ parser: @parser,
+ logger: Logger.new(err))
+
+ err.string.must_include "#{file.path} is empty"
end
it 'should add the row to the results' do
@test_doc.add_row ['test']
@test_doc.add_row 'test', 'test2'
- @test_doc.results.must_equal [[%w(test), %w(test test2)]]
+ @test_doc.results.must_equal [[%w[test], %w[test test2]]]
end
it 'should be possible to not use outputs' do
parser = Class.new do
define_method(:outputs) { [] }
@@ -131,7 +137,6 @@
parser: parser)
test_doc.add_row ['a'], output: 1
test_doc.add_row ['b'], output: 0
test_doc.results.must_equal [[['b']], [['a']]]
end
-
end