Skip to content

Commit

Permalink
More precise tests for #line (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevecheckoway authored Nov 11, 2018
1 parent 69f53a0 commit c8ae064
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
14 changes: 12 additions & 2 deletions nokogumbo-import/ext/nokogumbo/nokogumbo.c
Original file line number Diff line number Diff line change
Expand Up @@ -709,10 +709,12 @@ static VALUE fragment_continue(ParseArgs *args) {

// Initialize the Nokogumbo class and fetch constants we will use later.
void Init_nokogumbo() {
rb_funcall(rb_mKernel, rb_intern("gem"), 1, rb_utf8_str_new_static("nokogiri", 8));
rb_funcall(rb_mKernel, rb_intern_const("gem"), 1, rb_utf8_str_new_static("nokogiri", 8));
rb_require("nokogiri");

#ifndef NGLIB
VALUE line_supported = Qtrue;

#if !NGLIB
// Class constants.
VALUE mNokogiri = rb_const_get(rb_cObject, rb_intern_const("Nokogiri"));
VALUE mNokogiriXml = rb_const_get(mNokogiri, rb_intern_const("XML"));
Expand All @@ -725,6 +727,9 @@ void Init_nokogumbo() {
// Interned symbols.
new = rb_intern_const("new");
node_name_ = rb_intern_const("node_name=");

// #line is not supported (returns 0)
line_supported = Qfalse;
#endif

// Class constants.
Expand All @@ -739,6 +744,11 @@ void Init_nokogumbo() {
VALUE Gumbo = rb_define_module("Nokogumbo");
rb_define_singleton_method(Gumbo, "parse", parse, 4);
rb_define_singleton_method(Gumbo, "fragment", fragment, 5);

// Add private constant for testing.
rb_define_const(Gumbo, "LINE_SUPPORTED", line_supported);
rb_funcall(Gumbo, rb_intern_const("private_constant"), 1,
rb_utf8_str_new_cstr("LINE_SUPPORTED"));
}

// vim: set shiftwidth=2 softtabstop=2 tabstop=8 expandtab:
15 changes: 10 additions & 5 deletions nokogumbo-import/test/test_nokogumbo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,31 +223,36 @@ def test_document_encoding
end

def test_line_text
skip unless Nokogumbo.const_get(:LINE_SUPPORTED)
doc = Nokogiri.HTML5("<!DOCTYPE html>\ntext node")
assert_includes [0, 2], doc.at_xpath('/html/body/text()').line
assert_equal 2, doc.at_xpath('/html/body/text()').line
end

def test_line_comment
skip unless Nokogumbo.const_get(:LINE_SUPPORTED)
doc = Nokogiri.HTML5("<!DOCTYPE html>\n\n<!-- comment -->")
assert_includes [0, 3], doc.at_xpath('/comment()').line
assert_equal 3, doc.at_xpath('/comment()').line
end

def test_line_element
skip unless Nokogumbo.const_get(:LINE_SUPPORTED)
doc = Nokogiri.HTML5("<!DOCTYPE html>\n<p>")
assert_includes [0, 2], doc.at_xpath('/html/body/p').line
assert_equal 2, doc.at_xpath('/html/body/p').line
end

def test_line_template
skip unless Nokogumbo.const_get(:LINE_SUPPORTED)
doc = Nokogiri.HTML5("<!DOCTYPE html>\n\n<template></template>")
assert_includes [0, 3], doc.at_xpath('/html/head/template').line
assert_equal 3, doc.at_xpath('/html/head/template').line
end

def test_line_cdata
skip unless Nokogumbo.const_get(:LINE_SUPPORTED)
html = "<!DOCTYPE html>\n<svg>\n<script><![CDATA[ ]]></script></svg>"
doc = Nokogiri.HTML5(html)
node = doc.at_xpath('/html/body/svg:svg/svg:script/text()')
assert node.cdata?
assert_includes [0, 3], node.line
assert_equal 3, node.line
end

private
Expand Down

0 comments on commit c8ae064

Please sign in to comment.