require "vcr"
VCR.configure do |config|
config.cassette_library_dir = "spec/vcr_cassettes"
config.hook_into :webmock
config.default_cassette_options = {
clean_outdated_http_interactions: true,
re_record_interval: 1512000,
record: :once,
}
end
require "simplecov"
SimpleCov.start do
add_filter "/spec/"
end
require "bundler/setup"
require "asciidoctor"
require "metanorma-iso"
require "rspec/matchers"
require "equivalent-xml"
require "metanorma"
require "metanorma/iso"
require "iev"
require "rexml/document"
RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = ".rspec_status"
# Disable RSpec exposing methods globally on `Module` and `main`
config.disable_monkey_patching!
config.expect_with :rspec do |c|
c.syntax = :expect
end
# config.around do |example|
# Dir.mktmpdir("rspec-") do |dir|
# tmp_assets = File.join(dir, "spec/assets/")
# FileUtils.mkdir_p tmp_assets
# FileUtils.cp_r Dir.glob("spec/assets/*"), tmp_assets
# Dir.chdir(dir) { example.run }
# end
# end
end
def strip_guid(xml)
xml.gsub(%r{ id="_[^"]+"}, ' id="_"').gsub(%r{ target="_[^"]+"}, ' target="_"')
end
def metadata(hash)
hash.sort.to_h.delete_if do |_, v|
v.nil? || (v.respond_to?(:empty?) && v.empty?)
end
end
def xmlpp(xml)
s = ""
f = REXML::Formatters::Pretty.new(2)
f.compact = true
f.write(REXML::Document.new(xml
.gsub(%r{20[0-9-]+}, "")), s)
s
end
ASCIIDOC_BLANK_HDR = <<~"HDR".freeze
= Document title
Author
:docfile: test.adoc
:nodoc:
:novalid:
:no-isobib:
HDR
AMD_BLANK_HDR = <<~"HDR".freeze
= Document title
Author
:docfile: test.adoc
:nodoc:
:novalid:
:no-isobib:
:doctype: amendment
HDR
ISOBIB_BLANK_HDR = <<~"HDR".freeze
= Document title
Author
:docfile: test.adoc
:nodoc:
:novalid:
:no-isobib-cache:
HDR
FLUSH_CACHE_ISOBIB_BLANK_HDR = <<~"HDR".freeze
= Document title
Author
:docfile: test.adoc
:nodoc:
:novalid:
:flush-caches:
HDR
CACHED_ISOBIB_BLANK_HDR = <<~"HDR".freeze
= Document title
Author
:docfile: test.adoc
:nodoc:
:novalid:
HDR
LOCAL_CACHED_ISOBIB_BLANK_HDR = <<~"HDR".freeze
= Document title
Author
:docfile: test.adoc
:nodoc:
:novalid:
:local-cache:
HDR
VALIDATING_BLANK_HDR = <<~"HDR".freeze
= Document title
Author
:docfile: test.adoc
:nodoc:
:no-isobib:
HDR
ASCIIDOCTOR_ISO_DIR = Pathname
.new(File.dirname(__FILE__)) / "../lib/metanorma/iso"
BOILERPLATE =
HTMLEntities.new.decode(
File.read(ASCIIDOCTOR_ISO_DIR / "boilerplate.xml", encoding: "utf-8")
.gsub(/\{\{ agency \}\}/, "ISO")
.gsub(/\{\{ docyear \}\}/, Date.today.year.to_s)
.gsub(/\{% if unpublished %\}.*\{% endif %\}/m, "")
.gsub(/(?<=\p{Alnum})'(?=\p{Alpha})/, "’"),
)
BOILERPLATE_FR =
HTMLEntities.new.decode(
File.read(ASCIIDOCTOR_ISO_DIR / "boilerplate-fr.xml", encoding: "utf-8")
.gsub(/\{\{ agency \}\}/, "ISO")
.gsub(/\{\{ docyear \}\}/, Date.today.year.to_s)
.gsub(/\{% if unpublished %\}.*\{% endif %\}/m, "")
.gsub(/(?<=\p{Alnum})'(?=\p{Alpha})/, "’"),
)
BLANK_HDR1 = <<~"HDR".freeze
International Organization for Standardization
ISO
International Organization for Standardization
ISO
en
60
60
#{Time.new.year}
International Organization for Standardization
ISO
article
International standard
HDR
BLANK_HDR = <<~"HDR".freeze
#{BLANK_HDR1}
#{BOILERPLATE}
HDR
BLANK_HDR_FR = <<~"HDR".freeze
#{BLANK_HDR1.sub(%r{en}, 'fr')}
#{BOILERPLATE_FR}
HDR
TERM_BOILERPLATE = <<~TERM.freeze
For the purposes of this document,
the following terms and definitions apply.
ISO and IEC maintain terminological databases for use in
standardization at the following addresses:
TERM
HTML_HDR = <<~HDR.freeze
HDR
WORD_HDR = <<~HDR.freeze
test
HDR
OPTIONS = [backend: :iso, header_footer: true].freeze
def mock_pdf
allow(::Mn2pdf).to receive(:convert) do |url, output,|
FileUtils.cp(url.gsub(/"/, ""), output.gsub(/"/, ""))
end
end
def mock_sts
allow(::Mn2sts).to receive(:convert) do |url, output,|
FileUtils.cp(url.gsub(/"/, ""), output.gsub(/"/, ""))
end
end
private
def get_xml(search, code, opts)
c = code.gsub(%r{[/\s:-]}, "_").sub(%r{_+$}, "").downcase
o = opts.keys.join "_"
file = "spec/examples/#{[c, o].join '_'}.xml"
if File.exist? file
File.read file
else
result = search.call(code)
hit = result&.first&.first
xml = hit.to_xml nil, opts
File.write file, xml
xml
end
end
def mock_open_uri(code)
expect(Iev).to receive(:get).with(code, "en") do |m, *args|
file = "spec/examples/#{code.tr('-', '_')}.html"
File.write file, m.call(*args).read unless File.exist? file
File.read file
end.at_least :once
end