require "spec_helper"
require "fileutils"
RSpec.describe Metanorma::Utils do
it "has a version number" do
expect(Metanorma::Utils::VERSION).not_to be nil
end
it "capitalises" do
expect(Metanorma::Utils.strict_capitalize_phrase("ABC def gHI"))
.to eq "ABC Def GHI"
expect(Metanorma::Utils.strict_capitalize_first("aBC def gHI"))
.to eq "ABC def gHI"
end
it "converts OS-specific external path" do
pwd = if !!((RUBY_PLATFORM =~ /(win|w)(32|64)$/) ||
(RUBY_PLATFORM =~ /mswin|mingw/))
`echo %cd%`
else `pwd`
end
expect(Metanorma::Utils.external_path(FileUtils.pwd.strip)).to eq pwd.strip
end
it "applies Asciidoctor substitutions" do
require "metanorma-standoc"
expect(Metanorma::Utils.asciidoc_sub("A -- B"))
.to eq "A — B"
expect(Metanorma::Utils.asciidoc_sub("*A* stem:[x]"))
.to eq "A x"
end
it "finds file path of docfile" do
d = Dummy.new
expect(Metanorma::Utils.localdir(d)).to eq "./"
d.docfile = "spec/utils_spec.rb"
expect(Metanorma::Utils.localdir(d)).to eq "spec/"
end
it "applies smart formatting" do
expect(Metanorma::Utils.smartformat("A - B A -- B A--B '80s '80' "))
.to eq "A — B A — B A—B ’80s ‘80’ <A>"
end
it "applies en-dash normalisation" do
a = Nokogiri::XML(<<~INPUT)
A -- B A - B
A--B
INPUT
Metanorma::Utils.endash_date(a)
expect(a.to_xml).to be_equivalent_to <<~OUTPUT
A–B A–BA–B
OUTPUT
end
it "sets hash values by dotted key path" do
a = {}
a = Metanorma::Utils.set_nested_value(a, ["X"], "x")
a = Metanorma::Utils.set_nested_value(a, ["X1"], 9)
a = Metanorma::Utils.set_nested_value(a, ["X2"], [3])
a = Metanorma::Utils.set_nested_value(a, ["X3"], { "a" => "b" })
expect(a.to_s).to be_equivalent_to <<~OUTPUT
{"X"=>"x", "X1"=>9, "X2"=>[3], "X3"=>{"a"=>"b"}}
OUTPUT
a = Metanorma::Utils.set_nested_value(a, ["X2"], 4)
a = Metanorma::Utils.set_nested_value(a, ["X1"], 4)
expect(a.to_s).to be_equivalent_to <<~OUTPUT
{"X"=>"x", "X1"=>[9, 4], "X2"=>[3, 4], "X3"=>{"a"=>"b"}}
OUTPUT
a = Metanorma::Utils.set_nested_value(a, ["X2", "A"], 5)
a = Metanorma::Utils.set_nested_value(a, ["X2a"], [])
a = Metanorma::Utils.set_nested_value(a, ["X2a", "A"], 6)
a = Metanorma::Utils.set_nested_value(a, ["X4", "A"], 10)
a = Metanorma::Utils.set_nested_value(a, ["X3", "A"], 7)
a = Metanorma::Utils.set_nested_value(a, ["X3", "a"], 8)
a = Metanorma::Utils.set_nested_value(a, ["X1", "a"], 11)
expect(a.to_s).to be_equivalent_to <<~OUTPUT
{"X"=>"x", "X1"=>[9, 4, {"a"=>11}], "X2"=>[3, 4, {"A"=>5}], "X3"=>{"a"=>["b", 8], "A"=>7}, "X2a"=>[{"A"=>6}], "X4"=>{"A"=>10}}
OUTPUT
end
it "maps languages to scripts" do
expect(Metanorma::Utils.default_script("hi")).to eq "Deva"
expect(Metanorma::Utils.rtl_script?(Metanorma::Utils.default_script("el")))
.to eq false
expect(Metanorma::Utils.rtl_script?(Metanorma::Utils.default_script("fa")))
.to eq true
end
# not testing Asciidoctor log extraction here
it "generates log" do
xml = Nokogiri::XML(<<~INPUT)
c
INPUT
FileUtils.rm_f("log.txt")
log = Metanorma::Utils::Log.new
log.add("Category 1", nil, "Message 1")
log.add("Category 1", "node", "Message 2")
log.add("Category 2", xml.at("//xml/a/b"), "Message 3")
log.write("log.txt")
expect(File.exist?("log.txt")).to be true
file = File.read("log.txt", encoding: "utf-8")
expect(file).to eq <<~OUTPUT
log.txt errors
== Category 1
(): Message 1
(node): Message 2
== Category 2
(XML Line 000003): Message 3
c
OUTPUT
end
it "deals with illegal characters in log" do
FileUtils.rm_f("log.txt")
log = Metanorma::Utils::Log.new
log.add("Category 1", nil, "é\xc2")
log.write("log.txt")
expect(File.exist?("log.txt")).to be true
file = File.read("log.txt", encoding: "utf-8")
expect(file).to eq <<~OUTPUT
log.txt errors
== Category 1
(): é�
OUTPUT
end
it "parses CSV" do
expect(Metanorma::Utils.csv_split("A;'B;C'")).to eq ["A", "'B", "C'"]
expect(Metanorma::Utils.csv_split('A;"B;C"')).to eq ["A", "B;C"]
expect(Metanorma::Utils.csv_split('A; "B;C"')).to eq ["A", "B;C"]
expect(Metanorma::Utils.csv_split('A; "B;C"', ",")).to eq ['A; "B;C"']
expect(Metanorma::Utils.csv_split('A, "B,C"', ",")).to eq ["A", "B,C"]
end
it "parses attributes from definition list" do
xml = Nokogiri::XML(<<~INPUT)
- attr1
- value1
- attr2
value1
value cont
INPUT
Metanorma::Utils.dl_to_attrs(xml.at("//dummy"), xml.at("//dl"), "attr1")
Metanorma::Utils.dl_to_attrs(xml.at("//dummy"), xml.at("//dl"), "attr2")
expect(xml.to_xml).to be_equivalent_to <<~OUTPUT
- attr1
- value1
- attr2
value1
value cont
OUTPUT
end
it "parses attributes from definition list" do
xml = Nokogiri::XML(<<~INPUT)
- attr1
- value1
- attr2
value1
value cont
INPUT
Metanorma::Utils.dl_to_attrs(xml.at("//dummy"), xml.at("//dl"), "attr1")
Metanorma::Utils.dl_to_attrs(xml.at("//dummy"), xml.at("//dl"), "attr2")
expect(xml.to_xml).to be_equivalent_to <<~OUTPUT
- attr1
- value1
- attr2
value1
value cont
OUTPUT
end
it "parses elements from definition list" do
xml = Nokogiri::XML(<<~INPUT)
A
A
A
- attr1
- value1
- attr2
value1
value cont
INPUT
Metanorma::Utils.dl_to_elems(xml.at("//dummy1"), xml.at("//dummy"),
xml.at("//dl"), "attr1")
Metanorma::Utils.dl_to_elems(xml.at("//dummy1"), xml.at("//dummy"),
xml.at("//dl"), "attr2")
expect(xml.to_xml).to be_equivalent_to <<~OUTPUT
A
A
Avalue1
value cont
- attr1
- attr2
value1
OUTPUT
end
end